Mercurial > repos > mingchen0919 > aurora_star
view vakata-jstree-3.3.5/src/jstree.checkbox.js @ 0:25602263cff0 draft default tip
planemo upload commit 841d8b22bf9f1aaed6bfe8344b60617f45b275b2-dirty
author | mingchen0919 |
---|---|
date | Sun, 30 Dec 2018 13:11:48 -0500 |
parents | |
children |
line wrap: on
line source
/** * ### Checkbox plugin * * This plugin renders checkbox icons in front of each node, making multiple selection much easier. * It also supports tri-state behavior, meaning that if a node has a few of its children checked it will be rendered as undetermined, and state will be propagated up. */ /*globals jQuery, define, exports, require, document */ (function (factory) { "use strict"; if (typeof define === 'function' && define.amd) { define('jstree.checkbox', ['jquery','jstree'], factory); } else if(typeof exports === 'object') { factory(require('jquery'), require('jstree')); } else { factory(jQuery, jQuery.jstree); } }(function ($, jstree, undefined) { "use strict"; if($.jstree.plugins.checkbox) { return; } var _i = document.createElement('I'); _i.className = 'jstree-icon jstree-checkbox'; _i.setAttribute('role', 'presentation'); /** * stores all defaults for the checkbox plugin * @name $.jstree.defaults.checkbox * @plugin checkbox */ $.jstree.defaults.checkbox = { /** * a boolean indicating if checkboxes should be visible (can be changed at a later time using `show_checkboxes()` and `hide_checkboxes`). Defaults to `true`. * @name $.jstree.defaults.checkbox.visible * @plugin checkbox */ visible : true, /** * a boolean indicating if checkboxes should cascade down and have an undetermined state. Defaults to `true`. * @name $.jstree.defaults.checkbox.three_state * @plugin checkbox */ three_state : true, /** * a boolean indicating if clicking anywhere on the node should act as clicking on the checkbox. Defaults to `true`. * @name $.jstree.defaults.checkbox.whole_node * @plugin checkbox */ whole_node : true, /** * a boolean indicating if the selected style of a node should be kept, or removed. Defaults to `true`. * @name $.jstree.defaults.checkbox.keep_selected_style * @plugin checkbox */ keep_selected_style : true, /** * This setting controls how cascading and undetermined nodes are applied. * If 'up' is in the string - cascading up is enabled, if 'down' is in the string - cascading down is enabled, if 'undetermined' is in the string - undetermined nodes will be used. * If `three_state` is set to `true` this setting is automatically set to 'up+down+undetermined'. Defaults to ''. * @name $.jstree.defaults.checkbox.cascade * @plugin checkbox */ cascade : '', /** * This setting controls if checkbox are bound to the general tree selection or to an internal array maintained by the checkbox plugin. Defaults to `true`, only set to `false` if you know exactly what you are doing. * @name $.jstree.defaults.checkbox.tie_selection * @plugin checkbox */ tie_selection : true, /** * This setting controls if cascading down affects disabled checkboxes * @name $.jstree.defaults.checkbox.cascade_to_disabled * @plugin checkbox */ cascade_to_disabled : true, /** * This setting controls if cascading down affects hidden checkboxes * @name $.jstree.defaults.checkbox.cascade_to_hidden * @plugin checkbox */ cascade_to_hidden : true }; $.jstree.plugins.checkbox = function (options, parent) { this.bind = function () { parent.bind.call(this); this._data.checkbox.uto = false; this._data.checkbox.selected = []; if(this.settings.checkbox.three_state) { this.settings.checkbox.cascade = 'up+down+undetermined'; } this.element .on("init.jstree", $.proxy(function () { this._data.checkbox.visible = this.settings.checkbox.visible; if(!this.settings.checkbox.keep_selected_style) { this.element.addClass('jstree-checkbox-no-clicked'); } if(this.settings.checkbox.tie_selection) { this.element.addClass('jstree-checkbox-selection'); } }, this)) .on("loading.jstree", $.proxy(function () { this[ this._data.checkbox.visible ? 'show_checkboxes' : 'hide_checkboxes' ](); }, this)); if(this.settings.checkbox.cascade.indexOf('undetermined') !== -1) { this.element .on('changed.jstree uncheck_node.jstree check_node.jstree uncheck_all.jstree check_all.jstree move_node.jstree copy_node.jstree redraw.jstree open_node.jstree', $.proxy(function () { // only if undetermined is in setting if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); } this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50); }, this)); } if(!this.settings.checkbox.tie_selection) { this.element .on('model.jstree', $.proxy(function (e, data) { var m = this._model.data, p = m[data.parent], dpc = data.nodes, i, j; for(i = 0, j = dpc.length; i < j; i++) { m[dpc[i]].state.checked = m[dpc[i]].state.checked || (m[dpc[i]].original && m[dpc[i]].original.state && m[dpc[i]].original.state.checked); if(m[dpc[i]].state.checked) { this._data.checkbox.selected.push(dpc[i]); } } }, this)); } if(this.settings.checkbox.cascade.indexOf('up') !== -1 || this.settings.checkbox.cascade.indexOf('down') !== -1) { this.element .on('model.jstree', $.proxy(function (e, data) { var m = this._model.data, p = m[data.parent], dpc = data.nodes, chd = [], c, i, j, k, l, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection; if(s.indexOf('down') !== -1) { // apply down if(p.state[ t ? 'selected' : 'checked' ]) { for(i = 0, j = dpc.length; i < j; i++) { m[dpc[i]].state[ t ? 'selected' : 'checked' ] = true; } this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(dpc); } else { for(i = 0, j = dpc.length; i < j; i++) { if(m[dpc[i]].state[ t ? 'selected' : 'checked' ]) { for(k = 0, l = m[dpc[i]].children_d.length; k < l; k++) { m[m[dpc[i]].children_d[k]].state[ t ? 'selected' : 'checked' ] = true; } this._data[ t ? 'core' : 'checkbox' ].selected = this._data[ t ? 'core' : 'checkbox' ].selected.concat(m[dpc[i]].children_d); } } } } if(s.indexOf('up') !== -1) { // apply up for(i = 0, j = p.children_d.length; i < j; i++) { if(!m[p.children_d[i]].children.length) { chd.push(m[p.children_d[i]].parent); } } chd = $.vakata.array_unique(chd); for(k = 0, l = chd.length; k < l; k++) { p = m[chd[k]]; while(p && p.id !== $.jstree.root) { c = 0; for(i = 0, j = p.children.length; i < j; i++) { c += m[p.children[i]].state[ t ? 'selected' : 'checked' ]; } if(c === j) { p.state[ t ? 'selected' : 'checked' ] = true; this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id); tmp = this.get_node(p, true); if(tmp && tmp.length) { tmp.attr('aria-selected', true).children('.jstree-anchor').addClass( t ? 'jstree-clicked' : 'jstree-checked'); } } else { break; } p = this.get_node(p.parent); } } } this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected); }, this)) .on(this.settings.checkbox.tie_selection ? 'select_node.jstree' : 'check_node.jstree', $.proxy(function (e, data) { var self = this, obj = data.node, m = this._model.data, par = this.get_node(obj.parent), i, j, c, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection, sel = {}, cur = this._data[ t ? 'core' : 'checkbox' ].selected; for (i = 0, j = cur.length; i < j; i++) { sel[cur[i]] = true; } // apply down if(s.indexOf('down') !== -1) { //this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_unique(this._data[ t ? 'core' : 'checkbox' ].selected.concat(obj.children_d)); var selectedIds = this._cascade_new_checked_state(obj.id, true); var temp = obj.children_d.concat(obj.id); for (i = 0, j = temp.length; i < j; i++) { if (selectedIds.indexOf(temp[i]) > -1) { sel[temp[i]] = true; } else { delete sel[temp[i]]; } } } // apply up if(s.indexOf('up') !== -1) { while(par && par.id !== $.jstree.root) { c = 0; for(i = 0, j = par.children.length; i < j; i++) { c += m[par.children[i]].state[ t ? 'selected' : 'checked' ]; } if(c === j) { par.state[ t ? 'selected' : 'checked' ] = true; sel[par.id] = true; //this._data[ t ? 'core' : 'checkbox' ].selected.push(par.id); tmp = this.get_node(par, true); if(tmp && tmp.length) { tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked'); } } else { break; } par = this.get_node(par.parent); } } cur = []; for (i in sel) { if (sel.hasOwnProperty(i)) { cur.push(i); } } this._data[ t ? 'core' : 'checkbox' ].selected = cur; }, this)) .on(this.settings.checkbox.tie_selection ? 'deselect_all.jstree' : 'uncheck_all.jstree', $.proxy(function (e, data) { var obj = this.get_node($.jstree.root), m = this._model.data, i, j, tmp; for(i = 0, j = obj.children_d.length; i < j; i++) { tmp = m[obj.children_d[i]]; if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) { tmp.original.state.undetermined = false; } } }, this)) .on(this.settings.checkbox.tie_selection ? 'deselect_node.jstree' : 'uncheck_node.jstree', $.proxy(function (e, data) { var self = this, obj = data.node, dom = this.get_node(obj, true), i, j, tmp, s = this.settings.checkbox.cascade, t = this.settings.checkbox.tie_selection, cur = this._data[ t ? 'core' : 'checkbox' ].selected, sel = {}, stillSelectedIds = [], allIds = obj.children_d.concat(obj.id); // apply down if(s.indexOf('down') !== -1) { var selectedIds = this._cascade_new_checked_state(obj.id, false); cur = cur.filter(function(id) { return allIds.indexOf(id) === -1 || selectedIds.indexOf(id) > -1; }); } // only apply up if cascade up is enabled and if this node is not selected // (if all child nodes are disabled and cascade_to_disabled === false then this node will till be selected). if(s.indexOf('up') !== -1 && cur.indexOf(obj.id) === -1) { for(i = 0, j = obj.parents.length; i < j; i++) { tmp = this._model.data[obj.parents[i]]; tmp.state[ t ? 'selected' : 'checked' ] = false; if(tmp && tmp.original && tmp.original.state && tmp.original.state.undetermined) { tmp.original.state.undetermined = false; } tmp = this.get_node(obj.parents[i], true); if(tmp && tmp.length) { tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked'); } } cur = cur.filter(function(id) { return obj.parents.indexOf(id) === -1; }); } this._data[ t ? 'core' : 'checkbox' ].selected = cur; }, this)); } if(this.settings.checkbox.cascade.indexOf('up') !== -1) { this.element .on('delete_node.jstree', $.proxy(function (e, data) { // apply up (whole handler) var p = this.get_node(data.parent), m = this._model.data, i, j, c, tmp, t = this.settings.checkbox.tie_selection; while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) { c = 0; for(i = 0, j = p.children.length; i < j; i++) { c += m[p.children[i]].state[ t ? 'selected' : 'checked' ]; } if(j > 0 && c === j) { p.state[ t ? 'selected' : 'checked' ] = true; this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id); tmp = this.get_node(p, true); if(tmp && tmp.length) { tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked'); } } else { break; } p = this.get_node(p.parent); } }, this)) .on('move_node.jstree', $.proxy(function (e, data) { // apply up (whole handler) var is_multi = data.is_multi, old_par = data.old_parent, new_par = this.get_node(data.parent), m = this._model.data, p, c, i, j, tmp, t = this.settings.checkbox.tie_selection; if(!is_multi) { p = this.get_node(old_par); while(p && p.id !== $.jstree.root && !p.state[ t ? 'selected' : 'checked' ]) { c = 0; for(i = 0, j = p.children.length; i < j; i++) { c += m[p.children[i]].state[ t ? 'selected' : 'checked' ]; } if(j > 0 && c === j) { p.state[ t ? 'selected' : 'checked' ] = true; this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id); tmp = this.get_node(p, true); if(tmp && tmp.length) { tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked'); } } else { break; } p = this.get_node(p.parent); } } p = new_par; while(p && p.id !== $.jstree.root) { c = 0; for(i = 0, j = p.children.length; i < j; i++) { c += m[p.children[i]].state[ t ? 'selected' : 'checked' ]; } if(c === j) { if(!p.state[ t ? 'selected' : 'checked' ]) { p.state[ t ? 'selected' : 'checked' ] = true; this._data[ t ? 'core' : 'checkbox' ].selected.push(p.id); tmp = this.get_node(p, true); if(tmp && tmp.length) { tmp.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked'); } } } else { if(p.state[ t ? 'selected' : 'checked' ]) { p.state[ t ? 'selected' : 'checked' ] = false; this._data[ t ? 'core' : 'checkbox' ].selected = $.vakata.array_remove_item(this._data[ t ? 'core' : 'checkbox' ].selected, p.id); tmp = this.get_node(p, true); if(tmp && tmp.length) { tmp.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked'); } } else { break; } } p = this.get_node(p.parent); } }, this)); } }; /** * get an array of all nodes whose state is "undetermined" * @name get_undetermined([full]) * @param {boolean} full: if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned * @return {Array} * @plugin checkbox */ this.get_undetermined = function (full) { if (this.settings.checkbox.cascade.indexOf('undetermined') === -1) { return []; } var i, j, k, l, o = {}, m = this._model.data, t = this.settings.checkbox.tie_selection, s = this._data[ t ? 'core' : 'checkbox' ].selected, p = [], tt = this, r = []; for(i = 0, j = s.length; i < j; i++) { if(m[s[i]] && m[s[i]].parents) { for(k = 0, l = m[s[i]].parents.length; k < l; k++) { if(o[m[s[i]].parents[k]] !== undefined) { break; } if(m[s[i]].parents[k] !== $.jstree.root) { o[m[s[i]].parents[k]] = true; p.push(m[s[i]].parents[k]); } } } } // attempt for server side undetermined state this.element.find('.jstree-closed').not(':has(.jstree-children)') .each(function () { var tmp = tt.get_node(this), tmp2; if(!tmp) { return; } if(!tmp.state.loaded) { if(tmp.original && tmp.original.state && tmp.original.state.undetermined && tmp.original.state.undetermined === true) { if(o[tmp.id] === undefined && tmp.id !== $.jstree.root) { o[tmp.id] = true; p.push(tmp.id); } for(k = 0, l = tmp.parents.length; k < l; k++) { if(o[tmp.parents[k]] === undefined && tmp.parents[k] !== $.jstree.root) { o[tmp.parents[k]] = true; p.push(tmp.parents[k]); } } } } else { for(i = 0, j = tmp.children_d.length; i < j; i++) { tmp2 = m[tmp.children_d[i]]; if(!tmp2.state.loaded && tmp2.original && tmp2.original.state && tmp2.original.state.undetermined && tmp2.original.state.undetermined === true) { if(o[tmp2.id] === undefined && tmp2.id !== $.jstree.root) { o[tmp2.id] = true; p.push(tmp2.id); } for(k = 0, l = tmp2.parents.length; k < l; k++) { if(o[tmp2.parents[k]] === undefined && tmp2.parents[k] !== $.jstree.root) { o[tmp2.parents[k]] = true; p.push(tmp2.parents[k]); } } } } } }); for (i = 0, j = p.length; i < j; i++) { if(!m[p[i]].state[ t ? 'selected' : 'checked' ]) { r.push(full ? m[p[i]] : p[i]); } } return r; }; /** * set the undetermined state where and if necessary. Used internally. * @private * @name _undetermined() * @plugin checkbox */ this._undetermined = function () { if(this.element === null) { return; } var p = this.get_undetermined(false), i, j, s; this.element.find('.jstree-undetermined').removeClass('jstree-undetermined'); for (i = 0, j = p.length; i < j; i++) { s = this.get_node(p[i], true); if(s && s.length) { s.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-undetermined'); } } }; this.redraw_node = function(obj, deep, is_callback, force_render) { obj = parent.redraw_node.apply(this, arguments); if(obj) { var i, j, tmp = null, icon = null; for(i = 0, j = obj.childNodes.length; i < j; i++) { if(obj.childNodes[i] && obj.childNodes[i].className && obj.childNodes[i].className.indexOf("jstree-anchor") !== -1) { tmp = obj.childNodes[i]; break; } } if(tmp) { if(!this.settings.checkbox.tie_selection && this._model.data[obj.id].state.checked) { tmp.className += ' jstree-checked'; } icon = _i.cloneNode(false); if(this._model.data[obj.id].state.checkbox_disabled) { icon.className += ' jstree-checkbox-disabled'; } tmp.insertBefore(icon, tmp.childNodes[0]); } } if(!is_callback && this.settings.checkbox.cascade.indexOf('undetermined') !== -1) { if(this._data.checkbox.uto) { clearTimeout(this._data.checkbox.uto); } this._data.checkbox.uto = setTimeout($.proxy(this._undetermined, this), 50); } return obj; }; /** * show the node checkbox icons * @name show_checkboxes() * @plugin checkbox */ this.show_checkboxes = function () { this._data.core.themes.checkboxes = true; this.get_container_ul().removeClass("jstree-no-checkboxes"); }; /** * hide the node checkbox icons * @name hide_checkboxes() * @plugin checkbox */ this.hide_checkboxes = function () { this._data.core.themes.checkboxes = false; this.get_container_ul().addClass("jstree-no-checkboxes"); }; /** * toggle the node icons * @name toggle_checkboxes() * @plugin checkbox */ this.toggle_checkboxes = function () { if(this._data.core.themes.checkboxes) { this.hide_checkboxes(); } else { this.show_checkboxes(); } }; /** * checks if a node is in an undetermined state * @name is_undetermined(obj) * @param {mixed} obj * @return {Boolean} */ this.is_undetermined = function (obj) { obj = this.get_node(obj); var s = this.settings.checkbox.cascade, i, j, t = this.settings.checkbox.tie_selection, d = this._data[ t ? 'core' : 'checkbox' ].selected, m = this._model.data; if(!obj || obj.state[ t ? 'selected' : 'checked' ] === true || s.indexOf('undetermined') === -1 || (s.indexOf('down') === -1 && s.indexOf('up') === -1)) { return false; } if(!obj.state.loaded && obj.original.state.undetermined === true) { return true; } for(i = 0, j = obj.children_d.length; i < j; i++) { if($.inArray(obj.children_d[i], d) !== -1 || (!m[obj.children_d[i]].state.loaded && m[obj.children_d[i]].original.state.undetermined)) { return true; } } return false; }; /** * disable a node's checkbox * @name disable_checkbox(obj) * @param {mixed} obj an array can be used too * @trigger disable_checkbox.jstree * @plugin checkbox */ this.disable_checkbox = function (obj) { var t1, t2, dom; if($.isArray(obj)) { obj = obj.slice(); for(t1 = 0, t2 = obj.length; t1 < t2; t1++) { this.disable_checkbox(obj[t1]); } return true; } obj = this.get_node(obj); if(!obj || obj.id === $.jstree.root) { return false; } dom = this.get_node(obj, true); if(!obj.state.checkbox_disabled) { obj.state.checkbox_disabled = true; if(dom && dom.length) { dom.children('.jstree-anchor').children('.jstree-checkbox').addClass('jstree-checkbox-disabled'); } /** * triggered when an node's checkbox is disabled * @event * @name disable_checkbox.jstree * @param {Object} node * @plugin checkbox */ this.trigger('disable_checkbox', { 'node' : obj }); } }; /** * enable a node's checkbox * @name disable_checkbox(obj) * @param {mixed} obj an array can be used too * @trigger enable_checkbox.jstree * @plugin checkbox */ this.enable_checkbox = function (obj) { var t1, t2, dom; if($.isArray(obj)) { obj = obj.slice(); for(t1 = 0, t2 = obj.length; t1 < t2; t1++) { this.enable_checkbox(obj[t1]); } return true; } obj = this.get_node(obj); if(!obj || obj.id === $.jstree.root) { return false; } dom = this.get_node(obj, true); if(obj.state.checkbox_disabled) { obj.state.checkbox_disabled = false; if(dom && dom.length) { dom.children('.jstree-anchor').children('.jstree-checkbox').removeClass('jstree-checkbox-disabled'); } /** * triggered when an node's checkbox is enabled * @event * @name enable_checkbox.jstree * @param {Object} node * @plugin checkbox */ this.trigger('enable_checkbox', { 'node' : obj }); } }; this.activate_node = function (obj, e) { if($(e.target).hasClass('jstree-checkbox-disabled')) { return false; } if(this.settings.checkbox.tie_selection && (this.settings.checkbox.whole_node || $(e.target).hasClass('jstree-checkbox'))) { e.ctrlKey = true; } if(this.settings.checkbox.tie_selection || (!this.settings.checkbox.whole_node && !$(e.target).hasClass('jstree-checkbox'))) { return parent.activate_node.call(this, obj, e); } if(this.is_disabled(obj)) { return false; } if(this.is_checked(obj)) { this.uncheck_node(obj, e); } else { this.check_node(obj, e); } this.trigger('activate_node', { 'node' : this.get_node(obj) }); }; /** * Cascades checked state to a node and all its descendants. This function does NOT affect hidden and disabled nodes (or their descendants). * However if these unaffected nodes are already selected their ids will be included in the returned array. * @private * @param {string} id the node ID * @param {bool} checkedState should the nodes be checked or not * @returns {Array} Array of all node id's (in this tree branch) that are checked. */ this._cascade_new_checked_state = function (id, checkedState) { var self = this; var t = this.settings.checkbox.tie_selection; var node = this._model.data[id]; var selectedNodeIds = []; var selectedChildrenIds = [], i, j, selectedChildIds; if ( (this.settings.checkbox.cascade_to_disabled || !node.state.disabled) && (this.settings.checkbox.cascade_to_hidden || !node.state.hidden) ) { //First try and check/uncheck the children if (node.children) { for (i = 0, j = node.children.length; i < j; i++) { var childId = node.children[i]; selectedChildIds = self._cascade_new_checked_state(childId, checkedState); selectedNodeIds = selectedNodeIds.concat(selectedChildIds); if (selectedChildIds.indexOf(childId) > -1) { selectedChildrenIds.push(childId); } } } var dom = self.get_node(node, true); //A node's state is undetermined if some but not all of it's children are checked/selected . var undetermined = selectedChildrenIds.length > 0 && selectedChildrenIds.length < node.children.length; if(node.original && node.original.state && node.original.state.undetermined) { node.original.state.undetermined = undetermined; } //If a node is undetermined then remove selected class if (undetermined) { node.state[ t ? 'selected' : 'checked' ] = false; dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked'); } //Otherwise, if the checkedState === true (i.e. the node is being checked now) and all of the node's children are checked (if it has any children), //check the node and style it correctly. else if (checkedState && selectedChildrenIds.length === node.children.length) { node.state[ t ? 'selected' : 'checked' ] = checkedState; selectedNodeIds.push(node.id); dom.attr('aria-selected', true).children('.jstree-anchor').addClass(t ? 'jstree-clicked' : 'jstree-checked'); } else { node.state[ t ? 'selected' : 'checked' ] = false; dom.attr('aria-selected', false).children('.jstree-anchor').removeClass(t ? 'jstree-clicked' : 'jstree-checked'); } } else { selectedChildIds = this.get_checked_descendants(id); if (node.state[ t ? 'selected' : 'checked' ]) { selectedChildIds.push(node.id); } selectedNodeIds = selectedNodeIds.concat(selectedChildIds); } return selectedNodeIds; }; /** * Gets ids of nodes selected in branch (of tree) specified by id (does not include the node specified by id) * @name get_checked_descendants(obj) * @param {string} id the node ID * @return {Array} array of IDs * @plugin checkbox */ this.get_checked_descendants = function (id) { var self = this; var t = self.settings.checkbox.tie_selection; var node = self._model.data[id]; return node.children_d.filter(function(_id) { return self._model.data[_id].state[ t ? 'selected' : 'checked' ]; }); }; /** * check a node (only if tie_selection in checkbox settings is false, otherwise select_node will be called internally) * @name check_node(obj) * @param {mixed} obj an array can be used to check multiple nodes * @trigger check_node.jstree * @plugin checkbox */ this.check_node = function (obj, e) { if(this.settings.checkbox.tie_selection) { return this.select_node(obj, false, true, e); } var dom, t1, t2, th; if($.isArray(obj)) { obj = obj.slice(); for(t1 = 0, t2 = obj.length; t1 < t2; t1++) { this.check_node(obj[t1], e); } return true; } obj = this.get_node(obj); if(!obj || obj.id === $.jstree.root) { return false; } dom = this.get_node(obj, true); if(!obj.state.checked) { obj.state.checked = true; this._data.checkbox.selected.push(obj.id); if(dom && dom.length) { dom.children('.jstree-anchor').addClass('jstree-checked'); } /** * triggered when an node is checked (only if tie_selection in checkbox settings is false) * @event * @name check_node.jstree * @param {Object} node * @param {Array} selected the current selection * @param {Object} event the event (if any) that triggered this check_node * @plugin checkbox */ this.trigger('check_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e }); } }; /** * uncheck a node (only if tie_selection in checkbox settings is false, otherwise deselect_node will be called internally) * @name uncheck_node(obj) * @param {mixed} obj an array can be used to uncheck multiple nodes * @trigger uncheck_node.jstree * @plugin checkbox */ this.uncheck_node = function (obj, e) { if(this.settings.checkbox.tie_selection) { return this.deselect_node(obj, false, e); } var t1, t2, dom; if($.isArray(obj)) { obj = obj.slice(); for(t1 = 0, t2 = obj.length; t1 < t2; t1++) { this.uncheck_node(obj[t1], e); } return true; } obj = this.get_node(obj); if(!obj || obj.id === $.jstree.root) { return false; } dom = this.get_node(obj, true); if(obj.state.checked) { obj.state.checked = false; this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, obj.id); if(dom.length) { dom.children('.jstree-anchor').removeClass('jstree-checked'); } /** * triggered when an node is unchecked (only if tie_selection in checkbox settings is false) * @event * @name uncheck_node.jstree * @param {Object} node * @param {Array} selected the current selection * @param {Object} event the event (if any) that triggered this uncheck_node * @plugin checkbox */ this.trigger('uncheck_node', { 'node' : obj, 'selected' : this._data.checkbox.selected, 'event' : e }); } }; /** * checks all nodes in the tree (only if tie_selection in checkbox settings is false, otherwise select_all will be called internally) * @name check_all() * @trigger check_all.jstree, changed.jstree * @plugin checkbox */ this.check_all = function () { if(this.settings.checkbox.tie_selection) { return this.select_all(); } var tmp = this._data.checkbox.selected.concat([]), i, j; this._data.checkbox.selected = this._model.data[$.jstree.root].children_d.concat(); for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) { if(this._model.data[this._data.checkbox.selected[i]]) { this._model.data[this._data.checkbox.selected[i]].state.checked = true; } } this.redraw(true); /** * triggered when all nodes are checked (only if tie_selection in checkbox settings is false) * @event * @name check_all.jstree * @param {Array} selected the current selection * @plugin checkbox */ this.trigger('check_all', { 'selected' : this._data.checkbox.selected }); }; /** * uncheck all checked nodes (only if tie_selection in checkbox settings is false, otherwise deselect_all will be called internally) * @name uncheck_all() * @trigger uncheck_all.jstree * @plugin checkbox */ this.uncheck_all = function () { if(this.settings.checkbox.tie_selection) { return this.deselect_all(); } var tmp = this._data.checkbox.selected.concat([]), i, j; for(i = 0, j = this._data.checkbox.selected.length; i < j; i++) { if(this._model.data[this._data.checkbox.selected[i]]) { this._model.data[this._data.checkbox.selected[i]].state.checked = false; } } this._data.checkbox.selected = []; this.element.find('.jstree-checked').removeClass('jstree-checked'); /** * triggered when all nodes are unchecked (only if tie_selection in checkbox settings is false) * @event * @name uncheck_all.jstree * @param {Object} node the previous selection * @param {Array} selected the current selection * @plugin checkbox */ this.trigger('uncheck_all', { 'selected' : this._data.checkbox.selected, 'node' : tmp }); }; /** * checks if a node is checked (if tie_selection is on in the settings this function will return the same as is_selected) * @name is_checked(obj) * @param {mixed} obj * @return {Boolean} * @plugin checkbox */ this.is_checked = function (obj) { if(this.settings.checkbox.tie_selection) { return this.is_selected(obj); } obj = this.get_node(obj); if(!obj || obj.id === $.jstree.root) { return false; } return obj.state.checked; }; /** * get an array of all checked nodes (if tie_selection is on in the settings this function will return the same as get_selected) * @name get_checked([full]) * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned * @return {Array} * @plugin checkbox */ this.get_checked = function (full) { if(this.settings.checkbox.tie_selection) { return this.get_selected(full); } return full ? $.map(this._data.checkbox.selected, $.proxy(function (i) { return this.get_node(i); }, this)) : this._data.checkbox.selected; }; /** * get an array of all top level checked nodes (ignoring children of checked nodes) (if tie_selection is on in the settings this function will return the same as get_top_selected) * @name get_top_checked([full]) * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned * @return {Array} * @plugin checkbox */ this.get_top_checked = function (full) { if(this.settings.checkbox.tie_selection) { return this.get_top_selected(full); } var tmp = this.get_checked(true), obj = {}, i, j, k, l; for(i = 0, j = tmp.length; i < j; i++) { obj[tmp[i].id] = tmp[i]; } for(i = 0, j = tmp.length; i < j; i++) { for(k = 0, l = tmp[i].children_d.length; k < l; k++) { if(obj[tmp[i].children_d[k]]) { delete obj[tmp[i].children_d[k]]; } } } tmp = []; for(i in obj) { if(obj.hasOwnProperty(i)) { tmp.push(i); } } return full ? $.map(tmp, $.proxy(function (i) { return this.get_node(i); }, this)) : tmp; }; /** * get an array of all bottom level checked nodes (ignoring selected parents) (if tie_selection is on in the settings this function will return the same as get_bottom_selected) * @name get_bottom_checked([full]) * @param {mixed} full if set to `true` the returned array will consist of the full node objects, otherwise - only IDs will be returned * @return {Array} * @plugin checkbox */ this.get_bottom_checked = function (full) { if(this.settings.checkbox.tie_selection) { return this.get_bottom_selected(full); } var tmp = this.get_checked(true), obj = [], i, j; for(i = 0, j = tmp.length; i < j; i++) { if(!tmp[i].children.length) { obj.push(tmp[i].id); } } return full ? $.map(obj, $.proxy(function (i) { return this.get_node(i); }, this)) : obj; }; this.load_node = function (obj, callback) { var k, l, i, j, c, tmp; if(!$.isArray(obj) && !this.settings.checkbox.tie_selection) { tmp = this.get_node(obj); if(tmp && tmp.state.loaded) { for(k = 0, l = tmp.children_d.length; k < l; k++) { if(this._model.data[tmp.children_d[k]].state.checked) { c = true; this._data.checkbox.selected = $.vakata.array_remove_item(this._data.checkbox.selected, tmp.children_d[k]); } } } } return parent.load_node.apply(this, arguments); }; this.get_state = function () { var state = parent.get_state.apply(this, arguments); if(this.settings.checkbox.tie_selection) { return state; } state.checkbox = this._data.checkbox.selected.slice(); return state; }; this.set_state = function (state, callback) { var res = parent.set_state.apply(this, arguments); if(res && state.checkbox) { if(!this.settings.checkbox.tie_selection) { this.uncheck_all(); var _this = this; $.each(state.checkbox, function (i, v) { _this.check_node(v); }); } delete state.checkbox; this.set_state(state, callback); return false; } return res; }; this.refresh = function (skip_loading, forget_state) { if(!this.settings.checkbox.tie_selection) { this._data.checkbox.selected = []; } return parent.refresh.apply(this, arguments); }; }; // include the checkbox plugin by default // $.jstree.defaults.plugins.push("checkbox"); }));