ArrayList.js 3.4 KB

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