source: Dev/trunk/src/client/dojox/collections/Dictionary.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 3.0 KB
Line 
1define(["dojo/_base/kernel", "dojo/_base/array", "./_base"], function(dojo, darray, dxc){
2
3        dxc.Dictionary=function(/*dojox.collections.Dictionary?*/ dictionary){
4                // summary:
5                //              Returns an object of type dojox.collections.Dictionary
6                var items={};
7                this.count=0;
8
9                //      comparator for property addition and access.
10                var testObject={};
11
12                this.add=function(/*string*/ k, /*object*/ v){
13                        // summary:
14                        //              Add a new item to the Dictionary.
15                        var b=(k in items);
16                        items[k]=new dxc.DictionaryEntry(k,v);
17                        if(!b){
18                                this.count++;
19                        }
20                };
21                this.clear=function(){
22                        // summary:
23                        //              Clears the internal dictionary.
24                        items={};
25                        this.count=0;
26                };
27                this.clone=function(){
28                        // summary:
29                        //              Returns a new instance of dojox.collections.Dictionary; note the the dictionary is a clone but items might not be.
30                        return new dxc.Dictionary(this);        //      dojox.collections.Dictionary
31                };
32                this.contains=this.containsKey=function(/*string*/ k){
33                        // summary:
34                        //              Check to see if the dictionary has an entry at key "k".
35                        if(testObject[k]){
36                                return false;                   // bool
37                        }
38                        return (items[k]!=null);        //      bool
39                };
40                this.containsValue=function(/*object*/ v){
41                        // summary:
42                        //              Check to see if the dictionary has an entry with value "v".
43                        var e=this.getIterator();
44                        while(e.get()){
45                                if(e.element.value==v){
46                                        return true;    //      bool
47                                }
48                        }
49                        return false;   //      bool
50                };
51                this.entry=function(/*string*/ k){
52                        // summary:
53                        //              Accessor method; similar to dojox.collections.Dictionary.item but returns the actual Entry object.
54                        return items[k];        //      dojox.collections.DictionaryEntry
55                };
56                this.forEach=function(/*function*/ fn, /*object?*/ scope){
57                        // summary:
58                        //              functional iterator, following the mozilla spec.
59                        var a=[];       //      Create an indexing array
60                        for(var p in items) {
61                                if(!testObject[p]){
62                                        a.push(items[p]);       //      fill it up
63                                }
64                        }
65                        dojo.forEach(a, fn, scope);
66                };
67                this.getKeyList=function(){
68                        // summary:
69                        //              Returns an array of the keys in the dictionary.
70                        return (this.getIterator()).map(function(entry){
71                                return entry.key;
72                        });     //      array
73                };
74                this.getValueList=function(){
75                        // summary:
76                        //              Returns an array of the values in the dictionary.
77                        return (this.getIterator()).map(function(entry){
78                                return entry.value;
79                        });     //      array
80                };
81                this.item=function(/*string*/ k){
82                        // summary:
83                        //              Accessor method.
84                        if(k in items){
85                                return items[k].valueOf();      //      object
86                        }
87                        return undefined;       //      object
88                };
89                this.getIterator=function(){
90                        // summary:
91                        //              Gets a dojox.collections.DictionaryIterator for iteration purposes.
92                        return new dxc.DictionaryIterator(items);       //      dojox.collections.DictionaryIterator
93                };
94                this.remove=function(/*string*/ k){
95                        // summary:
96                        //              Removes the item at k from the internal collection.
97                        if(k in items && !testObject[k]){
98                                delete items[k];
99                                this.count--;
100                                return true;    //      bool
101                        }
102                        return false;   //      bool
103                };
104
105                if (dictionary){
106                        var e=dictionary.getIterator();
107                        while(e.get()) {
108                                 this.add(e.element.key, e.element.value);
109                        }
110                }
111        };
112        return dxc.Dictionary;
113});
Note: See TracBrowser for help on using the repository browser.