@questwork/vue-q-list-vue3 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,241 @@
1
+ const { KeyValueObject } = require('../keyValueObject')
2
+ class QListButton {
3
+ constructor(options = {}) {
4
+ options = options || {}
5
+ this.active = (typeof options.active !== 'undefined') ? !!options.active : true
6
+ this.creator = options.creator
7
+ this.css = options.css || {}
8
+ this.deleted = options.deleted || false
9
+ this.endpoint = options.endpoint
10
+ this.httpMethod = options.httpMethod
11
+ this.icon = options.icon
12
+ this.label = options.label
13
+ this.linkTarget = options.linkTarget
14
+ this.onClick = options.onClick
15
+ this.owner = options.owner
16
+ this.preventDefault = options.preventDefault
17
+ this.qListButtonType = options.qListButtonType
18
+ this.qRow = options.qRow
19
+ this.restrictions = options.restrictions
20
+ this.showLabel = options.showLabel
21
+ this.title = options.title
22
+ this.url = options.url
23
+ }
24
+
25
+ static init(options) {
26
+ if (options instanceof QListButton) {
27
+ return options
28
+ }
29
+ const instance = new QListButton(options)
30
+ return instance.isValid ? instance : null
31
+ }
32
+
33
+ static initFromArray(arr = []) {
34
+ if (Array.isArray(arr)) {
35
+ return arr.map(this.init)
36
+ }
37
+ return []
38
+ }
39
+
40
+ static initOnlyValidFromArray(arr = []) {
41
+ return this.initFromArray(arr).filter((i) => i)
42
+ }
43
+
44
+ get isValid() {
45
+ if (this.qListButtonType) {
46
+ // version 1: actionBuilder
47
+ if (this.qListButtonType === 'actionBuilder') {
48
+ return true
49
+ } else {
50
+ return this.validation(this.restrictions) && this.qRow[this.qListButtonType] !== false
51
+ }
52
+ // if (this.qListButtonType === 'actionBuilder') {
53
+ // return true
54
+ // // version 2: use this.qRow[this.qListButtonType] to control
55
+ // } else if (this.qRow[this.qListButtonType] === undefined) {
56
+ // if (this.restrictions) {
57
+ // return this.validation(this.restrictions)
58
+ // }
59
+ // return true
60
+ // } else {
61
+ // if (this.restrictions) {
62
+ // return this.qRow[this.qListButtonType] && this.validation(this.restrictions)
63
+ // }
64
+ // return this.qRow[this.qListButtonType]
65
+ // }
66
+ } else {
67
+ return this.validation(this.restrictions)
68
+ }
69
+ }
70
+
71
+ click(...obj) {
72
+ if (this.onClick && typeof this.onClick === 'function') {
73
+ return this.onClick(...obj)
74
+ }
75
+ }
76
+
77
+ makeCss() {
78
+ return `__q-list-button ${this.css.element || ''}`
79
+ }
80
+
81
+ validation(restrictions) {
82
+ if (!restrictions) {
83
+ return true
84
+ }
85
+ const [visibleRule] = KeyValueObject.getValuesByKey(restrictions, 'visible')
86
+
87
+ return getValidation({
88
+ rule: visibleRule,
89
+ qRow: this.qRow,
90
+ })
91
+ }
92
+ }
93
+
94
+ // function getValidation({ rule, qRow }) {
95
+ // const { key = '', value } = rule
96
+ // const [valueAttribute] = Object.keys(value)
97
+
98
+ // if (!key) {
99
+ // switch(valueAttribute) {
100
+ // case '$and': {
101
+ // const arr = value['$and']
102
+ // return arr.reduce((acc, item) => {
103
+ // return acc && getValidation({ rule: item, qRow})
104
+ // }, true)
105
+ // }
106
+ // case '$or': {
107
+ // const arr = value['$or']
108
+ // return arr.reduce((acc, item) => {
109
+ // return acc || getValidation({ rule: item, qRow})
110
+ // }, false)
111
+ // }
112
+ // }
113
+ // return false
114
+ // }
115
+
116
+ // const rowValue = qRow.get(key)
117
+ // switch (valueAttribute) {
118
+ // case '$empty': {
119
+ // return !rowValue === value['$empty']
120
+ // }
121
+ // case '$lte': {
122
+ // return rowValue <= value['$lte']
123
+ // }
124
+ // }
125
+ // return false
126
+ // }
127
+
128
+ function _getValidation(rule, data, getDataByKey) {
129
+ if (!rule) {
130
+ return true
131
+ }
132
+ if (!getDataByKey || typeof getDataByKey !== 'function') {
133
+ return false
134
+ }
135
+ const { key = '', value } = rule
136
+ const [valueAttribute] = Object.keys(value)
137
+
138
+ if (!key) {
139
+ switch (valueAttribute) {
140
+ case '$and': {
141
+ const arr = value['$and']
142
+ return arr.reduce((acc, item) => (acc && _getValidation(item, data, getDataByKey)), true)
143
+ }
144
+ case '$or': {
145
+ const arr = value['$or']
146
+ return arr.reduce((acc, item) => (acc || _getValidation(item, data, getDataByKey)), false)
147
+ }
148
+ default:
149
+ return false
150
+ }
151
+ }
152
+
153
+ const rowValue = getDataByKey(key, data)
154
+ switch (valueAttribute) {
155
+ case '$empty': {
156
+ return !rowValue === !!value['$empty']
157
+ }
158
+ case '$eq': {
159
+ return rowValue === value['$eq']
160
+ }
161
+ case '$gt': {
162
+ return rowValue > value['$gt']
163
+ }
164
+ case '$gte': {
165
+ return rowValue >= value['$gte']
166
+ }
167
+ case '$lt': {
168
+ return rowValue < value['$lt']
169
+ }
170
+ case '$lte': {
171
+ return rowValue <= value['$lte']
172
+ }
173
+ case '$in': {
174
+ debugger
175
+ console.log('rowValuda', rowValue)
176
+ if (Array.isArray(rowValue)) {
177
+ return !!rowValue.find((e) => (value['$in'].includes(e)))
178
+ }
179
+ if (typeof rowValue !== 'object') {
180
+ return !!value['$in'].includes(rowValue)
181
+ }
182
+ return false
183
+ }
184
+ case '$inValue': {
185
+ const result = getDataByKey(value['$inValue'], data)
186
+ const _value = Array.isArray(result) ? result : []
187
+ if (Array.isArray(rowValue)) {
188
+ return !!rowValue.find((e) => (_value.includes(e)))
189
+ }
190
+ if (typeof rowValue === 'string') {
191
+ return !!_value.includes(rowValue)
192
+ }
193
+ return false
194
+ }
195
+ case '$ne': {
196
+ return rowValue !== value['$ne']
197
+ }
198
+ case '$notIn': {
199
+ if (Array.isArray(rowValue)) {
200
+ return !rowValue.find((e) => (value['$notIn'].includes(e)))
201
+ }
202
+ if (typeof rowValue !== 'object') {
203
+ return !value['$notIn'].includes(rowValue)
204
+ }
205
+ return false
206
+ }
207
+ case '$notInValue': {
208
+ const result = getDataByKey(value['$notInValue'], data)
209
+ const _value = Array.isArray(result) ? result : []
210
+ if (Array.isArray(rowValue)) {
211
+ return !rowValue.find((e) => (_value.includes(e)))
212
+ }
213
+ if (typeof rowValue !== 'object') {
214
+ return !_value.includes(rowValue)
215
+ }
216
+ return false
217
+ }
218
+ case '$range': {
219
+ const [min, max] = value['$range']
220
+ if (typeof min === 'number' && typeof max === 'number' && rowValue >= min && rowValue <= max) {
221
+ return true
222
+ }
223
+ return false
224
+ }
225
+ default:
226
+ return false
227
+ }
228
+ }
229
+
230
+ function getDataByKey(key, data) {
231
+ return data.get(key)
232
+ }
233
+
234
+ function getValidation({ rule, qRow }) {
235
+ return _getValidation(rule, qRow, getDataByKey)
236
+ }
237
+
238
+ module.exports = {
239
+ QListButton
240
+ }
241
+
@@ -0,0 +1,5 @@
1
+ const { QRow } = require('./qRow')
2
+
3
+ module.exports = {
4
+ QRow
5
+ }
@@ -0,0 +1,212 @@
1
+ const { dateHelper } = require('@questwork/utilities/lib/dateHelper')
2
+
3
+ class QRow {
4
+ constructor(options = {}) {
5
+ options = options || {}
6
+ this.active = (typeof options.active !== 'undefined') ? !!options.active : true
7
+ this.creator = options.creator
8
+ this.cssMaker = options.cssMaker
9
+ this.cssNames = options.cssNames || []
10
+ this.deleted = options.deleted || false
11
+ this.duration = options.duration
12
+ this.getActions = options.getActions
13
+ this.new = options.new || false
14
+ this.owner = options.owner
15
+ this.row = options.row
16
+ this.shouldBeDisabled = setshouldBeDisabled(options.shouldBeDisabled || null)
17
+ this.shouldBeVisible = setShouldBeVisible(options.shouldBeVisible || null)
18
+ this.uniqueKey = options.uniqueKey
19
+ this.unread = options.unread || false
20
+ }
21
+
22
+ // Class methods
23
+ static dummyData() {
24
+ return {
25
+ active: true,
26
+ creator: '1234',
27
+ deleted: false,
28
+ new: true,
29
+ owner: '9876',
30
+ row: {
31
+ firstName: 'Peter',
32
+ sex: 'M'
33
+ },
34
+ unread: true,
35
+ }
36
+ }
37
+ static init(options = {}) {
38
+ if (options instanceof QRow) {
39
+ return options
40
+ }
41
+ const instance = new QRow(options)
42
+ return instance.isValid ? instance : null
43
+ }
44
+ static initFromArray(arr = []) {
45
+ if (Array.isArray(arr)) {
46
+ return arr.map(this.init)
47
+ }
48
+ return []
49
+ }
50
+ static initOnlyValidFromArray(arr = []) {
51
+ return this.initFromArray(arr).filter((i) => i)
52
+ }
53
+
54
+ // getters
55
+ get css() {
56
+ return this.cssNames.join(' ')
57
+ }
58
+
59
+ get isValid() {
60
+ return !!this.row
61
+ }
62
+ get read() {
63
+ return !this.unread
64
+ }
65
+
66
+ // instance methods
67
+ addCss(className) {
68
+ if (!this.cssNames.includes(className)) {
69
+ this.cssNames.push(className)
70
+ }
71
+ return this
72
+ }
73
+
74
+ get(key) {
75
+ if (key) {
76
+ if (key.includes('qRow')) {
77
+ const arr = key.split('.')
78
+ arr.shift()
79
+ return getRecursiveValue(this, arr)
80
+ // const realKey = arr[1]
81
+ // return this[realKey]
82
+ }
83
+ if (key.includes('.')) {
84
+ const arr = key.split('.')
85
+ return getRecursiveValue(this.row, arr)
86
+ }
87
+ return this.row[key]
88
+ }
89
+ return this.row
90
+ }
91
+
92
+ getUniqueKey() {
93
+ if (this.uniqueKey) {
94
+ return this.get(this.uniqueKey)
95
+ }
96
+ return null
97
+ }
98
+
99
+ isBetween(myTimestamp, dateRange) {
100
+ let result = true
101
+ if (dateRange) {
102
+ const timestamp = new Date().getTime()
103
+ if (dateRange.start) {
104
+ const { start } = dateRange
105
+ result = result && (timestamp >= dateHelper(myTimestamp).add(start).valueOf())
106
+ }
107
+ if (dateRange.end) {
108
+ const { end } = dateRange
109
+ result = result && (timestamp <= dateHelper(myTimestamp).add(end).valueOf())
110
+ }
111
+ }
112
+ return result
113
+ }
114
+
115
+ isSameCreator(user) {
116
+ return user
117
+ ? (user === this.creator)
118
+ : false
119
+ }
120
+ isSameOwner(user) {
121
+ return user
122
+ ? (user === this.owner)
123
+ : false
124
+ }
125
+
126
+ getStatus() {
127
+ const statusList = []
128
+ if (this.active) {
129
+ statusList.push('active')
130
+ }
131
+ if (this.deleted) {
132
+ statusList.push('deleted')
133
+ }
134
+ if (this.new) {
135
+ statusList.push('new')
136
+ }
137
+ if (this.unread) {
138
+ statusList.push('unread')
139
+ }
140
+ return statusList
141
+ }
142
+
143
+ makeCss() {
144
+ const cssArr = this.cssMaker && typeof this.cssMaker === 'function' ? [this.cssMaker(this)] : []
145
+ return cssArr
146
+ }
147
+
148
+ removeCss(className) {
149
+ this.cssNames = this.cssNames.filter((e) => (e !== className))
150
+ return this
151
+ }
152
+
153
+ toggleActive() {
154
+ this.active = !this.active
155
+ this.new = false
156
+ this.unread = false
157
+ return this
158
+ }
159
+ toggleDeleted() {
160
+ this.deleted = !this.deleted
161
+ this.new = false
162
+ this.unread = false
163
+ return this
164
+ }
165
+ toggleNew() {
166
+ this.new = !this.new
167
+ return this
168
+ }
169
+ toggleRead() {
170
+ this.unread = !this.unread
171
+ return this
172
+ }
173
+ }
174
+
175
+ function getRecursiveValue(obj, arr) {
176
+ if (arr.length === 0) {
177
+ return obj
178
+ }
179
+ const key = arr.shift()
180
+ // if (obj && obj[key] !== undefined && obj[key] !== null && (obj[key].toString() || obj[key].toString() === '')) {
181
+ // return getRecursiveValue(obj[key], arr)
182
+ // }
183
+ if (obj && obj[key]) {
184
+ return getRecursiveValue(obj[key], arr)
185
+ }
186
+ if (obj && key) {
187
+ return obj[key]
188
+ }
189
+ return ''
190
+ }
191
+
192
+ function setshouldBeDisabled(shouldBeDisabled) {
193
+ return (component) => {
194
+ if(!shouldBeDisabled || typeof shouldBeDisabled !== 'function') {
195
+ return false
196
+ }
197
+ return !!shouldBeDisabled(component)
198
+ }
199
+ }
200
+
201
+ function setShouldBeVisible(shouldBeVisible) {
202
+ return (component) => {
203
+ if(!shouldBeVisible || typeof shouldBeVisible !== 'function') {
204
+ return true
205
+ }
206
+ return !!shouldBeVisible(component)
207
+ }
208
+ }
209
+
210
+ module.exports = {
211
+ QRow
212
+ }
@@ -0,0 +1,185 @@
1
+ const { expect } = require('chai')
2
+ const { QRow } = require('./qRow')
3
+
4
+ describe.only('QRow', function () {
5
+ let qRow
6
+ let qRows
7
+ const user = '9876'
8
+ const dummy = {
9
+ ...QRow.dummyData()
10
+ }
11
+
12
+ describe('.init', function () {
13
+ it('should return null if data is missing', async function () {
14
+ qRow = QRow.init()
15
+ expect(qRow).to.eql(null)
16
+ qRow = QRow.init(null)
17
+ expect(qRow).to.eql(null)
18
+ qRow = QRow.init({
19
+ ...dummy,
20
+ row: null
21
+ })
22
+ expect(qRow).to.eql(null)
23
+ })
24
+
25
+ it('should init a valid instance', async function () {
26
+ qRow = QRow.init(dummy)
27
+ expect(qRow instanceof QRow).to.eql(true)
28
+ expect(qRow.isValid).to.eql(true)
29
+ })
30
+ })
31
+
32
+ describe('.initFromArray', function () {
33
+ it('should init all object in array as valid object instance', async function () {
34
+ const qRowsToInit = [dummy, dummy, dummy]
35
+ qRows = QRow.initFromArray(qRowsToInit)
36
+ expect(qRows).to.have.lengthOf(qRowsToInit.length)
37
+ qRows.forEach((el) => {
38
+ expect(el instanceof QRow).to.eql(true)
39
+ })
40
+ })
41
+
42
+ it('should skip null in array as valid object instance', async function () {
43
+ const qRowsToInit = [dummy, null, dummy]
44
+ qRows = QRow.initOnlyValidFromArray(qRowsToInit)
45
+ expect(qRows).to.have.lengthOf(2)
46
+ qRows.forEach((el) => {
47
+ expect(el instanceof QRow).to.eql(true)
48
+ })
49
+ })
50
+ })
51
+
52
+ describe('.get', function () {
53
+ it('should get the corresponding key from data', async function () {
54
+ qRow = QRow.init(dummy)
55
+ const firstName = qRow.get('firstName')
56
+ expect(firstName).to.eql(dummy.row.firstName)
57
+ })
58
+
59
+ it('getter read to return opposite of unread', async function () {
60
+ qRow = QRow.init(dummy)
61
+ expect(qRow.unread).to.eql(true)
62
+ expect(qRow.read).to.eql(false)
63
+ })
64
+ })
65
+
66
+ describe('instance methods', function () {
67
+ it('replace setUnread() by toggleRead()', async function () {
68
+ qRow = QRow.init(dummy)
69
+ expect(qRow.setUnread).to.eql(undefined)
70
+ expect(typeof qRow.toggleRead).to.eql('function')
71
+ })
72
+
73
+ it('toggleRead() will toggle unread', async function () {
74
+ qRow = QRow.init(dummy)
75
+ expect(qRow.unread).to.eql(true)
76
+ expect(qRow.read).to.eql(false)
77
+ qRow.toggleRead()
78
+ expect(qRow.read).to.eql(true)
79
+ qRow.toggleRead()
80
+ expect(qRow.read).to.eql(false)
81
+ })
82
+
83
+ it('toggleNew() will toggle new', async function () {
84
+ qRow = QRow.init(dummy)
85
+ expect(qRow.new).to.eql(true)
86
+ qRow.toggleNew()
87
+ expect(qRow.new).to.eql(false)
88
+ qRow.toggleNew()
89
+ expect(qRow.new).to.eql(true)
90
+ })
91
+
92
+ it('isSameCreator(user) will verify the creator id', async function () {
93
+ qRow = QRow.init(dummy)
94
+ expect(qRow.isSameCreator(user)).to.eql(false)
95
+ })
96
+
97
+ it('isSameCreator(user) with valid user', async function () {
98
+ qRow = QRow.init(dummy)
99
+ const validUser = '1234'
100
+ expect(qRow.isSameCreator(validUser)).to.eql(true)
101
+ })
102
+
103
+ it('isSameCreator(user) with invalid creator', async function () {
104
+ qRow = QRow.init({
105
+ ...dummy,
106
+ creator: null
107
+ })
108
+ expect(qRow.isSameCreator(user)).to.eql(false)
109
+ })
110
+
111
+ it('isSameOwner(user) will verify the owner id', async function () {
112
+ qRow = QRow.init(dummy)
113
+ expect(qRow.isSameOwner(user)).to.eql(true)
114
+ })
115
+
116
+ it('isSameOwner(user) with invalid user', async function () {
117
+ qRow = QRow.init(dummy)
118
+ const inValidUser = '1234'
119
+ expect(qRow.isSameOwner(inValidUser)).to.eql(false)
120
+ })
121
+
122
+ it('isSameOwner(user) with invalid owner', async function () {
123
+ qRow = QRow.init({
124
+ ...dummy,
125
+ owner: null
126
+ })
127
+ expect(qRow.isSameOwner(user)).to.eql(false)
128
+ })
129
+ })
130
+
131
+ describe('.shouldBeDisabled', function() {
132
+ it('should return boolean', async function () {
133
+ qRow = QRow.init({
134
+ ...dummy,
135
+ })
136
+ expect(qRow.shouldBeDisabled(dummy)).to.eql(false)
137
+ qRow = QRow.init({
138
+ ...dummy,
139
+ shouldBeDisabled: true
140
+ })
141
+ expect(qRow.shouldBeDisabled(dummy)).to.eql(false)
142
+ qRow = QRow.init({
143
+ ...dummy,
144
+ shouldBeDisabled: (dummy) => {
145
+ return dummy.unread
146
+ }
147
+ })
148
+ expect(qRow.shouldBeDisabled(dummy)).to.eql(true)
149
+ qRow = QRow.init({
150
+ ...dummy,
151
+ shouldBeDisabled: (dummy) => {
152
+ return dummy.deleted
153
+ }
154
+ })
155
+ expect(qRow.shouldBeDisabled(dummy)).to.eql(false)
156
+ })
157
+ })
158
+ describe('.shouldBeVisible', function() {
159
+ it('should return boolean', async function () {
160
+ qRow = QRow.init({
161
+ ...dummy,
162
+ })
163
+ expect(qRow.shouldBeVisible(dummy)).to.eql(true)
164
+ qRow = QRow.init({
165
+ ...dummy,
166
+ shouldBeVisible: false
167
+ })
168
+ expect(qRow.shouldBeVisible(dummy)).to.eql(true)
169
+ qRow = QRow.init({
170
+ ...dummy,
171
+ shouldBeVisible: (dummy) => {
172
+ return dummy.deleted
173
+ }
174
+ })
175
+ expect(qRow.shouldBeVisible(dummy)).to.eql(false)
176
+ qRow = QRow.init({
177
+ ...dummy,
178
+ shouldBeVisible: (dummy) => {
179
+ return dummy.unread
180
+ }
181
+ })
182
+ expect(qRow.shouldBeVisible(dummy)).to.eql(true)
183
+ })
184
+ })
185
+ })
@@ -0,0 +1,15 @@
1
+ const sinon = require('sinon')
2
+
3
+ before(function before() {
4
+ })
5
+
6
+ after(function after() {
7
+ })
8
+
9
+ beforeEach(function beforeEach() {
10
+ this.sandbox = sinon.createSandbox()
11
+ })
12
+
13
+ afterEach(function afterEach() {
14
+ this.sandbox.restore()
15
+ })