Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugin.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('plugin', function(Y) {
  9. /**
  10. * Provides the base Plugin class, which plugin developers should extend, when creating custom plugins
  11. *
  12. * @module plugin
  13. */
  14. /**
  15. * The base class for all Plugin instances.
  16. *
  17. * @class Plugin.Base
  18. * @extends Base
  19. * @param {Object} config Configuration object with property name/value pairs.
  20. */
  21. function Plugin(config) {
  22. if (! (this.hasImpl && this.hasImpl(Y.Plugin.Base)) ) {
  23. Plugin.superclass.constructor.apply(this, arguments);
  24. } else {
  25. Plugin.prototype.initializer.apply(this, arguments);
  26. }
  27. }
  28. /**
  29. * Object defining the set of attributes supported by the Plugin.Base class
  30. *
  31. * @property ATTRS
  32. * @type Object
  33. * @static
  34. */
  35. Plugin.ATTRS = {
  36. /**
  37. * The plugin's host object.
  38. *
  39. * @attribute host
  40. * @writeonce
  41. * @type Plugin.Host
  42. */
  43. host : {
  44. writeOnce: true
  45. }
  46. };
  47. /**
  48. * The string identifying the Plugin.Base class. Plugins extending
  49. * Plugin.Base should set their own NAME value.
  50. *
  51. * @property NAME
  52. * @type String
  53. * @static
  54. */
  55. Plugin.NAME = 'plugin';
  56. /**
  57. * The name of the property the the plugin will be attached to
  58. * when plugged into a Plugin Host. Plugins extending Plugin.Base,
  59. * should set their own NS value.
  60. *
  61. * @property NS
  62. * @type String
  63. * @static
  64. */
  65. Plugin.NS = 'plugin';
  66. Y.extend(Plugin, Y.Base, {
  67. /**
  68. * The list of event handles for event listeners or AOP injected methods
  69. * applied by the plugin to the host object.
  70. *
  71. * @property _handles
  72. * @private
  73. * @type Array
  74. * @value null
  75. */
  76. _handles: null,
  77. /**
  78. * Initializer lifecycle implementation.
  79. *
  80. * @method initializer
  81. * @param {Object} config Configuration object with property name/value pairs.
  82. */
  83. initializer : function(config) {
  84. this._handles = [];
  85. },
  86. /**
  87. * Destructor lifecycle implementation.
  88. *
  89. * Removes any event listeners or injected methods applied by the Plugin
  90. *
  91. * @method destructor
  92. */
  93. destructor: function() {
  94. // remove all handles
  95. if (this._handles) {
  96. for (var i = 0, l = this._handles.length; i < l; i++) {
  97. this._handles[i].detach();
  98. }
  99. }
  100. },
  101. /**
  102. * Listens for the "on" moment of events fired by the host,
  103. * or injects code "before" a given method on the host.
  104. *
  105. * @method doBefore
  106. *
  107. * @param strMethod {String} The event to listen for, or method to inject logic before.
  108. * @param fn {Function} The handler function. For events, the "on" moment listener. For methods, the function to execute before the given method is executed.
  109. * @param context {Object} An optional context to call the handler with. The default context is the plugin instance.
  110. * @return handle {EventHandle} The detach handle for the handler.
  111. */
  112. doBefore: function(strMethod, fn, context) {
  113. var host = this.get("host"), handle;
  114. if (strMethod in host) { // method
  115. handle = this.beforeHostMethod(strMethod, fn, context);
  116. } else if (host.on) { // event
  117. handle = this.onHostEvent(strMethod, fn, context);
  118. }
  119. return handle;
  120. },
  121. /**
  122. * Listens for the "after" moment of events fired by the host,
  123. * or injects code "after" a given method on the host.
  124. *
  125. * @method doAfter
  126. *
  127. * @param strMethod {String} The event to listen for, or method to inject logic after.
  128. * @param fn {Function} The handler function. For events, the "after" moment listener. For methods, the function to execute after the given method is executed.
  129. * @param context {Object} An optional context to call the handler with. The default context is the plugin instance.
  130. * @return handle {EventHandle} The detach handle for the listener.
  131. */
  132. doAfter: function(strMethod, fn, context) {
  133. var host = this.get("host"), handle;
  134. if (strMethod in host) { // method
  135. handle = this.afterHostMethod(strMethod, fn, context);
  136. } else if (host.after) { // event
  137. handle = this.afterHostEvent(strMethod, fn, context);
  138. }
  139. return handle;
  140. },
  141. /**
  142. * Listens for the "on" moment of events fired by the host object.
  143. *
  144. * Listeners attached through this method will be detached when the plugin is unplugged.
  145. *
  146. * @method onHostEvent
  147. * @param {String | Object} type The event type.
  148. * @param {Function} fn The listener.
  149. * @param {Object} context The execution context. Defaults to the plugin instance.
  150. * @return handle {EventHandle} The detach handle for the listener.
  151. */
  152. onHostEvent : function(type, fn, context) {
  153. var handle = this.get("host").on(type, fn, context || this);
  154. this._handles.push(handle);
  155. return handle;
  156. },
  157. /**
  158. * Listens for the "after" moment of events fired by the host object.
  159. *
  160. * Listeners attached through this method will be detached when the plugin is unplugged.
  161. *
  162. * @method afterHostEvent
  163. * @param {String | Object} type The event type.
  164. * @param {Function} fn The listener.
  165. * @param {Object} context The execution context. Defaults to the plugin instance.
  166. * @return handle {EventHandle} The detach handle for the listener.
  167. */
  168. afterHostEvent : function(type, fn, context) {
  169. var handle = this.get("host").after(type, fn, context || this);
  170. this._handles.push(handle);
  171. return handle;
  172. },
  173. /**
  174. * Injects a function to be executed before a given method on host object.
  175. *
  176. * The function will be detached when the plugin is unplugged.
  177. *
  178. * @method beforeHostMethod
  179. * @param {String} method The name of the method to inject the function before.
  180. * @param {Function} fn The function to inject.
  181. * @param {Object} context The execution context. Defaults to the plugin instance.
  182. * @return handle {EventHandle} The detach handle for the injected function.
  183. */
  184. beforeHostMethod : function(strMethod, fn, context) {
  185. var handle = Y.Do.before(fn, this.get("host"), strMethod, context || this);
  186. this._handles.push(handle);
  187. return handle;
  188. },
  189. /**
  190. * Injects a function to be executed after a given method on host object.
  191. *
  192. * The function will be detached when the plugin is unplugged.
  193. *
  194. * @method afterHostMethod
  195. * @param {String} method The name of the method to inject the function after.
  196. * @param {Function} fn The function to inject.
  197. * @param {Object} context The execution context. Defaults to the plugin instance.
  198. * @return handle {EventHandle} The detach handle for the injected function.
  199. */
  200. afterHostMethod : function(strMethod, fn, context) {
  201. var handle = Y.Do.after(fn, this.get("host"), strMethod, context || this);
  202. this._handles.push(handle);
  203. return handle;
  204. },
  205. toString: function() {
  206. return this.constructor.NAME + '[' + this.constructor.NS + ']';
  207. }
  208. });
  209. Y.namespace("Plugin").Base = Plugin;
  210. }, '3.4.0' ,{requires:['base-base']});