| 1 |
Rickshaw.namespace('Rickshaw.Graph.RangeSlider.Preview'); |
| 2 |
|
| 3 |
Rickshaw.Graph.RangeSlider.Preview = Rickshaw.Class.create({ |
| 4 |
|
| 5 |
initialize: function(args) { |
| 6 |
|
| 7 |
if (!args.element) throw "Rickshaw.Graph.RangeSlider.Preview needs a reference to an element"; |
| 8 |
if (!args.graph && !args.graphs) throw "Rickshaw.Graph.RangeSlider.Preview needs a reference to an graph or an array of graphs"; |
| 9 |
|
| 10 |
this.element = args.element; |
| 11 |
this.element.style.position = 'relative'; |
| 12 |
|
| 13 |
this.graphs = args.graph ? [ args.graph ] : args.graphs; |
| 14 |
|
| 15 |
this.defaults = { |
| 16 |
height: 75, |
| 17 |
width: 400, |
| 18 |
gripperColor: undefined, |
| 19 |
frameTopThickness: 3, |
| 20 |
frameHandleThickness: 10, |
| 21 |
frameColor: "#d4d4d4", |
| 22 |
frameOpacity: 1, |
| 23 |
minimumFrameWidth: 0, |
| 24 |
heightRatio: 0.2 |
| 25 |
}; |
| 26 |
|
| 27 |
this.heightRatio = args.heightRatio || this.defaults.heightRatio; |
| 28 |
this.defaults.gripperColor = d3.rgb(this.defaults.frameColor).darker().toString(); |
| 29 |
|
| 30 |
this.configureCallbacks = []; |
| 31 |
this.slideCallbacks = []; |
| 32 |
|
| 33 |
this.previews = []; |
| 34 |
|
| 35 |
if (!args.width) this.widthFromGraph = true; |
| 36 |
if (!args.height) this.heightFromGraph = true; |
| 37 |
|
| 38 |
if (this.widthFromGraph || this.heightFromGraph) { |
| 39 |
this.graphs[0].onConfigure(function () { |
| 40 |
this.configure(args); this.render(); |
| 41 |
}.bind(this)); |
| 42 |
} |
| 43 |
|
| 44 |
args.width = args.width || this.graphs[0].width || this.defaults.width; |
| 45 |
args.height = args.height || this.graphs[0].height * this.heightRatio || this.defaults.height; |
| 46 |
|
| 47 |
this.configure(args); |
| 48 |
this.render(); |
| 49 |
}, |
| 50 |
|
| 51 |
onSlide: function(callback) { |
| 52 |
this.slideCallbacks.push(callback); |
| 53 |
}, |
| 54 |
|
| 55 |
onConfigure: function(callback) { |
| 56 |
this.configureCallbacks.push(callback); |
| 57 |
}, |
| 58 |
|
| 59 |
configure: function(args) { |
| 60 |
|
| 61 |
this.config = this.config || {}; |
| 62 |
|
| 63 |
this.configureCallbacks.forEach(function(callback) { |
| 64 |
callback(args); |
| 65 |
}); |
| 66 |
|
| 67 |
Rickshaw.keys(this.defaults).forEach(function(k) { |
| 68 |
this.config[k] = k in args ? args[k] |
| 69 |
: k in this.config ? this.config[k] |
| 70 |
: this.defaults[k]; |
| 71 |
}, this); |
| 72 |
|
| 73 |
if ('width' in args || 'height' in args) { |
| 74 |
|
| 75 |
if (this.widthFromGraph) { |
| 76 |
this.config.width = this.graphs[0].width; |
| 77 |
} |
| 78 |
|
| 79 |
if (this.heightFromGraph) { |
| 80 |
this.config.height = this.graphs[0].height * this.heightRatio; |
| 81 |
this.previewHeight = this.config.height; |
| 82 |
} |
| 83 |
|
| 84 |
this.previews.forEach(function(preview) { |
| 85 |
|
| 86 |
var height = this.previewHeight / this.graphs.length - this.config.frameTopThickness * 2; |
| 87 |
var width = this.config.width - this.config.frameHandleThickness * 2; |
| 88 |
preview.setSize({ width: width, height: height }); |
| 89 |
|
| 90 |
if (this.svg) { |
| 91 |
var svgHeight = height + this.config.frameHandleThickness * 2; |
| 92 |
var svgWidth = width + this.config.frameHandleThickness * 2; |
| 93 |
this.svg.style("width", svgWidth + "px"); |
| 94 |
this.svg.style("height", svgHeight + "px"); |
| 95 |
} |
| 96 |
}, this); |
| 97 |
} |
| 98 |
}, |
| 99 |
|
| 100 |
render: function() { |
| 101 |
|
| 102 |
var self = this; |
| 103 |
|
| 104 |
this.svg = d3.select(this.element) |
| 105 |
.selectAll("svg.rickshaw_range_slider_preview") |
| 106 |
.data([null]); |
| 107 |
|
| 108 |
this.previewHeight = this.config.height - (this.config.frameTopThickness * 2); |
| 109 |
this.previewWidth = this.config.width - (this.config.frameHandleThickness * 2); |
| 110 |
|
| 111 |
this.currentFrame = [0, this.previewWidth]; |
| 112 |
|
| 113 |
var buildGraph = function(parent, index) { |
| 114 |
|
| 115 |
var graphArgs = Rickshaw.extend({}, parent.config); |
| 116 |
var height = self.previewHeight / self.graphs.length; |
| 117 |
var renderer = parent.renderer.name; |
| 118 |
|
| 119 |
Rickshaw.extend(graphArgs, { |
| 120 |
element: this.appendChild(document.createElement("div")), |
| 121 |
height: height, |
| 122 |
width: self.previewWidth, |
| 123 |
series: parent.series, |
| 124 |
renderer: renderer |
| 125 |
}); |
| 126 |
|
| 127 |
var graph = new Rickshaw.Graph(graphArgs); |
| 128 |
self.previews.push(graph); |
| 129 |
|
| 130 |
parent.onUpdate(function() { graph.render(); self.render() }); |
| 131 |
|
| 132 |
parent.onConfigure(function(args) { |
| 133 |
// don't propagate height |
| 134 |
delete args.height; |
| 135 |
args.width = args.width - self.config.frameHandleThickness * 2; |
| 136 |
graph.configure(args); |
| 137 |
graph.render(); |
| 138 |
}); |
| 139 |
|
| 140 |
graph.render(); |
| 141 |
}; |
| 142 |
|
| 143 |
var graphContainer = d3.select(this.element) |
| 144 |
.selectAll("div.rickshaw_range_slider_preview_container") |
| 145 |
.data(this.graphs); |
| 146 |
|
| 147 |
var translateCommand = "translate(" + |
| 148 |
this.config.frameHandleThickness + "px, " + |
| 149 |
this.config.frameTopThickness + "px)"; |
| 150 |
|
| 151 |
graphContainer.enter() |
| 152 |
.append("div") |
| 153 |
.classed("rickshaw_range_slider_preview_container", true) |
| 154 |
.style("-webkit-transform", translateCommand) |
| 155 |
.style("-moz-transform", translateCommand) |
| 156 |
.style("-ms-transform", translateCommand) |
| 157 |
.style("transform", translateCommand) |
| 158 |
.each(buildGraph); |
| 159 |
|
| 160 |
graphContainer.exit() |
| 161 |
.remove(); |
| 162 |
|
| 163 |
// Use the first graph as the "master" for the frame state |
| 164 |
var masterGraph = this.graphs[0]; |
| 165 |
|
| 166 |
var domainScale = d3.scale.linear() |
| 167 |
.domain([0, this.previewWidth]) |
| 168 |
.range(masterGraph.dataDomain()); |
| 169 |
|
| 170 |
var currentWindow = [masterGraph.window.xMin, masterGraph.window.xMax]; |
| 171 |
|
| 172 |
this.currentFrame[0] = currentWindow[0] === undefined ? |
| 173 |
0 : Math.round(domainScale.invert(currentWindow[0])); |
| 174 |
|
| 175 |
if (this.currentFrame[0] < 0) this.currentFrame[0] = 0; |
| 176 |
|
| 177 |
this.currentFrame[1] = currentWindow[1] === undefined ? |
| 178 |
this.previewWidth : domainScale.invert(currentWindow[1]); |
| 179 |
|
| 180 |
if (this.currentFrame[1] - this.currentFrame[0] < self.config.minimumFrameWidth) { |
| 181 |
this.currentFrame[1] = (this.currentFrame[0] || 0) + self.config.minimumFrameWidth; |
| 182 |
} |
| 183 |
|
| 184 |
this.svg.enter() |
| 185 |
.append("svg") |
| 186 |
.classed("rickshaw_range_slider_preview", true) |
| 187 |
.style("height", this.config.height + "px") |
| 188 |
.style("width", this.config.width + "px") |
| 189 |
.style("position", "absolute") |
| 190 |
.style("top", 0); |
| 191 |
|
| 192 |
this._renderDimming(); |
| 193 |
this._renderFrame(); |
| 194 |
this._renderGrippers(); |
| 195 |
this._renderHandles(); |
| 196 |
this._renderMiddle(); |
| 197 |
|
| 198 |
this._registerMouseEvents(); |
| 199 |
}, |
| 200 |
|
| 201 |
_renderDimming: function() { |
| 202 |
|
| 203 |
var element = this.svg |
| 204 |
.selectAll("path.dimming") |
| 205 |
.data([null]); |
| 206 |
|
| 207 |
element.enter() |
| 208 |
.append("path") |
| 209 |
.attr("fill", "white") |
| 210 |
.attr("fill-opacity", "0.7") |
| 211 |
.attr("fill-rule", "evenodd") |
| 212 |
.classed("dimming", true); |
| 213 |
|
| 214 |
var path = ""; |
| 215 |
path += " M " + this.config.frameHandleThickness + " " + this.config.frameTopThickness; |
| 216 |
path += " h " + this.previewWidth; |
| 217 |
path += " v " + this.previewHeight; |
| 218 |
path += " h " + -this.previewWidth; |
| 219 |
path += " z "; |
| 220 |
path += " M " + Math.max(this.currentFrame[0], this.config.frameHandleThickness) + " " + this.config.frameTopThickness; |
| 221 |
path += " H " + Math.min(this.currentFrame[1] + this.config.frameHandleThickness * 2, this.previewWidth + this.config.frameHandleThickness); |
| 222 |
path += " v " + this.previewHeight; |
| 223 |
path += " H " + Math.max(this.currentFrame[0], this.config.frameHandleThickness); |
| 224 |
path += " z"; |
| 225 |
|
| 226 |
element.attr("d", path); |
| 227 |
}, |
| 228 |
|
| 229 |
_renderFrame: function() { |
| 230 |
|
| 231 |
var element = this.svg |
| 232 |
.selectAll("path.frame") |
| 233 |
.data([null]); |
| 234 |
|
| 235 |
element.enter() |
| 236 |
.append("path") |
| 237 |
.attr("stroke", "white") |
| 238 |
.attr("stroke-width", "1px") |
| 239 |
.attr("stroke-linejoin", "round") |
| 240 |
.attr("fill", this.config.frameColor) |
| 241 |
.attr("fill-opacity", this.config.frameOpacity) |
| 242 |
.attr("fill-rule", "evenodd") |
| 243 |
.classed("frame", true); |
| 244 |
|
| 245 |
var path = ""; |
| 246 |
path += " M " + this.currentFrame[0] + " 0"; |
| 247 |
path += " H " + (this.currentFrame[1] + (this.config.frameHandleThickness * 2)); |
| 248 |
path += " V " + this.config.height; |
| 249 |
path += " H " + (this.currentFrame[0]); |
| 250 |
path += " z"; |
| 251 |
path += " M " + (this.currentFrame[0] + this.config.frameHandleThickness) + " " + this.config.frameTopThickness; |
| 252 |
path += " H " + (this.currentFrame[1] + this.config.frameHandleThickness); |
| 253 |
path += " v " + this.previewHeight; |
| 254 |
path += " H " + (this.currentFrame[0] + this.config.frameHandleThickness); |
| 255 |
path += " z"; |
| 256 |
|
| 257 |
element.attr("d", path); |
| 258 |
}, |
| 259 |
|
| 260 |
_renderGrippers: function() { |
| 261 |
|
| 262 |
var gripper = this.svg.selectAll("path.gripper") |
| 263 |
.data([null]); |
| 264 |
|
| 265 |
gripper.enter() |
| 266 |
.append("path") |
| 267 |
.attr("stroke", this.config.gripperColor) |
| 268 |
.classed("gripper", true); |
| 269 |
|
| 270 |
var path = ""; |
| 271 |
|
| 272 |
[0.4, 0.6].forEach(function(spacing) { |
| 273 |
path += " M " + Math.round((this.currentFrame[0] + (this.config.frameHandleThickness * spacing))) + " " + Math.round(this.config.height * 0.3); |
| 274 |
path += " V " + Math.round(this.config.height * 0.7); |
| 275 |
path += " M " + Math.round((this.currentFrame[1] + (this.config.frameHandleThickness * (1 + spacing)))) + " " + Math.round(this.config.height * 0.3); |
| 276 |
path += " V " + Math.round(this.config.height * 0.7); |
| 277 |
}.bind(this)); |
| 278 |
|
| 279 |
gripper.attr("d", path); |
| 280 |
}, |
| 281 |
|
| 282 |
_renderHandles: function() { |
| 283 |
|
| 284 |
var leftHandle = this.svg.selectAll("rect.left_handle") |
| 285 |
.data([null]); |
| 286 |
|
| 287 |
leftHandle.enter() |
| 288 |
.append("rect") |
| 289 |
.attr('width', this.config.frameHandleThickness) |
| 290 |
.style("cursor", "ew-resize") |
| 291 |
.style("fill-opacity", "0") |
| 292 |
.classed("left_handle", true); |
| 293 |
|
| 294 |
leftHandle |
| 295 |
.attr('x', this.currentFrame[0]) |
| 296 |
.attr('height', this.config.height); |
| 297 |
|
| 298 |
var rightHandle = this.svg.selectAll("rect.right_handle") |
| 299 |
.data([null]); |
| 300 |
|
| 301 |
rightHandle.enter() |
| 302 |
.append("rect") |
| 303 |
.attr('width', this.config.frameHandleThickness) |
| 304 |
.style("cursor", "ew-resize") |
| 305 |
.style("fill-opacity", "0") |
| 306 |
.classed("right_handle", true); |
| 307 |
|
| 308 |
rightHandle |
| 309 |
.attr('x', this.currentFrame[1] + this.config.frameHandleThickness) |
| 310 |
.attr('height', this.config.height); |
| 311 |
}, |
| 312 |
|
| 313 |
_renderMiddle: function() { |
| 314 |
|
| 315 |
var middleHandle = this.svg.selectAll("rect.middle_handle") |
| 316 |
.data([null]); |
| 317 |
|
| 318 |
middleHandle.enter() |
| 319 |
.append("rect") |
| 320 |
.style("cursor", "move") |
| 321 |
.style("fill-opacity", "0") |
| 322 |
.classed("middle_handle", true); |
| 323 |
|
| 324 |
middleHandle |
| 325 |
.attr('width', Math.max(0, this.currentFrame[1] - this.currentFrame[0])) |
| 326 |
.attr('x', this.currentFrame[0] + this.config.frameHandleThickness) |
| 327 |
.attr('height', this.config.height); |
| 328 |
}, |
| 329 |
|
| 330 |
_registerMouseEvents: function() { |
| 331 |
|
| 332 |
var element = d3.select(this.element); |
| 333 |
|
| 334 |
var drag = { |
| 335 |
target: null, |
| 336 |
start: null, |
| 337 |
stop: null, |
| 338 |
left: false, |
| 339 |
right: false, |
| 340 |
rigid: false |
| 341 |
}; |
| 342 |
|
| 343 |
var self = this; |
| 344 |
|
| 345 |
function onMousemove(datum, index) { |
| 346 |
|
| 347 |
drag.stop = self._getClientXFromEvent(d3.event, drag); |
| 348 |
var distanceTraveled = drag.stop - drag.start; |
| 349 |
var frameAfterDrag = self.frameBeforeDrag.slice(0); |
| 350 |
var minimumFrameWidth = self.config.minimumFrameWidth; |
| 351 |
|
| 352 |
if (drag.rigid) { |
| 353 |
minimumFrameWidth = self.frameBeforeDrag[1] - self.frameBeforeDrag[0]; |
| 354 |
} |
| 355 |
if (drag.left) { |
| 356 |
frameAfterDrag[0] = Math.max(frameAfterDrag[0] + distanceTraveled, 0); |
| 357 |
} |
| 358 |
if (drag.right) { |
| 359 |
frameAfterDrag[1] = Math.min(frameAfterDrag[1] + distanceTraveled, self.previewWidth); |
| 360 |
} |
| 361 |
|
| 362 |
var currentFrameWidth = frameAfterDrag[1] - frameAfterDrag[0]; |
| 363 |
|
| 364 |
if (currentFrameWidth <= minimumFrameWidth) { |
| 365 |
|
| 366 |
if (drag.left) { |
| 367 |
frameAfterDrag[0] = frameAfterDrag[1] - minimumFrameWidth; |
| 368 |
} |
| 369 |
if (drag.right) { |
| 370 |
frameAfterDrag[1] = frameAfterDrag[0] + minimumFrameWidth; |
| 371 |
} |
| 372 |
if (frameAfterDrag[0] <= 0) { |
| 373 |
frameAfterDrag[1] -= frameAfterDrag[0]; |
| 374 |
frameAfterDrag[0] = 0; |
| 375 |
} |
| 376 |
if (frameAfterDrag[1] >= self.previewWidth) { |
| 377 |
frameAfterDrag[0] -= (frameAfterDrag[1] - self.previewWidth); |
| 378 |
frameAfterDrag[1] = self.previewWidth; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
self.graphs.forEach(function(graph) { |
| 383 |
|
| 384 |
var domainScale = d3.scale.linear() |
| 385 |
.interpolate(d3.interpolateNumber) |
| 386 |
.domain([0, self.previewWidth]) |
| 387 |
.range(graph.dataDomain()); |
| 388 |
|
| 389 |
var windowAfterDrag = [ |
| 390 |
domainScale(frameAfterDrag[0]), |
| 391 |
domainScale(frameAfterDrag[1]) |
| 392 |
]; |
| 393 |
|
| 394 |
self.slideCallbacks.forEach(function(callback) { |
| 395 |
callback(graph, windowAfterDrag[0], windowAfterDrag[1]); |
| 396 |
}); |
| 397 |
|
| 398 |
if (frameAfterDrag[0] === 0) { |
| 399 |
windowAfterDrag[0] = undefined; |
| 400 |
} |
| 401 |
if (frameAfterDrag[1] === self.previewWidth) { |
| 402 |
windowAfterDrag[1] = undefined; |
| 403 |
} |
| 404 |
graph.window.xMin = windowAfterDrag[0]; |
| 405 |
graph.window.xMax = windowAfterDrag[1]; |
| 406 |
|
| 407 |
graph.update(); |
| 408 |
}); |
| 409 |
} |
| 410 |
|
| 411 |
function onMousedown() { |
| 412 |
drag.target = d3.event.target; |
| 413 |
drag.start = self._getClientXFromEvent(d3.event, drag); |
| 414 |
self.frameBeforeDrag = self.currentFrame.slice(); |
| 415 |
d3.event.preventDefault ? d3.event.preventDefault() : d3.event.returnValue = false; |
| 416 |
d3.select(document).on("mousemove.rickshaw_range_slider_preview", onMousemove); |
| 417 |
d3.select(document).on("mouseup.rickshaw_range_slider_preview", onMouseup); |
| 418 |
d3.select(document).on("touchmove.rickshaw_range_slider_preview", onMousemove); |
| 419 |
d3.select(document).on("touchend.rickshaw_range_slider_preview", onMouseup); |
| 420 |
d3.select(document).on("touchcancel.rickshaw_range_slider_preview", onMouseup); |
| 421 |
} |
| 422 |
|
| 423 |
function onMousedownLeftHandle(datum, index) { |
| 424 |
drag.left = true; |
| 425 |
onMousedown(); |
| 426 |
} |
| 427 |
|
| 428 |
function onMousedownRightHandle(datum, index) { |
| 429 |
drag.right = true; |
| 430 |
onMousedown(); |
| 431 |
} |
| 432 |
|
| 433 |
function onMousedownMiddleHandle(datum, index) { |
| 434 |
drag.left = true; |
| 435 |
drag.right = true; |
| 436 |
drag.rigid = true; |
| 437 |
onMousedown(); |
| 438 |
} |
| 439 |
|
| 440 |
function onMouseup(datum, index) { |
| 441 |
d3.select(document).on("mousemove.rickshaw_range_slider_preview", null); |
| 442 |
d3.select(document).on("mouseup.rickshaw_range_slider_preview", null); |
| 443 |
d3.select(document).on("touchmove.rickshaw_range_slider_preview", null); |
| 444 |
d3.select(document).on("touchend.rickshaw_range_slider_preview", null); |
| 445 |
d3.select(document).on("touchcancel.rickshaw_range_slider_preview", null); |
| 446 |
delete self.frameBeforeDrag; |
| 447 |
drag.left = false; |
| 448 |
drag.right = false; |
| 449 |
drag.rigid = false; |
| 450 |
} |
| 451 |
|
| 452 |
element.select("rect.left_handle").on("mousedown", onMousedownLeftHandle); |
| 453 |
element.select("rect.right_handle").on("mousedown", onMousedownRightHandle); |
| 454 |
element.select("rect.middle_handle").on("mousedown", onMousedownMiddleHandle); |
| 455 |
element.select("rect.left_handle").on("touchstart", onMousedownLeftHandle); |
| 456 |
element.select("rect.right_handle").on("touchstart", onMousedownRightHandle); |
| 457 |
element.select("rect.middle_handle").on("touchstart", onMousedownMiddleHandle); |
| 458 |
}, |
| 459 |
|
| 460 |
_getClientXFromEvent: function(event, drag) { |
| 461 |
|
| 462 |
switch (event.type) { |
| 463 |
case 'touchstart': |
| 464 |
case 'touchmove': |
| 465 |
var touchList = event.changedTouches; |
| 466 |
var touch = null; |
| 467 |
for (var touchIndex = 0; touchIndex < touchList.length; touchIndex++) { |
| 468 |
if (touchList[touchIndex].target === drag.target) { |
| 469 |
touch = touchList[touchIndex]; |
| 470 |
break; |
| 471 |
} |
| 472 |
} |
| 473 |
return touch !== null ? touch.clientX : undefined; |
| 474 |
|
| 475 |
default: |
| 476 |
return event.clientX; |
| 477 |
} |
| 478 |
} |
| 479 |
}); |
| 480 |
|