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