material 0.9.19 → 0.9.21

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "material",
3
- "version": "0.9.19",
3
+ "version": "0.9.21",
4
4
  "description": "A lightweight implementation of Material Design Components for the web - ES6",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -17,7 +17,7 @@ class Component {
17
17
  }
18
18
 
19
19
  destroy () {
20
- console.log('dest5roy')
20
+ // console.log('destroy')
21
21
  if (this.options.transition && this.element) {
22
22
  this.element.classList.remove('show')
23
23
 
package/src/class/view.js CHANGED
@@ -14,18 +14,15 @@ class View extends Component {
14
14
  }
15
15
 
16
16
  on (event, listener) {
17
- if (!this.events[event]) {
18
- this.events[event] = []
19
- }
17
+ this.events[event] ??= []
20
18
  this.events[event].push(listener)
21
19
 
22
20
  return this
23
21
  }
24
22
 
25
23
  emit (event, ...args) {
26
- if (this.events[event]) {
27
- this.events[event].forEach(listener => listener.apply(this, args))
28
- }
24
+ this.events?.[event]?.forEach(listener => listener.apply(this, args))
25
+
29
26
  return this
30
27
  }
31
28
  }
package/src/datehour.js CHANGED
@@ -3,25 +3,30 @@ import Component from './class/component'
3
3
  class DateHour extends Component {
4
4
  static defaults = {
5
5
  class: 'date',
6
- tag: 'span'
6
+ tag: 'span',
7
+ locale: navigator.language,
8
+ format: { // Changed from 'options' to 'format'
9
+ dateStyle: 'short',
10
+ timeStyle: 'short',
11
+ hour12: false
12
+ }
13
+ }
14
+
15
+ constructor (options) {
16
+ super(options)
17
+ this.formatter = new Intl.DateTimeFormat(
18
+ this.options.locale,
19
+ this.options.format // Using format instead of options
20
+ )
7
21
  }
8
22
 
9
23
  set (date) {
10
- // console.log('set', src)
11
24
  const d = new Date(date)
12
- const formatted = this.format(d)
13
- this.element.innerHTML = formatted
25
+ this.element.innerHTML = this.format(d)
14
26
  }
15
27
 
16
28
  format (date) {
17
- let hours = date.getHours()
18
- let minutes = date.getMinutes()
19
- const ampm = hours >= 12 ? 'pm' : 'am'
20
- hours = hours % 12
21
- hours = hours || 12 // the hour '0' should be '12'
22
- minutes = minutes < 10 ? '0' + minutes : minutes
23
- const strTime = hours + ':' + minutes + ' ' + ampm
24
- return date.getMonth() + 1 + '/' + date.getDate() + '/' + date.getFullYear() + ' ' + strTime
29
+ return this.formatter.format(date)
25
30
  }
26
31
  }
27
32
 
package/src/loader.js CHANGED
@@ -98,7 +98,9 @@ class Loader {
98
98
  }
99
99
 
100
100
  setText (text) {
101
- this.text.innerHTML = text
101
+ if (text) {
102
+ this.text.innerHTML = text
103
+ }
102
104
  }
103
105
 
104
106
  show () {
package/src/module/dom.js CHANGED
@@ -1,7 +1,6 @@
1
1
  // DOM manipulation function
2
2
 
3
3
  function append (container, element) {
4
- console.log(container, element)
5
4
  container.appendChild(element)
6
5
  return element
7
6
  }
@@ -16,7 +16,7 @@ function create (schema, container, structure = {}, level = 0, components = [])
16
16
  const fragment = document.createDocumentFragment()
17
17
 
18
18
  if (!Array.isArray(schema)) {
19
- console.trace('Schema is not an array!')
19
+ console.error('Schema is not an array!', container, level, schema)
20
20
  }
21
21
 
22
22
  for (let i = 0; i < schema.length; i++) {
@@ -0,0 +1,341 @@
1
+ import init from './mixin/init'
2
+ import build from './element/build'
3
+ import control from './mixin/control'
4
+
5
+ import events from './mixin/events'
6
+ import insert from './element/insert'
7
+ import offset from './element/offset'
8
+ import classify from './module/classify'
9
+ // import control from './control';
10
+ import * as css from './module/css'
11
+ import emitter from './module/emitter'
12
+
13
+ import icon from './skin/material/icon/pin.svg'
14
+
15
+ class Slider {
16
+ static defaults = {
17
+ class: 'slider',
18
+ type: 'control',
19
+ label: null,
20
+ checked: false,
21
+ error: false,
22
+ value: false,
23
+ range: [0, 100],
24
+ step: 5,
25
+ modules: [control, emitter],
26
+ mixins: [],
27
+ build: ['$root.material-slider', {},
28
+ ['label$label.slider-label', {}],
29
+ ['input$input'],
30
+ ['$control.slider-control', {},
31
+ ['$track.slider-track', {},
32
+ ['canvas$canvas.slider-canvas', {}],
33
+ ['$trackvalue.slider-track-value', {}],
34
+ ['$knob.slider-knob', {}],
35
+ ['$marker.slider-marker', {},
36
+ ['$value.slider-value', {}]
37
+ ]
38
+ ]
39
+ ]
40
+ ],
41
+ events: [
42
+ ['element.input.focus', 'focus'],
43
+ ['element.input.blur', 'blur']
44
+ ]
45
+ }
46
+
47
+ constructor (options) {
48
+ log.info('Slider')
49
+ this.init(this.options)
50
+ this.build(this.options)
51
+ this.setup()
52
+ }
53
+
54
+ init (options) {
55
+ this.options = Object.assign({}, Slider.defaults, options || {})
56
+ init(this)
57
+ }
58
+
59
+ setup () {
60
+ events.attach(this.options.events, this)
61
+ }
62
+
63
+ build () {
64
+ this.element = build(this.options.build)
65
+ this.element = this.element.root
66
+
67
+ classify(this.element, this.options)
68
+
69
+ if (this.options.container) {
70
+ insert(this.element, this.options.container)
71
+ }
72
+
73
+ const value = this.element.marker.innerHTML
74
+ this.element.marker.innerHTML = icon + value
75
+
76
+ if (this.options.type) {
77
+ css.add(this.element, 'type-' + this.options.type)
78
+ }
79
+
80
+ // init input
81
+ if (this.options.disabled) {
82
+ this.disable(true)
83
+ }
84
+
85
+ // if (this.options.name) {
86
+ // this.element.dataset.name = name
87
+ // this.element.input.name = name
88
+ // }
89
+
90
+ // init text
91
+ const text = this.options.label || this.options.text
92
+ this.element.label.textContent = text
93
+
94
+ this.options.label = this.options.label || this.options.text
95
+
96
+ this.initTrack()
97
+
98
+ const delay = 50
99
+
100
+ setTimeout(() => {
101
+ this.initCanvas()
102
+ }, delay)
103
+ }
104
+
105
+ initCanvas () {
106
+ window.addEventListener('resize', () => {
107
+ console.log('resize')
108
+ this.drawCanvas()
109
+ }, false)
110
+ this.drawCanvas()
111
+ }
112
+
113
+ drawCanvas () {
114
+ const width = offset(this.element.track, 'width')
115
+ const height = offset(this.element.track, 'height')
116
+
117
+ this.element.canvas.width = width
118
+ this.element.canvas.height = height
119
+
120
+ const context = this.element.canvas.getContext('2d')
121
+ context.lineWidth = 2
122
+ context.beginPath()
123
+
124
+ context.moveTo(0, (height / 2) + 1)
125
+ context.lineTo(width, (height / 2) + 1)
126
+ context.strokeStyle = 'rgba(34, 31, 31, .26)'
127
+ context.stroke()
128
+ }
129
+
130
+ /**
131
+ * [buildControl description]
132
+ * @return {?} [description]
133
+ */
134
+ initTrack () {
135
+ this.element.track.addEventListener('mousedown', (ev) => {
136
+ if (this.disabled === true) return
137
+ this.initTrackSize()
138
+ const position = ev.layerX
139
+ this.update(position)
140
+ })
141
+
142
+ this.element.knob.addEventListener('click', (ev) => {
143
+ ev.stopPropagation()
144
+ })
145
+
146
+ this.initDragging()
147
+
148
+ const delay = 100
149
+
150
+ setTimeout(() => {
151
+ this.setValue(this.options.value)
152
+ }, delay)
153
+ }
154
+
155
+ initTrackSize () {
156
+ this._tracksize = offset(this.element.track, 'width')
157
+ this._knobsize = offset(this.element.knob, 'width')
158
+ this._markersize = 32 /* offset(this.element.marker, 'width') */
159
+ this._trackleft = offset(this.element.track, 'left')
160
+ return this
161
+ }
162
+
163
+ /**
164
+ * [initDragging description]
165
+ * @return {?} [description]
166
+ */
167
+ initDragging () {
168
+ this.element.knob.onmousedown = (e) => {
169
+ if (this.disabled === true) return
170
+
171
+ e.stopPropagation()
172
+ e = e || window.event
173
+
174
+ css.add(this.element.control, 'dragging')
175
+
176
+ let start = 0
177
+ let position = 0
178
+
179
+ if (e.pageX) start = e.pageX
180
+ else if (e.clientX) start = e.clientX
181
+
182
+ start = this._trackleft
183
+ document.body.onmousemove = (e) => {
184
+ if (this.disabled === true) return
185
+ console.log('mousedown', this.disabled)
186
+
187
+ e = e || window.event
188
+ let end = 0
189
+ if (e.pageX) end = e.pageX
190
+ else if (e.clientX) end = e.clientX
191
+
192
+ position = end - start
193
+ this.update(position)
194
+ }
195
+ document.body.onmouseup = (e) => {
196
+ document.body.onmousemove = document.body.onmouseup = null
197
+
198
+ e = e || window.event
199
+ let end = 0
200
+ if (e.pageX) end = e.pageX
201
+ else if (e.clientX) end = e.clientX
202
+
203
+ position = end - start
204
+ this.update(position)
205
+ css.remove(this.element.control, 'dragging')
206
+ }
207
+ }
208
+ }
209
+
210
+ update (position) {
211
+ const size = this._tracksize
212
+ const range = this.options.range[1] - this.options.range[0]
213
+
214
+ if (position > size) {
215
+ position = size
216
+ }
217
+
218
+ if (position < 0) {
219
+ position = 0
220
+ }
221
+
222
+ const ratio = (size / position)
223
+ const value = Math.round((range / ratio)) + this.options.range[0]
224
+
225
+ if (position === 0) {
226
+ css.remove(this.element.knob, 'notnull')
227
+ }
228
+
229
+ this.element.knob.style.left = position - this._knobsize / 2 + 'px'
230
+ this.element.trackvalue.style.width = (position) + 'px'
231
+ this.element.marker.style.left = position - this._markersize / 2 + 'px'
232
+
233
+ this.element.value.textContent = value
234
+
235
+ this.element.input.value = value
236
+ if (value > this.options.range[0]) {
237
+ css.add(this.element.knob, 'notnull')
238
+ } else {
239
+ css.remove(this.element.knob, 'notnull')
240
+ }
241
+ }
242
+
243
+ updateValue (value) {
244
+ this.initTrackSize()
245
+
246
+ let size = offset(this.element.track, 'width')
247
+ size = parseInt(size)
248
+
249
+ const range = this.options.range[1] - this.options.range[0]
250
+ const ratio = value * 100 / range
251
+ const position = Math.round(size * ratio / 100)
252
+
253
+ this.update(position)
254
+
255
+ return this
256
+ }
257
+
258
+ /**
259
+ * [insert description]
260
+ * @param {?} container [description]
261
+ * @param {?} context [description]
262
+ * @return {?} [description]
263
+ */
264
+ insert (container, context) {
265
+ insert(this.element, container, context)
266
+ }
267
+
268
+ /**
269
+ * Setter
270
+ * @param {string} prop
271
+ * @param {string} value
272
+ */
273
+ set (prop, value) {
274
+ switch (prop) {
275
+ case 'value':
276
+ this.setValue(value)
277
+ break
278
+ case 'label':
279
+ this.setLabel(value)
280
+ break
281
+ default:
282
+ this.setValue(prop)
283
+ }
284
+
285
+ return this
286
+ }
287
+
288
+ /**
289
+ * Getter
290
+ * @param {string} prop
291
+ * @param {string} value
292
+ */
293
+ get (prop) {
294
+ let value
295
+
296
+ switch (prop) {
297
+ case 'value':
298
+ value = this.getValue()
299
+ break
300
+ case 'name':
301
+ value = this.name
302
+ break
303
+ default:
304
+ return this.getValue()
305
+ }
306
+
307
+ return value
308
+ }
309
+
310
+ /**
311
+ * [getValue description]
312
+ * @return {Object} The class instance
313
+ */
314
+ getValue () {
315
+ return this.element.input.value
316
+ }
317
+
318
+ /**
319
+ * [setValue description]
320
+ * @param {string} value [description]
321
+ */
322
+ setValue (value) {
323
+ value = value || this.options.range[0]
324
+ this.element.input.value = value
325
+ this.updateValue(value)
326
+ }
327
+
328
+ /**
329
+ * [setLabel description]
330
+ * @param {?} text [description]
331
+ */
332
+ setLabel (text) {
333
+ text = text || this.options.label || this.options.text
334
+
335
+ if (text !== null && this.label) {
336
+ this.label.textContent = text
337
+ }
338
+ }
339
+ }
340
+
341
+ export default Slider
package/src/switch.js CHANGED
@@ -100,7 +100,7 @@ class Switch extends EventEmitter {
100
100
  this.setValue(value, silent)
101
101
  break
102
102
  case 'text':
103
- this.setValue(value)
103
+ this.setText(value)
104
104
  break
105
105
  case 'disabled':
106
106
  if (value === true) {
@@ -160,17 +160,15 @@ class Switch extends EventEmitter {
160
160
  this.ui.input.checked = true
161
161
  this.checked = true
162
162
  this.value = true
163
- if (!silent) {
164
- this.emit('change', this.checked)
165
- }
166
163
  } else {
167
164
  this.element.classList.remove('is-checked')
168
165
  this.ui.input.checked = false
169
166
  this.checked = false
170
167
  this.value = false
171
- if (!silent) {
172
- this.emit('change', this.checked)
173
- }
168
+ }
169
+
170
+ if (!silent) {
171
+ this.emit('change', this.checked)
174
172
  }
175
173
  return this
176
174
  }
package/src/switcher.js CHANGED
@@ -169,7 +169,7 @@ class Switcher extends EventEmitter {
169
169
  return this
170
170
  }
171
171
 
172
- setValue (value) {
172
+ setValue (value, silent) {
173
173
  // console.log('value', value, this.options.list, this.element)
174
174
  const list = this.options.list
175
175
  if (this.options.mode === 'unique') {
@@ -193,6 +193,15 @@ class Switcher extends EventEmitter {
193
193
  }
194
194
  }
195
195
 
196
+ get () {
197
+ // console.log('get', this.selected)
198
+ if (this.options.mode === 'unique') {
199
+ return this.selected[0]
200
+ } else {
201
+ return this.selected
202
+ }
203
+ }
204
+
196
205
  addOptions (list) {
197
206
  // console.log('setOptions', list, this.options.list)
198
207
 
@@ -236,15 +245,6 @@ class Switcher extends EventEmitter {
236
245
  // console.log('setText', value)
237
246
  this.setLabel(value)
238
247
  }
239
-
240
- get () {
241
- // console.log('get', this.selected)
242
- if (this.options.mode === 'unique') {
243
- return this.selected[0]
244
- } else {
245
- return this.selected
246
- }
247
- }
248
248
  }
249
249
 
250
250
  export default Switcher
package/src/text.js CHANGED
@@ -19,7 +19,7 @@ class Text extends Component {
19
19
 
20
20
  set (text) {
21
21
  // console.log('set', text)
22
- if (text === 'undefined') text = ''
22
+ if (text === 'undefined' || !text) text = ''
23
23
 
24
24
  const label = this.options.label || ''
25
25
 
package/src/textfield.js CHANGED
@@ -2,6 +2,7 @@ import emitter from './module/emitter'
2
2
  import events from './module/events'
3
3
  import dataset from './module/dataset'
4
4
  import attributes from './module/attributes'
5
+ import * as css from './module/css'
5
6
 
6
7
  class Textfield {
7
8
  static isComponent () {
@@ -38,7 +39,7 @@ class Textfield {
38
39
  this.element.classList.add('textfield')
39
40
 
40
41
  if (this.options.class !== 'textfield') {
41
- this.element.classList.add(this.options.class)
42
+ css.add(this.element, this.options.class)
42
43
  }
43
44
 
44
45
  this.buildLabel()
@@ -1,139 +0,0 @@
1
- import scrollbar from '../module/scrollbar'
2
- import dataset from '../module/dataset'
3
-
4
- import { create } from '../module/layout'
5
- import Virtual from './virtual'
6
- import Loading from '../loading'
7
-
8
- export default {
9
- build (data) {
10
- // console.log('options', this.options.name)
11
-
12
- this.dataId = this.options.dataId || '_id'
13
-
14
- this.element = document.createElement('div')
15
- this.element.classList.add('list')
16
-
17
- if (this.options.class) {
18
- this.addClass(this.options.class)
19
- }
20
-
21
- this.element.addEventListener('click', (e) => {
22
- e.stopPropagation()
23
- })
24
-
25
- if (this.options.name) {
26
- this.name = this.options.name
27
- }
28
-
29
- if (this.options.data) {
30
- dataset(this.element, this.options.data)
31
- }
32
-
33
- this.layout = create(this.options.layout.main, this.element)
34
- this.ui = this.layout.component
35
-
36
- // prepare loading
37
- // this.ui.loading = new Loading()
38
-
39
- if (this.options.container) {
40
- this.options.container.appendChild(this.element)
41
- }
42
-
43
- const scrollbarOffset = scrollbar()
44
-
45
- // console.log('scrollbarOffset', scrollbarOffset)
46
- if (this.ui && this.ui.body) {
47
- // console.log('body padding', 11 - scrollbarOffset + 'px')
48
- this.ui.body.style.marginRight = 11 - scrollbarOffset + 'px'
49
- this.ui.body.classList.add('offset' + scrollbarOffset)
50
- }
51
-
52
- this.buildVirtual()
53
- },
54
-
55
- addClass (c) {
56
- const list = c.split(' ')
57
-
58
- for (let i = 0; i < list.length; i++) {
59
- this.element.classList.add(list[i])
60
- }
61
- },
62
-
63
- buildVirtual () {
64
- // console.log('buildVirtual', this.options.class)
65
- let height = 80
66
- this.size = this.options.list.size
67
- this.stop = false
68
-
69
- if (this.options.item && this.options.item.height) {
70
- height = this.options.item.height
71
- }
72
-
73
- this.virtual = new Virtual({
74
- view: this.options.name,
75
- container: this.ui.body,
76
- itemHeight: height,
77
- size: this.size,
78
- render: (i) => {
79
- // console.log('render', this.data[i])
80
- return this.renderItem(this.data[i])
81
- }
82
- }).on('next', (total) => {
83
- // console.log('next', total)
84
- if (this.stop) return
85
-
86
- // console.log('next', total, this.page, this.size)
87
- this.page = this.page || 1
88
- // console.log('slide', total, this.options.list.size)
89
- // var page = Math.ceil(total / this.size) + 1
90
-
91
- this.page++
92
-
93
- if (this.mode === 'search') {
94
- this.search(this.ui['search-input'].input.value, this.page, this.size, true)
95
- } else {
96
- // console.log('request', this.page, this.size, true)
97
- this.request(this.page, this.size, true)
98
- }
99
- }).on('progress', (progress) => {
100
- // console.log('progress', progress, this.count)
101
-
102
- if (this.statusDisplay) {
103
- const percent = parseInt(progress / this.count * 100)
104
- this.statusDisplay('count', percent + '% | ' + progress + ' / ' + this.count)
105
- }
106
-
107
- this.onProgress(progress)
108
- })
109
- // .on('size', (size) => {
110
- // console.log('!!!!size', size)
111
- // this.size = size
112
- // })
113
- //
114
- },
115
-
116
- onProgress (value) {
117
- // console.log('progress', value, this._value)
118
-
119
- if (this.ui.head) {
120
- if (this.ui.body.scrollTop > 0 && this.ui.head) {
121
- this.ui.head.classList.add('scrolled')
122
- } else if (this.ui.head) {
123
- this.ui.head.classList.remove('scrolled')
124
- }
125
- }
126
-
127
- if (value > this._value) {
128
- // console.log('scrolling down')
129
- this.emit('scrolling', 'down')
130
- } else if (value < this._value) {
131
- // console.log('scrolling up')
132
- this.emit('scrolling', 'up')
133
- }
134
-
135
- this._value = value
136
-
137
- this.scrollTop = this.ui.body.scrollTop
138
- }
139
- }