X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=experimental%2Fpalette%2Fpalette.js;h=514463f554c45d374cb8ec6497c0b672c9133a12;hb=68c27ff931144658a04facb8be769e736b0db9d6;hp=cb6a03fa18cbebe6f32fe46a8094ddef27acc4ca;hpb=59a80f4ab1a7a79d00dbd8ff0ca12cdd275a8702;p=dygraphs.git diff --git a/experimental/palette/palette.js b/experimental/palette/palette.js index cb6a03f..514463f 100644 --- a/experimental/palette/palette.js +++ b/experimental/palette/palette.js @@ -25,147 +25,109 @@ */ "use strict"; -function Palette() { +/** + * scope is either "global", "series", "x", "y" or "y2". + */ +function Palette(scope) { + // Contains pair of "input" (the input object) and "row" (the parent row) + // Also contains functionString. this.model = {}; + // This is meant to be overridden by a palette host. this.onchange = function() {}; - this.filterBar = null; -} - -Palette.createChild = function(type, parentElement) { - var element = document.createElement(type); - parentElement.appendChild(element); - return element; -}; - -function Tooltip(parent) { - if (!parent) { - parent = document.getElementsByTagName("body")[0]; - } - this.elem = Palette.createChild("div", parent); - this.title = Palette.createChild("div", this.elem); - this.elem.className = "tooltip"; - this.title.className = "title"; - this.type = Palette.createChild("div", this.elem); - this.type.className = "type"; - this.body = Palette.createChild("div", this.elem); - this.body.className = "body"; - this.hide(); + this.scope = scope; + this.root = null; } -Tooltip.prototype.show = function(source, event, title, type, body) { - this.title.innerHTML = title; - this.body.innerHTML = body; - this.type.innerHTML = type; - - var getTopLeft = function(element) { - var x = element.offsetLeft; - var y = element.offsetTop; - element = element.offsetParent; - - while(element != null) { - x = parseInt(x) + parseInt(element.offsetLeft); - y = parseInt(y) + parseInt(element.offsetTop); - element = element.offsetParent; - } - return [y, x]; - } - - this.elem.style.height = source.style.height; - this.elem.style.width = "280"; - var topLeft = getTopLeft(source); - this.elem.style.top = parseInt(topLeft[0] + source.offsetHeight) + 'px'; - this.elem.style.left = parseInt(topLeft[1] + 10) + 'px'; - this.elem.style.visibility = "visible"; -} - -Tooltip.prototype.hide = function() { - this.elem.style.visibility = "hidden"; -} - -Palette.prototype.create = function(document, parentElement) { +Palette.prototype.create = function(parentElement) { var palette = this; - var table = Palette.createChild("div", parentElement); - table.className = "palette"; - table.width="300px"; + var table = $("
") + .addClass("palette") + .width(300) + .appendTo(parentElement); + this.root = table; this.tooltip = new Tooltip(); - var row = Palette.createChild("div", table); - row.style.visibility = "visible"; - row.className = "header"; - - Palette.createChild("span", row).innerText = "Filter:"; - this.filterBar = Palette.createChild("input", Palette.createChild("span", row)); - this.filterBar.onkeyup = function() { - palette.filter(palette.filterBar.value) - }; - var go = document.createElement("button"); - Palette.createChild("span", row).appendChild(go); - go.innerText = "Redraw" - go.onclick = function() { - palette.onchange(); - }; - - for (var opt in opts) { + // One row per option. + $.each(opts, function(opt, optEntry) { try { - if (opts.hasOwnProperty(opt)) { - var type = opts[opt].type; - var isFunction = type.indexOf("function(") == 0; - var row = Palette.createChild("div", table); - row.onmouseover = function(source, title, type, body, e) { - return function(e) { - palette.tooltip.show(source, e, title, type, body); - }; - } (row, opt, type, Dygraph.OPTIONS_REFERENCE[opt].description); - row.onmouseout = function() { palette.tooltip.hide(); }; - - var div = Palette.createChild("span", row); - div.innerText = opt; - div.className = "name"; + var scope = optEntry.scope || [ "global" ]; // Scope can be empty, infer "global" only. + var valid = scope[0] == "*" || $.inArray(palette.scope, scope) >= 0; + if (!valid) { + return; + } - var value = Palette.createChild("span", row); - value.className = "option"; + var type = optEntry.type; + var isFunction = type.indexOf("function(") == 0; - if (isFunction) { - var input = Palette.createChild("button", value); - input.onclick = function(opt, palette) { - return function(event) { - var entry = palette.model[opt]; - var inputValue = entry.functionString; - if (inputValue == null || inputValue.length == 0) { - inputValue = opts[opt].type + "{ }"; - } - var value = prompt("enter function", inputValue); - if (value != null) { - if (value.length == 0) { - value = null; + var input; + if (isFunction) { + input = $("