| 1 |
define([ |
| 2 |
"dojo/_base/kernel", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/declare", |
| 5 |
"dojo/_base/array", |
| 6 |
"dojo/_base/connect", |
| 7 |
"./_Events", |
| 8 |
"./_FocusManager", |
| 9 |
"../util" |
| 10 |
], function(dojo, lang, declare, array, connect, _Events, _FocusManager, util){ |
| 11 |
|
| 12 |
var _PluginManager = declare("dojox.grid.enhanced._PluginManager", null, { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
_options: null, |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
_plugins: null, |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
_connects: null, |
| 36 |
|
| 37 |
constructor: function(inGrid){ |
| 38 |
this.grid = inGrid; |
| 39 |
this._store = inGrid.store; |
| 40 |
this._options = {}; |
| 41 |
this._plugins = []; |
| 42 |
this._connects = []; |
| 43 |
this._parseProps(this.grid.plugins); |
| 44 |
|
| 45 |
inGrid.connect(inGrid, "_setStore", lang.hitch(this, function(store){ |
| 46 |
if(this._store !== store){ |
| 47 |
this.forEach('onSetStore', [store, this._store]); |
| 48 |
this._store = store; |
| 49 |
} |
| 50 |
})); |
| 51 |
}, |
| 52 |
startup: function(){ |
| 53 |
this.forEach('onStartUp'); |
| 54 |
}, |
| 55 |
preInit: function(){ |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
this.grid.focus.destroy(); |
| 60 |
this.grid.focus = new _FocusManager(this.grid); |
| 61 |
new _Events(this.grid); |
| 62 |
this._init(true); |
| 63 |
this.forEach('onPreInit'); |
| 64 |
}, |
| 65 |
postInit: function(){ |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
this._init(false); |
| 70 |
|
| 71 |
array.forEach(this.grid.views.views, this._initView, this); |
| 72 |
this._connects.push(connect.connect(this.grid.views, 'addView', lang.hitch(this, this._initView))); |
| 73 |
|
| 74 |
if(this._plugins.length > 0){ |
| 75 |
var edit = this.grid.edit; |
| 76 |
if(edit){ edit.styleRow = function(inRow){}; } |
| 77 |
} |
| 78 |
this.forEach('onPostInit'); |
| 79 |
}, |
| 80 |
forEach: function(func, args){ |
| 81 |
array.forEach(this._plugins, function(p){ |
| 82 |
if(!p || !p[func]){ return; } |
| 83 |
p[func].apply(p, args ? args : []); |
| 84 |
}); |
| 85 |
}, |
| 86 |
_parseProps: function(plugins){ |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
if(!plugins){ return; } |
| 92 |
|
| 93 |
var p, loading = {}, options = this._options, grid = this.grid; |
| 94 |
var registry = _PluginManager.registry; |
| 95 |
for(p in plugins){ |
| 96 |
if(plugins[p]){ |
| 97 |
this._normalize(p, plugins, registry, loading); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if(options.dnd){ |
| 102 |
options.columnReordering = false; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
lang.mixin(grid, options); |
| 107 |
}, |
| 108 |
_normalize: function(p, plugins, registry, loading){ |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
if(!registry[p]){ throw new Error('Plugin ' + p + ' is required.');} |
| 120 |
|
| 121 |
if(loading[p]){ throw new Error('Recursive cycle dependency is not supported.'); } |
| 122 |
|
| 123 |
var options = this._options; |
| 124 |
if(options[p]){ return options[p]; } |
| 125 |
|
| 126 |
loading[p] = true; |
| 127 |
|
| 128 |
options[p] = lang.mixin({}, registry[p], lang.isObject(plugins[p]) ? plugins[p] : {}); |
| 129 |
|
| 130 |
var dependencies = options[p]['dependency']; |
| 131 |
if(dependencies){ |
| 132 |
if(!lang.isArray(dependencies)){ |
| 133 |
dependencies = options[p]['dependency'] = [dependencies]; |
| 134 |
} |
| 135 |
array.forEach(dependencies, function(dependency){ |
| 136 |
if(!this._normalize(dependency, plugins, registry, loading)){ |
| 137 |
throw new Error('Plugin ' + dependency + ' is required.'); |
| 138 |
} |
| 139 |
}, this); |
| 140 |
} |
| 141 |
delete loading[p]; |
| 142 |
return options[p]; |
| 143 |
}, |
| 144 |
_init: function(pre){ |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
var p, preInit, options = this._options; |
| 150 |
for(p in options){ |
| 151 |
preInit = options[p]['preInit']; |
| 152 |
if((pre ? preInit : !preInit) && options[p]['class'] && !this.pluginExisted(p)){ |
| 153 |
this.loadPlugin(p); |
| 154 |
} |
| 155 |
} |
| 156 |
}, |
| 157 |
loadPlugin: function(name){ |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
var option = this._options[name]; |
| 165 |
if(!option){ return null; } |
| 166 |
|
| 167 |
var plugin = this.getPlugin(name); |
| 168 |
if(plugin){ return plugin; } |
| 169 |
|
| 170 |
var dependencies = option['dependency']; |
| 171 |
array.forEach(dependencies, function(dependency){ |
| 172 |
if(!this.loadPlugin(dependency)){ |
| 173 |
throw new Error('Plugin ' + dependency + ' is required.'); |
| 174 |
} |
| 175 |
}, this); |
| 176 |
var cls = option['class']; |
| 177 |
delete option['class']; |
| 178 |
plugin = new this.getPluginClazz(cls)(this.grid, option); |
| 179 |
this._plugins.push(plugin); |
| 180 |
return plugin; |
| 181 |
}, |
| 182 |
_initView: function(view){ |
| 183 |
|
| 184 |
|
| 185 |
if(!view){ return; } |
| 186 |
|
| 187 |
util.funnelEvents(view.contentNode, view, "doContentEvent", ['mouseup', 'mousemove']); |
| 188 |
util.funnelEvents(view.headerNode, view, "doHeaderEvent", ['mouseup']); |
| 189 |
}, |
| 190 |
pluginExisted: function(name){ |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
return !!this.getPlugin(name); |
| 198 |
}, |
| 199 |
getPlugin: function(name){ |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
var plugins = this._plugins; |
| 207 |
name = name.toLowerCase(); |
| 208 |
for(var i = 0, len = plugins.length; i < len; i++){ |
| 209 |
if(name == plugins[i]['name'].toLowerCase()){ |
| 210 |
return plugins[i]; |
| 211 |
} |
| 212 |
} |
| 213 |
return null; |
| 214 |
}, |
| 215 |
getPluginClazz: function(clazz){ |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
if(lang.isFunction(clazz)){ |
| 221 |
return clazz; |
| 222 |
} |
| 223 |
var errorMsg = 'Please make sure Plugin "' + clazz + '" is existed.'; |
| 224 |
try{ |
| 225 |
var cls = lang.getObject(clazz); |
| 226 |
if(!cls){ throw new Error(errorMsg); } |
| 227 |
return cls; |
| 228 |
}catch(e){ |
| 229 |
throw new Error(errorMsg); |
| 230 |
} |
| 231 |
}, |
| 232 |
isFixedCell: function(cell){ |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
return cell && (cell.isRowSelector || cell.fixedPos); |
| 242 |
}, |
| 243 |
destroy: function(){ |
| 244 |
|
| 245 |
|
| 246 |
array.forEach(this._connects, connect.disconnect); |
| 247 |
this.forEach('destroy'); |
| 248 |
if(this.grid.unwrap){ |
| 249 |
this.grid.unwrap(); |
| 250 |
} |
| 251 |
delete this._connects; |
| 252 |
delete this._plugins; |
| 253 |
delete this._options; |
| 254 |
} |
| 255 |
}); |
| 256 |
|
| 257 |
_PluginManager.registerPlugin = function(clazz, props){ |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if(!clazz){ |
| 265 |
console.warn("Failed to register plugin, class missed!"); |
| 266 |
return; |
| 267 |
} |
| 268 |
var cls = _PluginManager; |
| 269 |
cls.registry = cls.registry || {}; |
| 270 |
cls.registry[clazz.prototype.name] = lang.mixin({"class": clazz}, (props ? props : {})); |
| 271 |
}; |
| 272 |
|
| 273 |
return _PluginManager; |
| 274 |
|
| 275 |
}); |