Iterator.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. if (!dojo._hasResource["dojox.widget.Iterator"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.widget.Iterator"] = true;
  6. dojo.provide("dojox.widget.Iterator");
  7. dojo.require("dijit.Declaration");
  8. dojo.experimental("dojox.widget.Iterator"); // level: prototype, designed
  9. // for dijit.chat.demo
  10. /*
  11. * example: from markup: | <span dojoType="dojo.data.ItemFileReadStore" |
  12. * jsId="cstore" url="countries.json"></span> | | <div> | <div
  13. * dojoType="dojox.widget.Iterator" store="cstore" | query="{ name: 'A*'}"> |
  14. * ${name} is a ${type} | </div> | </div>
  15. *
  16. * example: programmatic: | var store = new dojo.data.ItemFileReadStore({
  17. * url: "countries.json" }); | | var iter = new dojox.widget.Iterator({ |
  18. * store: store, | template: "" | }); |
  19. *
  20. * example: programmatic from an array of objects: | var dataArr = [ | {
  21. * name: "foo", valueAttr: "bar" }, | { name: "thinger", valueAttr: "blah" } | ]; | |
  22. * var iter = new dojox.widget.Iterator({ | data: dataArr, | template: "" |
  23. * });
  24. *
  25. * example: programmatic from an array of strings: | var dataArr = [ | {
  26. * name: "foo", valueAttr: "bar" }, | { name: "thinger", valueAttr: "blah" } | ]; | |
  27. * var iter = new dojox.widget.Iterator({ | data: dataArr, | template: "" |
  28. * });
  29. *
  30. */
  31. dojo.declare("dojox.widget.Iterator", [dijit.Declaration], {
  32. constructor : (function() {
  33. var ctr = 0;
  34. return function() {
  35. this.attrs = [];
  36. this.children = [];
  37. this.widgetClass = "dojox.widget.Iterator._classes._" + (ctr++);
  38. }
  39. })(),
  40. start : 0,
  41. fetchMax : 1000,
  42. query : {
  43. name : "*"
  44. },
  45. attrs : [],
  46. defaultValue : "",
  47. widgetCtor : null,
  48. dataValues : [], // an array of strings
  49. data : null, // should be a reference to an Array
  50. store : null,
  51. _srcIndex : 0,
  52. _srcParent : null,
  53. _setSrcIndex : function(s) {
  54. this._srcIndex = 0;
  55. this._srcParent = s.parentNode;
  56. var ts = s;
  57. while (ts.previousSibling) {
  58. this._srcIndex++;
  59. ts = ts.previousSibling;
  60. };
  61. },
  62. postscript : function(p, s) {
  63. // figure out the position of the source node in it's parent
  64. this._setSrcIndex(s);
  65. // this._srcIndex = dojo.query(">", this._srcParent).indexOf(s);
  66. this.inherited("postscript", arguments);
  67. var wc = this.widgetCtor = dojo.getObject(this.widgetClass);
  68. this.attrs = dojo.map(wc.prototype.templateString
  69. .match(/\$\{([^\s\:\}]+)(?:\:([^\s\:\}]+))?\}/g),
  70. function(s) {
  71. return s.slice(2, -1);
  72. });
  73. dojo.forEach(this.attrs, function(m) {
  74. wc.prototype[m] = "";
  75. });
  76. this.update();
  77. },
  78. clear : function() {
  79. if (this.children.length) {
  80. this._setSrcIndex(this.children[0].domNode);
  81. }
  82. dojo.forEach(this.children, "item.destroy();");
  83. this.children = [];
  84. },
  85. update : function() {
  86. if (this.store) {
  87. // we're executing a query
  88. this.fetch();
  89. } else {
  90. // we came from an array of objects. Easier!
  91. this.onDataAvailable(this.data || this.dataValues);
  92. }
  93. },
  94. _addItem : function(/* Object */config, idx) {
  95. if (dojo.isString(config)) {
  96. config = {
  97. value : config
  98. };
  99. }
  100. var widget = new this.widgetCtor(config);
  101. this.children.push(widget);
  102. dojo.place(widget.domNode, this._srcParent, this._srcIndex + idx);
  103. },
  104. getAttrValuesObj : function(item) {
  105. var obj = {};
  106. if (dojo.isString(item)) {
  107. dojo.forEach(this.attrs, function(attr) {
  108. obj[attr] = (attr == "value")
  109. ? item
  110. : this.defaultValue;
  111. }, this);
  112. } else {
  113. dojo.forEach(this.attrs, function(attr) {
  114. if (this.store) {
  115. obj[attr] = this.store.getValue(item, attr)
  116. || this.defaultValue;
  117. } else {
  118. obj[attr] = item[attr] || this.defaultValue;
  119. }
  120. }, this);
  121. }
  122. return obj;
  123. },
  124. onDataAvailable : function(data) {
  125. this.clear();
  126. // console.debug(data);
  127. dojo.forEach(data, function(item, idx) {
  128. this._addItem(this.getAttrValuesObj(item), idx);
  129. }, this);
  130. },
  131. fetch : function(query, start, end) {
  132. this.store.fetch({
  133. query : query || this.query,
  134. start : start || this.start,
  135. count : end || this.fetchMax,
  136. onComplete : dojo.hitch(this, "onDataAvailable")
  137. ,
  138. });
  139. }
  140. });
  141. dojox.widget.Iterator._classes = {};
  142. }