4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-util-HashMap-method-constructor'><span id='Ext-util-HashMap'>/**
19 </span></span> * @class Ext.util.HashMap
21 * Represents a collection of a set of key and value pairs. Each key in the HashMap
22 * must be unique, the same key cannot exist twice. Access to items is provided via
23 * the key only. Sample usage:
24 * <pre><code>
25 var map = new Ext.util.HashMap();
30 map.each(function(key, value, length){
31 console.log(key, value, length);
33 * </code></pre>
36 * <p>The HashMap is an unordered class,
37 * there is no guarantee when iterating over the items that they will be in any particular
38 * order. If this is required, then use a {@link Ext.util.MixedCollection}.
41 * @param {Object} config The configuration options
43 Ext.define('Ext.util.HashMap', {
45 <span id='Ext-util-HashMap-cfg-keyFn'> /**
46 </span> * @cfg {Function} keyFn A function that is used to retrieve a default key for a passed object.
47 * A default is provided that returns the <b>id</b> property on the object. This function is only used
48 * if the add method is called with a single argument.
52 observable: 'Ext.util.Observable'
55 constructor: function(config) {
59 <span id='Ext-util-HashMap-event-add'> /**
61 * Fires when a new item is added to the hash
62 * @param {Ext.util.HashMap} this.
63 * @param {String} key The key of the added item.
64 * @param {Object} value The value of the added item.
67 <span id='Ext-util-HashMap-event-clear'> /**
68 </span> * @event clear
69 * Fires when the hash is cleared.
70 * @param {Ext.util.HashMap} this.
73 <span id='Ext-util-HashMap-event-remove'> /**
74 </span> * @event remove
75 * Fires when an item is removed from the hash.
76 * @param {Ext.util.HashMap} this.
77 * @param {String} key The key of the removed item.
78 * @param {Object} value The value of the removed item.
81 <span id='Ext-util-HashMap-event-replace'> /**
82 </span> * @event replace
83 * Fires when an item is replaced in the hash.
84 * @param {Ext.util.HashMap} this.
85 * @param {String} key The key of the replaced item.
86 * @param {Object} value The new value for the item.
87 * @param {Object} old The old value for the item.
92 me.mixins.observable.constructor.call(me, config);
96 <span id='Ext-util-HashMap-method-getCount'> /**
97 </span> * Gets the number of items in the hash.
98 * @return {Number} The number of items in the hash.
100 getCount: function() {
104 <span id='Ext-util-HashMap-method-getData'> /**
105 </span> * Implementation for being able to extract the key from an object if only
106 * a single argument is passed.
108 * @param {String} key The key
109 * @param {Object} value The value
110 * @return {Array} [key, value]
112 getData: function(key, value) {
113 // if we have no value, it means we need to get the key from the object
114 if (value === undefined) {
116 key = this.getKey(value);
122 <span id='Ext-util-HashMap-method-getKey'> /**
123 </span> * Extracts the key from an object. This is a default implementation, it may be overridden
125 * @param {Object} o The object to get the key from
126 * @return {String} The key to use.
128 getKey: function(o) {
132 <span id='Ext-util-HashMap-method-add'> /**
133 </span> * Adds an item to the collection. Fires the {@link #add} event when complete.
134 * @param {String} key <p>The key to associate with the item, or the new item.</p>
135 * <p>If a {@link #getKey} implementation was specified for this HashMap,
136 * or if the key of the stored items is in a property called <tt><b>id</b></tt>,
137 * the HashMap will be able to <i>derive</i> the key for the new item.
138 * In this case just pass the new item in this parameter.</p>
139 * @param {Object} o The item to add.
140 * @return {Object} The item added.
142 add: function(key, value) {
146 if (arguments.length === 1) {
148 key = me.getKey(value);
151 if (me.containsKey(key)) {
152 me.replace(key, value);
155 data = me.getData(key, value);
160 me.fireEvent('add', me, key, value);
164 <span id='Ext-util-HashMap-method-replace'> /**
165 </span> * Replaces an item in the hash. If the key doesn't exist, the
166 * {@link #add} method will be used.
167 * @param {String} key The key of the item.
168 * @param {Object} value The new value for the item.
169 * @return {Object} The new value of the item.
171 replace: function(key, value) {
176 if (!me.containsKey(key)) {
181 me.fireEvent('replace', me, key, value, old);
185 <span id='Ext-util-HashMap-method-remove'> /**
186 </span> * Remove an item from the hash.
187 * @param {Object} o The value of the item to remove.
188 * @return {Boolean} True if the item was successfully removed.
190 remove: function(o) {
191 var key = this.findKey(o);
192 if (key !== undefined) {
193 return this.removeAtKey(key);
198 <span id='Ext-util-HashMap-method-removeAtKey'> /**
199 </span> * Remove an item from the hash.
200 * @param {String} key The key to remove.
201 * @return {Boolean} True if the item was successfully removed.
203 removeAtKey: function(key) {
207 if (me.containsKey(key)) {
211 me.fireEvent('remove', me, key, value);
217 <span id='Ext-util-HashMap-method-get'> /**
218 </span> * Retrieves an item with a particular key.
219 * @param {String} key The key to lookup.
220 * @return {Object} The value at that key. If it doesn't exist, <tt>undefined</tt> is returned.
223 return this.map[key];
226 <span id='Ext-util-HashMap-method-clear'> /**
227 </span> * Removes all items from the hash.
228 * @return {Ext.util.HashMap} this
230 clear: function(/* private */ initial) {
234 if (initial !== true) {
235 me.fireEvent('clear', me);
240 <span id='Ext-util-HashMap-method-containsKey'> /**
241 </span> * Checks whether a key exists in the hash.
242 * @param {String} key The key to check for.
243 * @return {Boolean} True if they key exists in the hash.
245 containsKey: function(key) {
246 return this.map[key] !== undefined;
249 <span id='Ext-util-HashMap-method-contains'> /**
250 </span> * Checks whether a value exists in the hash.
251 * @param {Object} value The value to check for.
252 * @return {Boolean} True if the value exists in the dictionary.
254 contains: function(value) {
255 return this.containsKey(this.findKey(value));
258 <span id='Ext-util-HashMap-method-getKeys'> /**
259 </span> * Return all of the keys in the hash.
260 * @return {Array} An array of keys.
262 getKeys: function() {
263 return this.getArray(true);
266 <span id='Ext-util-HashMap-method-getValues'> /**
267 </span> * Return all of the values in the hash.
268 * @return {Array} An array of values.
270 getValues: function() {
271 return this.getArray(false);
274 <span id='Ext-util-HashMap-method-getArray'> /**
275 </span> * Gets either the keys/values in an array from the hash.
277 * @param {Boolean} isKey True to extract the keys, otherwise, the value
278 * @return {Array} An array of either keys/values from the hash.
280 getArray: function(isKey) {
285 if (map.hasOwnProperty(key)) {
286 arr.push(isKey ? key: map[key]);
292 <span id='Ext-util-HashMap-method-each'> /**
293 </span> * Executes the specified function once for each item in the hash.
294 * Returning false from the function will cease iteration.
296 * The paramaters passed to the function are:
297 * <div class="mdetail-params"><ul>
298 * <li><b>key</b> : String<p class="sub-desc">The key of the item</p></li>
299 * <li><b>value</b> : Number<p class="sub-desc">The value of the item</p></li>
300 * <li><b>length</b> : Number<p class="sub-desc">The total number of items in the hash</p></li>
301 * </ul></div>
302 * @param {Function} fn The function to execute.
303 * @param {Object} scope The scope to execute in. Defaults to <tt>this</tt>.
304 * @return {Ext.util.HashMap} this
306 each: function(fn, scope) {
307 // copy items so they may be removed during iteration.
308 var items = Ext.apply({}, this.map),
310 length = this.length;
312 scope = scope || this;
314 if (items.hasOwnProperty(key)) {
315 if (fn.call(scope, key, items[key], length) === false) {
323 <span id='Ext-util-HashMap-method-clone'> /**
324 </span> * Performs a shallow copy on this hash.
325 * @return {Ext.util.HashMap} The new hash object.
328 var hash = new this.self(),
332 hash.suspendEvents();
334 if (map.hasOwnProperty(key)) {
335 hash.add(key, map[key]);
342 <span id='Ext-util-HashMap-method-findKey'> /**
344 * Find the key for a value.
345 * @param {Object} value The value to find.
346 * @return {Object} The value of the item. Returns <tt>undefined</tt> if not found.
348 findKey: function(value) {
353 if (map.hasOwnProperty(key) && map[key] === value) {