| 1 |
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/_base/connect", "dojox/lang/functional"], |
| 2 |
function(Lang, declare, ArrayUtil, connect, df){ |
| 3 |
|
| 4 |
return declare("dojox.charting.DataSeries", null, { |
| 5 |
constructor: function(store, kwArgs, value){ |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
this.store = store; |
| 21 |
this.kwArgs = kwArgs; |
| 22 |
|
| 23 |
if(value){ |
| 24 |
if(Lang.isFunction(value)){ |
| 25 |
this.value = value; |
| 26 |
}else if(Lang.isObject(value)){ |
| 27 |
this.value = Lang.hitch(this, "_dictValue", |
| 28 |
df.keys(value), value); |
| 29 |
}else{ |
| 30 |
this.value = Lang.hitch(this, "_fieldValue", value); |
| 31 |
} |
| 32 |
}else{ |
| 33 |
this.value = Lang.hitch(this, "_defaultValue"); |
| 34 |
} |
| 35 |
|
| 36 |
this.data = []; |
| 37 |
|
| 38 |
this._events = []; |
| 39 |
|
| 40 |
if(this.store.getFeatures()["dojo.data.api.Notification"]){ |
| 41 |
this._events.push( |
| 42 |
connect.connect(this.store, "onNew", this, "_onStoreNew"), |
| 43 |
connect.connect(this.store, "onDelete", this, "_onStoreDelete"), |
| 44 |
connect.connect(this.store, "onSet", this, "_onStoreSet") |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
this._initialRendering = true; |
| 49 |
this.fetch(); |
| 50 |
}, |
| 51 |
|
| 52 |
destroy: function(){ |
| 53 |
|
| 54 |
|
| 55 |
ArrayUtil.forEach(this._events, connect.disconnect); |
| 56 |
}, |
| 57 |
|
| 58 |
setSeriesObject: function(series){ |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
this.series = series; |
| 64 |
}, |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
_dictValue: function(keys, dict, store, item){ |
| 69 |
var o = {}; |
| 70 |
ArrayUtil.forEach(keys, function(key){ |
| 71 |
o[key] = store.getValue(item, dict[key]); |
| 72 |
}); |
| 73 |
return o; |
| 74 |
}, |
| 75 |
|
| 76 |
_fieldValue: function(field, store, item){ |
| 77 |
return store.getValue(item, field); |
| 78 |
}, |
| 79 |
|
| 80 |
_defaultValue: function(store, item){ |
| 81 |
return store.getValue(item, "value"); |
| 82 |
}, |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
fetch: function(){ |
| 87 |
|
| 88 |
|
| 89 |
if(!this._inFlight){ |
| 90 |
this._inFlight = true; |
| 91 |
var kwArgs = Lang.delegate(this.kwArgs); |
| 92 |
kwArgs.onComplete = Lang.hitch(this, "_onFetchComplete"); |
| 93 |
kwArgs.onError = Lang.hitch(this, "onFetchError"); |
| 94 |
this.store.fetch(kwArgs); |
| 95 |
} |
| 96 |
}, |
| 97 |
|
| 98 |
_onFetchComplete: function(items, request){ |
| 99 |
this.items = items; |
| 100 |
this._buildItemMap(); |
| 101 |
this.data = ArrayUtil.map(this.items, function(item){ |
| 102 |
return this.value(this.store, item); |
| 103 |
}, this); |
| 104 |
this._pushDataChanges(); |
| 105 |
this._inFlight = false; |
| 106 |
}, |
| 107 |
|
| 108 |
onFetchError: function(errorData, request){ |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
this._inFlight = false; |
| 114 |
}, |
| 115 |
|
| 116 |
_buildItemMap: function(){ |
| 117 |
if(this.store.getFeatures()["dojo.data.api.Identity"]){ |
| 118 |
var itemMap = {}; |
| 119 |
ArrayUtil.forEach(this.items, function(item, index){ |
| 120 |
itemMap[this.store.getIdentity(item)] = index; |
| 121 |
}, this); |
| 122 |
this.itemMap = itemMap; |
| 123 |
} |
| 124 |
}, |
| 125 |
|
| 126 |
_pushDataChanges: function(){ |
| 127 |
if(this.series){ |
| 128 |
this.series.chart.updateSeries(this.series.name, this, this._initialRendering); |
| 129 |
this._initialRendering = false; |
| 130 |
this.series.chart.delayedRender(); |
| 131 |
} |
| 132 |
}, |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
_onStoreNew: function(){ |
| 137 |
|
| 138 |
this.fetch(); |
| 139 |
}, |
| 140 |
|
| 141 |
_onStoreDelete: function(item){ |
| 142 |
|
| 143 |
|
| 144 |
if(this.items){ |
| 145 |
var flag = ArrayUtil.some(this.items, function(it, index){ |
| 146 |
if(it === item){ |
| 147 |
this.items.splice(index, 1); |
| 148 |
this._buildItemMap(); |
| 149 |
this.data.splice(index, 1); |
| 150 |
return true; |
| 151 |
} |
| 152 |
return false; |
| 153 |
}, this); |
| 154 |
if(flag){ |
| 155 |
this._pushDataChanges(); |
| 156 |
} |
| 157 |
} |
| 158 |
}, |
| 159 |
|
| 160 |
_onStoreSet: function(item){ |
| 161 |
if(this.itemMap){ |
| 162 |
|
| 163 |
var id = this.store.getIdentity(item), index = this.itemMap[id]; |
| 164 |
if(typeof index == "number"){ |
| 165 |
this.data[index] = this.value(this.store, this.items[index]); |
| 166 |
this._pushDataChanges(); |
| 167 |
} |
| 168 |
}else{ |
| 169 |
|
| 170 |
if(this.items){ |
| 171 |
var flag = ArrayUtil.some(this.items, function(it, index){ |
| 172 |
if(it === item){ |
| 173 |
this.data[index] = this.value(this.store, it); |
| 174 |
return true; |
| 175 |
} |
| 176 |
return false; |
| 177 |
}, this); |
| 178 |
if(flag){ |
| 179 |
this._pushDataChanges(); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
}); |
| 185 |
}); |