_base.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. define("dojox/collections/_base", ["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 = dojox.collections;
  6. =====*/
  7. collections.DictionaryEntry=function(/* string */k, /* object */v){
  8. // summary
  9. // return an object of type dojox.collections.DictionaryEntry
  10. this.key=k;
  11. this.value=v;
  12. this.valueOf=function(){
  13. return this.value; // object
  14. };
  15. this.toString=function(){
  16. return String(this.value); // string
  17. };
  18. }
  19. /* Iterators
  20. * The collections.Iterators (Iterator and DictionaryIterator) are built to
  21. * work with the Collections included in this module. However, they *can*
  22. * be used with arrays and objects, respectively, should one choose to do so.
  23. */
  24. collections.Iterator=function(/* array */a){
  25. // summary
  26. // return an object of type dojox.collections.Iterator
  27. var position=0;
  28. this.element=a[position]||null;
  29. this.atEnd=function(){
  30. // summary
  31. // Test to see if the internal cursor has reached the end of the internal collection.
  32. return (position>=a.length); // bool
  33. };
  34. this.get=function(){
  35. // summary
  36. // Get the next member in the collection.
  37. if(this.atEnd()){
  38. return null; // object
  39. }
  40. this.element=a[position++];
  41. return this.element; // object
  42. };
  43. this.map=function(/* function */fn, /* object? */scope){
  44. // summary
  45. // Functional iteration with optional scope.
  46. return arr.map(a, fn, scope);
  47. };
  48. this.reset=function(){
  49. // summary
  50. // reset the internal cursor.
  51. position=0;
  52. this.element=a[position];
  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. return collections;
  99. });