| 1 |
define(["dojo/_base/array", |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/query", |
| 4 |
"dojo/_base/connect", |
| 5 |
"dojo/_base/Color", |
| 6 |
"./Legend", |
| 7 |
"dijit/form/CheckBox", |
| 8 |
"../action2d/Highlight", |
| 9 |
"dojox/lang/functional", |
| 10 |
"dojox/gfx/fx", |
| 11 |
"dojo/keys", |
| 12 |
"dojo/dom-construct", |
| 13 |
"dojo/dom-prop", |
| 14 |
"dijit/registry" |
| 15 |
], function(arrayUtil, declare, query, hub, Color, Legend, CheckBox, Highlight, df, fx, keys, dom, domProp, registry){ |
| 16 |
|
| 17 |
var FocusManager = declare(null, { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
constructor: function(legend){ |
| 24 |
this.legend = legend; |
| 25 |
this.index = 0; |
| 26 |
this.horizontalLength = this._getHrizontalLength(); |
| 27 |
arrayUtil.forEach(legend.legends, function(item, i){ |
| 28 |
if(i > 0){ |
| 29 |
query("input", item).attr("tabindex", -1); |
| 30 |
} |
| 31 |
}); |
| 32 |
this.firstLabel = query("input", legend.legends[0])[0]; |
| 33 |
hub.connect(this.firstLabel, "focus", this, function(){this.legend.active = true;}); |
| 34 |
hub.connect(this.legend.domNode, "keydown", this, "_onKeyEvent"); |
| 35 |
}, |
| 36 |
_getHrizontalLength: function(){ |
| 37 |
var horizontal = this.legend.horizontal; |
| 38 |
if(typeof horizontal == "number"){ |
| 39 |
return Math.min(horizontal, this.legend.legends.length); |
| 40 |
}else if(!horizontal){ |
| 41 |
return 1; |
| 42 |
}else{ |
| 43 |
return this.legend.legends.length; |
| 44 |
} |
| 45 |
}, |
| 46 |
_onKeyEvent: function(e){ |
| 47 |
|
| 48 |
if(!this.legend.active){ |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
if(e.keyCode == keys.TAB){ |
| 53 |
this.legend.active = false; |
| 54 |
return; |
| 55 |
} |
| 56 |
|
| 57 |
var max = this.legend.legends.length; |
| 58 |
switch(e.keyCode){ |
| 59 |
case keys.LEFT_ARROW: |
| 60 |
this.index--; |
| 61 |
if(this.index < 0){ |
| 62 |
this.index += max; |
| 63 |
} |
| 64 |
break; |
| 65 |
case keys.RIGHT_ARROW: |
| 66 |
this.index++; |
| 67 |
if(this.index >= max){ |
| 68 |
this.index -= max; |
| 69 |
} |
| 70 |
break; |
| 71 |
case keys.UP_ARROW: |
| 72 |
if(this.index - this.horizontalLength >= 0){ |
| 73 |
this.index -= this.horizontalLength; |
| 74 |
} |
| 75 |
break; |
| 76 |
case keys.DOWN_ARROW: |
| 77 |
if(this.index + this.horizontalLength < max){ |
| 78 |
this.index += this.horizontalLength; |
| 79 |
} |
| 80 |
break; |
| 81 |
default: |
| 82 |
return; |
| 83 |
} |
| 84 |
this._moveToFocus(); |
| 85 |
Event.stop(e); |
| 86 |
}, |
| 87 |
_moveToFocus: function(){ |
| 88 |
query("input", this.legend.legends[this.index])[0].focus(); |
| 89 |
} |
| 90 |
}); |
| 91 |
|
| 92 |
var FakeHighlight = declare(Highlight, { |
| 93 |
connect: function(){} |
| 94 |
}); |
| 95 |
|
| 96 |
var SelectableLegend = declare("dojox.charting.widget.SelectableLegend", Legend, { |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
outline: false, |
| 102 |
transitionFill: null, |
| 103 |
transitionStroke: null, |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
autoScale: false, |
| 108 |
|
| 109 |
postCreate: function(){ |
| 110 |
this.legends = []; |
| 111 |
this.legendAnim = {}; |
| 112 |
this._cbs = []; |
| 113 |
this.inherited(arguments); |
| 114 |
}, |
| 115 |
refresh: function(){ |
| 116 |
this.legends = []; |
| 117 |
this._clearLabels(); |
| 118 |
this.inherited(arguments); |
| 119 |
this._applyEvents(); |
| 120 |
new FocusManager(this); |
| 121 |
}, |
| 122 |
_clearLabels: function(){ |
| 123 |
var cbs = this._cbs; |
| 124 |
while(cbs.length){ |
| 125 |
cbs.pop().destroyRecursive(); |
| 126 |
} |
| 127 |
}, |
| 128 |
_addLabel: function(dyn, label){ |
| 129 |
this.inherited(arguments); |
| 130 |
|
| 131 |
var legendNodes = query("td", this.legendBody); |
| 132 |
var currentLegendNode = legendNodes[legendNodes.length - 1]; |
| 133 |
this.legends.push(currentLegendNode); |
| 134 |
var checkbox = new CheckBox({checked: true}); |
| 135 |
this._cbs.push(checkbox); |
| 136 |
dom.place(checkbox.domNode, currentLegendNode, "first"); |
| 137 |
|
| 138 |
var clabel = query("label", currentLegendNode)[0]; |
| 139 |
domProp.set(clabel, "for", checkbox.id); |
| 140 |
}, |
| 141 |
_applyEvents: function(){ |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
if(this.chart.dirty){ |
| 148 |
return; |
| 149 |
} |
| 150 |
arrayUtil.forEach(this.legends, function(legend, i){ |
| 151 |
var targetData, plotName, seriesName; |
| 152 |
if(this._isPie()){ |
| 153 |
targetData = this.chart.stack[0]; |
| 154 |
plotName = targetData.name; |
| 155 |
seriesName = this.chart.series[0].name; |
| 156 |
}else{ |
| 157 |
targetData = this.chart.series[i]; |
| 158 |
plotName = targetData.plot; |
| 159 |
seriesName = targetData.name; |
| 160 |
} |
| 161 |
|
| 162 |
var legendCheckBox = registry.byNode(query(".dijitCheckBox", legend)[0]); |
| 163 |
legendCheckBox.set("checked", !this._isHidden(plotName, i)); |
| 164 |
hub.connect(legendCheckBox, "onClick", this, function(e){ |
| 165 |
this.toogle(plotName, i, !legendCheckBox.get("checked")); |
| 166 |
e.stopPropagation(); |
| 167 |
}); |
| 168 |
|
| 169 |
var legendIcon = query(".dojoxLegendIcon", legend)[0], |
| 170 |
iconShape = this._getFilledShape(this._surfaces[i].children); |
| 171 |
arrayUtil.forEach(["onmouseenter", "onmouseleave"], function(event){ |
| 172 |
hub.connect(legendIcon, event, this, function(e){ |
| 173 |
this._highlight(e, iconShape, i, !legendCheckBox.get("checked"), seriesName, plotName); |
| 174 |
}); |
| 175 |
}, this); |
| 176 |
},this); |
| 177 |
}, |
| 178 |
_isHidden: function(plotName, index){ |
| 179 |
if(this._isPie()){ |
| 180 |
return arrayUtil.indexOf(this.chart.getPlot(plotName).runFilter, index) != -1; |
| 181 |
}else{ |
| 182 |
return this.chart.series[index].hidden |
| 183 |
} |
| 184 |
}, |
| 185 |
toogle: function(plotName, index, hide){ |
| 186 |
var plot = this.chart.getPlot(plotName); |
| 187 |
if(this._isPie()){ |
| 188 |
if(arrayUtil.indexOf(plot.runFilter, index) != -1){ |
| 189 |
if(!hide){ |
| 190 |
plot.runFilter = arrayUtil.filter(plot.runFilter, function(item){ |
| 191 |
return item != index; |
| 192 |
}); |
| 193 |
} |
| 194 |
}else{ |
| 195 |
if(hide){ |
| 196 |
plot.runFilter.push(index); |
| 197 |
} |
| 198 |
} |
| 199 |
}else{ |
| 200 |
this.chart.series[index].hidden = hide; |
| 201 |
} |
| 202 |
this.autoScale ? this.chart.dirty = true: plot.dirty = true; |
| 203 |
this.chart.render(); |
| 204 |
}, |
| 205 |
_highlight: function(e, iconShape, index, isOff, seriesName, plotName){ |
| 206 |
if(!isOff){ |
| 207 |
var anim = this._getAnim(plotName), |
| 208 |
isPie = this._isPie(), |
| 209 |
type = formatEventType(e.type); |
| 210 |
|
| 211 |
var label = { |
| 212 |
shape: iconShape, |
| 213 |
index: isPie ? "legend" + index : "legend", |
| 214 |
run: {name: seriesName}, |
| 215 |
type: type |
| 216 |
}; |
| 217 |
anim.process(label); |
| 218 |
|
| 219 |
arrayUtil.forEach(this._getShapes(index, plotName), function(shape, i){ |
| 220 |
var o = { |
| 221 |
shape: shape, |
| 222 |
index: isPie ? index : i, |
| 223 |
run: {name: seriesName}, |
| 224 |
type: type |
| 225 |
}; |
| 226 |
anim.duration = 100; |
| 227 |
anim.process(o); |
| 228 |
}); |
| 229 |
} |
| 230 |
}, |
| 231 |
_getShapes: function(i, plotName){ |
| 232 |
var shapes = []; |
| 233 |
if(this._isPie()){ |
| 234 |
var decrease = 0; |
| 235 |
arrayUtil.forEach(this.chart.getPlot(plotName).runFilter, function(item){ |
| 236 |
if(i > item){ |
| 237 |
decrease++; |
| 238 |
} |
| 239 |
}); |
| 240 |
shapes.push(this.chart.stack[0].group.children[i-decrease]); |
| 241 |
}else if(this._isCandleStick(plotName)){ |
| 242 |
arrayUtil.forEach(this.chart.series[i].group.children, function(group){ |
| 243 |
arrayUtil.forEach(group.children, function(candle){ |
| 244 |
arrayUtil.forEach(candle.children, function(shape){ |
| 245 |
if(shape.shape.type !="line"){ |
| 246 |
shapes.push(shape); |
| 247 |
} |
| 248 |
}); |
| 249 |
}); |
| 250 |
}); |
| 251 |
}else{ |
| 252 |
shapes = this.chart.series[i].group.children; |
| 253 |
} |
| 254 |
return shapes; |
| 255 |
}, |
| 256 |
_getAnim: function(plotName){ |
| 257 |
if(!this.legendAnim[plotName]){ |
| 258 |
this.legendAnim[plotName] = new FakeHighlight(this.chart, plotName); |
| 259 |
} |
| 260 |
return this.legendAnim[plotName]; |
| 261 |
}, |
| 262 |
_getTransitionFill: function(plotName){ |
| 263 |
|
| 264 |
|
| 265 |
if(this.chart.stack[this.chart.plots[plotName]].declaredClass.indexOf("dojox.charting.plot2d.Stacked") != -1){ |
| 266 |
return this.chart.theme.plotarea.fill; |
| 267 |
} |
| 268 |
return null; |
| 269 |
}, |
| 270 |
_getFilledShape: function(shapes){ |
| 271 |
|
| 272 |
|
| 273 |
var i = 0; |
| 274 |
while(shapes[i]){ |
| 275 |
if(shapes[i].getFill())return shapes[i]; |
| 276 |
i++; |
| 277 |
} |
| 278 |
return null; |
| 279 |
}, |
| 280 |
_isPie: function(){ |
| 281 |
return this.chart.stack[0].declaredClass == "dojox.charting.plot2d.Pie"; |
| 282 |
}, |
| 283 |
_isCandleStick: function(plotName){ |
| 284 |
return this.chart.stack[this.chart.plots[plotName]].declaredClass == "dojox.charting.plot2d.Candlesticks"; |
| 285 |
}, |
| 286 |
destroy: function(){ |
| 287 |
this._clearLabels(); |
| 288 |
this.inherited(arguments); |
| 289 |
} |
| 290 |
}); |
| 291 |
|
| 292 |
function formatEventType(type){ |
| 293 |
if(type == "mouseenter")return "onmouseover"; |
| 294 |
if(type == "mouseleave")return "onmouseout"; |
| 295 |
return "on" + type; |
| 296 |
} |
| 297 |
|
| 298 |
return SelectableLegend; |
| 299 |
}); |