jstree.changed.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * ### Changed plugin
  3. *
  4. * This plugin adds more information to the `changed.jstree` event. The new data is contained in the `changed` event data property, and contains a lists of `selected` and `deselected` nodes.
  5. */
  6. /*globals jQuery, define, exports, require, document */
  7. (function (factory) {
  8. "use strict";
  9. if (typeof define === 'function' && define.amd) {
  10. define('jstree.changed', ['jquery','jstree'], factory);
  11. }
  12. else if(typeof exports === 'object') {
  13. factory(require('jquery'), require('jstree'));
  14. }
  15. else {
  16. factory(jQuery, jQuery.jstree);
  17. }
  18. }(function ($, jstree, undefined) {
  19. "use strict";
  20. if($.jstree.plugins.changed) { return; }
  21. $.jstree.plugins.changed = function (options, parent) {
  22. var last = [];
  23. this.trigger = function (ev, data) {
  24. var i, j;
  25. if(!data) {
  26. data = {};
  27. }
  28. if(ev.replace('.jstree','') === 'changed') {
  29. data.changed = { selected : [], deselected : [] };
  30. var tmp = {};
  31. for(i = 0, j = last.length; i < j; i++) {
  32. tmp[last[i]] = 1;
  33. }
  34. for(i = 0, j = data.selected.length; i < j; i++) {
  35. if(!tmp[data.selected[i]]) {
  36. data.changed.selected.push(data.selected[i]);
  37. }
  38. else {
  39. tmp[data.selected[i]] = 2;
  40. }
  41. }
  42. for(i = 0, j = last.length; i < j; i++) {
  43. if(tmp[last[i]] === 1) {
  44. data.changed.deselected.push(last[i]);
  45. }
  46. }
  47. last = data.selected.slice();
  48. }
  49. /**
  50. * triggered when selection changes (the "changed" plugin enhances the original event with more data)
  51. * @event
  52. * @name changed.jstree
  53. * @param {Object} node
  54. * @param {Object} action the action that caused the selection to change
  55. * @param {Array} selected the current selection
  56. * @param {Object} changed an object containing two properties `selected` and `deselected` - both arrays of node IDs, which were selected or deselected since the last changed event
  57. * @param {Object} event the event (if any) that triggered this changed event
  58. * @plugin changed
  59. */
  60. parent.trigger.call(this, ev, data);
  61. };
  62. this.refresh = function (skip_loading, forget_state) {
  63. last = [];
  64. return parent.refresh.apply(this, arguments);
  65. };
  66. };
  67. }));