Give subwidget method support for an optional hash of default values.
[mkws-moved-to-github.git] / src / mkws-widget.js
1 // Factory function for widget objects.
2 function widget($, team, type, node) {
3   // Static register of attributes that do not contribute to config
4   var ignoreAttrs = {
5     id:1, 'class':1, style:1, name:1, action:1, type:1, size:1,
6     value:1, width:1, valign:1
7   };
8
9   var that = {
10     team: team,
11     type: type,
12     node: $(node),
13     config: mkws.objectInheritingFrom(team.config())
14   };
15
16   function log(s) {
17     team.log(s);
18   }
19   that.log = log;
20
21   that.toString = function() {
22     return '[Widget ' + team.name() + ':' + type + ']';
23   };
24
25   that.value = function() {
26     return node.value;
27   };
28
29   // Returns the HTML of a subwidget of the specified type. It gets
30   // the same attributes at the parent widget that invokes this
31   // function, except where overrides are passed in. If defaults are
32   // also provided, then these are used when the parent widget
33   // provides no values.
34   that.subwidget = function(type, overrides, defaults) {
35     var attrs = {};
36     
37     // Copy locally-set properties from the parent widget
38     for (var name in this.config) {
39       if (this.config.hasOwnProperty(name)) {
40         attrs[name] = this.config[name];
41         log(this + " copied property " + name + "='" + attrs[name] + "' to " + type + " subwidget");
42       }
43     }
44     
45     for (var name in overrides) {
46       attrs[name] = overrides[name];
47       log(this + " overrode property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
48     }
49
50     if (defaults) {
51       for (var name in defaults) {
52         if (!attrs[name]) {
53           attrs[name] = defaults[name];
54           log(this + " fell back to default property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
55         }
56       }
57     }
58
59     var s = [];
60     s.push('<div class="mkws', type, ' mkwsTeam_', team.name(), '"');
61     for (var name in attrs) {    
62       s.push(' ', name, '="', attrs[name], '"');
63     }
64     s.push('></div>');
65     return s.join('');
66   };
67
68   for (var i = 0; i < node.attributes.length; i++) {
69     var a = node.attributes[i];
70     if (a.name === 'data-mkws-config') {
71       // Treat as a JSON fragment configuring just this widget
72       log(node + ": parsing config fragment '" + a.value + "'");
73       var data;
74       try {
75         data = $.parseJSON(a.value);
76         for (var key in data) {
77           log(node + ": adding config element " + key + "='" + data[key] + "'");
78           that.config[key] = data[key];
79         }
80       } catch (err) {
81         alert("Can't parse " + node + " data-mkws-config as JSON: " + a.value);
82       }
83     } else if (a.name.match (/^data-mkws-/)) {
84       var name = a.name.replace(/^data-mkws-/, '')
85       that.config[name] = a.value;
86       log(that + ": set data-mkws attribute " + name + "='" + a.value + "'");
87     } else if (!ignoreAttrs[a.name]) {
88       that.config[a.name] = a.value;
89       log(that + ": set regular attribute " + a.name + "='" + a.value + "'");
90     }
91   }
92
93   var fn = mkws.promotionFunction(type);
94   if (fn) {
95     fn.call(that);
96     log("made " + type + " widget(node=" + node + ")");
97   } else if (type.match(/-Container-(narrow|wide)$/)) {
98     // Not really a widget: no need to log its lack of promotion
99   } else {
100     log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
101   }
102
103   return that;
104 }
105
106
107 // Utility function for use by all widgets that can invoke autosearch.
108 widget.autosearch = function(widget) {
109   widget.team.queue("ready").subscribe(function() {
110     var query = widget.config.autosearch;
111     if (query) {
112       if (query.match(/^!param!/)) {
113         var param = query.replace(/^!param!/, '');
114         query = mkws.getParameterByName(param);
115         widget.log("obtained query '" + query + "' from param '" + param + "'");
116         if (!query) {
117           alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
118         }
119       } else if (query.match(/^!path!/)) {
120         var index = query.replace(/^!path!/, '');
121         var path = window.location.pathname.split('/');
122         query = path[path.length - index];
123         widget.log("obtained query '" + query + "' from path-component '" + index + "'");
124         if (!query) {
125           alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
126         }
127       } else if (query.match(/^!var!/)) {
128         var name = query.replace(/^!var!/, '');
129         query = window[name]; // It's ridiculous that this works
130         widget.log("obtained query '" + query + "' from variable '" + name + "'");
131         if (!query) {
132           alert("This page has a MasterKey widget that needs a query specified by the '" + name + "' variable");
133         }
134       }
135
136       var sortOrder = widget.config.sort;
137       var maxrecs = widget.config.maxrecs;
138       var perpage = widget.config.perpage;
139       var limit = widget.config.limit;
140       var targets = widget.config.targets;
141       var targetfilter = widget.config.targetfilter;
142       var target = widget.config.target;
143       if (target) targetfilter = 'udb=="' + target + '"';
144
145       var s = "running auto search: '" + query + "'";
146       if (sortOrder) s += " sorted by '" + sortOrder + "'";
147       if (maxrecs) s += " restricted to " + maxrecs + " records";
148       if (perpage) s += " with " + perpage + " per page";
149       if (limit) s += " limited by '" + limit + "'";
150       if (targets) s += " in targets '" + targets + "'";
151       if (targetfilter) s += " constrained by targetfilter '" + targetfilter + "'";
152       widget.log(s);
153
154       widget.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
155     }
156   });
157 };
158
159
160 // Utility function for all widgets that want to hide in narrow windows
161 widget.hideWhenNarrow = function(widget) {
162   widget.team.queue("resize-narrow").subscribe(function(n) {
163     widget.node.hide();
164   });
165   widget.team.queue("resize-wide").subscribe(function(n) {
166     widget.node.show();
167   });
168 };
169
170