AbstractManager.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Base Manager class
  3. */
  4. Ext.define('Ext.AbstractManager', {
  5. /* Begin Definitions */
  6. requires: ['Ext.util.HashMap'],
  7. /* End Definitions */
  8. typeName: 'type',
  9. constructor: function(config) {
  10. Ext.apply(this, config || {});
  11. /**
  12. * @property {Ext.util.HashMap} all
  13. * Contains all of the items currently managed
  14. */
  15. this.all = new Ext.util.HashMap();
  16. this.types = {};
  17. },
  18. /**
  19. * Returns an item by id.
  20. * For additional details see {@link Ext.util.HashMap#get}.
  21. * @param {String} id The id of the item
  22. * @return {Object} The item, undefined if not found.
  23. */
  24. get : function(id) {
  25. return this.all.get(id);
  26. },
  27. /**
  28. * Registers an item to be managed
  29. * @param {Object} item The item to register
  30. */
  31. register: function(item) {
  32. //<debug>
  33. var all = this.all,
  34. key = all.getKey(item);
  35. if (all.containsKey(key)) {
  36. Ext.Error.raise('Registering duplicate id "' + key + '" with this manager');
  37. }
  38. //</debug>
  39. this.all.add(item);
  40. },
  41. /**
  42. * Unregisters an item by removing it from this manager
  43. * @param {Object} item The item to unregister
  44. */
  45. unregister: function(item) {
  46. this.all.remove(item);
  47. },
  48. /**
  49. * Registers a new item constructor, keyed by a type key.
  50. * @param {String} type The mnemonic string by which the class may be looked up.
  51. * @param {Function} cls The new instance class.
  52. */
  53. registerType : function(type, cls) {
  54. this.types[type] = cls;
  55. cls[this.typeName] = type;
  56. },
  57. /**
  58. * Checks if an item type is registered.
  59. * @param {String} type The mnemonic string by which the class may be looked up
  60. * @return {Boolean} Whether the type is registered.
  61. */
  62. isRegistered : function(type){
  63. return this.types[type] !== undefined;
  64. },
  65. /**
  66. * Creates and returns an instance of whatever this manager manages, based on the supplied type and
  67. * config object.
  68. * @param {Object} config The config object
  69. * @param {String} defaultType If no type is discovered in the config object, we fall back to this type
  70. * @return {Object} The instance of whatever this manager is managing
  71. */
  72. create: function(config, defaultType) {
  73. var type = config[this.typeName] || config.type || defaultType,
  74. Constructor = this.types[type];
  75. //<debug>
  76. if (Constructor === undefined) {
  77. Ext.Error.raise("The '" + type + "' type has not been registered with this manager");
  78. }
  79. //</debug>
  80. return new Constructor(config);
  81. },
  82. /**
  83. * Registers a function that will be called when an item with the specified id is added to the manager.
  84. * This will happen on instantiation.
  85. * @param {String} id The item id
  86. * @param {Function} fn The callback function. Called with a single parameter, the item.
  87. * @param {Object} scope The scope (this reference) in which the callback is executed.
  88. * Defaults to the item.
  89. */
  90. onAvailable : function(id, fn, scope){
  91. var all = this.all,
  92. item,
  93. callback;
  94. if (all.containsKey(id)) {
  95. item = all.get(id);
  96. fn.call(scope || item, item);
  97. } else {
  98. callback = function(map, key, item){
  99. if (key == id) {
  100. fn.call(scope || item, item);
  101. all.un('add', callback);
  102. }
  103. };
  104. all.on('add', callback);
  105. }
  106. },
  107. /**
  108. * Executes the specified function once for each item in the collection.
  109. * @param {Function} fn The function to execute.
  110. * @param {String} fn.key The key of the item
  111. * @param {Number} fn.value The value of the item
  112. * @param {Number} fn.length The total number of items in the collection
  113. * @param {Boolean} fn.return False to cease iteration.
  114. * @param {Object} scope The scope to execute in. Defaults to `this`.
  115. */
  116. each: function(fn, scope){
  117. this.all.each(fn, scope || this);
  118. },
  119. /**
  120. * Gets the number of items in the collection.
  121. * @return {Number} The number of items in the collection.
  122. */
  123. getCount: function(){
  124. return this.all.getCount();
  125. }
  126. });