Calculate query up front, not just on "ready" event.
[mkws-moved-to-github.git] / src / mkws-widget.js
index 6bb9aaa..534e4b3 100644 (file)
@@ -9,7 +9,7 @@ function widget($, team, type, node) {
   var that = {
     team: team,
     type: type,
-    node: node,
+    node: $(node),
     config: mkws.objectInheritingFrom(team.config())
   };
 
@@ -24,7 +24,47 @@ function widget($, team, type, node) {
 
   that.value = function() {
     return node.value;
-  }
+  };
+
+  // Returns the HTML of a subwidget of the specified type. It gets
+  // the same attributes at the parent widget that invokes this
+  // function, except where overrides are passed in. If defaults are
+  // also provided, then these are used when the parent widget
+  // provides no values.
+  that.subwidget = function(type, overrides, defaults) {
+    var attrs = { _team: team.name() };
+    
+    // Copy locally-set properties from the parent widget
+    for (var name in this.config) {
+      if (this.config.hasOwnProperty(name)) {
+        attrs[name] = this.config[name];
+        log(this + " copied property " + name + "='" + attrs[name] + "' to " + type + " subwidget");
+      }
+    }
+    
+    for (var name in overrides) {
+      attrs[name] = overrides[name];
+      log(this + " overrode property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
+    }
+
+    if (defaults) {
+      for (var name in defaults) {
+        if (!attrs[name]) {
+          attrs[name] = defaults[name];
+          log(this + " fell back to default property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
+        }
+      }
+    }
+
+    var s = [];
+    s.push('<div class="mkws', type, ' mkwsTeam_', attrs._team, '"');
+    for (var name in attrs) {    
+      if (name !== '_team')
+        s.push(' ', name, '="', attrs[name], '"');
+    }
+    s.push('></div>');
+    return s.join('');
+  };
 
   for (var i = 0; i < node.attributes.length; i++) {
     var a = node.attributes[i];
@@ -44,10 +84,10 @@ function widget($, team, type, node) {
     } else if (a.name.match (/^data-mkws-/)) {
       var name = a.name.replace(/^data-mkws-/, '')
       that.config[name] = a.value;
-      log(node + ": set data-mkws attribute " + name + "='" + a.value + "'");
+      log(that + ": set data-mkws attribute " + name + "='" + a.value + "'");
     } else if (!ignoreAttrs[a.name]) {
       that.config[a.name] = a.value;
-      log(node + ": set regular attribute " + a.name + "='" + a.value + "'");
+      log(that + ": set regular attribute " + a.name + "='" + a.value + "'");
     }
   }
 
@@ -55,6 +95,8 @@ function widget($, team, type, node) {
   if (fn) {
     fn.call(that);
     log("made " + type + " widget(node=" + node + ")");
+  } else if (type.match(/-Container-(narrow|wide)$/)) {
+    // Not really a widget: no need to log its lack of promotion
   } else {
     log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
   }
@@ -65,33 +107,35 @@ function widget($, team, type, node) {
 
 // Utility function for use by all widgets that can invoke autosearch.
 widget.autosearch = function(widget) {
-  widget.team.queue("ready").subscribe(function() {
-    var query = widget.config.autosearch;
-    if (query) {
-      if (query.match(/^!param!/)) {
-        var param = query.replace(/^!param!/, '');
-        query = mkws.getParameterByName(param);
-        widget.log("obtained query '" + query + "' from param '" + param + "'");
-        if (!query) {
-          alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
-        }
-      } else if (query.match(/^!path!/)) {
-        var index = query.replace(/^!path!/, '');
-        var path = window.location.pathname.split('/');
-        query = path[path.length - index];
-        widget.log("obtained query '" + query + "' from path-component '" + index + "'");
-        if (!query) {
-          alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
-        }
-      } else if (query.match(/^!var!/)) {
-        var name = query.replace(/^!var!/, '');
-        query = window[name]; // It's ridiculous that this works
-        widget.log("obtained query '" + query + "' from variable '" + name + "'");
-        if (!query) {
-          alert("This page has a MasterKey widget that needs a query specified by the '" + name + "' variable");
-        }
+  var query = widget.config.autosearch;
+  if (query) {
+    if (query.match(/^!param!/)) {
+      var param = query.replace(/^!param!/, '');
+      query = mkws.getParameterByName(param);
+      widget.log("obtained query '" + query + "' from param '" + param + "'");
+      if (!query) {
+        alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
       }
+    } else if (query.match(/^!path!/)) {
+      var index = query.replace(/^!path!/, '');
+      var path = window.location.pathname.split('/');
+      query = path[path.length - index];
+      widget.log("obtained query '" + query + "' from path-component '" + index + "'");
+      if (!query) {
+        alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
+      }
+    } else if (query.match(/^!var!/)) {
+      var name = query.replace(/^!var!/, '');
+      query = window[name]; // It's ridiculous that this works
+      widget.log("obtained query '" + query + "' from variable '" + name + "'");
+      if (!query) {
+        alert("This page has a MasterKey widget that needs a query specified by the '" + name + "' variable");
+      }
+    }
+  }
 
+  widget.team.queue("ready").subscribe(function() {
+    if (query) {
       var sortOrder = widget.config.sort;
       var maxrecs = widget.config.maxrecs;
       var perpage = widget.config.perpage;
@@ -119,10 +163,10 @@ widget.autosearch = function(widget) {
 // Utility function for all widgets that want to hide in narrow windows
 widget.hideWhenNarrow = function(widget) {
   widget.team.queue("resize-narrow").subscribe(function(n) {
-    $(widget.node).hide();
+    widget.node.hide();
   });
   widget.team.queue("resize-wide").subscribe(function(n) {
-    $(widget.node).show();
+    widget.node.show();
   });
 };