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