From: Robert Konigsberg Date: Fri, 28 Dec 2012 23:11:13 +0000 (-0500) Subject: Add "to hash" feature to make palette more useful! X-Git-Tag: v1.0.0~139^2~10 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=cd1d97d83260cf571584e4d2544f3cc6264b31a2;p=dygraphs.git Add "to hash" feature to make palette more useful! --- diff --git a/experimental/palette/multi-palette.js b/experimental/palette/multi-palette.js index f378d82..4497121 100644 --- a/experimental/palette/multi-palette.js +++ b/experimental/palette/multi-palette.js @@ -55,7 +55,14 @@ MultiPalette.prototype.create = function(parentElement) { selectorRow .append($("").text("Option Set:")) - .append(optionSelector); + .append(optionSelector) + .append($("") + .append($("") + .addClass("link") + .text("to hash") + .css("float", "right") + .css("padding-right", "8px") + .click(function() { self.showHash(); }))); var filter = function() { $.each(self.palettes, function(key, value) { @@ -72,7 +79,6 @@ MultiPalette.prototype.create = function(parentElement) { .append($("").append(this.filterBar)) .append($("") .append($("") - .attr("href", "#") .addClass("link") .text("Redraw") .css("float", "right") @@ -104,6 +110,55 @@ MultiPalette.prototype.activate = function(key) { this.activePalette.root.style.display = "block"; } +MultiPalette.prototype.showHash = function() { + var hash = this.read(); + var textarea = new TextArea(); + textarea.cancel.style.display = "none"; + + /* + * JSON.stringify isn't built to be nice to functions. The following fixes + * this. + * + * First, val.toString only does part of the work, turning it into + * "function () {\n alert(\"p-click!\");\n}", + * + * {start,end}Marker make the surrounding quotes easy to find, and then + * remove them. It also converts the instances of \n and \" so the + * result looks like: + * function () { + * alert("p-click!"); + * }", + */ + var startMarker = "<~%!<"; + var endMarker = ">!%~>"; + var replacer = function(key, val) { + if (typeof val === 'function') { + return startMarker + val.toString() + endMarker; + } + return val; + } + var text = JSON.stringify(hash, replacer, 2); + console.log(text); + while(true) { + var start = text.indexOf(startMarker); + var end = text.indexOf(endMarker); + if (start == -1) { + break; + } + var substring = text.substring(start + startMarker.length, end); + while(substring.indexOf("\\n") >= 0) { + substring = substring.replace("\\n", "\n"); + } + while(substring.indexOf("\\\"") >= 0) { + substring = substring.replace("\\\"", "\""); + } + text = text.substring(0, start - 1) + + substring + + text.substring(end + endMarker.length + 1); + } + textarea.show("options", text); +} + /** * Read from palette */