_Plugin.js 5.4 KB

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