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. If defaults are
32 // also provided, then these are used when the parent widget
33 // provides no values.
34 that.subwidget = function(type, overrides, defaults) {
35 var attrs = { _team: team.name() };
37 // Copy locally-set properties from the parent widget
38 for (var name in this.config) {
39 if (this.config.hasOwnProperty(name)) {
40 attrs[name] = this.config[name];
41 log(this + " copied property " + name + "='" + attrs[name] + "' to " + type + " subwidget");
45 for (var name in overrides) {
46 attrs[name] = overrides[name];
47 log(this + " overrode property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
51 for (var name in defaults) {
53 attrs[name] = defaults[name];
54 log(this + " fell back to default property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
60 s.push('<div class="mkws', type, ' mkwsTeam_', attrs._team, '"');
61 for (var name in attrs) {
63 s.push(' ', name, '="', attrs[name], '"');
69 // Utility function for use by all widgets that can invoke autosearch.
70 that.autosearch = 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 // Stash this for subsequent inspection
99 widget.team.config().query = query;
101 widget.team.queue("ready").subscribe(function() {
102 // Postpone testing for the configuration items: these are not
103 // yet set for Record subclass widgets that fill them in in the
104 // subclass, as widget.autosearch is called in the superclass,
105 // before the subclass fiddles with the configuration.
106 var sortOrder = widget.config.sort;
107 var maxrecs = widget.config.maxrecs;
108 var perpage = widget.config.perpage;
109 var limit = widget.config.limit;
110 var targets = widget.config.targets;
111 var targetfilter = widget.config.targetfilter;
112 var target = widget.config.target;
113 if (target) targetfilter = 'udb=="' + target + '"';
115 var s = "running auto search: '" + query + "'";
116 if (sortOrder) s += " sorted by '" + sortOrder + "'";
117 if (maxrecs) s += " restricted to " + maxrecs + " records";
118 if (perpage) s += " with " + perpage + " per page";
119 if (limit) s += " limited by '" + limit + "'";
120 if (targets) s += " in targets '" + targets + "'";
121 if (targetfilter) s += " constrained by targetfilter '" + targetfilter + "'";
124 widget.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
129 // Utility function for all widgets that want to hide in narrow windows
130 that.hideWhenNarrow = function() {
132 this.team.queue("resize-narrow").subscribe(function(n) {
135 this.team.queue("resize-wide").subscribe(function(n) {
141 for (var i = 0; i < node.attributes.length; i++) {
142 var a = node.attributes[i];
143 if (a.name === 'data-mkws-config') {
144 // Treat as a JSON fragment configuring just this widget
145 log(node + ": parsing config fragment '" + a.value + "'");
148 data = $.parseJSON(a.value);
149 for (var key in data) {
150 log(node + ": adding config element " + key + "='" + data[key] + "'");
151 that.config[key] = data[key];
154 alert("Can't parse " + node + " data-mkws-config as JSON: " + a.value);
156 } else if (a.name.match (/^data-mkws-/)) {
157 var name = a.name.replace(/^data-mkws-/, '')
158 that.config[name] = a.value;
159 log(that + ": set data-mkws attribute " + name + "='" + a.value + "'");
160 } else if (!ignoreAttrs[a.name]) {
161 that.config[a.name] = a.value;
162 log(that + ": set regular attribute " + a.name + "='" + a.value + "'");
166 var fn = mkws.promotionFunction(type);
169 log("made " + type + " widget(node=" + node + ")");
170 } else if (type.match(/-Container-(narrow|wide)$/)) {
171 // Not really a widget: no need to log its lack of promotion
173 log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");