New utility function subwidget, making it easier to write compoound
[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.
32   that.subwidget = function(type, overrides) {
33     var attrs = {};
34     
35     // Copy locally-set properties from the parent widget
36     for (var name in this.config) {
37       if (this.config.hasOwnProperty(name)) {
38         attrs[name] = this.config[name];
39         log(this + " copied property " + name + "='" + attrs[name] + "' to " + type + " subwidget");
40       }
41     }
42     
43     for (var name in overrides) {
44       attrs[name] = overrides[name];
45       log(this + " overrode property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
46     }
47
48     var s = [];
49     s.push('<div class="mkws', type, ' ', team.name(), '"');
50     for (var name in attrs) {    
51       s.push(' ', name, '="', attrs[name], '"');
52     }
53     s.push('></div>');
54     return s.join('');
55   };
56
57   for (var i = 0; i < node.attributes.length; i++) {
58     var a = node.attributes[i];
59     if (a.name === 'data-mkws-config') {
60       // Treat as a JSON fragment configuring just this widget
61       log(node + ": parsing config fragment '" + a.value + "'");
62       var data;
63       try {
64         data = $.parseJSON(a.value);
65         for (var key in data) {
66           log(node + ": adding config element " + key + "='" + data[key] + "'");
67           that.config[key] = data[key];
68         }
69       } catch (err) {
70         alert("Can't parse " + node + " data-mkws-config as JSON: " + a.value);
71       }
72     } else if (a.name.match (/^data-mkws-/)) {
73       var name = a.name.replace(/^data-mkws-/, '')
74       that.config[name] = a.value;
75       log(that + ": set data-mkws attribute " + name + "='" + a.value + "'");
76     } else if (!ignoreAttrs[a.name]) {
77       that.config[a.name] = a.value;
78       log(that + ": set regular attribute " + a.name + "='" + a.value + "'");
79     }
80   }
81
82   var fn = mkws.promotionFunction(type);
83   if (fn) {
84     fn.call(that);
85     log("made " + type + " widget(node=" + node + ")");
86   } else if (type.match(/-Container-(narrow|wide)$/)) {
87     // Not really a widget: no need to log its lack of promotion
88   } else {
89     log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
90   }
91
92   return that;
93 }
94
95
96 // Utility function for use by all widgets that can invoke autosearch.
97 widget.autosearch = function(widget) {
98   widget.team.queue("ready").subscribe(function() {
99     var query = widget.config.autosearch;
100     if (query) {
101       if (query.match(/^!param!/)) {
102         var param = query.replace(/^!param!/, '');
103         query = mkws.getParameterByName(param);
104         widget.log("obtained query '" + query + "' from param '" + param + "'");
105         if (!query) {
106           alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
107         }
108       } else if (query.match(/^!path!/)) {
109         var index = query.replace(/^!path!/, '');
110         var path = window.location.pathname.split('/');
111         query = path[path.length - index];
112         widget.log("obtained query '" + query + "' from path-component '" + index + "'");
113         if (!query) {
114           alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
115         }
116       } else if (query.match(/^!var!/)) {
117         var name = query.replace(/^!var!/, '');
118         query = window[name]; // It's ridiculous that this works
119         widget.log("obtained query '" + query + "' from variable '" + name + "'");
120         if (!query) {
121           alert("This page has a MasterKey widget that needs a query specified by the '" + name + "' variable");
122         }
123       }
124
125       var sortOrder = widget.config.sort;
126       var maxrecs = widget.config.maxrecs;
127       var perpage = widget.config.perpage;
128       var limit = widget.config.limit;
129       var targets = widget.config.targets;
130       var targetfilter = widget.config.targetfilter;
131       var target = widget.config.target;
132       if (target) targetfilter = 'udb=="' + target + '"';
133
134       var s = "running auto search: '" + query + "'";
135       if (sortOrder) s += " sorted by '" + sortOrder + "'";
136       if (maxrecs) s += " restricted to " + maxrecs + " records";
137       if (perpage) s += " with " + perpage + " per page";
138       if (limit) s += " limited by '" + limit + "'";
139       if (targets) s += " in targets '" + targets + "'";
140       if (targetfilter) s += " constrained by targetfilter '" + targetfilter + "'";
141       widget.log(s);
142
143       widget.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
144     }
145   });
146 };
147
148
149 // Utility function for all widgets that want to hide in narrow windows
150 widget.hideWhenNarrow = function(widget) {
151   widget.team.queue("resize-narrow").subscribe(function(n) {
152     widget.node.hide();
153   });
154   widget.team.queue("resize-wide").subscribe(function(n) {
155     widget.node.show();
156   });
157 };
158
159