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