Skip to content

Commit

Permalink
Merge pull request #338 from nextcloud/refactor-#337-refactor-oop
Browse files Browse the repository at this point in the history
refactor: OO approach for better handling of columns and more
  • Loading branch information
Hephi2 committed Jun 27, 2023
2 parents 971c46e + ac05604 commit 6f5318d
Show file tree
Hide file tree
Showing 45 changed files with 1,206 additions and 1,330 deletions.
66 changes: 3 additions & 63 deletions src/modules/main/modals/CreateRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,7 @@
</div>
</div>
<div v-for="column in columns" :key="column.id">
<TextLineForm v-if="column.type === 'text' && column.subtype === 'line'"
:column="column"
:value.sync="row[column.id]" />
<TextLongForm v-if="column.type === 'text' && column.subtype === 'long'"
:column="column"
:value.sync="row[column.id]" />
<TextLinkForm v-if="column.type === 'text' && column.subtype === 'link'"
:column="column"
:value.sync="row[column.id]" />
<TextRichForm v-if="column.type === 'text' && column.subtype === 'rich'"
:column="column"
:value.sync="row[column.id]" />
<NumberForm v-if="column.type === 'number' && !column.subtype"
:column="column"
:value.sync="row[column.id]" />
<NumberStarsForm v-if="column.type === 'number' && column.subtype === 'stars'"
:column="column"
:value.sync="row[column.id]" />
<NumberProgressForm v-if="column.type === 'number' && column.subtype === 'progress'"
:column="column"
:value.sync="row[column.id]" />
<SelectionForm v-if="column.type === 'selection' && !column.subtype"
:column="column"
:value.sync="row[column.id]" />
<SelectionMultiForm v-if="column.type === 'selection' && column.subtype === 'multi'"
:column="column"
:value.sync="row[column.id]" />
<SelectionCheckForm v-if="column.type === 'selection' && column.subtype === 'check'"
:column="column"
:value.sync="row[column.id]" />
<DatetimeForm v-if="column.type === 'datetime' && !column.subtype"
:column="column"
:value.sync="row[column.id]" />
<DatetimeDateForm v-if="column.type === 'datetime' && column.subtype === 'date'"
:column="column"
:value.sync="row[column.id]" />
<DatetimeTimeForm v-if="column.type === 'datetime' && column.subtype === 'time'"
<ColumnFormComponent
:column="column"
:value.sync="row[column.id]" />
</div>
Expand All @@ -70,37 +34,13 @@ import { NcModal, NcCheckboxRadioSwitch } from '@nextcloud/vue'
import { showError, showSuccess, showWarning } from '@nextcloud/dialogs'
import '@nextcloud/dialogs/dist/index.css'
import { mapGetters } from 'vuex'
import TextLineForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextLineForm.vue'
import TextLongForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextLongForm.vue'
import TextLinkForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextLinkForm.vue'
import NumberForm from '../../../shared/components/ncTable/partials/rowTypePartials/NumberForm.vue'
import NumberStarsForm from '../../../shared/components/ncTable/partials/rowTypePartials/NumberStarsForm.vue'
import NumberProgressForm from '../../../shared/components/ncTable/partials/rowTypePartials/NumberProgressForm.vue'
import SelectionCheckForm from '../../../shared/components/ncTable/partials/rowTypePartials/SelectionCheckForm.vue'
import SelectionForm from '../../../shared/components/ncTable/partials/rowTypePartials/SelectionForm.vue'
import SelectionMultiForm from '../../../shared/components/ncTable/partials/rowTypePartials/SelectionMultiForm.vue'
import DatetimeForm from '../../../shared/components/ncTable/partials/rowTypePartials/DatetimeForm.vue'
import DatetimeDateForm from '../../../shared/components/ncTable/partials/rowTypePartials/DatetimeDateForm.vue'
import DatetimeTimeForm from '../../../shared/components/ncTable/partials/rowTypePartials/DatetimeTimeForm.vue'
import TextRichForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextRichForm.vue'
import ColumnFormComponent from '../partials/ColumnFormComponent.vue'
export default {
name: 'CreateRow',
components: {
SelectionCheckForm,
SelectionForm,
SelectionMultiForm,
NcModal,
TextLineForm,
TextLongForm,
TextLinkForm,
TextRichForm,
NumberForm,
NumberStarsForm,
NumberProgressForm,
DatetimeForm,
DatetimeDateForm,
DatetimeTimeForm,
ColumnFormComponent,
NcCheckboxRadioSwitch,
},
props: {
Expand Down
66 changes: 3 additions & 63 deletions src/modules/main/modals/EditRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,7 @@
</div>
</div>
<div v-for="column in columns" :key="column.id">
<TextLineForm v-if="column.type === 'text' && column.subtype === 'line'"
:column="column"
:value.sync="localRow[column.id]" />
<TextLongForm v-if="column.type === 'text' && column.subtype === 'long'"
:column="column"
:value.sync="localRow[column.id]" />
<TextRichForm v-if="column.type === 'text' && column.subtype === 'rich'"
:column="column"
:value.sync="localRow[column.id]" />
<TextLinkForm v-if="column.type === 'text' && column.subtype === 'link'"
:column="column"
:value.sync="localRow[column.id]" />
<NumberForm v-if="column.type === 'number' && !column.subtype"
:column="column"
:value.sync="localRow[column.id]" />
<NumberStarsForm v-if="column.type === 'number' && column.subtype === 'stars'"
:column="column"
:value.sync="localRow[column.id]" />
<NumberProgressForm v-if="column.type === 'number' && column.subtype === 'progress'"
:column="column"
:value.sync="localRow[column.id]" />
<SelectionForm v-if="column.type === 'selection' && !column.subtype"
:column="column"
:value.sync="localRow[column.id]" />
<SelectionMultiForm v-if="column.type === 'selection' && column.subtype === 'multi'"
:column="column"
:value.sync="localRow[column.id]" />
<SelectionCheckForm v-if="column.type === 'selection' && column.subtype === 'check'"
:column="column"
:value.sync="localRow[column.id]" />
<DatetimeForm v-if="column.type === 'datetime' && !column.subtype"
:column="column"
:value.sync="localRow[column.id]" />
<DatetimeDateForm v-if="column.type === 'datetime' && column.subtype === 'date'"
:column="column"
:value.sync="localRow[column.id]" />
<DatetimeTimeForm v-if="column.type === 'datetime' && column.subtype === 'time'"
<ColumnFormComponent
:column="column"
:value.sync="localRow[column.id]" />
</div>
Expand Down Expand Up @@ -75,39 +39,15 @@ import { NcModal, NcButton } from '@nextcloud/vue'
import { showError, showWarning } from '@nextcloud/dialogs'
import '@nextcloud/dialogs/dist/index.css'
import { mapGetters } from 'vuex'
import TextLineForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextLineForm.vue'
import TextLongForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextLongForm.vue'
import TextRichForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextRichForm.vue'
import TextLinkForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextLinkForm.vue'
import NumberForm from '../../../shared/components/ncTable/partials/rowTypePartials/NumberForm.vue'
import NumberStarsForm from '../../../shared/components/ncTable/partials/rowTypePartials/NumberStarsForm.vue'
import NumberProgressForm from '../../../shared/components/ncTable/partials/rowTypePartials/NumberProgressForm.vue'
import SelectionCheckForm from '../../../shared/components/ncTable/partials/rowTypePartials/SelectionCheckForm.vue'
import SelectionForm from '../../../shared/components/ncTable/partials/rowTypePartials/SelectionForm.vue'
import SelectionMultiForm from '../../../shared/components/ncTable/partials/rowTypePartials/SelectionMultiForm.vue'
import DatetimeForm from '../../../shared/components/ncTable/partials/rowTypePartials/DatetimeForm.vue'
import DatetimeDateForm from '../../../shared/components/ncTable/partials/rowTypePartials/DatetimeDateForm.vue'
import DatetimeTimeForm from '../../../shared/components/ncTable/partials/rowTypePartials/DatetimeTimeForm.vue'
import ColumnFormComponent from '../partials/ColumnFormComponent.vue'
import tablePermissions from '../mixins/tablePermissions.js'
export default {
name: 'EditRow',
components: {
SelectionCheckForm,
SelectionForm,
SelectionMultiForm,
NcModal,
TextLineForm,
TextLongForm,
TextRichForm,
TextLinkForm,
NumberForm,
NumberStarsForm,
NumberProgressForm,
DatetimeForm,
DatetimeDateForm,
DatetimeTimeForm,
NcButton,
ColumnFormComponent,
},
mixins: [tablePermissions],
props: {
Expand Down
80 changes: 80 additions & 0 deletions src/modules/main/partials/ColumnFormComponent.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<template>
<component :is="getFormComponent" :column="column"
:value.sync="value_data" />
</template>

<script>
import TextLineForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextLineForm.vue'
import TextLongForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextLongForm.vue'
import TextLinkForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextLinkForm.vue'
import NumberForm from '../../../shared/components/ncTable/partials/rowTypePartials/NumberForm.vue'
import NumberStarsForm from '../../../shared/components/ncTable/partials/rowTypePartials/NumberStarsForm.vue'
import NumberProgressForm from '../../../shared/components/ncTable/partials/rowTypePartials/NumberProgressForm.vue'
import SelectionCheckForm from '../../../shared/components/ncTable/partials/rowTypePartials/SelectionCheckForm.vue'
import SelectionForm from '../../../shared/components/ncTable/partials/rowTypePartials/SelectionForm.vue'
import SelectionMultiForm from '../../../shared/components/ncTable/partials/rowTypePartials/SelectionMultiForm.vue'
import DatetimeForm from '../../../shared/components/ncTable/partials/rowTypePartials/DatetimeForm.vue'
import DatetimeDateForm from '../../../shared/components/ncTable/partials/rowTypePartials/DatetimeDateForm.vue'
import DatetimeTimeForm from '../../../shared/components/ncTable/partials/rowTypePartials/DatetimeTimeForm.vue'
import TextRichForm from '../../../shared/components/ncTable/partials/rowTypePartials/TextRichForm.vue'
import { AbstractColumn } from '../../../shared/components/ncTable/mixins/columnClass.js'
export default {
name: 'ColumnFormComponent',
components: {
SelectionCheckForm,
SelectionForm,
SelectionMultiForm,
TextLineForm,
TextLongForm,
TextLinkForm,
TextRichForm,
NumberForm,
NumberStarsForm,
NumberProgressForm,
DatetimeForm,
DatetimeDateForm,
DatetimeTimeForm,
},
props: {
column: {
type: AbstractColumn,
default: null,
},
value: {
type: undefined,
default: null,
},
},
data() {
return {
value_data: this.value,
}
},
computed: {
getFormComponent() {
const form = this.snakeToCamel(this.column.type) + 'Form'
if (this.$options.components && this.$options.components[form]) {
return form
} else {
throw Error('Form ' + form + ' does no exist')
}
},
},
watch: {
value_data(val) {
this.$emit('update:value', this.value_data)
},
},
methods: {
snakeToCamel(str) {
str = str.toLowerCase().replace(/([-_][a-z])/g, group =>
group
.toUpperCase()
.replace('_', '')
.replace('-', '')
)
return str.charAt(0).toUpperCase() + str.slice(1)
},
},
}
</script>
3 changes: 2 additions & 1 deletion src/pages/DefaultMainView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import DeleteRows from '../modules/main/modals/DeleteRows.vue'
import EmptyTable from '../modules/main/sections/EmptyTable.vue'
import permissionsMixin from '../shared/components/ncTable/mixins/permissionsMixin.js'
import { emit } from '@nextcloud/event-bus'
import { parseCol } from '../shared/components/ncTable/mixins/columnParser.js'
export default {
name: 'DefaultMainView',
Expand Down Expand Up @@ -80,7 +81,7 @@ export default {
},
computed: {
...mapState({
columns: state => state.data.columns,
columns: state => state.data.columns.map(col => parseCol(col)),
loading: state => state.data.loading,
rows: state => state.data.rows,
view: state => state.data.view,
Expand Down
103 changes: 103 additions & 0 deletions src/shared/components/ncTable/mixins/columnClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { Filters } from './filter.js'
import { MagicFields } from './magicFields.js'

export class AbstractColumn {

type = null

constructor(data) {
this.createdAt = data.createdAt
this.createdBy = data.createdBy
this.id = data.id
this.lastEditAt = data.lastEditAt
this.lastEditBy = data.lastEditBy
this.mandatory = data.mandatory
this.orderWeight = data.orderWeight
this.tableId = data.tableId
this.title = data.title
this.description = data.description
}

canSort() {
return typeof this.sort === 'function'
}

getPossibleOperators() {
return Object.values(Filters).filter(fil => fil.goodFor.includes(this.type))
}

getPossibleMagicFields() {
return Object.values(MagicFields).filter(item => item.goodFor.includes(this.type))
}

isSearchStringFound(cellValue, cell, searchString) {
if (cellValue != null && cellValue.toLowerCase().includes(searchString)) {
cell.searchStringFound = true
return true
}
return false
}

isFilterFound(filterMethod, cell) {
if (filterMethod()) {
cell.filterFound = true
return true
}
return false
}

}

export class AbstractNumberColumn extends AbstractColumn {

constructor(data) {
super(data)
this.numberDefault = data.numberDefault
}

default() {
return this.numberDefault
}

}

export class AbstractDatetimeColumn extends AbstractColumn {

constructor(data) {
super(data)
this.datetimeDefault = data.datetimeDefault
}

formatValue(value) {}

default() {
return this.datetimeDefault
}

}

export class AbstractTextColumn extends AbstractColumn {

constructor(data) {
super(data)
this.textDefault = data.textDefault
}

default() {
return this.textDefault
}

}

export class AbstractSelectionColumn extends AbstractColumn {

constructor(data) {
super(data)
this.selectionDefault = data.selectionDefault
}

default() {
return this.selectionDefault
}

}
15 changes: 15 additions & 0 deletions src/shared/components/ncTable/mixins/columnHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const ColumnTypes = {
TextLine: 'text-line',
TextLong: 'text-long',
TextRich: 'text-rich',
Selection: 'selection',
SelectionMulti: 'selection-multi',
Number: 'number',
SelectionCheck: 'selection-check',
TextLink: 'text-link',
NumberStars: 'number-stars',
NumberProgress: 'number-progress',
DatetimeDate: 'datetime-date',
DatetimeTime: 'datetime-time',
Datetime: 'datetime',
}
Loading

0 comments on commit 6f5318d

Please sign in to comment.