_base.js 2.9 KB

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