sample.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*global jQuery */
  2. // wrap in IIFE and pass jQuery as $
  3. (function ($, undefined) {
  4. "use strict";
  5. // some private plugin stuff if needed
  6. var private_var = null;
  7. // extending the defaults
  8. $.jstree.defaults.sample = {
  9. sample_option : 'sample_val'
  10. };
  11. // the actual plugin code
  12. $.jstree.plugins.sample = function (options, parent) {
  13. // own function
  14. this.sample_function = function (arg) {
  15. // you can chain this method if needed and available
  16. if(parent.sample_function) { parent.sample_function.call(this, arg); }
  17. };
  18. // *SPECIAL* FUNCTIONS
  19. this.init = function (el, options) {
  20. // do not forget parent
  21. parent.init.call(this, el, options);
  22. };
  23. // bind events if needed
  24. this.bind = function () {
  25. // call parent function first
  26. parent.bind.call(this);
  27. // do(stuff);
  28. };
  29. // unbind events if needed (all in jquery namespace are taken care of by the core)
  30. this.unbind = function () {
  31. // do(stuff);
  32. // call parent function last
  33. parent.unbind.call(this);
  34. };
  35. this.teardown = function () {
  36. // do not forget parent
  37. parent.teardown.call(this);
  38. };
  39. // state management - get and restore
  40. this.get_state = function () {
  41. // always get state from parent first
  42. var state = parent.get_state.call(this);
  43. // add own stuff to state
  44. state.sample = { 'var' : 'val' };
  45. return state;
  46. };
  47. this.set_state = function (state, callback) {
  48. // only process your part if parent returns true
  49. // there will be multiple times with false
  50. if(parent.set_state.call(this, state, callback)) {
  51. // check the key you set above
  52. if(state.sample) {
  53. // do(stuff); // like calling this.sample_function(state.sample.var);
  54. // remove your part of the state, call again and RETURN FALSE, the next cycle will be TRUE
  55. delete state.sample;
  56. this.set_state(state, callback);
  57. return false;
  58. }
  59. // return true if your state is gone (cleared in the previous step)
  60. return true;
  61. }
  62. // parent was false - return false too
  63. return false;
  64. };
  65. // node transportation
  66. this.get_json = function (obj, options, flat) {
  67. // get the node from the parent
  68. var tmp = parent.get_json.call(this, obj, options, flat), i, j;
  69. if($.isArray(tmp)) {
  70. for(i = 0, j = tmp.length; i < j; i++) {
  71. tmp[i].sample = 'value';
  72. }
  73. }
  74. else {
  75. tmp.sample = 'value';
  76. }
  77. // return the original / modified node
  78. return tmp;
  79. };
  80. };
  81. // attach to document ready if needed
  82. $(function () {
  83. // do(stuff);
  84. });
  85. // you can include the sample plugin in all instances by default
  86. $.jstree.defaults.plugins.push("sample");
  87. })(jQuery);