4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../resources/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'>/**
19 </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 Ext.define('Ext.util.HashMap', {
43 observable: 'Ext.util.Observable'
46 <span id='Ext-util-HashMap-cfg-keyFn'> /**
47 </span> * @cfg {Function} keyFn A function that is used to retrieve a default key for a passed object.
48 * A default is provided that returns the <b>id</b> property on the object. This function is only used
49 * if the add method is called with a single argument.
52 <span id='Ext-util-HashMap-method-constructor'> /**
53 </span> * Creates new HashMap.
54 * @param {Object} config (optional) Config object.
56 constructor: function(config) {
57 config = config || {};
63 <span id='Ext-util-HashMap-event-add'> /**
65 * Fires when a new item is added to the hash
66 * @param {Ext.util.HashMap} this.
67 * @param {String} key The key of the added item.
68 * @param {Object} value The value of the added item.
71 <span id='Ext-util-HashMap-event-clear'> /**
72 </span> * @event clear
73 * Fires when the hash is cleared.
74 * @param {Ext.util.HashMap} this.
77 <span id='Ext-util-HashMap-event-remove'> /**
78 </span> * @event remove
79 * Fires when an item is removed from the hash.
80 * @param {Ext.util.HashMap} this.
81 * @param {String} key The key of the removed item.
82 * @param {Object} value The value of the removed item.
85 <span id='Ext-util-HashMap-event-replace'> /**
86 </span> * @event replace
87 * Fires when an item is replaced in the hash.
88 * @param {Ext.util.HashMap} this.
89 * @param {String} key The key of the replaced item.
90 * @param {Object} value The new value for the item.
91 * @param {Object} old The old value for the item.
96 me.mixins.observable.constructor.call(me, config);
104 <span id='Ext-util-HashMap-method-getCount'> /**
105 </span> * Gets the number of items in the hash.
106 * @return {Number} The number of items in the hash.
108 getCount: function() {
112 <span id='Ext-util-HashMap-method-getData'> /**
113 </span> * Implementation for being able to extract the key from an object if only
114 * a single argument is passed.
116 * @param {String} key The key
117 * @param {Object} value The value
118 * @return {Array} [key, value]
120 getData: function(key, value) {
121 // if we have no value, it means we need to get the key from the object
122 if (value === undefined) {
124 key = this.getKey(value);
130 <span id='Ext-util-HashMap-method-getKey'> /**
131 </span> * Extracts the key from an object. This is a default implementation, it may be overridden
132 * @param {Object} o The object to get the key from
133 * @return {String} The key to use.
135 getKey: function(o) {
139 <span id='Ext-util-HashMap-method-add'> /**
140 </span> * Adds an item to the collection. Fires the {@link #add} event when complete.
141 * @param {String} key <p>The key to associate with the item, or the new item.</p>
142 * <p>If a {@link #getKey} implementation was specified for this HashMap,
143 * or if the key of the stored items is in a property called <tt><b>id</b></tt>,
144 * the HashMap will be able to <i>derive</i> the key for the new item.
145 * In this case just pass the new item in this parameter.</p>
146 * @param {Object} o The item to add.
147 * @return {Object} The item added.
149 add: function(key, value) {
153 if (arguments.length === 1) {
155 key = me.getKey(value);
158 if (me.containsKey(key)) {
159 return me.replace(key, value);
162 data = me.getData(key, value);
167 me.fireEvent('add', me, key, value);
171 <span id='Ext-util-HashMap-method-replace'> /**
172 </span> * Replaces an item in the hash. If the key doesn't exist, the
173 * {@link #add} method will be used.
174 * @param {String} key The key of the item.
175 * @param {Object} value The new value for the item.
176 * @return {Object} The new value of the item.
178 replace: function(key, value) {
183 if (!me.containsKey(key)) {
188 me.fireEvent('replace', me, key, value, old);
192 <span id='Ext-util-HashMap-method-remove'> /**
193 </span> * Remove an item from the hash.
194 * @param {Object} o The value of the item to remove.
195 * @return {Boolean} True if the item was successfully removed.
197 remove: function(o) {
198 var key = this.findKey(o);
199 if (key !== undefined) {
200 return this.removeAtKey(key);
205 <span id='Ext-util-HashMap-method-removeAtKey'> /**
206 </span> * Remove an item from the hash.
207 * @param {String} key The key to remove.
208 * @return {Boolean} True if the item was successfully removed.
210 removeAtKey: function(key) {
214 if (me.containsKey(key)) {
218 me.fireEvent('remove', me, key, value);
224 <span id='Ext-util-HashMap-method-get'> /**
225 </span> * Retrieves an item with a particular key.
226 * @param {String} key The key to lookup.
227 * @return {Object} The value at that key. If it doesn't exist, <tt>undefined</tt> is returned.
230 return this.map[key];
233 <span id='Ext-util-HashMap-method-clear'> /**
234 </span> * Removes all items from the hash.
235 * @return {Ext.util.HashMap} this
237 clear: function(/* private */ initial) {
241 if (initial !== true) {
242 me.fireEvent('clear', me);
247 <span id='Ext-util-HashMap-method-containsKey'> /**
248 </span> * Checks whether a key exists in the hash.
249 * @param {String} key The key to check for.
250 * @return {Boolean} True if they key exists in the hash.
252 containsKey: function(key) {
253 return this.map[key] !== undefined;
256 <span id='Ext-util-HashMap-method-contains'> /**
257 </span> * Checks whether a value exists in the hash.
258 * @param {Object} value The value to check for.
259 * @return {Boolean} True if the value exists in the dictionary.
261 contains: function(value) {
262 return this.containsKey(this.findKey(value));
265 <span id='Ext-util-HashMap-method-getKeys'> /**
266 </span> * Return all of the keys in the hash.
267 * @return {Array} An array of keys.
269 getKeys: function() {
270 return this.getArray(true);
273 <span id='Ext-util-HashMap-method-getValues'> /**
274 </span> * Return all of the values in the hash.
275 * @return {Array} An array of values.
277 getValues: function() {
278 return this.getArray(false);
281 <span id='Ext-util-HashMap-method-getArray'> /**
282 </span> * Gets either the keys/values in an array from the hash.
284 * @param {Boolean} isKey True to extract the keys, otherwise, the value
285 * @return {Array} An array of either keys/values from the hash.
287 getArray: function(isKey) {
292 if (map.hasOwnProperty(key)) {
293 arr.push(isKey ? key: map[key]);
299 <span id='Ext-util-HashMap-method-each'> /**
300 </span> * Executes the specified function once for each item in the hash.
301 * Returning false from the function will cease iteration.
303 * The paramaters passed to the function are:
304 * <div class="mdetail-params"><ul>
305 * <li><b>key</b> : String<p class="sub-desc">The key of the item</p></li>
306 * <li><b>value</b> : Number<p class="sub-desc">The value of the item</p></li>
307 * <li><b>length</b> : Number<p class="sub-desc">The total number of items in the hash</p></li>
308 * </ul></div>
309 * @param {Function} fn The function to execute.
310 * @param {Object} scope The scope to execute in. Defaults to <tt>this</tt>.
311 * @return {Ext.util.HashMap} this
313 each: function(fn, scope) {
314 // copy items so they may be removed during iteration.
315 var items = Ext.apply({}, this.map),
317 length = this.length;
319 scope = scope || this;
321 if (items.hasOwnProperty(key)) {
322 if (fn.call(scope, key, items[key], length) === false) {
330 <span id='Ext-util-HashMap-method-clone'> /**
331 </span> * Performs a shallow copy on this hash.
332 * @return {Ext.util.HashMap} The new hash object.
335 var hash = new this.self(),
339 hash.suspendEvents();
341 if (map.hasOwnProperty(key)) {
342 hash.add(key, map[key]);
349 <span id='Ext-util-HashMap-method-findKey'> /**
351 * Find the key for a value.
352 * @param {Object} value The value to find.
353 * @return {Object} The value of the item. Returns <tt>undefined</tt> if not found.
355 findKey: function(value) {
360 if (map.hasOwnProperty(key) && map[key] === value) {