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.

pluginhost-config.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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('pluginhost-config', function(Y) {
  9. /**
  10. * Adds pluginhost constructor configuration and static configuration support
  11. * @submodule pluginhost-config
  12. */
  13. var PluginHost = Y.Plugin.Host,
  14. L = Y.Lang;
  15. /**
  16. * A protected initialization method, used by the host class to initialize
  17. * plugin configurations passed the constructor, through the config object.
  18. *
  19. * Host objects should invoke this method at the appropriate time in their
  20. * construction lifecycle.
  21. *
  22. * @method _initConfigPlugins
  23. * @param {Object} config The configuration object passed to the constructor
  24. * @protected
  25. * @for Plugin.Host
  26. */
  27. PluginHost.prototype._initConfigPlugins = function(config) {
  28. // Class Configuration
  29. var classes = (this._getClasses) ? this._getClasses() : [this.constructor],
  30. plug = [],
  31. unplug = {},
  32. constructor, i, classPlug, classUnplug, pluginClassName;
  33. // TODO: Room for optimization. Can we apply statically/unplug in same pass?
  34. for (i = classes.length - 1; i >= 0; i--) {
  35. constructor = classes[i];
  36. classUnplug = constructor._UNPLUG;
  37. if (classUnplug) {
  38. // subclasses over-write
  39. Y.mix(unplug, classUnplug, true);
  40. }
  41. classPlug = constructor._PLUG;
  42. if (classPlug) {
  43. // subclasses over-write
  44. Y.mix(plug, classPlug, true);
  45. }
  46. }
  47. for (pluginClassName in plug) {
  48. if (plug.hasOwnProperty(pluginClassName)) {
  49. if (!unplug[pluginClassName]) {
  50. this.plug(plug[pluginClassName]);
  51. }
  52. }
  53. }
  54. // User Configuration
  55. if (config && config.plugins) {
  56. this.plug(config.plugins);
  57. }
  58. };
  59. /**
  60. * Registers plugins to be instantiated at the class level (plugins
  61. * which should be plugged into every instance of the class by default).
  62. *
  63. * @method plug
  64. * @static
  65. *
  66. * @param {Function} hostClass The host class on which to register the plugins
  67. * @param {Function | Array} plugin Either the plugin class, an array of plugin classes or an array of objects (with fn and cfg properties defined)
  68. * @param {Object} config (Optional) If plugin is the plugin class, the configuration for the plugin
  69. * @for Plugin.Host
  70. */
  71. PluginHost.plug = function(hostClass, plugin, config) {
  72. // Cannot plug into Base, since Plugins derive from Base [ will cause infinite recurrsion ]
  73. var p, i, l, name;
  74. if (hostClass !== Y.Base) {
  75. hostClass._PLUG = hostClass._PLUG || {};
  76. if (!L.isArray(plugin)) {
  77. if (config) {
  78. plugin = {fn:plugin, cfg:config};
  79. }
  80. plugin = [plugin];
  81. }
  82. for (i = 0, l = plugin.length; i < l;i++) {
  83. p = plugin[i];
  84. name = p.NAME || p.fn.NAME;
  85. hostClass._PLUG[name] = p;
  86. }
  87. }
  88. };
  89. /**
  90. * Unregisters any class level plugins which have been registered by the host class, or any
  91. * other class in the hierarchy.
  92. *
  93. * @method unplug
  94. * @static
  95. *
  96. * @param {Function} hostClass The host class from which to unregister the plugins
  97. * @param {Function | Array} plugin The plugin class, or an array of plugin classes
  98. * @for Plugin.Host
  99. */
  100. PluginHost.unplug = function(hostClass, plugin) {
  101. var p, i, l, name;
  102. if (hostClass !== Y.Base) {
  103. hostClass._UNPLUG = hostClass._UNPLUG || {};
  104. if (!L.isArray(plugin)) {
  105. plugin = [plugin];
  106. }
  107. for (i = 0, l = plugin.length; i < l; i++) {
  108. p = plugin[i];
  109. name = p.NAME;
  110. if (!hostClass._PLUG[name]) {
  111. hostClass._UNPLUG[name] = p;
  112. } else {
  113. delete hostClass._PLUG[name];
  114. }
  115. }
  116. }
  117. };
  118. }, '3.4.0' ,{requires:['pluginhost-base']});