Iterator.js 3.9 KB

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