The "to hash" textbox now accepts changes in the palette.
[dygraphs.git] / experimental / palette / palette.js
index 65ef612..514463f 100644 (file)
@@ -38,103 +38,96 @@ function Palette(scope) {
   this.root = null;
 }
 
-Palette.createChild = function(type, parentElement, className) {
-  var element = document.createElement(type);
-  parentElement.appendChild(element);
-  if (className) {
-    element.className = className;
-  }
-  return element;
-};
-
 Palette.prototype.create = function(parentElement) {
   var palette = this;
 
-  var table = Palette.createChild("div", parentElement[0], "palette");
-  this.root = table;
-  table.width="300px";
+  var table = $("<div>")
+      .addClass("palette")
+      .width(300)
+      .appendTo(parentElement);
 
+  this.root = table;
   this.tooltip = new Tooltip();
 
-  // Build the header
-  var header = Palette.createChild("div", table, "header");
-  header.style.visibility = "visible";
-
   // One row per option.
-  for (var opt in opts) {
+  $.each(opts, function(opt, optEntry) {
     try {
-      if (opts.hasOwnProperty(opt)) {
-        var type = opts[opt].type;
+      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 scope = opts[opt].scope || [ "global" ]; // Scope can be empty, infer "global" only.
-        var valid = scope[0] == "*" || $.inArray(this.scope, scope) >= 0;
-        if (!valid) {
-          continue;
-        }
+      var type = optEntry.type;
+      var isFunction = type.indexOf("function(") == 0;
+
+      var input;
+      if (isFunction) {
+        input = $("<button>")
+            .click(function(opt, palette) {
+               return function(event) {
+                 var entry = palette.model[opt];
+                 var inputValue = entry.functionString;
+                 var type = opts[opt].type;
+                 if (inputValue == null || inputValue.length == 0) {
+                   inputValue = type + "{\n\n}";
+                 }
+                 var textarea = new TextArea();
+                 textarea.show(opt, inputValue);
+                 textarea.okCallback = function(value) {
+                   if (value != inputValue) {
+                     entry.functionString = value;
+                     entry.input.textContent = value ? "defined" : "not defined";
+                     palette.onchange();
+                   }
+                 };
+               };
+             } (opt, palette) // Instantiating this inner function.
+           );
+      } else if (type == "boolean") {
+        input = $("<button>")
+            .click(function(event) {
+              var btn = event.target;
+              if (btn.value == "none") {
+                Palette.populateBooleanButton(btn, "true");
+              } else if (btn.value == "true") {
+                Palette.populateBooleanButton(btn, "false");
+              } else {
+                Palette.populateBooleanButton(btn, "none");
+              }
+              palette.onchange();
+            });
+      } else {
+        input = $("<input>", { type: "text" })
+            .addClass("textInput")
+            .keypress(function(event) {
+              var keycode = event.which;
+              if (keycode == 13 || keycode == 8) {
+                palette.onchange();
+              }
+            });
+      }
 
-        var isFunction = type.indexOf("function(") == 0;
-        var row = Palette.createChild("div", table);
-        row.onmouseover = function(source, title, type, body) {
+      var row = $("<div>")
+          .append($("<span>").addClass("name").text(opt))
+          .append($("<span>").addClass("option")
+              .append(input));
+
+      row.mouseover(function(source, title, type, body) {
           return function() {
             palette.tooltip.show(source, title, type, body);
           };
-        } (row, opt, type, Dygraph.OPTIONS_REFERENCE[opt].description);
-        row.onmouseout = function() { palette.tooltip.hide(); };
-
-        var div = Palette.createChild("span", row, "name");
-        div.textContent = opt;
+        } (row, opt, type, Dygraph.OPTIONS_REFERENCE[opt].description))
+        .mouseout(function() { palette.tooltip.hide(); })
 
-        var value = Palette.createChild("span", row, "option");
+      row.appendTo(table);
 
-        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 + "{\n\n}";
-               }
-                    var textarea = new TextArea();
-                    textarea.show(opt, inputValue);
-                    textarea.okCallback = function(value) {
-                 if (value != inputValue) {
-                   entry.functionString = value;
-                   entry.input.textContent = value ? "defined" : "not defined";
-                   palette.onchange();
-                 }
-               }
-             }
-           }(opt, this);
-        } else if (type == "boolean") {
-           var input = Palette.createChild("button", value);
-           input.onclick = function(e) {
-             var btn = e.target;
-             if (btn.value == "none") {
-               Palette.populateBooleanButton(btn, "true");
-             } else if (btn.value == "true") {
-               Palette.populateBooleanButton(btn, "false");
-             } else {
-               Palette.populateBooleanButton(btn, "none");
-             }
-             palette.onchange();
-           };
-        } else {
-          var input = Palette.createChild("input", value, "textInput");
-          input.type="text";
-          input.onkeypress = function(event) {
-            var keycode = event.which;
-            if (keycode == 13 || keycode == 8) {
-              palette.onchange();
-            }
-          }
-        }
-        this.model[opt] = { input: input, row: row };
-      }
+      palette.model[opt] = { input: input, row: row };
     } catch(err) {
       throw "For option " + opt + ":" + err;
     }
-  }
+  });
+
   this.filter("");
 }
 
@@ -150,21 +143,28 @@ Palette.parseBooleanArray = function(value) {
   if (value == null || value.length == 0) {
     return null;
   }
-  return value.split(',').map(function(x) { return x.trim() == "true"; });
+  return value.split(',').map(function(x) {
+    return x.trim() == "true";
+  });
 }
 
 Palette.parseFloatArray = function(value) {
   if (value == null || value.length == 0) {
     return null;
   }
-  return value.split(',').map(function(x) { return parseFloat(x); });
+  return value.split(',').map(function(x) {
+    return parseFloat(x);
+  });
 }
 
 Palette.parseIntArray = function(value) {
   if (value == null || value.length == 0) {
     return null;
   }
-  return value.split(',').map(function(x) { return parseInt(x); });
+
+  return value.split(',').map(function(x) {
+    return parseInt(x);
+  });
 }
 
 Palette.prototype.read = function() {
@@ -173,7 +173,7 @@ Palette.prototype.read = function() {
     if (this.model.hasOwnProperty(opt)) {
       var type = opts[opt].type;
       var isFunction = type.indexOf("function(") == 0;
-      var input = this.model[opt].input;
+      var input = this.model[opt].input[0]; // jquery dereference.
       var value = isFunction ? this.model[opt].functionString : input.value;
       if (value && value.length != 0) {
         if (type == "boolean") {
@@ -215,10 +215,13 @@ Palette.prototype.read = function() {
  * Write to input elements.
  */
 Palette.prototype.write = function(hash) {
+  if (!hash) {
+    return;
+  }
   var results = {};
   for (var opt in this.model) {
     if (this.model.hasOwnProperty(opt)) {
-      var input = this.model[opt].input;
+      var input = this.model[opt].input[0]; // jquery dereference
       var type = opts[opt].type;
       var value = hash[opt];
       if (type == "boolean") {
@@ -256,9 +259,9 @@ Palette.prototype.filter = function(pattern) {
     if (this.model.hasOwnProperty(opt)) {
       var row = this.model[opt].row;
       var matches = opt.toLowerCase().indexOf(pattern) >= 0;
-      row.style.display = matches ? "block" : "none";
+      row.toggle(matches);
       if (matches) {
-        row.className = even ? "even" : "odd";
+        row.attr("class", even ? "even" : "odd");
         even = !even;
       }
     }