|
| 1 | +class AuthorOptionElement extends HTMLElement { |
| 2 | + constructor () { |
| 3 | + super() |
| 4 | + |
| 5 | + this.UTIL.defineAttributes({ |
| 6 | + disabled: false, |
| 7 | + hover: false, |
| 8 | + id: '', |
| 9 | + label: '', |
| 10 | + selected: false, |
| 11 | + value: '' |
| 12 | + }) |
| 13 | + |
| 14 | + this.UTIL.defineProperties({ |
| 15 | + defaultSelected: false, |
| 16 | + |
| 17 | + form: { |
| 18 | + readonly: true, |
| 19 | + private: true |
| 20 | + }, |
| 21 | + |
| 22 | + index: { |
| 23 | + readonly: true, |
| 24 | + get: () => this.parentNode.options.findIndex(option => option.displayElement === this) |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + this.UTIL.definePrivateMethods({ |
| 29 | + mouseButtonDown: evt => { |
| 30 | + let code = evt.buttons !== undefined ? evt.buttons : evt.nativeEvent.which |
| 31 | + return code >= 1 |
| 32 | + }, |
| 33 | + |
| 34 | + mousemoveHandler: evt => this.emit('option.hovered', this.index), |
| 35 | + |
| 36 | + mouseoutHandler: evt => this.hover = false, |
| 37 | + |
| 38 | + mouseoverHandler: evt => { |
| 39 | + let mousedown = this.PRIVATE.mouseButtonDown(evt) |
| 40 | + |
| 41 | + if (!(this.parentNode.multiple && mousedown)) { |
| 42 | + this.hover = true |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + let { shiftKey, metaKey, ctrlKey } = evt |
| 47 | + this.PRIVATE.select(shiftKey, metaKey, ctrlKey, mousedown) |
| 48 | + }, |
| 49 | + |
| 50 | + parentStateChangeHandler: evt => { |
| 51 | + let { name, value } = evt.detail |
| 52 | + |
| 53 | + switch (name) { |
| 54 | + case 'multiple': |
| 55 | + if (value) { |
| 56 | + this.removeEventListener('mouseup', this.PRIVATE.selectionHandler) |
| 57 | + this.addEventListener('mousedown', this.PRIVATE.selectionHandler) |
| 58 | + } else { |
| 59 | + this.addEventListener('mouseup', this.PRIVATE.selectionHandler) |
| 60 | + this.removeEventListener('mousedown', this.PRIVATE.selectionHandler) |
| 61 | + } |
| 62 | + break |
| 63 | + } |
| 64 | + }, |
| 65 | + |
| 66 | + select: (shiftKey = false, metaKey = false, ctrlKey = false, mousedown = false) => { |
| 67 | + let { index } = this |
| 68 | + this.emit('option.selected', {index, shiftKey, metaKey, ctrlKey, mousedown}, this.parentNode) |
| 69 | + }, |
| 70 | + |
| 71 | + selectionHandler: evt => { |
| 72 | + let { shiftKey, metaKey, ctrlKey } = evt |
| 73 | + this.PRIVATE.select(shiftKey, metaKey, ctrlKey) |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + this.UTIL.registerListeners(this, { |
| 78 | + connected: () => { |
| 79 | + this.parentNode.on('state.change', this.PRIVATE.parentStateChangeHandler) |
| 80 | + }, |
| 81 | + |
| 82 | + disconnected: () => { |
| 83 | + this.off('mousedown', this.PRIVATE.selectionHandler) |
| 84 | + this.parentNode.off('state.change', this.PRIVATE.parentStateChangeHandler) |
| 85 | + }, |
| 86 | + |
| 87 | + mouseover: this.PRIVATE.mouseoverHandler, |
| 88 | + mousemove: this.PRIVATE.mousemoveHandler, |
| 89 | + mouseout: this.PRIVATE.mouseoutHandler, |
| 90 | + mouseup: this.PRIVATE.selectionHandler |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + static get observedAttributes () { |
| 95 | + return ['disabled', 'hover', 'label', 'selected', 'value'] |
| 96 | + } |
| 97 | + |
| 98 | + get text () { |
| 99 | + return this.innerHTML |
| 100 | + } |
| 101 | + |
| 102 | + set text (content) { |
| 103 | + this.innerHTML = content |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @method remove |
| 108 | + * Remove this option from the DOM. |
| 109 | + * @override |
| 110 | + */ |
| 111 | + remove () { |
| 112 | + this.parentNode.options.splice(this.index, 1) |
| 113 | + super.remove() |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +customElements.define('author-option', AuthorOptionElement) |
0 commit comments