_Plugin.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.grid.enhanced._Plugin"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.grid.enhanced._Plugin"] = true;
  8. dojo.provide("dojox.grid.enhanced._Plugin");
  9. dojo.require("dojox.grid.EnhancedGrid");
  10. dojo.declare("dojox.grid.enhanced._Plugin", null, {
  11. // summary:
  12. // Base class for all plugins.
  13. //
  14. // description:
  15. // Provides common plugin functionality and basic life cycle management.
  16. //
  17. // Each concrete plugin must have a name field and is responsible for registering itself to the global plugin registry
  18. // e.g. for dnd plugin:
  19. // | dojox.grid.EnhancedGrid.registerPlugin("dnd" /*plugin name*/,
  20. // | dojox.grid.enhanced.plugins.DnD /*full class name of a plugin*/
  21. // | {"preInit": false, "dependency": ["nestedSorting"]} /*properties*/);
  22. //
  23. // [Keywords] of plugin properties(case sensitive)
  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. // | dojo.declare("mygrid.MyDnD", dojox.grid.enhanced.plugins.DnD, {
  33. // | name:"dnd" //still reuse the plugin name
  34. // | constructor: function(inGrid, option){ ... }
  35. // | });
  36. // | dojox.grid.EnhancedGrid.registerPlugin("dnd", mygrid.MyDnD);
  37. //
  38. // 2. Add new plugin - PluginA
  39. // | dojo.declare("mygrid.PluginA", dojox.grid.enhanced._Plugin, {
  40. // | name: "pA",
  41. // | constructor: function(inGrid, option){ ... }
  42. // | });
  43. // | dojox.grid.EnhancedGrid.registerPlugin("pA",mygrid.PluginA);
  44. //
  45. // 3. Use plugins
  46. // | dojo.require("mygrid.MyDnD");
  47. // | dojo.require("mygrid.PluginA");
  48. // | <script type="text/javascript">
  49. // | var grid = new dojox.grid.EnhancedGrid(
  50. // | {plugins: {dnd:true, pA:true}, ... }, dojo.byId("gridDiv"));
  51. // | grid.startup();
  52. // | </script>
  53. //name: String
  54. // Plugin name, e.g. 'nestedSorting', 'dnd'...
  55. name: 'plugin',
  56. //grid: Object
  57. // Grid that the plugin belongs to
  58. grid: null,
  59. //option: Object
  60. // Plugin properties - leveraged with default and user specified properties.
  61. // e.g. for dnd plugin, it may look like {"class": dojox.grid.enhanced.plugins.DnD, "dependency": ["nestedSorting"], ...}
  62. option: {},
  63. //_connects: Array
  64. // List of all connections.
  65. _connects: [],
  66. //_subscribes: Array
  67. // List of all subscribes.
  68. _subscribes: [],
  69. //privates: Object
  70. // Private properties/methods shouldn't be mixin-ed anytime.
  71. privates: {},
  72. constructor: function(inGrid, option){
  73. this.grid = inGrid;
  74. this.option = option;
  75. this._connects = [];
  76. this._subscribes = [];
  77. this.privates = dojo.mixin({},dojox.grid.enhanced._Plugin.prototype);
  78. this.init();
  79. },
  80. init: function(){},
  81. onPreInit: function(){},
  82. onPostInit: function(){},
  83. onStartUp: function(){},
  84. connect: function(obj, event, method){
  85. // summary:
  86. // Connects specified obj/event to specified method of this object.
  87. // example:
  88. // | var plugin = new dojox.grid.enhanced._Plugin(grid,"myPlugin",{...});
  89. // | // when foo.bar() is called, call the listener in the scope of plugin
  90. // | plugin.connect(foo, "bar", function(){
  91. // | console.debug(this.xxx());//"this" - plugin scope
  92. // | });
  93. var conn = dojo.connect(obj, event, this, method);
  94. this._connects.push(conn);
  95. return conn;
  96. },
  97. disconnect: function(handle){
  98. // summary:
  99. // Disconnects handle and removes it from connection list.
  100. dojo.some(this._connects, function(conn, i, conns){
  101. if(conn == handle){
  102. dojo.disconnect(handle);
  103. conns.splice(i, 1);
  104. return true;
  105. }
  106. return false;
  107. });
  108. },
  109. subscribe: function(topic, method){
  110. // summary:
  111. // Subscribes to the specified topic and calls the specified method
  112. // of this object.
  113. // example:
  114. // | var plugin = new dojox.grid.enhanced._Plugin(grid,"myPlugin",{...});
  115. // | // when /my/topic is published, call the subscriber in the scope of plugin
  116. // | // with passed parameter - "v"
  117. // | plugin.subscribe("/my/topic", function(v){
  118. // | console.debug(this.xxx(v));//"this" - plugin scope
  119. // | });
  120. var subscribe = dojo.subscribe(topic, this, method);
  121. this._subscribes.push(subscribe);
  122. return subscribe;
  123. },
  124. unsubscribe: function(handle){
  125. // summary:
  126. // Un-subscribes handle and removes it from subscriptions list.
  127. dojo.some(this._subscribes, function(subscribe, i, subscribes){
  128. if(subscribe == handle){
  129. dojo.unsubscribe(handle);
  130. subscribes.splice(i, 1);
  131. return true;
  132. }
  133. return false;
  134. });
  135. },
  136. onSetStore: function(store){
  137. // summary:
  138. // Called when store is changed.
  139. },
  140. destroy: function(){
  141. // summary:
  142. // Destroy all resources.
  143. dojo.forEach(this._connects, dojo.disconnect);
  144. dojo.forEach(this._subscribes, dojo.unsubscribe);
  145. delete this._connects;
  146. delete this._subscribes;
  147. delete this.option;
  148. delete this.privates;
  149. //console.log('Plugin [', this.name, '].destroy() executed!');
  150. }
  151. });
  152. //Each plugin is responsible for registering itself
  153. // e.g. for DnD plugin(name:'dnd'):
  154. // | dojox.grid.EnhancedGrid.registerPlugin(dojox.grid.enhanced.plugins.DnD/*class*/,
  155. // | {"dependency": ["nestedSorting"]}/*Optional - properties*/);
  156. }