annotate vakata-jstree-3.3.5/src/sample.js @ 0:7a3bb304fe1d draft

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