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
14 config: mkws.objectInheritingFrom(team.config())
22 that.toString = function() {
23 return '[Widget ' + team.name() + ':' + type + ']';
26 that.value = function() {
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 + "'");
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];
43 alert("Can't parse " + node + " data-mkws-config as JSON: " + a.value);
45 } else if (a.name.match (/^data-mkws-/)) {
46 var name = a.name.replace(/^data-mkws-/, '')
47 that.config[name] = a.value;
48 log(node + ": set data-mkws attribute " + name + "='" + a.value + "'");
49 } else if (!ignoreAttrs[a.name]) {
50 that.config[a.name] = a.value;
51 log(node + ": set regular attribute " + a.name + "='" + a.value + "'");
55 var fn = mkws.promotionFunction(type);
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
62 log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
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;
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 + "'");
79 alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
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 + "'");
87 alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
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 + "'");
94 alert("This page has a MasterKey widget that needs a query specified by the '" + name + "' variable");
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 + '"';
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 + "'";
116 widget.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
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();
127 widget.team.queue("resize-wide").subscribe(function(n) {
128 widget.jqnode.show();