source: Dev/branches/rest-dojo-ui/client/dojox/collections/Dictionary.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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