Iterator.js 4.1 KB

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