The "to hash" textbox now accepts changes in the palette.
[dygraphs.git] / experimental / palette / palette.js
index 5a2891c..514463f 100644 (file)
  */
 "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.innerText = type; // innerText for arrays.
-
-  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 = $("<div>")
+      .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.type = "search";
-  this.filterBar.onkeyup = function() {
-    palette.filter(palette.filterBar.value)
-  };
-  this.filterBar.onclick = this.filterBar.onkeyup;
-  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 = $("<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}";
                  }
-                 if (value != inputValue) {
-                   entry.functionString = value;
-                   entry.input.innerText = value ? "defined" : "not defined";
-                   palette.onchange();
-                 }
-               }
-             }
-           }(opt, this);
-        } else {
-          var input = Palette.createChild("input", value);
-          input.onkeypress = function(event) {
-            var keycode = event.which;
-            if (keycode == 13 || keycode == 8) {
+                 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();
-            }
-          }
-
-          input.type="text";
-        }
-        this.model[opt] = { input: input, row: row };
+            });
+      } else {
+        input = $("<input>", { type: "text" })
+            .addClass("textInput")
+            .keypress(function(event) {
+              var keycode = event.which;
+              if (keycode == 13 || keycode == 8) {
+                palette.onchange();
+              }
+            });
       }
+
+      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))
+        .mouseout(function() { palette.tooltip.hide(); })
+
+      row.appendTo(table);
+
+      palette.model[opt] = { input: input, row: row };
     } catch(err) {
       throw "For option " + opt + ":" + err;
     }
-  }
+  });
+
   this.filter("");
 }
 
@@ -183,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() {
@@ -206,11 +173,17 @@ 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") {
-          results[opt] = value == "true";
+          if (value == "false") {
+            results[opt] = false;
+          }
+          if (value == "true") {
+            results[opt] = true;
+          }
+          // Ignore value == "none"
         } else if (type == "int") {
           results[opt] = parseInt(value);
         } else if (type == "float") {
@@ -221,6 +194,8 @@ Palette.prototype.read = function() {
           results[opt] = Palette.parseFloatArray(value);
         } else if (type == "array<boolean>") {
           results[opt] = Palette.parseBooleanArray(value);
+        } else if (type == "array<int>") {
+          results[opt] = Palette.parseIntArray(value);
         } else if (type == "array<Date>") {
           results[opt] = Palette.parseIntArray(value);
         } else if (isFunction) {
@@ -240,14 +215,19 @@ Palette.prototype.read = function() {
  * Write to input elements.
  */
 Palette.prototype.write = function(hash) {
+  if (!hash) {
+    return;
+  }
   var results = {};
   for (var opt in this.model) {
-    //  && hash.hasOwnProperty(opt)
     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 == "array<string>") {
+      if (type == "boolean") {
+        var text = value == true ? "true" : (value == false ? "false" : "none");
+        Palette.populateBooleanButton(input, text);
+      } else if (type == "array<string>") {
         if (value) {
           input.value = value.join("; ");
         }
@@ -256,10 +236,10 @@ Palette.prototype.write = function(hash) {
           input.value = value.join(", ");
         }
       } else if (type.indexOf("function(") == 0) {
-        input.innerText = value ? "defined" : "not defined";
+        input.textContent = value ? "defined" : "not defined";
         this.model[opt].functionString = value ? value.toString() : null;
       } else {
-        if (value) {
+        if (value != undefined) {
           input.value = value;
         }
       }
@@ -267,6 +247,11 @@ Palette.prototype.write = function(hash) {
   }
 }
 
+Palette.populateBooleanButton = function(button, value) {
+  button.innerHTML = value;
+  button.value = value;
+}
+
 Palette.prototype.filter = function(pattern) {
   pattern = pattern.toLowerCase();
   var even = true;
@@ -274,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;
       }
     }