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