/ext-4.1.0_b3/src/AbstractManager.js

https://bitbucket.org/srogerf/javascript · JavaScript · 144 lines · 59 code · 20 blank · 65 comment · 12 complexity · 56640fb3bd60d001388c9e415ebb1e8b MD5 · raw file

  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. if (all.containsKey(id)) {
  94. item = all.get(id);
  95. fn.call(scope || item, item);
  96. } else {
  97. all.on('add', function(map, key, item){
  98. if (key == id) {
  99. fn.call(scope || item, item);
  100. all.un('add', fn, scope);
  101. }
  102. });
  103. }
  104. },
  105. /**
  106. * Executes the specified function once for each item in the collection.
  107. * @param {Function} fn The function to execute.
  108. * @param {String} fn.key The key of the item
  109. * @param {Number} fn.value The value of the item
  110. * @param {Number} fn.length The total number of items in the collection
  111. * @param {Boolean} fn.return False to cease iteration.
  112. * @param {Object} scope The scope to execute in. Defaults to `this`.
  113. */
  114. each: function(fn, scope){
  115. this.all.each(fn, scope || this);
  116. },
  117. /**
  118. * Gets the number of items in the collection.
  119. * @return {Number} The number of items in the collection.
  120. */
  121. getCount: function(){
  122. return this.all.getCount();
  123. }
  124. });