PluginManager.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Provides a registry of available Plugin classes indexed by a mnemonic code known as the Plugin's ptype.
  3. *
  4. * A plugin may be specified simply as a *config object* as long as the correct `ptype` is specified:
  5. *
  6. * {
  7. * ptype: 'gridviewdragdrop',
  8. * dragText: 'Drag and drop to reorganize'
  9. * }
  10. *
  11. * Or just use the ptype on its own:
  12. *
  13. * 'gridviewdragdrop'
  14. *
  15. * Alternatively you can instantiate the plugin with Ext.create:
  16. *
  17. * Ext.create('Ext.grid.plugin.DragDrop', {
  18. * dragText: 'Drag and drop to reorganize'
  19. * })
  20. */
  21. Ext.define('Ext.PluginManager', {
  22. extend: 'Ext.AbstractManager',
  23. alternateClassName: 'Ext.PluginMgr',
  24. singleton: true,
  25. typeName: 'ptype',
  26. /**
  27. * Creates a new Plugin from the specified config object using the config object's ptype to determine the class to
  28. * instantiate.
  29. * @param {Object} config A configuration object for the Plugin you wish to create.
  30. * @param {Function} defaultType (optional) The constructor to provide the default Plugin type if the config object does not
  31. * contain a `ptype`. (Optional if the config contains a `ptype`).
  32. * @return {Ext.Component} The newly instantiated Plugin.
  33. */
  34. create : function(config, defaultType){
  35. if (config.init) {
  36. return config;
  37. } else {
  38. return Ext.createByAlias('plugin.' + (config.ptype || defaultType), config);
  39. }
  40. // Prior system supported Singleton plugins.
  41. //var PluginCls = this.types[config.ptype || defaultType];
  42. //if (PluginCls.init) {
  43. // return PluginCls;
  44. //} else {
  45. // return new PluginCls(config);
  46. //}
  47. },
  48. //create: function(plugin, defaultType) {
  49. // if (plugin instanceof this) {
  50. // return plugin;
  51. // } else {
  52. // var type, config = {};
  53. //
  54. // if (Ext.isString(plugin)) {
  55. // type = plugin;
  56. // }
  57. // else {
  58. // type = plugin[this.typeName] || defaultType;
  59. // config = plugin;
  60. // }
  61. //
  62. // return Ext.createByAlias('plugin.' + type, config);
  63. // }
  64. //},
  65. /**
  66. * Returns all plugins registered with the given type. Here, 'type' refers to the type of plugin, not its ptype.
  67. * @param {String} type The type to search for
  68. * @param {Boolean} defaultsOnly True to only return plugins of this type where the plugin's isDefault property is
  69. * truthy
  70. * @return {Ext.AbstractPlugin[]} All matching plugins
  71. */
  72. findByType: function(type, defaultsOnly) {
  73. var matches = [],
  74. types = this.types,
  75. name,
  76. item;
  77. for (name in types) {
  78. if (!types.hasOwnProperty(name)) {
  79. continue;
  80. }
  81. item = types[name];
  82. if (item.type == type && (!defaultsOnly || (defaultsOnly === true && item.isDefault))) {
  83. matches.push(item);
  84. }
  85. }
  86. return matches;
  87. }
  88. }, function() {
  89. /**
  90. * Shorthand for {@link Ext.PluginManager#registerType}
  91. * @param {String} ptype The ptype mnemonic string by which the Plugin class
  92. * may be looked up.
  93. * @param {Function} cls The new Plugin class.
  94. * @member Ext
  95. * @method preg
  96. */
  97. Ext.preg = function() {
  98. return Ext.PluginManager.registerType.apply(Ext.PluginManager, arguments);
  99. };
  100. });