1 | define([ |
---|
2 | "dojo/_base/kernel", |
---|
3 | "dojo/_base/lang", |
---|
4 | "dojo/_base/declare", |
---|
5 | "dojo/_base/array", |
---|
6 | "dojo/_base/connect", |
---|
7 | "../EnhancedGrid" |
---|
8 | ], function(dojo, lang, declare, array, connect){ |
---|
9 | |
---|
10 | return declare("dojox.grid.enhanced._Plugin", null, { |
---|
11 | // summary: |
---|
12 | // Base class for all plugins. |
---|
13 | // description: |
---|
14 | // Provides common plugin functionality and basic life cycle management. |
---|
15 | // |
---|
16 | // Each concrete plugin must have a name field and is responsible for registering itself to the global plugin registry |
---|
17 | // e.g. for dnd plugin: |
---|
18 | // | dojox.grid.EnhancedGrid.registerPlugin("dnd" /*plugin name*/, |
---|
19 | // | dojox.grid.enhanced.plugins.DnD /*full class name of a plugin*/ |
---|
20 | // | {"preInit": false, "dependency": ["nestedSorting"]} /*properties*/); |
---|
21 | // |
---|
22 | // [Keywords] of plugin properties (case sensitive): |
---|
23 | // |
---|
24 | // - "preInit": boolean, whether a plugin should be created before EnhancedGrid.postCreate(), |
---|
25 | // false by default(plugins are created after EnhancedGrid.postCreate()). |
---|
26 | // - "dependency": array or string, plugin(s) indicated by "dependency" will be created before the current one. |
---|
27 | // Note: recursive cycle dependencies are not supported e.g. following dependency is invalid: |
---|
28 | // pluginA -> pluginB -> pluginA |
---|
29 | // |
---|
30 | // example: |
---|
31 | // 1. Customize default DnD plugin |
---|
32 | // |
---|
33 | // | declare("mygrid.MyDnD", dojox.grid.enhanced.plugins.DnD, { |
---|
34 | // | name:"dnd" //still reuse the plugin name |
---|
35 | // | constructor: function(inGrid, option){ ... } |
---|
36 | // | }); |
---|
37 | // | dojox.grid.EnhancedGrid.registerPlugin("dnd", mygrid.MyDnD); |
---|
38 | // |
---|
39 | // 2. Add new plugin - PluginA |
---|
40 | // |
---|
41 | // | declare("mygrid.PluginA", dojox.grid.enhanced._Plugin, { |
---|
42 | // | name: "pA", |
---|
43 | // | constructor: function(inGrid, option){ ... } |
---|
44 | // | }); |
---|
45 | // | dojox.grid.EnhancedGrid.registerPlugin("pA",mygrid.PluginA); |
---|
46 | // |
---|
47 | // 3. Use plugins |
---|
48 | // |
---|
49 | // | dojo.require("mygrid.MyDnD"); |
---|
50 | // | dojo.require("mygrid.PluginA"); |
---|
51 | // | |
---|
52 | // | <script type="text/javascript"> |
---|
53 | // | var grid = new dojox.grid.EnhancedGrid( |
---|
54 | // | {plugins: {dnd:true, pA:true}, ... }, dojo.byId("gridDiv")); |
---|
55 | // | grid.startup(); |
---|
56 | // | </script> |
---|
57 | |
---|
58 | // name: String |
---|
59 | // Plugin name, e.g. 'nestedSorting', 'dnd'... |
---|
60 | name: 'plugin', |
---|
61 | |
---|
62 | // grid: Object |
---|
63 | // Grid that the plugin belongs to |
---|
64 | grid: null, |
---|
65 | |
---|
66 | // option: Object |
---|
67 | // Plugin properties - leveraged with default and user specified properties. |
---|
68 | // e.g. for dnd plugin, it may look like {"class": dojox.grid.enhanced.plugins.DnD, "dependency": ["nestedSorting"], ...} |
---|
69 | option: {}, |
---|
70 | |
---|
71 | // _connects: Array |
---|
72 | // List of all connections. |
---|
73 | _connects: [], |
---|
74 | |
---|
75 | // _subscribes: Array |
---|
76 | // List of all subscribes. |
---|
77 | _subscribes: [], |
---|
78 | |
---|
79 | // privates: Object |
---|
80 | // Private properties/methods shouldn't be mixin-ed anytime. |
---|
81 | privates: {}, |
---|
82 | |
---|
83 | constructor: function(inGrid, option){ |
---|
84 | this.grid = inGrid; |
---|
85 | this.option = option; |
---|
86 | this._connects = []; |
---|
87 | this._subscribes = []; |
---|
88 | this.privates = lang.mixin({},dojox.grid.enhanced._Plugin.prototype); |
---|
89 | this.init(); |
---|
90 | }, |
---|
91 | |
---|
92 | init: function(){}, |
---|
93 | |
---|
94 | onPreInit: function(){}, |
---|
95 | |
---|
96 | onPostInit: function(){}, |
---|
97 | |
---|
98 | onStartUp: function(){}, |
---|
99 | |
---|
100 | connect: function(obj, event, method){ |
---|
101 | // summary: |
---|
102 | // Connects specified obj/event to specified method of this object. |
---|
103 | // example: |
---|
104 | // | var plugin = new dojox.grid.enhanced._Plugin(grid,"myPlugin",{...}); |
---|
105 | // | // when foo.bar() is called, call the listener in the scope of plugin |
---|
106 | // | plugin.connect(foo, "bar", function(){ |
---|
107 | // | console.debug(this.xxx());//"this" - plugin scope |
---|
108 | // | }); |
---|
109 | var conn = connect.connect(obj, event, this, method); |
---|
110 | this._connects.push(conn); |
---|
111 | return conn; |
---|
112 | }, |
---|
113 | disconnect: function(handle){ |
---|
114 | // summary: |
---|
115 | // Disconnects handle and removes it from connection list. |
---|
116 | array.some(this._connects, function(conn, i, conns){ |
---|
117 | if(conn == handle){ |
---|
118 | connect.disconnect(handle); |
---|
119 | conns.splice(i, 1); |
---|
120 | return true; |
---|
121 | } |
---|
122 | return false; |
---|
123 | }); |
---|
124 | }, |
---|
125 | subscribe: function(topic, method){ |
---|
126 | // summary: |
---|
127 | // Subscribes to the specified topic and calls the specified method |
---|
128 | // of this object. |
---|
129 | // example: |
---|
130 | // | var plugin = new dojox.grid.enhanced._Plugin(grid,"myPlugin",{...}); |
---|
131 | // | // when /my/topic is published, call the subscriber in the scope of plugin |
---|
132 | // | // with passed parameter - "v" |
---|
133 | // | plugin.subscribe("/my/topic", function(v){ |
---|
134 | // | console.debug(this.xxx(v));//"this" - plugin scope |
---|
135 | // | }); |
---|
136 | var subscribe = connect.subscribe(topic, this, method); |
---|
137 | this._subscribes.push(subscribe); |
---|
138 | return subscribe; |
---|
139 | }, |
---|
140 | unsubscribe: function(handle){ |
---|
141 | // summary: |
---|
142 | // Un-subscribes handle and removes it from subscriptions list. |
---|
143 | array.some(this._subscribes, function(subscribe, i, subscribes){ |
---|
144 | if(subscribe == handle){ |
---|
145 | connect.unsubscribe(handle); |
---|
146 | subscribes.splice(i, 1); |
---|
147 | return true; |
---|
148 | } |
---|
149 | return false; |
---|
150 | }); |
---|
151 | }, |
---|
152 | onSetStore: function(store){ |
---|
153 | // summary: |
---|
154 | // Called when store is changed. |
---|
155 | }, |
---|
156 | destroy: function(){ |
---|
157 | // summary: |
---|
158 | // Destroy all resources. |
---|
159 | array.forEach(this._connects, connect.disconnect); |
---|
160 | array.forEach(this._subscribes, connect.unsubscribe); |
---|
161 | delete this._connects; |
---|
162 | delete this._subscribes; |
---|
163 | delete this.option; |
---|
164 | delete this.privates; |
---|
165 | //console.log('Plugin [', this.name, '].destroy() executed!'); |
---|
166 | } |
---|
167 | }); |
---|
168 | |
---|
169 | //Each plugin is responsible for registering itself |
---|
170 | // e.g. for DnD plugin(name:'dnd'): |
---|
171 | // | dojox.grid.EnhancedGrid.registerPlugin(dojox.grid.enhanced.plugins.DnD/*class*/, |
---|
172 | // | {"dependency": ["nestedSorting"]}/*Optional - properties*/); |
---|
173 | |
---|
174 | }); |
---|