dragselect 2.2.2 → 2.3.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.
@@ -1,6 +1,6 @@
1
1
  /***
2
2
 
3
- ~~~ Version 2.2.2 ~~~
3
+ ~~~ Version 2.3.0 ~~~
4
4
 
5
5
  ******************************************
6
6
 
@@ -416,9 +416,9 @@
416
416
 
417
417
  /** @typedef {'Interaction:init'|'Interaction:start'|'Interaction:end'|'Interaction:update'|'Area:modified'|'Area:scroll'|'PointerStore:updated'|'Selected:added'|'Selected:removed'|'Selectable:click'|'Selectable:pointer'|'KeyStore:down'|'KeyStore:up'} DSInternalEventNames */
418
418
 
419
- /** @typedef {'Interaction:init:pre'|'Interaction:start:pre'|'Interaction:end:pre'|'Interaction:update:pre'|'Area:modified:pre'|'Area:scroll:pre'|'PointerStore:updated:pre'|'Selected:added:pre'|'Selected:removed:pre'|'Selectable:click:pre'|'Selectable:pointer:pre'|'KeyStore:down:pre'|'KeyStore:up:pre'|'Drag:keyboardDrag:pre'} DSInternalEventNamesPre */
419
+ /** @typedef {'Interaction:init:pre'|'Interaction:start:pre'|'Interaction:end:pre'|'Interaction:update:pre'|'Area:modified:pre'|'Area:scroll:pre'|'PointerStore:updated:pre'|'Selected:added:pre'|'Selected:removed:pre'|'Selectable:click:pre'|'Selectable:pointer:pre'|'KeyStore:down:pre'|'KeyStore:up:pre'} DSInternalEventNamesPre */
420
420
 
421
- /** @typedef {DSEventNames|DSInternalEventNames} DSCallbackNames the name of the callback */
421
+ /** @typedef {DSEventNames|DSInternalEventNames|DSInternalEventNamesPre} DSCallbackNames the name of the callback */
422
422
 
423
423
  /** @typedef {{top:number,left:number,bottom:number,right:number,width:number,height:number}} DSBoundingRect */
424
424
 
@@ -1261,12 +1261,6 @@
1261
1261
  // event, isDraggingKeyboard
1262
1262
  'Interaction:end': [{
1263
1263
  name: 'callback'
1264
- }],
1265
- // event, isDraggingKeyboard
1266
- 'Drag:keyboardDrag': [{
1267
- name: 'dragstart'
1268
- }, {
1269
- name: 'dragmove'
1270
1264
  }] // event, isDraggingKeyboard
1271
1265
 
1272
1266
  };
@@ -1320,6 +1314,13 @@
1320
1314
  element.style.height = "".concat(pos.height, "px");
1321
1315
  });
1322
1316
 
1317
+ /**
1318
+ * @typedef {Object} AreaProps
1319
+ * @property {DSArea} area
1320
+ * @property {PubSub} PS
1321
+ * @property {number} zoom
1322
+ */
1323
+
1323
1324
  var Area = /*#__PURE__*/function () {
1324
1325
  /**
1325
1326
  * @type {DSModificationCallback}
@@ -1363,16 +1364,13 @@
1363
1364
 
1364
1365
  /**
1365
1366
  * @constructor Area
1366
- * @param {Object} obj
1367
- * @param {DSArea} obj.area
1368
- * @param {PubSub} obj.PS
1369
- * @param {number} obj.zoom
1367
+ * @param {AreaProps} settings
1370
1368
  * @ignore
1371
1369
  */
1372
1370
  function Area(_ref) {
1373
1371
  var _this = this;
1374
1372
 
1375
- var area = _ref.area,
1373
+ var _area = _ref.area,
1376
1374
  PS = _ref.PS,
1377
1375
  zoom = _ref.zoom;
1378
1376
 
@@ -1394,6 +1392,26 @@
1394
1392
 
1395
1393
  _defineProperty(this, "_rect", void 0);
1396
1394
 
1395
+ _defineProperty(this, "setArea", function (area) {
1396
+ _this._node = area;
1397
+ handleElementPositionAttribute({
1398
+ computedStyle: _this.computedStyle,
1399
+ node: _this._node
1400
+ }); // first immediate debounce to update values after dom-update
1401
+
1402
+ setTimeout(function () {
1403
+ _this.PubSub.publish('Area:modified:pre', {
1404
+ item: _this
1405
+ });
1406
+
1407
+ _this.reset();
1408
+
1409
+ _this.PubSub.publish('Area:modified', {
1410
+ item: _this
1411
+ });
1412
+ });
1413
+ });
1414
+
1397
1415
  _defineProperty(this, "start", function () {
1398
1416
  addModificationObservers(_this.parentNodes, _this._modificationCallback, _this._modificationObserver);
1399
1417
  });
@@ -1412,42 +1430,40 @@
1412
1430
  });
1413
1431
 
1414
1432
  _defineProperty(this, "scroll", function (directions, multiplier) {
1415
- scrollElement(_this._node, directions, multiplier);
1416
-
1417
- _this.PubSub.publish('Area:scroll', {
1433
+ var data = {
1418
1434
  scroll_directions: directions,
1419
1435
  scroll_multiplier: multiplier
1420
- });
1436
+ };
1437
+
1438
+ _this.PubSub.publish('Area:scroll:pre', data);
1439
+
1440
+ scrollElement(_this._node, directions, multiplier);
1441
+
1442
+ _this.PubSub.publish('Area:scroll', data);
1421
1443
  });
1422
1444
 
1423
- this._node = area;
1424
1445
  this._zoom = zoom;
1425
1446
  this.PubSub = PS;
1426
- handleElementPositionAttribute({
1427
- computedStyle: this.computedStyle,
1428
- node: this._node
1429
- });
1447
+ this.setArea(_area);
1430
1448
  this._modificationCallback = debounce(function (event) {
1431
- _this.reset();
1432
-
1433
- _this.PubSub.publish('Area:modified', {
1449
+ _this.PubSub.publish('Area:modified:pre', {
1434
1450
  event: event,
1435
1451
  item: _this
1436
1452
  });
1437
- }, 60);
1438
- this._modificationObserver = new MutationObserver(this._modificationCallback); // first immediate debounce to update values after dom-update
1439
1453
 
1440
- setTimeout(function () {
1441
1454
  _this.reset();
1442
1455
 
1443
1456
  _this.PubSub.publish('Area:modified', {
1444
1457
  event: event,
1445
1458
  item: _this
1446
1459
  });
1447
- });
1460
+ }, 60);
1461
+ this._modificationObserver = new MutationObserver(this._modificationCallback);
1448
1462
  this.PubSub.subscribe('Interaction:init', this.start);
1449
1463
  this.PubSub.subscribe('Interaction:end', this.reset);
1450
1464
  }
1465
+ /** @param {DSArea} area */
1466
+
1451
1467
 
1452
1468
  _createClass(Area, [{
1453
1469
  key: "HTMLNode",
@@ -1624,12 +1640,13 @@
1624
1640
  var event = _ref2.event,
1625
1641
  key = _ref2.key;
1626
1642
  if (!_this._keyboardDrag || !_this._dragKeysFlat.includes(key) || !_this.DS.SelectedSet.size || !_this._draggability || _this.DS["continue"]) return;
1627
-
1628
- _this.DS.publish('Interaction:start', {
1643
+ var publishData = {
1629
1644
  event: event,
1630
1645
  isDragging: true,
1631
1646
  isDraggingKeyboard: true
1632
- });
1647
+ };
1648
+
1649
+ _this.DS.publish(['Interaction:start:pre', 'Interaction:start'], publishData);
1633
1650
 
1634
1651
  _this._elements = _this.DS.getSelection();
1635
1652
 
@@ -1655,23 +1672,20 @@
1655
1672
  });
1656
1673
  });
1657
1674
 
1658
- _this.DS.publish('Interaction:update', {
1659
- event: event,
1660
- isDragging: true,
1661
- isDraggingKeyboard: true
1662
- });
1675
+ _this.DS.publish(['Interaction:update:pre', 'Interaction:update'], publishData);
1663
1676
  });
1664
1677
 
1665
1678
  _defineProperty(this, "keyboardEnd", function (_ref3) {
1666
1679
  var event = _ref3.event,
1667
1680
  key = _ref3.key;
1668
1681
  if (!_this._keyboardDrag || !_this._dragKeysFlat.includes(key) || !_this.DS.SelectedSet.size || !_this._draggability) return;
1669
-
1670
- _this.DS.publish('Interaction:end', {
1682
+ var publishData = {
1671
1683
  event: event,
1672
1684
  isDragging: _this._draggability,
1673
1685
  isDraggingKeyboard: true
1674
- });
1686
+ };
1687
+
1688
+ _this.DS.publish(['Interaction:end:pre', 'Interaction:end'], publishData);
1675
1689
  });
1676
1690
 
1677
1691
  _defineProperty(this, "start", function (_ref4) {
@@ -1822,6 +1836,10 @@
1822
1836
  _defineProperty(this, "isDragging", void 0);
1823
1837
 
1824
1838
  _defineProperty(this, "init", function () {
1839
+ return _this.DS.publish('Interaction:init:pre', {});
1840
+ });
1841
+
1842
+ _defineProperty(this, "_init", function () {
1825
1843
  _this.stop();
1826
1844
 
1827
1845
  _this._areaElement.addEventListener('mousedown', _this.start);
@@ -1834,6 +1852,13 @@
1834
1852
  });
1835
1853
 
1836
1854
  _defineProperty(this, "start", function (event) {
1855
+ return _this.DS.publish('Interaction:start:pre', {
1856
+ event: event,
1857
+ isDragging: _this.isDragging
1858
+ });
1859
+ });
1860
+
1861
+ _defineProperty(this, "_start", function (event) {
1837
1862
  if (event.type === 'touchstart') event.preventDefault(); // Call preventDefault() to prevent double click issue, see https://github.com/ThibaultJanBeyer/DragSelect/pull/29 & https://developer.mozilla.org/vi/docs/Web/API/Touch_events/Supporting_both_TouchEvent_and_MouseEvent
1838
1863
 
1839
1864
  if (!_this._canInteract(event)) return;
@@ -1878,8 +1903,7 @@
1878
1903
  PointerStore = _this$DS$stores.PointerStore,
1879
1904
  KeyStore = _this$DS$stores.KeyStore,
1880
1905
  SelectableSet = _this$DS.SelectableSet,
1881
- SelectedSet = _this$DS.SelectedSet,
1882
- publish = _this$DS.publish;
1906
+ SelectedSet = _this$DS.SelectedSet;
1883
1907
  PointerStore.start(event);
1884
1908
  var node =
1885
1909
  /** @type {any} */
@@ -1887,10 +1911,9 @@
1887
1911
  if (!SelectableSet.has(node)) return;
1888
1912
  if (!KeyStore.isMultiSelectKeyPressed(event)) SelectedSet.clear();
1889
1913
  SelectedSet.toggle(node);
1890
- publish('Interaction:end', {
1891
- event: event,
1892
- isDragging: _this.isDragging
1893
- }); // simulate mouse-up (that does not exist on keyboard)
1914
+
1915
+ _this.reset(); // simulate mouse-up (that does not exist on keyboard)
1916
+
1894
1917
  });
1895
1918
 
1896
1919
  _defineProperty(this, "stop", function () {
@@ -1912,7 +1935,7 @@
1912
1935
  var event = _ref3.event,
1913
1936
  scroll_directions = _ref3.scroll_directions,
1914
1937
  scroll_multiplier = _ref3.scroll_multiplier;
1915
- if (_this.isInteracting) _this.DS.publish('Interaction:update', {
1938
+ if (_this.isInteracting) _this.DS.publish(['Interaction:update:pre', 'Interaction:update'], {
1916
1939
  event: event,
1917
1940
  scroll_directions: scroll_directions,
1918
1941
  scroll_multiplier: scroll_multiplier,
@@ -1921,6 +1944,13 @@
1921
1944
  });
1922
1945
 
1923
1946
  _defineProperty(this, "reset", function (event) {
1947
+ return _this.DS.publish('Interaction:end:pre', {
1948
+ event: event,
1949
+ isDragging: _this.isDragging
1950
+ });
1951
+ });
1952
+
1953
+ _defineProperty(this, "_reset", function (event) {
1924
1954
  var isDragging = _this.isDragging;
1925
1955
 
1926
1956
  _this.stop();
@@ -1943,6 +1973,15 @@
1943
1973
  var event = _ref4.event;
1944
1974
  return _this.start(event);
1945
1975
  });
1976
+ this.DS.subscribe('Interaction:start:pre', function (_ref5) {
1977
+ var event = _ref5.event;
1978
+ return _this._start(event);
1979
+ });
1980
+ this.DS.subscribe('Interaction:init:pre', this._init);
1981
+ this.DS.subscribe('Interaction:end:pre', function (_ref6) {
1982
+ var event = _ref6.event;
1983
+ return _this._reset(event);
1984
+ });
1946
1985
  this.DS.subscribe('Area:scroll', this.update);
1947
1986
  }
1948
1987
 
@@ -1980,9 +2019,11 @@
1980
2019
  return Interaction;
1981
2020
  }();
1982
2021
 
1983
- var PubSub = function PubSub() {
2022
+ var PubSub = function PubSub(_ref) {
1984
2023
  var _this = this;
1985
2024
 
2025
+ var DS = _ref.DS;
2026
+
1986
2027
  _classCallCheck(this, PubSub);
1987
2028
 
1988
2029
  _defineProperty(this, "subscribers", {});
@@ -2002,17 +2043,44 @@
2002
2043
  });
2003
2044
 
2004
2045
  _defineProperty(this, "publish", function (eventName, data) {
2005
- var _this$subscribers$, _this$subscribers$eve;
2046
+ if (Array.isArray(eventName)) eventName.forEach(function (name) {
2047
+ return _this._publish(name, data);
2048
+ });else _this._publish(eventName, data);
2049
+ });
2006
2050
 
2007
- if (!Array.isArray(_this.subscribers[eventName])) return;
2008
- (_this$subscribers$ = _this.subscribers["".concat(eventName, ":pre")]) === null || _this$subscribers$ === void 0 ? void 0 : _this$subscribers$.forEach(function (callback) {
2009
- return callback(data);
2010
- });
2011
- (_this$subscribers$eve = _this.subscribers[eventName]) === null || _this$subscribers$eve === void 0 ? void 0 : _this$subscribers$eve.forEach(function (callback) {
2012
- return callback(data);
2013
- });
2051
+ _defineProperty(this, "_publish", function (eventName, data) {
2052
+ var subscribers = _this.subscribers[eventName];
2053
+ if (!Array.isArray(subscribers)) return;
2054
+ if (eventName.includes(":pre")) _this._handlePrePublish(subscribers, data);else _this._handlePublish(subscribers, data);
2014
2055
  });
2015
- };
2056
+
2057
+ _defineProperty(this, "_handlePublish", function (subscribers, data) {
2058
+ for (var i = 0, il = subscribers.length; i < il; i++) {
2059
+ if (_this.DS.stopped) return;
2060
+ subscribers[i](data);
2061
+ }
2062
+ });
2063
+
2064
+ _defineProperty(this, "_handlePrePublish", function (subscribers, data) {
2065
+ var i = subscribers.length;
2066
+
2067
+ while (i--) {
2068
+ if (_this.DS.stopped) return;
2069
+ subscribers[i](data);
2070
+ }
2071
+ });
2072
+
2073
+ this.DS = DS;
2074
+ }
2075
+ /**
2076
+ * Subscribe to an event
2077
+ * @memberof DragSelect#
2078
+ * @function subscribe
2079
+ * @param {DSCallbackNames} eventName
2080
+ * @param {DSCallback} callback
2081
+ * @returns {number} event id, can be used to unsubscribe more efficiently
2082
+ */
2083
+ ;
2016
2084
 
2017
2085
  var SelectableSet = /*#__PURE__*/function (_Set) {
2018
2086
  _inherits(SelectableSet, _Set);
@@ -2092,13 +2160,13 @@
2092
2160
  });
2093
2161
 
2094
2162
  _defineProperty(_assertThisInitialized(_this), "_onClick", function (event) {
2095
- return _this.DS.publish('Selectable:click', {
2163
+ return _this.DS.publish(['Selectable:click:pre', 'Selectable:click'], {
2096
2164
  event: event
2097
2165
  });
2098
2166
  });
2099
2167
 
2100
2168
  _defineProperty(_assertThisInitialized(_this), "_onPointer", function (event) {
2101
- return _this.DS.publish('Selectable:pointer', {
2169
+ return _this.DS.publish(['Selectable:pointer:pre', 'Selectable:pointer'], {
2102
2170
  event: event
2103
2171
  });
2104
2172
  });
@@ -2230,15 +2298,17 @@
2230
2298
  key: "add",
2231
2299
  value: function add(element) {
2232
2300
  if (_get(_getPrototypeOf(SelectedSet.prototype), "has", this).call(this, element)) return;
2301
+ var publishData = {
2302
+ items: this.elements,
2303
+ item: element
2304
+ };
2305
+ this.DS.publish('Selected:added:pre', publishData);
2233
2306
 
2234
2307
  _get(_getPrototypeOf(SelectedSet.prototype), "add", this).call(this, element);
2235
2308
 
2236
2309
  element.classList.add(this._className);
2237
- this.DS.publish('Selected:added', {
2238
- items: this.elements,
2239
- item: element
2240
- });
2241
2310
  element.style.zIndex = "".concat((parseInt(element.style.zIndex) || 0) + 1);
2311
+ this.DS.publish('Selected:added', publishData);
2242
2312
  return this;
2243
2313
  }
2244
2314
  /** @param {DSElement} element */
@@ -2247,15 +2317,17 @@
2247
2317
  key: "delete",
2248
2318
  value: function _delete(element) {
2249
2319
  if (!_get(_getPrototypeOf(SelectedSet.prototype), "has", this).call(this, element)) return;
2320
+ var publishData = {
2321
+ items: this.elements,
2322
+ item: element
2323
+ };
2324
+ this.DS.publish('Selected:removed:pre', publishData);
2250
2325
 
2251
2326
  var deleted = _get(_getPrototypeOf(SelectedSet.prototype), "delete", this).call(this, element);
2252
2327
 
2253
2328
  element.classList.remove(this._className);
2254
- this.DS.publish('Selected:removed', {
2255
- items: this.elements,
2256
- item: element
2257
- });
2258
2329
  element.style.zIndex = "".concat((parseInt(element.style.zIndex) || 0) - 1);
2330
+ this.DS.publish('Selected:removed', publishData);
2259
2331
  return deleted;
2260
2332
  }
2261
2333
  }, {
@@ -2357,6 +2429,7 @@
2357
2429
 
2358
2430
  var multiSelectionToggle = _this.DS.stores.KeyStore.isMultiSelectKeyPressed(event) && _this._multiSelectToggling;
2359
2431
 
2432
+ if (_this.DS["continue"]) return;
2360
2433
  select.forEach(function (element) {
2361
2434
  return handleSelection({
2362
2435
  element: element,
@@ -2699,6 +2772,11 @@
2699
2772
  _defineProperty(this, "keydown", function (event) {
2700
2773
  var key = event.key.toLowerCase();
2701
2774
 
2775
+ _this.DS.publish('KeyStore:down:pre', {
2776
+ event: event,
2777
+ key: key
2778
+ });
2779
+
2702
2780
  _this._currentValues.add(key);
2703
2781
 
2704
2782
  _this.DS.publish('KeyStore:down', {
@@ -2710,6 +2788,11 @@
2710
2788
  _defineProperty(this, "keyup", function (event) {
2711
2789
  var key = event.key.toLowerCase();
2712
2790
 
2791
+ _this.DS.publish('KeyStore:up:pre', {
2792
+ event: event,
2793
+ key: key
2794
+ });
2795
+
2713
2796
  _this._currentValues["delete"](key);
2714
2797
 
2715
2798
  _this.DS.publish('KeyStore:up', {
@@ -2864,6 +2947,11 @@
2864
2947
 
2865
2948
  _defineProperty(this, "update", function (event) {
2866
2949
  if (!event) return;
2950
+
2951
+ _this.DS.publish('PointerStore:updated:pre', {
2952
+ event: event
2953
+ });
2954
+
2867
2955
  _this.currentVal = _this.getPointerPosition(event);
2868
2956
  if (!_this._isMouseInteraction) return;
2869
2957
 
@@ -3206,7 +3294,9 @@
3206
3294
  _defineProperty(this, "continue", false);
3207
3295
 
3208
3296
  _defineProperty(this, "start", function () {
3209
- return _this.Interaction.init();
3297
+ _this.stopped = false;
3298
+
3299
+ _this.Interaction.init();
3210
3300
  });
3211
3301
 
3212
3302
  _defineProperty(this, "break", function () {
@@ -3253,7 +3343,9 @@
3253
3343
  return _this.Interaction.isDragging;
3254
3344
  });
3255
3345
 
3256
- this.PubSub = new PubSub();
3346
+ this.PubSub = new PubSub({
3347
+ DS: this
3348
+ });
3257
3349
  this.subscribe = this.PubSub.subscribe;
3258
3350
  this.unsubscribe = this.PubSub.unsubscribe;
3259
3351
  this.publish = this.PubSub.publish;
@@ -3458,6 +3550,7 @@
3458
3550
  this.stores.ScrollStore.stop();
3459
3551
  if (remove) this.SelectableSet.clear();
3460
3552
  if (fromSelection) this.SelectedSet.clear();
3553
+ this.stopped = true;
3461
3554
  }
3462
3555
  /**
3463
3556
  * Utility to override DragSelect internal functionality: