dragselect 2.3.1 → 2.4.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +45 -23
  3. package/deploying.md +7 -4
  4. package/dist/DragSelect.d.ts +10 -18
  5. package/dist/DragSelect.es6m.js +412 -579
  6. package/dist/DragSelect.js +412 -579
  7. package/dist/ds.es6m.min.js +1 -1
  8. package/dist/ds.min.js +1 -1
  9. package/dist/methods/createSelectorAreaElement.d.ts +1 -1
  10. package/dist/methods/getOverflowEdges.d.ts +1 -1
  11. package/dist/methods/hydrateSettings.d.ts +3 -0
  12. package/dist/methods/index.d.ts +1 -0
  13. package/dist/modules/Area.d.ts +10 -15
  14. package/dist/modules/Drag.d.ts +4 -40
  15. package/dist/modules/Interaction.d.ts +2 -31
  16. package/dist/modules/SelectableSet.d.ts +6 -38
  17. package/dist/modules/SelectedSet.d.ts +6 -10
  18. package/dist/modules/Selection.d.ts +2 -14
  19. package/dist/modules/Selector.d.ts +5 -11
  20. package/dist/modules/SelectorArea.d.ts +4 -17
  21. package/dist/stores/KeyStore.d.ts +2 -14
  22. package/dist/stores/ScrollStore.d.ts +2 -9
  23. package/dist/stores/SettingsStore.d.ts +39 -0
  24. package/dist/stores/index.d.ts +1 -0
  25. package/dist/types.d.ts +13 -32
  26. package/package.json +3 -3
  27. package/src/DragSelect.js +22 -175
  28. package/src/methods/createSelectorAreaElement.js +1 -3
  29. package/src/methods/hydrateSettings.js +109 -0
  30. package/src/methods/index.js +1 -0
  31. package/src/modules/Area.js +24 -22
  32. package/src/modules/Drag.js +37 -71
  33. package/src/modules/Interaction.js +14 -45
  34. package/src/modules/SelectableSet.js +29 -54
  35. package/src/modules/SelectedSet.js +4 -13
  36. package/src/modules/Selection.js +6 -19
  37. package/src/modules/Selector.js +21 -8
  38. package/src/modules/SelectorArea.js +12 -17
  39. package/src/stores/KeyStore.js +11 -37
  40. package/src/stores/ScrollStore.js +17 -17
  41. package/src/stores/SettingsStore.js +67 -0
  42. package/src/stores/index.js +1 -0
  43. package/src/types.js +4 -7
@@ -3,26 +3,6 @@ import DragSelect from '../DragSelect'
3
3
  import '../types'
4
4
 
5
5
  export default class Interaction {
6
- /**
7
- * @type {DSArea}
8
- * @private
9
- * */
10
- _areaElement
11
- /**
12
- * @type {boolean}
13
- * @private
14
- * */
15
- _draggability
16
- /**
17
- * @type {boolean}
18
- * @private
19
- * */
20
- _immediateDrag
21
- /**
22
- * @type {string}
23
- * @private
24
- * */
25
- _selectableClass
26
6
  /** @type {boolean} */
27
7
  isInteracting
28
8
  /** @type {boolean} */
@@ -30,26 +10,13 @@ export default class Interaction {
30
10
 
31
11
  /**
32
12
  * @constructor Interaction
33
- * @param {Object} obj
34
- * @param {DragSelect} obj.DS
35
- * @param {DSArea} obj.areaElement
36
- * @param {boolean} obj.draggability
37
- * @param {boolean} obj.immediateDrag
38
- * @param {string} obj.selectableClass
13
+ * @param {{DS:DragSelect}} obj
39
14
  * @ignore
40
15
  */
41
- constructor({
42
- DS,
43
- areaElement,
44
- draggability,
45
- immediateDrag,
46
- selectableClass,
47
- }) {
48
- this._areaElement = areaElement
49
- this._draggability = draggability
50
- this._immediateDrag = immediateDrag
51
- this._selectableClass = selectableClass
16
+ constructor({ DS }) {
52
17
  this.DS = DS
18
+ // @ts-ignore: @todo: update to typescript
19
+ this.DS.subscribe('Settings:updated:area', this.init)
53
20
  this.DS.subscribe('PointerStore:updated', this.update)
54
21
  this.DS.subscribe('Selectable:click', this.onClick)
55
22
  this.DS.subscribe('Selectable:pointer', ({ event }) => this.start(event))
@@ -64,8 +31,8 @@ export default class Interaction {
64
31
  init = () => this.DS.publish('Interaction:init:pre', {})
65
32
  _init = () => {
66
33
  this.stop()
67
- this._areaElement.addEventListener('mousedown', this.start)
68
- this._areaElement.addEventListener('touchstart', this.start, {
34
+ this.DS.Area.HTMLNode.addEventListener('mousedown', this.start)
35
+ this.DS.Area.HTMLNode.addEventListener('touchstart', this.start, {
69
36
  passive: false,
70
37
  })
71
38
  this.DS.publish('Interaction:init', {})
@@ -123,16 +90,17 @@ export default class Interaction {
123
90
  */
124
91
  isDragEvent = (event) => {
125
92
  const clickedElement = /** @type {Element} */ (event.target).closest(
126
- `.${this._selectableClass}`
93
+ `.${this.DS.stores.SettingsStore.s.selectableClass}`
127
94
  )
95
+
128
96
  if (
129
- !this._draggability ||
97
+ !this.DS.stores.SettingsStore.s.draggability ||
130
98
  this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) ||
131
99
  !clickedElement
132
100
  )
133
101
  return false
134
102
 
135
- if (this._immediateDrag) {
103
+ if (this.DS.stores.SettingsStore.s.immediateDrag) {
136
104
  if (!this.DS.SelectedSet.size)
137
105
  this.DS.SelectedSet.add(/** @type {DSElement} */ (clickedElement))
138
106
  else if (!this.DS.SelectedSet.has(clickedElement)) {
@@ -166,7 +134,8 @@ export default class Interaction {
166
134
  const node = /** @type {any} */ (event.target)
167
135
  if (!SelectableSet.has(node)) return
168
136
 
169
- if (!KeyStore.isMultiSelectKeyPressed(event)) SelectedSet.clear()
137
+ if (!KeyStore.isMultiSelectKeyPressed(event))
138
+ SelectedSet.clear()
170
139
  SelectedSet.toggle(node)
171
140
 
172
141
  this.reset() // simulate mouse-up (that does not exist on keyboard)
@@ -175,8 +144,8 @@ export default class Interaction {
175
144
  stop = () => {
176
145
  this.isInteracting = false
177
146
  this.isDragging = false
178
- this._areaElement.removeEventListener('mousedown', this.start)
179
- this._areaElement.removeEventListener('touchstart', this.start, {
147
+ this.DS.Area.HTMLNode.removeEventListener('mousedown', this.start)
148
+ this.DS.Area.HTMLNode.removeEventListener('touchstart', this.start, {
180
149
  // @ts-ignore
181
150
  passive: false,
182
151
  })
@@ -5,67 +5,37 @@ import DragSelect from '../DragSelect'
5
5
  import { toArray, handleElementPositionAttribute } from '../methods'
6
6
 
7
7
  export default class SelectableSet extends Set {
8
- /**
9
- * @type {DSElements}
10
- * @private
11
- * */
12
- _initElements
13
- /**
14
- * @type {string}
15
- * @private
16
- * */
17
- _className
18
- /**
19
- * @type {string}
20
- * @private
21
- * */
22
- _hoverClassName
23
- /**
24
- * @type {boolean}
25
- * @private
26
- * */
27
- _useTransform
28
- /**
29
- * @type {boolean}
30
- * @private
31
- * */
32
- _draggability
33
-
34
8
  /**
35
9
  * @constructor SelectableSet
36
- * @param {Object} p
37
- * @param {DSInputElements} p.elements
38
- * @param {DragSelect} p.DS
39
- * @param {string} p.className
40
- * @param {string} p.hoverClassName
41
- * @param {boolean} p.useTransform
42
- * @param {boolean} p.draggability
10
+ * @param {{DS:DragSelect}} obj
43
11
  * @ignore
44
12
  */
45
- constructor({
46
- elements,
47
- className,
48
- hoverClassName,
49
- draggability,
50
- useTransform,
51
- DS,
52
- }) {
13
+ constructor({ DS }) {
53
14
  super()
54
15
  this.DS = DS
55
- this._initElements = toArray(elements)
56
- this._className = className
57
- this._hoverClassName = hoverClassName
58
- this._useTransform = useTransform
59
- this._draggability = draggability
60
-
61
16
  this.DS.subscribe('Interaction:init', this.init)
17
+ // @ts-ignore: @todo: update to typescript
18
+ this.DS.PubSub.subscribe('Settings:updated:selectables', () => {
19
+ this.clear()
20
+ this.init()
21
+ })
22
+ // @ts-ignore: @todo: update to typescript
23
+ this.DS.subscribe('Settings:updated:selectableClass', ({ settings }) => {
24
+ this.forEach((el) => {
25
+ el.classList.remove(settings['selectableClass:pre'])
26
+ el.classList.add(settings['selectableClass'])
27
+ })
28
+ })
62
29
  }
63
30
 
64
- init = () => this._initElements.forEach((el) => this.add(el))
31
+ init = () =>
32
+ toArray(this.DS.stores.SettingsStore.s.selectables).forEach((el) =>
33
+ this.add(el)
34
+ )
65
35
 
66
36
  /** @param {DSElement} element */
67
37
  add(element) {
68
- element.classList.add(this._className)
38
+ element.classList.add(this.DS.stores.SettingsStore.s.selectableClass)
69
39
  element.addEventListener('click', this._onClick)
70
40
  element.addEventListener('mousedown', this._onPointer)
71
41
  element.addEventListener('touchstart', this._onPointer, {
@@ -73,7 +43,10 @@ export default class SelectableSet extends Set {
73
43
  passive: false,
74
44
  })
75
45
 
76
- if (this._draggability && !this._useTransform)
46
+ if (
47
+ this.DS.stores.SettingsStore.s.draggability &&
48
+ !this.DS.stores.SettingsStore.s.useTransform
49
+ )
77
50
  handleElementPositionAttribute({
78
51
  computedStyle: window.getComputedStyle(element),
79
52
  node: element,
@@ -84,8 +57,8 @@ export default class SelectableSet extends Set {
84
57
 
85
58
  /** @param {DSElement} element */
86
59
  delete(element) {
87
- element.classList.remove(this._className)
88
- element.classList.remove(this._hoverClassName)
60
+ element.classList.remove(this.DS.stores.SettingsStore.s.selectableClass)
61
+ element.classList.remove(this.DS.stores.SettingsStore.s.hoverClass)
89
62
  element.removeEventListener('click', this._onClick)
90
63
  element.removeEventListener('mousedown', this._onPointer)
91
64
  element.removeEventListener('touchstart', this._onPointer, {
@@ -97,8 +70,10 @@ export default class SelectableSet extends Set {
97
70
 
98
71
  clear = () => this.forEach((el) => this.delete(el))
99
72
 
100
- _onClick = (event) => this.DS.publish(['Selectable:click:pre', 'Selectable:click'], { event })
101
- _onPointer = (event) => this.DS.publish(['Selectable:pointer:pre', 'Selectable:pointer'], { event })
73
+ _onClick = (event) =>
74
+ this.DS.publish(['Selectable:click:pre', 'Selectable:click'], { event })
75
+ _onPointer = (event) =>
76
+ this.DS.publish(['Selectable:pointer:pre', 'Selectable:pointer'], { event })
102
77
 
103
78
  /** @param {DSElements} elements */
104
79
  addAll = (elements) => elements.forEach((el) => this.add(el))
@@ -3,23 +3,14 @@ import '../types'
3
3
  import DragSelect from '../DragSelect'
4
4
 
5
5
  export default class SelectedSet extends Set {
6
- /**
7
- * @type {string}
8
- * @private
9
- * */
10
- _className
11
-
12
6
  /**
13
7
  * @constructor SelectableSet
14
- * @param {Object} p
15
- * @param {DragSelect} p.DS
16
- * @param {string} p.className
8
+ * @param {{DS:DragSelect}} obj
17
9
  * @ignore
18
10
  */
19
- constructor({ className, DS }) {
11
+ constructor({ DS }) {
20
12
  super()
21
13
  this.DS = DS
22
- this._className = className
23
14
  }
24
15
 
25
16
  /** @param {DSElement} element */
@@ -31,7 +22,7 @@ export default class SelectedSet extends Set {
31
22
  }
32
23
  this.DS.publish('Selected:added:pre', publishData)
33
24
  super.add(element)
34
- element.classList.add(this._className)
25
+ element.classList.add(this.DS.stores.SettingsStore.s.selectedClass)
35
26
  element.style.zIndex = `${(parseInt(element.style.zIndex) || 0) + 1}`
36
27
  this.DS.publish('Selected:added', publishData)
37
28
  return this
@@ -46,7 +37,7 @@ export default class SelectedSet extends Set {
46
37
  }
47
38
  this.DS.publish('Selected:removed:pre', publishData)
48
39
  const deleted = super.delete(element)
49
- element.classList.remove(this._className)
40
+ element.classList.remove(this.DS.stores.SettingsStore.s.selectedClass)
50
41
  element.style.zIndex = `${(parseInt(element.style.zIndex) || 0) - 1}`
51
42
  this.DS.publish('Selected:removed', publishData)
52
43
  return deleted
@@ -10,26 +10,13 @@ export default class Selection {
10
10
  * @private
11
11
  * */
12
12
  _prevSelectedSet
13
- /**
14
- * @type {string}
15
- * @private
16
- * */
17
- _hoverClassName
18
- /**
19
- * @type {boolean}
20
- * @private
21
- * */
22
- _multiSelectToggling
23
13
 
24
14
  /**
25
15
  * @constructor Selection
26
- * @param {{ DS:DragSelect, hoverClassName:string, multiSelectToggling:boolean }} p
16
+ * @param {{ DS:DragSelect }} p
27
17
  * @ignore
28
18
  */
29
- constructor({ DS, hoverClassName, multiSelectToggling }) {
30
- this._hoverClassName = hoverClassName
31
- this._multiSelectToggling = multiSelectToggling
32
-
19
+ constructor({ DS }) {
33
20
  this.DS = DS
34
21
  this.DS.subscribe('Interaction:start', this.start)
35
22
  this.DS.subscribe('Interaction:update', this.update)
@@ -88,8 +75,8 @@ export default class Selection {
88
75
  }
89
76
 
90
77
  const multiSelectionToggle =
91
- this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) &&
92
- this._multiSelectToggling
78
+ (this.DS.stores.KeyStore.isMultiSelectKeyPressed(event)) &&
79
+ this.DS.stores.SettingsStore.s.multiSelectToggling
93
80
 
94
81
  if (this.DS.continue) return
95
82
  select.forEach((element) =>
@@ -98,7 +85,7 @@ export default class Selection {
98
85
  force,
99
86
  multiSelectionToggle,
100
87
  SelectedSet: this.DS.SelectedSet,
101
- hoverClassName: this._hoverClassName,
88
+ hoverClassName: this.DS.stores.SettingsStore.s.hoverClass,
102
89
  })
103
90
  )
104
91
  unselect.forEach((element) =>
@@ -106,7 +93,7 @@ export default class Selection {
106
93
  element,
107
94
  force,
108
95
  SelectedSet: this.DS.SelectedSet,
109
- hoverClassName: this._hoverClassName,
96
+ hoverClassName: this.DS.stores.SettingsStore.s.hoverClass,
110
97
  PrevSelectedSet: this._prevSelectedSet,
111
98
  })
112
99
  )
@@ -18,23 +18,36 @@ export default class Selector {
18
18
 
19
19
  /**
20
20
  * @constructor Selector
21
- * @param {Object} p
22
- * @param {DragSelect} p.DS
23
- * @param {HTMLElement} p.selector
24
- * @param {string} p.selectorClass
25
- * @param {boolean} p.customStyles
21
+ * @param {{ DS:DragSelect }} p
26
22
  * @ignore
27
23
  */
28
- constructor({ DS, selector, selectorClass, customStyles }) {
24
+ constructor({ DS }) {
29
25
  this.DS = DS
30
26
 
31
- this.HTMLNode = selector || createSelectorElement(customStyles)
32
- this.HTMLNode.classList.add(selectorClass)
27
+ // @ts-ignore: @todo: update to typescript
28
+ this.DS.subscribe('Settings:updated:selectorClass', ({ settings }) => {
29
+ this.HTMLNode.classList.remove(settings['selectorClass:pre'])
30
+ this.HTMLNode.classList.add(settings['selectorClass'])
31
+ })
32
+ // @ts-ignore: @todo: update to typescript
33
+ this.DS.subscribe('Settings:updated:selector', this.attachSelector)
34
+ this.attachSelector()
33
35
 
34
36
  this.DS.subscribe('Interaction:start', this.start)
35
37
  this.DS.subscribe('Interaction:update', this.update)
36
38
  this.DS.subscribe('Interaction:end', this.stop)
37
39
  }
40
+
41
+ attachSelector = () => {
42
+ if (this.HTMLNode && this.DS.SelectorArea?.HTMLNode)
43
+ this.DS.SelectorArea.HTMLNode.removeChild(this.HTMLNode)
44
+ this.HTMLNode =
45
+ this.DS.stores.SettingsStore.s.selector ||
46
+ createSelectorElement(this.DS.stores.SettingsStore.s.customStyles)
47
+ this.HTMLNode.classList.add(this.DS.stores.SettingsStore.s.selectorClass)
48
+ if (this.HTMLNode && this.DS.SelectorArea?.HTMLNode)
49
+ this.DS.SelectorArea.HTMLNode.appendChild(this.HTMLNode)
50
+ }
38
51
 
39
52
  start = ({ isDragging }) => {
40
53
  if (isDragging) return
@@ -9,11 +9,6 @@ import {
9
9
  } from '../methods'
10
10
 
11
11
  export default class SelectorArea {
12
- /**
13
- * @type {number}
14
- * @private
15
- * */
16
- _autoScrollSpeed
17
12
  /**
18
13
  * @type {*}
19
14
  * @private
@@ -29,25 +24,25 @@ export default class SelectorArea {
29
24
  * @private
30
25
  */
31
26
  currentEdges = []
32
- /**
33
- * @type {Vect2}
34
- * @private
35
- */
36
- _overflowTolerance
37
27
 
38
28
  /**
39
29
  * @class SelectorArea
40
30
  * @constructor SelectorArea
41
- * @param {{ DS:DragSelect, selectorAreaClass:string, autoScrollSpeed:number, overflowTolerance:Vect2}} obj
31
+ * @param {{ DS:DragSelect }} obj
42
32
  * @ignore
43
33
  */
44
- constructor({ DS, selectorAreaClass, autoScrollSpeed, overflowTolerance }) {
45
- this._autoScrollSpeed = autoScrollSpeed
46
- this._overflowTolerance = overflowTolerance
34
+ constructor({ DS }) {
47
35
  this.DS = DS
48
36
 
49
- this.HTMLNode = createSelectorAreaElement(selectorAreaClass)
37
+ this.HTMLNode = createSelectorAreaElement()
38
+ // @ts-ignore: @todo: update to typescript
39
+ this.DS.subscribe('Settings:updated:selectorAreaClass', ({ settings }) => {
40
+ this.HTMLNode.classList.remove(settings['selectorAreaClass:pre'])
41
+ this.HTMLNode.classList.add(settings['selectorAreaClass'])
42
+ })
43
+ this.HTMLNode.classList.add(this.DS.stores.SettingsStore.s.selectorAreaClass)
50
44
 
45
+ this.DS.subscribe('Area:modified', this.updatePos)
51
46
  this.DS.subscribe('Area:modified', this.updatePos)
52
47
  this.DS.subscribe('Interaction:init', this.start)
53
48
  this.DS.subscribe('Interaction:start', this.startAutoScroll)
@@ -110,11 +105,11 @@ export default class SelectorArea {
110
105
  this.currentEdges = getOverflowEdges({
111
106
  elementRect: vect2.vect2rect(PointerStore.currentVal),
112
107
  containerRect: this.rect,
113
- tolerance: this._overflowTolerance,
108
+ tolerance: this.DS.stores.SettingsStore.s.overflowTolerance,
114
109
  })
115
110
 
116
111
  if (this.currentEdges.length)
117
- Area.scroll(this.currentEdges, this._autoScrollSpeed)
112
+ Area.scroll(this.currentEdges, this.DS.stores.SettingsStore.s.autoScrollSpeed)
118
113
  }
119
114
 
120
115
  stopAutoScroll = () => {
@@ -3,16 +3,6 @@ import '../types'
3
3
  import DragSelect from '../DragSelect'
4
4
 
5
5
  export default class KeyStore {
6
- /**
7
- * @type {boolean}
8
- * @private
9
- * */
10
- _multiSelectMode
11
- /**
12
- * @type {DSMultiSelectKeys}
13
- * @private
14
- * */
15
- _multiSelectKeys
16
6
  /**
17
7
  * @type {Set<string>}
18
8
  * @private
@@ -31,31 +21,11 @@ export default class KeyStore {
31
21
  /**
32
22
  * @class KeyStore
33
23
  * @constructor KeyStore
34
- * @param {{DS:DragSelect,multiSelectKeys:DSMultiSelectKeys,multiSelectMode:boolean}} p
24
+ * @param {{DS:DragSelect}} p
35
25
  * @ignore
36
26
  */
37
- constructor({ DS, multiSelectKeys, multiSelectMode }) {
27
+ constructor({ DS }) {
38
28
  this.DS = DS
39
- this._multiSelectMode = multiSelectMode
40
-
41
- // @TODO: remove after deprecation
42
- this._multiSelectKeys = multiSelectKeys.map((key) => {
43
- const deprecatedKeys = {
44
- ctrlKey: 'Control',
45
- shiftKey: 'Shift',
46
- metaKey: 'Meta',
47
- }
48
- /** @type {string} */
49
- const newName = deprecatedKeys[key]
50
- if (newName) {
51
- console.warn(
52
- `[DragSelect] ${key} is deprecated. Use "${newName}" instead. Act Now!. See docs for more info`
53
- )
54
- return newName.toLowerCase()
55
- }
56
- return key.toLowerCase()
57
- })
58
-
59
29
  this.DS.subscribe('Interaction:init', this.init)
60
30
  }
61
31
 
@@ -92,14 +62,18 @@ export default class KeyStore {
92
62
 
93
63
  /** @param {KeyboardEvent|MouseEvent|TouchEvent} [event] */
94
64
  isMultiSelectKeyPressed(event) {
95
- if (this._multiSelectMode) return true
96
- if (this.currentValues.some((key) => this._multiSelectKeys.includes(key)))
97
- return true
65
+ if(this.DS.stores.SettingsStore.s.multiSelectMode) return true
66
+ const multiSelectKeys = this.DS.stores.SettingsStore.s.multiSelectKeys.map(
67
+ (key) => key.toLocaleLowerCase()
68
+ )
98
69
  if (
99
- event &&
100
- this._multiSelectKeys.some((key) => event[this._keyMapping[key]])
70
+ this.currentValues.some((key) =>
71
+ multiSelectKeys.includes(key.toLocaleLowerCase())
72
+ )
101
73
  )
102
74
  return true
75
+ if (event && multiSelectKeys.some((key) => event[this._keyMapping[key]]))
76
+ return true
103
77
  return false
104
78
  }
105
79
 
@@ -12,10 +12,6 @@ export default class ScrollStore {
12
12
  * @type {Vect2}
13
13
  * @private */
14
14
  _currentVal
15
- /**
16
- * @type {DSArea}
17
- * @private */
18
- _areaElement
19
15
  /**
20
16
  * @type {boolean}
21
17
  * @private */
@@ -24,30 +20,34 @@ export default class ScrollStore {
24
20
  /**
25
21
  * @class ScrollStore
26
22
  * @constructor ScrollStore
27
- * @param {{ DS:DragSelect, areaElement: DSArea, zoom:number }} p
23
+ * @param {{ DS:DragSelect }} p
28
24
  * @ignore
29
25
  */
30
- constructor({ DS, areaElement, zoom }) {
31
- this._areaElement = areaElement
26
+ constructor({ DS }) {
32
27
  this.DS = DS
33
- this.zoom = zoom
34
-
35
28
  this.DS.subscribe('Interaction:init', this.init)
36
29
  this.DS.subscribe('Interaction:start', () => this.start())
37
30
  this.DS.subscribe('Interaction:end', () => this.reset())
38
31
  }
39
32
 
40
- init = () => this._areaElement.addEventListener('scroll', this.update)
33
+ init = () =>
34
+ this.DS.stores.SettingsStore.s.area.addEventListener('scroll', this.update)
41
35
 
42
36
  start = () => {
43
- this._currentVal = this._initialVal = getCurrentScroll(this._areaElement)
44
- this._areaElement.addEventListener('scroll', this.update)
37
+ this._currentVal = this._initialVal = getCurrentScroll(
38
+ this.DS.stores.SettingsStore.s.area
39
+ )
40
+ this.DS.stores.SettingsStore.s.area.addEventListener('scroll', this.update)
45
41
  }
46
42
 
47
- update = () => (this._currentVal = getCurrentScroll(this._areaElement))
43
+ update = () =>
44
+ (this._currentVal = getCurrentScroll(this.DS.stores.SettingsStore.s.area))
48
45
 
49
46
  stop = () => {
50
- this._areaElement.removeEventListener('scroll', this.update)
47
+ this.DS.stores.SettingsStore.s.area.removeEventListener(
48
+ 'scroll',
49
+ this.update
50
+ )
51
51
  this._initialVal = { x: 0, y: 0 }
52
52
  this._canScroll = null
53
53
  }
@@ -59,14 +59,14 @@ export default class ScrollStore {
59
59
 
60
60
  get canScroll() {
61
61
  if (typeof this._canScroll === 'boolean') return this._canScroll
62
- return (this._canScroll = canScroll(this._areaElement))
62
+ return (this._canScroll = canScroll(this.DS.stores.SettingsStore.s.area))
63
63
  }
64
64
 
65
65
  get scrollAmount() {
66
66
  const scrollDiff = vect2.calc(this.currentVal, '-', this.initialVal)
67
67
 
68
68
  // if area is zoomed, the scroll values are skewed, we need to fix that manually :(
69
- const zoom = vect2.num2vect(this.zoom)
69
+ const zoom = vect2.num2vect(this.DS.stores.SettingsStore.s.zoom)
70
70
  const zoomScroll = vect2.calc(
71
71
  vect2.calc(scrollDiff, '*', zoom),
72
72
  '-',
@@ -86,7 +86,7 @@ export default class ScrollStore {
86
86
 
87
87
  get currentVal() {
88
88
  if (!this._currentVal)
89
- this._currentVal = getCurrentScroll(this._areaElement)
89
+ this._currentVal = getCurrentScroll(this.DS.stores.SettingsStore.s.area)
90
90
  return this._currentVal
91
91
  }
92
92
  }
@@ -0,0 +1,67 @@
1
+ // @ts-check
2
+ import '../types'
3
+ import DragSelect from '../DragSelect'
4
+
5
+ import { hydrateSettings } from '../methods'
6
+
7
+ export default class SettingsStore {
8
+ /**
9
+ * @type {Settings}
10
+ * @private
11
+ * */
12
+ _settings = {}
13
+
14
+ /**
15
+ * Holds the settings and their previous value `:pre`
16
+ * @example {
17
+ * autoScrollSpeed: 3,
18
+ * 'autoScrollSpeed:pre': 5
19
+ * }
20
+ * @type {Settings}
21
+ * */
22
+ s = {}
23
+
24
+ /**
25
+ * @class ScrollStore
26
+ * @constructor ScrollStore
27
+ * @param {{ DS:DragSelect, settings:Settings }} p
28
+ * @ignore
29
+ */
30
+ constructor({
31
+ DS,
32
+ settings
33
+ }) {
34
+ this.DS = DS
35
+ this.DS.subscribe('Settings:updated:pre', this._update)
36
+ this.update({ settings, init: true })
37
+ }
38
+
39
+ /** @param {{settings: Settings, init?: boolean}} props */
40
+ update = ({ settings, init }) =>
41
+ this.DS.publish('Settings:updated:pre', { settings, ...init ? { init } : {} })
42
+ /** @param {{settings: Settings, init: boolean}} props */
43
+ _update = ({ settings, init }) => {
44
+ const _settings = hydrateSettings(settings, init)
45
+ for (const [key, value] of Object.entries(_settings)) {
46
+ if (!(key in this._settings)) {
47
+ Object.defineProperty(this.s, key, {
48
+ get: () => this._settings[key],
49
+ set: (newValue) => this.update({ settings: { [key]: newValue } }),
50
+ })
51
+ }
52
+
53
+ this._settings[`${key}:pre`] = this._settings[key]
54
+ this._settings[key] = value
55
+
56
+ const update = {
57
+ settings: {
58
+ [key]: this._settings[key],
59
+ [`${key}:pre`]: this._settings[`${key}:pre`],
60
+ },
61
+ }
62
+ this.DS.publish('Settings:updated', update)
63
+ // @ts-ignore: @todo: update to typescript
64
+ this.DS.publish(`Settings:updated:${key}`, update)
65
+ }
66
+ }
67
+ }
@@ -1,3 +1,4 @@
1
1
  export { default as KeyStore } from './KeyStore'
2
2
  export { default as PointerStore } from './PointerStore'
3
3
  export { default as ScrollStore } from './ScrollStore'
4
+ export { default as SettingsStore } from './SettingsStore'