1 | define(["dojo/_base/kernel", "dojo/_base/lang", "dojo/_base/array"], |
---|
2 | function(dojo, lang, arr){ |
---|
3 | var collections = lang.getObject("dojox.collections", true); |
---|
4 | |
---|
5 | collections.DictionaryEntry=function(/*string*/ k, /*object*/ v){ |
---|
6 | // summary: |
---|
7 | // return an object of type dojox.collections.DictionaryEntry |
---|
8 | this.key=k; |
---|
9 | this.value=v; |
---|
10 | this.valueOf=function(){ |
---|
11 | return this.value; // object |
---|
12 | }; |
---|
13 | this.toString=function(){ |
---|
14 | return String(this.value); // string |
---|
15 | }; |
---|
16 | } |
---|
17 | |
---|
18 | /* Iterators |
---|
19 | * The collections.Iterators (Iterator and DictionaryIterator) are built to |
---|
20 | * work with the Collections included in this module. However, they *can* |
---|
21 | * be used with arrays and objects, respectively, should one choose to do so. |
---|
22 | */ |
---|
23 | collections.Iterator=function(/*array*/ a){ |
---|
24 | // summary: |
---|
25 | // return an object of type dojox.collections.Iterator |
---|
26 | var position=0; |
---|
27 | this.element=a[position]||null; |
---|
28 | this.atEnd=function(){ |
---|
29 | // summary: |
---|
30 | // Test to see if the internal cursor has reached the end of the internal collection. |
---|
31 | return (position>=a.length); // bool |
---|
32 | }; |
---|
33 | this.get=function(){ |
---|
34 | // summary: |
---|
35 | // Get the next member in the collection. |
---|
36 | if(this.atEnd()){ |
---|
37 | return null; // object |
---|
38 | } |
---|
39 | this.element=a[position++]; |
---|
40 | return this.element; // object |
---|
41 | }; |
---|
42 | this.map=function(/*function*/ fn, /*object?*/ scope){ |
---|
43 | // summary: |
---|
44 | // Functional iteration with optional scope. |
---|
45 | return arr.map(a, fn, scope); |
---|
46 | }; |
---|
47 | this.reset=function(){ |
---|
48 | // summary: |
---|
49 | // reset the internal cursor. |
---|
50 | position=0; |
---|
51 | this.element=a[position]; |
---|
52 | }; |
---|
53 | } |
---|
54 | |
---|
55 | /* Notes: |
---|
56 | * The DictionaryIterator no longer supports a key and value property; |
---|
57 | * the reality is that you can use this to iterate over a JS object |
---|
58 | * being used as a hashtable. |
---|
59 | */ |
---|
60 | collections.DictionaryIterator=function(/*object*/ obj){ |
---|
61 | // summary: |
---|
62 | // return an object of type dojox.collections.DictionaryIterator |
---|
63 | var a=[]; // Create an indexing array |
---|
64 | var testObject={}; |
---|
65 | for(var p in obj){ |
---|
66 | if(!testObject[p]){ |
---|
67 | a.push(obj[p]); // fill it up |
---|
68 | } |
---|
69 | } |
---|
70 | var position=0; |
---|
71 | this.element=a[position]||null; |
---|
72 | this.atEnd=function(){ |
---|
73 | // summary: |
---|
74 | // Test to see if the internal cursor has reached the end of the internal collection. |
---|
75 | return (position>=a.length); // bool |
---|
76 | }; |
---|
77 | this.get=function(){ |
---|
78 | // summary: |
---|
79 | // Get the next member in the collection. |
---|
80 | if(this.atEnd()){ |
---|
81 | return null; // object |
---|
82 | } |
---|
83 | this.element=a[position++]; |
---|
84 | return this.element; // object |
---|
85 | }; |
---|
86 | this.map=function(/*function*/ fn, /*object?*/ scope){ |
---|
87 | // summary: |
---|
88 | // Functional iteration with optional scope. |
---|
89 | return arr.map(a, fn, scope); |
---|
90 | }; |
---|
91 | this.reset=function() { |
---|
92 | // summary: |
---|
93 | // reset the internal cursor. |
---|
94 | position=0; |
---|
95 | this.element=a[position]; |
---|
96 | }; |
---|
97 | }; |
---|
98 | |
---|
99 | return collections; |
---|
100 | }); |
---|