| 1 |
dojo.provide("dojox.widget.Iterator"); |
| 2 |
dojo.require("dijit.Declaration"); |
| 3 |
|
| 4 |
dojo.experimental("dojox.widget.Iterator"); |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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: [], |
| 76 |
data: null, |
| 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 |
|
| 93 |
this._setSrcIndex(s); |
| 94 |
|
| 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 |
|
| 121 |
this.fetch(); |
| 122 |
}else{ |
| 123 |
|
| 124 |
this.onDataAvailable(this.data||this.dataValues); |
| 125 |
} |
| 126 |
}, |
| 127 |
|
| 128 |
_addItem: function(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 |
|
| 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 = {}; |