material 0.9.12 → 0.9.14

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,142 @@
1
+ import Element from '../element'
2
+ import { create } from '../module/layout'
3
+
4
+ import dot from '../module/dot'
5
+
6
+ export default {
7
+
8
+ render (list, page, size) {
9
+ // console.log('render', list)
10
+ this.virtual.set(list, { page, size })
11
+
12
+ if (this.status && this.options.count !== true) {
13
+ this.status('count', list.length)
14
+ }
15
+ },
16
+
17
+ /**
18
+ * [add description]
19
+ * @param {[type]} info [description]
20
+ * @param {[type]} context [description]
21
+ */
22
+ renderItem (info, context) {
23
+ // console.log('renderItem', info)
24
+ info = info || {}
25
+
26
+ const container = this.ui.body
27
+
28
+ if (context === 'create') {
29
+ container.scrolltop = 0
30
+ }
31
+
32
+ // if (context === 'search') {
33
+ // container = this.ui['search-list']
34
+ // }
35
+
36
+ const element = new Element({
37
+ tag: 'li',
38
+ class: 'item'
39
+ })
40
+
41
+ if (this.id && this.id === info[this.dataId]) {
42
+ element.classList.add('selected')
43
+ }
44
+
45
+ element.dataset.id = info[this.dataId]
46
+ element.dataset.info = this.options.info
47
+
48
+ let layout = null
49
+
50
+ if (this.options.itemSwitch) {
51
+ const item = info[this.options.itemSwitch]
52
+
53
+ if (this.options.layout.item[item]) {
54
+ layout = create(this.options.layout.item[item], element)
55
+ } else {
56
+ layout = create(this.options.layout.item.base, element)
57
+ }
58
+ } else {
59
+ const itemName = this.options.itemName || 'item'
60
+ layout = create(this.options.layout[itemName], element)
61
+ }
62
+
63
+ // console.log('translate', layout)
64
+
65
+ if (this.translate && this.options.item && this.options.item.translate) {
66
+ // console.log('----')
67
+ this.translate(layout)
68
+ }
69
+
70
+ this.renderInfo(layout, info)
71
+
72
+ if (context === 'top' || context === 'create') {
73
+ container.insertBefore(element, container.firstChild)
74
+ this.selectCreated(info, element)
75
+ } else {
76
+ container.appendChild(element)
77
+ }
78
+
79
+ this.emit('infoRendered', info, element)
80
+
81
+ return element
82
+ },
83
+
84
+ /**
85
+ * [renderInfo description]
86
+ * @param {[type]} layout [description]
87
+ * @param {[type]} info [description]
88
+ * @return {[type]} [description]
89
+ */
90
+ renderInfo (layout, object) {
91
+ // console.log('renderInfo', object)
92
+ const info = dot(object)
93
+
94
+ const f = this.extractInfo(layout.component)
95
+
96
+ for (const field in f) {
97
+ if (f.hasOwnProperty(field)) {
98
+ if (f[field] && f[field].set) {
99
+ f[field].set(this.objectValueByDotKey(object, field))
100
+ } else {
101
+ // console.log('innerHTML', typeof f[field])
102
+ f[field].innerHTML = info[field]
103
+ }
104
+ }
105
+ }
106
+ },
107
+
108
+ objectValueByDotKey (object, dotkey) {
109
+ const keys = dotkey.split(/\./)
110
+
111
+ let value = Object.assign({}, object)
112
+
113
+ for (let i = 0; i < keys.length; i++) {
114
+ if (value !== undefined) {
115
+ value = value[keys[i]]
116
+ }
117
+ }
118
+
119
+ return value
120
+ },
121
+
122
+ extractInfo (object) {
123
+ // console.log('extractField', object)
124
+
125
+ const field = {}
126
+
127
+ for (const property in object) {
128
+ if (object.hasOwnProperty(property)) {
129
+ const infos = property.split(/\./)
130
+
131
+ if (infos[0] === 'info' && infos[1] !== undefined) {
132
+ const name = property.substr(5, property.length)
133
+ // console.log('field', name, property)
134
+
135
+ field[name] = object[property]
136
+ }
137
+ }
138
+ }
139
+
140
+ return field
141
+ }
142
+ }
@@ -0,0 +1,216 @@
1
+ export default {
2
+ requestById (id, cb) {
3
+ // log.error('requestById', id)
4
+ if (!id) return
5
+ return this.getPage(id, (data) => {
6
+ // log.error('getPage', data)
7
+ const page = data?.page || null
8
+ if (data?.count) this.virtual.setCount(data?.count)
9
+ this.request(page, null, null, cb)
10
+ })
11
+ },
12
+
13
+ request (page, size, more, cb) {
14
+ // console.log('request', page, size, more, cb)
15
+ if (more !== true) {
16
+ this.ui.body.innerHTML = ''
17
+ this.virtual.reset()
18
+ this.dataList = []
19
+ this.data = []
20
+ this.page = 1
21
+ this.stop = true
22
+ } else {
23
+ if (this.dataList.length < size) {
24
+ // console.log('no more request needed, data ', this.dataList.length)
25
+ return
26
+ }
27
+ }
28
+
29
+ this.data = this.data || []
30
+ let signal = null
31
+
32
+ if (this.abortController) {
33
+ // console.log('abort')
34
+ this.abortController.abort()
35
+ this.abortController = null
36
+ }
37
+
38
+ if (more !== true && window.AbortController) {
39
+ this.abortController = new AbortController()
40
+ signal = this.abortController.signal
41
+ }
42
+
43
+ page = page || 1
44
+ size = size || this.options.list.size
45
+ // console.log('size', this.options.name, size)
46
+
47
+ if (page === 1) {
48
+ this.ui.body.scrolltop = 0
49
+ }
50
+
51
+ const route = this.buildRoute(page, size)
52
+
53
+ if (more !== true && this.options.count) {
54
+ this.requestCount(route)
55
+ }
56
+
57
+ // console.log('route - ', this.rand, route)
58
+
59
+ fetch(route, { signal }).then((resp) => {
60
+ return resp.json()
61
+ }).then((data) => {
62
+ if (this.options.debug) {
63
+ console.log('data', route, data.length, data)
64
+ }
65
+
66
+ // console.log('route', route);
67
+
68
+ data = data || []
69
+
70
+ if (this.options.reverse) {
71
+ data.reverse()
72
+ }
73
+
74
+ if (data.error) {
75
+ console.log('error', data.error)
76
+ return
77
+ }
78
+
79
+ if (data.length < this.size && more) {
80
+ this.stop = true
81
+ } else {
82
+ this.stop = false
83
+ }
84
+
85
+ this.data = []
86
+ this.data = data
87
+ this.storeData(data, page, size)
88
+ this.render(data, page, size)
89
+
90
+ // if (more === true) {
91
+ // const a = this.data.concat(data)
92
+ // this.data = a
93
+ // this.storeData(data, more)
94
+ // this.virtual.add(this.data, { page, size })
95
+ // } else {
96
+ // this.data = []
97
+ // this.data = data
98
+ // this.storeData(data)
99
+ // this.render(data, page, size)
100
+ // }
101
+
102
+ if (cb) cb()
103
+
104
+ this.emit('fetched', data)
105
+
106
+ this.loaded = true
107
+
108
+ if (page === 1 && data.length < 1) {
109
+ this.emit('empty')
110
+ }
111
+ }).catch(function (e) {
112
+ // console.log('error', e.message)
113
+ })
114
+ },
115
+
116
+ getPage (id, cb) {
117
+ // log.info('getPage', id)
118
+ if (!id) {
119
+ return Promise.reject(new Error('id is required'))
120
+ }
121
+
122
+ const route = this.buildRoute(null, null, null, id)
123
+
124
+ // log.error('route', route)
125
+
126
+ return fetch(route)
127
+ .then((resp) => resp.json())
128
+ .then((data) => {
129
+ // log.debug('getPage data', data)
130
+ if (cb) cb(data)
131
+ return data
132
+ })
133
+ .catch((e) => {
134
+ // console.log('error', e.message)
135
+ return Promise.reject(e)
136
+ })
137
+ },
138
+
139
+ requestCount (route, signal) {
140
+ log.error('fetchCount', route + '&count=1')
141
+ fetch(route + '&count=1', { signal }).then((resp) => {
142
+ return resp.json()
143
+ }).then((data) => {
144
+ log.info('data', data.count)
145
+ this.count = data.count
146
+ this.statusDisplay('count', data.count)
147
+ log.info(this.virtual.setCount)
148
+ this.virtual.setCount(data.count)
149
+ }).catch(function (e) {
150
+ // console.log('error', e.message)
151
+ })
152
+ },
153
+
154
+ addParams (route, params) {
155
+ // log.info('addParams', route, params)
156
+
157
+ if (!route) {
158
+ console.log('error: no route specified!')
159
+
160
+ return
161
+ }
162
+
163
+ if (route.indexOf('?') > -1) {
164
+ // console.log('?', route, route.length, route[route.length - 1])
165
+ if (route[route.length - 1] === '&' || route[route.length - 1] === '?') {
166
+ route = route + params
167
+ } else {
168
+ route = route + '&' + params
169
+ }
170
+ } else {
171
+ // console.log('add ?')
172
+ route = route + '?' + params
173
+ }
174
+
175
+ return route
176
+ },
177
+
178
+ buildRoute (page, size, path, id) {
179
+ // log.info('buildroute', this.options.route, path)
180
+ path = path || 'list'
181
+
182
+ page = page || 1
183
+ size = size || this.options.list.size
184
+
185
+ let route = this.options.route
186
+
187
+ if (path) {
188
+ route = this.options.route[path]
189
+ }
190
+
191
+ if (this.routeAddOn) {
192
+ route = this.routeAddOn(route, path, id)
193
+ }
194
+
195
+ if (this.params && this.params()) {
196
+ // console.log('params', route, this.params())
197
+ route = this.addParams(route, this.params())
198
+ } else {
199
+
200
+ }
201
+
202
+ if (this.ui.filter && this.ui.filter.element.classList.contains('selected')) {
203
+ route = this.addParams(route, this.getFilter())
204
+ }
205
+
206
+ if (this.options.pagination) {
207
+ if (id) {
208
+ route = this.addParams(route, 'id=' + id + '&size=' + size)
209
+ } else {
210
+ route = this.addParams(route, 'page=' + page + '&size=' + size)
211
+ }
212
+ }
213
+
214
+ return route
215
+ }
216
+ }
@@ -0,0 +1,155 @@
1
+ export default {
2
+ search (keywords, page, size, more) {
3
+ // console.log('search', keywords, page, size, more)
4
+
5
+ this.emit('searching')
6
+
7
+ if (more !== true) {
8
+ this.ui.body.innerHTML = ''
9
+ this.virtual.reset()
10
+ this.data = []
11
+ this.dataList = []
12
+ this.data = []
13
+ this.page = 1
14
+ this.stop = false
15
+ }
16
+
17
+ this.data = this.data || []
18
+ let signal = null
19
+
20
+ if (this.abortController) {
21
+ this.abortController.abort()
22
+ this.abortController = null
23
+ }
24
+
25
+ if (more !== true) {
26
+ this.abortController = new AbortController()
27
+ signal = this.abortController.signal
28
+ }
29
+
30
+ page = page || 1
31
+ size = size || this.options.list.size
32
+
33
+ if (page === 1) {
34
+ this.ui.body.scrolltop = 0
35
+ }
36
+
37
+ let route = this.buildRoute(page, size, 'search')
38
+
39
+ route = this.addParams(route, 'search=' + keywords)
40
+
41
+ // console.log('route', route)
42
+
43
+ if (more !== true && this.options.count) {
44
+ this.requestCount(route)
45
+ }
46
+
47
+ fetch(route, { signal }).then((resp) => {
48
+ return resp.json()
49
+ }).then((data) => {
50
+ // console.log('data', data)
51
+ this.storeData(data)
52
+
53
+ this.data = data || []
54
+
55
+ // console.log('list', list)
56
+
57
+ if (data.error) {
58
+ console.log('error', data.error)
59
+ return
60
+ }
61
+
62
+ if (data.length < this.size && more) {
63
+ this.stop = true
64
+ }
65
+
66
+ if (more === true) {
67
+ const a = this.data.concat(data)
68
+ this.data = a
69
+ this.storeData(data, more)
70
+ this.virtual.add(this.data)
71
+ } else {
72
+ this.data = []
73
+ this.data = data
74
+ this.storeData(data)
75
+ this.render(data)
76
+ }
77
+
78
+ this.emit('fetched', data)
79
+ }).catch(function (e) {
80
+ // console.log('error', e.message)
81
+ })
82
+ },
83
+
84
+ cancelSearch () {
85
+ // console.log('cancel search')
86
+ this.ui.body.innerHTML = ''
87
+ },
88
+
89
+ /**
90
+ * [toggleSearch description]
91
+ * @return {[type]} [description]
92
+ */
93
+ toggleSearch (e) {
94
+ e.stopPropagation()
95
+ // console.log('toggle search', this.ui.body)
96
+ if (!this.ui['search-input'].element.classList.contains('show')) {
97
+ this.showSearch()
98
+ } else {
99
+ this.hideSearch()
100
+ }
101
+ },
102
+
103
+ /**
104
+ * [showSearch description]
105
+ * @return {[type]} [description]
106
+ */
107
+ showSearch (doNotEmpty) {
108
+ if (this.hideFilter) {
109
+ this.hideFilter()
110
+ }
111
+
112
+ // console.log('showSearch')
113
+ this.mode = 'search'
114
+ this.element.classList.add('search-mode')
115
+
116
+ if (this.ui.search) {
117
+ this.ui.search.element.classList.add('selected')
118
+ }
119
+
120
+ if (this.ui['search-input']) {
121
+ this.ui['search-input'].set()
122
+ this.ui['search-input'].show()
123
+ this.ui['search-input'].focus()
124
+ }
125
+
126
+ if (!doNotEmpty) this.ui.body.innerHTML = ''
127
+
128
+ // this.ui['search-list'].classList.add('show')
129
+ // this.ui.body.classList.add('hide')
130
+ },
131
+
132
+ /**
133
+ * [hideSearch description]
134
+ * @return {[type]} [description]
135
+ */
136
+ hideSearch (notfetch) {
137
+ // console.log('hideSearch', notfetch)
138
+ this.mode = 'standard'
139
+ this.element.classList.remove('search-mode')
140
+
141
+ if (this.ui.search) {
142
+ this.ui.search.element.classList.remove('selected')
143
+ }
144
+ if (this.ui['search-input']) {
145
+ this.ui['search-input'].hide()
146
+ this.ui['search-input'].set()
147
+ }
148
+ // this.ui['search-list'].classList.remove('show')
149
+ // this.ui.body.classList.remove('hide')
150
+ // console.log('request', notfetch)
151
+ if (!notfetch) {
152
+ this.request()
153
+ }
154
+ }
155
+ }