comparison vakata-jstree-3.3.5/src/sample.js @ 0:0aeed70b3bc5 draft default tip

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