diff --git a/src/js/textmode.js b/src/js/textmode.js index 33db742..2c8d0da 100644 --- a/src/js/textmode.js +++ b/src/js/textmode.js @@ -691,7 +691,7 @@ textmode.getTextSelection = function () { }; /** - * Callback registraion for selection changed + * Callback registraion for selection change * @param {selectionCallback} callback * * @callback selectionCallback diff --git a/src/js/treemode.js b/src/js/treemode.js index 78d28d4..42cb7c7 100644 --- a/src/js/treemode.js +++ b/src/js/treemode.js @@ -1027,7 +1027,7 @@ treemode._onMultiSelect = function (event) { } // deselect previous selection - this.deselect(); + this.deselect(false, false); // find the selected nodes in the range from first to last var start = this.multiselection.start; @@ -1068,8 +1068,10 @@ treemode._onMultiSelectEnd = function (event) { * deselect currently selected nodes * @param {boolean} [clearStartAndEnd=false] If true, the `start` and `end` * state is cleared too. + * @param {boolean} [noChangeEvent=false] If true, do not emit onSelectionChanged event */ -treemode.deselect = function (clearStartAndEnd) { +treemode.deselect = function (clearStartAndEnd, noChangeEvent) { + var selectionChanged = !!this.multiselection.nodes.length; this.multiselection.nodes.forEach(function (node) { node.setSelected(false); }); @@ -1079,6 +1081,12 @@ treemode.deselect = function (clearStartAndEnd) { this.multiselection.start = null; this.multiselection.end = null; } + + if (!noChangeEvent && selectionChanged) { + if (this._selectionChangedHandler) { + this._selectionChangedHandler([]); + } + } }; /** @@ -1091,7 +1099,7 @@ treemode.select = function (nodes) { } if (nodes) { - this.deselect(); + this.deselect(false, false); this.multiselection.nodes = nodes.slice(0); @@ -1099,6 +1107,10 @@ treemode.select = function (nodes) { nodes.forEach(function (node) { node.setSelected(true, node === first); }); + + if (this._selectionChangedHandler) { + this._selectionChangedHandler(nodes); + } } }; @@ -1322,6 +1334,27 @@ treemode.showContextMenu = function (anchor, onClose) { menu.show(anchor, this.content); }; +/** + * Get current selected nodes + * @return {Array} + */ +treemode.getNodeSelection = function () { + return this.multiselection.nodes || []; +}; + +/** + * Callback registraion for selection change + * @param {selectionCallback} callback + * + * @callback selectionCallback + * @param {Array} nodes selected nodes + */ +treemode.onNodeSelectionChanged = function (callback) { + if (typeof callback === 'function') { + this._selectionChangedHandler = util.debounce(callback, this.DEBOUNCE_INTERVAL); + } +}; + // define modes module.exports = [