1 | define(["dojo/_base/kernel", "dojo/_base/array", "./_base"], function(dojo, darray, dxc){ |
---|
2 | |
---|
3 | dxc.ArrayList=function(/*array?*/ arr){ |
---|
4 | // summary: |
---|
5 | // Returns a new object of type dojox.collections.ArrayList |
---|
6 | var items=[]; |
---|
7 | if(arr) items=items.concat(arr); |
---|
8 | this.count=items.length; |
---|
9 | this.add=function(/*object*/ obj){ |
---|
10 | // summary: |
---|
11 | // Add an element to the collection. |
---|
12 | items.push(obj); |
---|
13 | this.count=items.length; |
---|
14 | }; |
---|
15 | this.addRange=function(/*array*/ a){ |
---|
16 | // summary: |
---|
17 | // Add a range of objects to the ArrayList |
---|
18 | if(a.getIterator){ |
---|
19 | var e=a.getIterator(); |
---|
20 | while(!e.atEnd()){ |
---|
21 | this.add(e.get()); |
---|
22 | } |
---|
23 | this.count=items.length; |
---|
24 | }else{ |
---|
25 | for(var i=0; i<a.length; i++){ |
---|
26 | items.push(a[i]); |
---|
27 | } |
---|
28 | this.count=items.length; |
---|
29 | } |
---|
30 | }; |
---|
31 | this.clear=function(){ |
---|
32 | // summary: |
---|
33 | // Clear all elements out of the collection, and reset the count. |
---|
34 | items.splice(0, items.length); |
---|
35 | this.count=0; |
---|
36 | }; |
---|
37 | this.clone=function(){ |
---|
38 | // summary: |
---|
39 | // Clone the array list |
---|
40 | return new dxc.ArrayList(items); // dojox.collections.ArrayList |
---|
41 | }; |
---|
42 | this.contains=function(/*object*/ obj){ |
---|
43 | // summary: |
---|
44 | // Check to see if the passed object is a member in the ArrayList |
---|
45 | for(var i=0; i < items.length; i++){ |
---|
46 | if(items[i] == obj) { |
---|
47 | return true; // bool |
---|
48 | } |
---|
49 | } |
---|
50 | return false; // bool |
---|
51 | }; |
---|
52 | this.forEach=function(/*function*/ fn, /*object?*/ scope){ |
---|
53 | // summary: |
---|
54 | // functional iterator, following the mozilla spec. |
---|
55 | dojo.forEach(items, fn, scope); |
---|
56 | }; |
---|
57 | this.getIterator=function(){ |
---|
58 | // summary: |
---|
59 | // Get an Iterator for this object |
---|
60 | return new dxc.Iterator(items); // dojox.collections.Iterator |
---|
61 | }; |
---|
62 | this.indexOf=function(/*object*/ obj){ |
---|
63 | // summary: |
---|
64 | // Return the numeric index of the passed object; will return -1 if not found. |
---|
65 | for(var i=0; i < items.length; i++){ |
---|
66 | if(items[i] == obj) { |
---|
67 | return i; // int |
---|
68 | } |
---|
69 | } |
---|
70 | return -1; // int |
---|
71 | }; |
---|
72 | this.insert=function(/*int*/ i, /*object*/ obj){ |
---|
73 | // summary: |
---|
74 | // Insert the passed object at index i |
---|
75 | items.splice(i,0,obj); |
---|
76 | this.count=items.length; |
---|
77 | }; |
---|
78 | this.item=function(/*int*/ i){ |
---|
79 | // summary: |
---|
80 | // return the element at index i |
---|
81 | return items[i]; // object |
---|
82 | }; |
---|
83 | this.remove=function(/*object*/ obj){ |
---|
84 | // summary: |
---|
85 | // Look for the passed object, and if found, remove it from the internal array. |
---|
86 | var i=this.indexOf(obj); |
---|
87 | if(i >=0) { |
---|
88 | items.splice(i,1); |
---|
89 | } |
---|
90 | this.count=items.length; |
---|
91 | }; |
---|
92 | this.removeAt=function(/*int*/ i){ |
---|
93 | // summary: |
---|
94 | // Remove the element located at the given index. |
---|
95 | items.splice(i,1); |
---|
96 | this.count=items.length; |
---|
97 | }; |
---|
98 | this.reverse=function(){ |
---|
99 | // summary: |
---|
100 | // Reverse the internal array |
---|
101 | items.reverse(); |
---|
102 | }; |
---|
103 | this.sort=function(/*function?*/ fn){ |
---|
104 | // summary: |
---|
105 | // sort the internal array |
---|
106 | if(fn){ |
---|
107 | items.sort(fn); |
---|
108 | }else{ |
---|
109 | items.sort(); |
---|
110 | } |
---|
111 | }; |
---|
112 | this.setByIndex=function(/*int*/ i, /*object*/ obj){ |
---|
113 | // summary: |
---|
114 | // Set an element in the array by the passed index. |
---|
115 | items[i]=obj; |
---|
116 | this.count=items.length; |
---|
117 | }; |
---|
118 | this.toArray=function(){ |
---|
119 | // summary: |
---|
120 | // Return a new array with all of the items of the internal array concatenated. |
---|
121 | return [].concat(items); |
---|
122 | }; |
---|
123 | this.toString=function(/*string*/ delim){ |
---|
124 | // summary: |
---|
125 | // implementation of toString, follows [].toString(); |
---|
126 | return items.join((delim||",")); |
---|
127 | }; |
---|
128 | }; |
---|
129 | return dxc.ArrayList; |
---|
130 | }); |
---|