1 // Factory function for widget objects.
2 function widget($, team, type, node) {
3 // Static register of attributes that do not contribute to config
5 id:1, 'class':1, style:1, name:1, action:1, type:1, size:1,
6 value:1, width:1, valign:1
13 config: mkws.objectInheritingFrom(team.config())
21 that.toString = function() {
22 return '[Widget ' + team.name() + ':' + type + ']';
25 that.value = function() {
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 + "'");
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];
42 alert("Can't parse " + node + " data-mkws-config as JSON: " + a.value);
44 } else if (a.name.match (/^data-mkws-/)) {
45 var name = a.name.replace(/^data-mkws-/, '')
46 that.config[name] = a.value;
47 log(node + ": set data-mkws attribute " + name + "='" + a.value + "'");
48 } else if (!ignoreAttrs[a.name]) {
49 that.config[a.name] = a.value;
50 log(node + ": set regular attribute " + a.name + "='" + a.value + "'");
54 var fn = mkws.promotionFunction(type);
57 log("made " + type + " widget(node=" + node + ")");
59 log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
66 // Utility function for use by all widgets that can invoke autosearch.
67 widget.autosearch = function(widget) {
68 widget.team.queue("ready").subscribe(function() {
69 var query = widget.config.autosearch;
71 if (query.match(/^!param!/)) {
72 var param = query.replace(/^!param!/, '');
73 query = mkws.getParameterByName(param);
74 widget.log("obtained query '" + query + "' from param '" + param + "'");
76 alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
78 } else if (query.match(/^!path!/)) {
79 var index = query.replace(/^!path!/, '');
80 var path = window.location.pathname.split('/');
81 query = path[path.length - index];
82 widget.log("obtained query '" + query + "' from path-component '" + index + "'");
84 alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
86 } else if (query.match(/^!var!/)) {
87 var name = query.replace(/^!var!/, '');
88 query = window[name]; // It's ridiculous that this works
89 widget.log("obtained query '" + query + "' from variable '" + name + "'");
91 alert("This page has a MasterKey widget that needs a query specified by the '" + name + "' variable");
95 var sortOrder = widget.config.sort;
96 var maxrecs = widget.config.maxrecs;
97 var perpage = widget.config.perpage;
98 var limit = widget.config.limit;
99 var targets = widget.config.targets;
100 var targetfilter = widget.config.targetfilter;
101 var target = widget.config.target;
102 if (target) targetfilter = 'udb=="' + target + '"';
104 var s = "running auto search: '" + query + "'";
105 if (sortOrder) s += " sorted by '" + sortOrder + "'";
106 if (maxrecs) s += " restricted to " + maxrecs + " records";
107 if (perpage) s += " with " + perpage + " per page";
108 if (limit) s += " limited by '" + limit + "'";
109 if (targets) s += " in targets '" + targets + "'";
110 if (targetfilter) s += " constrained by targetfilter '" + targetfilter + "'";
113 widget.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
119 // Utility function for all widgets that want to hide in narrow windows
120 widget.hideWhenNarrow = function(widget) {
121 widget.team.queue("resize-narrow").subscribe(function(n) {
122 $(widget.node).hide();
124 widget.team.queue("resize-wide").subscribe(function(n) {
125 $(widget.node).show();