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 // 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) {
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");
43 for (var name in overrides) {
44 attrs[name] = overrides[name];
45 log(this + " overrode property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
49 s.push('<div class="mkws', type, ' mkwsTeam_', team.name(), '"');
50 for (var name in attrs) {
51 s.push(' ', name, '="', attrs[name], '"');
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 + "'");
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];
70 alert("Can't parse " + node + " data-mkws-config as JSON: " + a.value);
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 + "'");
82 var fn = mkws.promotionFunction(type);
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
89 log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
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;
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 + "'");
106 alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
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 + "'");
114 alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
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 + "'");
121 alert("This page has a MasterKey widget that needs a query specified by the '" + name + "' variable");
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 + '"';
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 + "'";
143 widget.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
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) {
154 widget.team.queue("resize-wide").subscribe(function(n) {