1 // Factory function for widget objects.
2 mkws.makeWidget = function($, 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)
18 that.toString = function() {
19 return '[Widget ' + team.name() + ':' + type + ']';
22 that.value = function() {
26 // Returns the HTML of a subwidget of the specified type. It gets
27 // the same attributes at the parent widget that invokes this
28 // function, except where overrides are passed in. If defaults are
29 // also provided, then these are used when the parent widget
30 // provides no values.
31 that.subwidget = function(type, overrides, defaults) {
32 var attrs = { _team: team.name() };
34 // Copy locally-set properties from the parent widget
35 for (var name in this.config) {
36 if (this.config.hasOwnProperty(name)) {
37 attrs[name] = this.config[name];
38 this.log(this + " copied property " + name + "='" + attrs[name] + "' to " + type + " subwidget");
42 for (var name in overrides) {
43 this.log(this + " overrode property " + name + "='" + overrides[name] + "' (was '" + attrs[name] + "') for " + type + " subwidget");
44 attrs[name] = overrides[name];
48 for (var name in defaults) {
50 attrs[name] = defaults[name];
51 this.log(this + " fell back to default property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
57 s.push('<div class="mkws', type, ' mkws-team-', attrs._team, '"');
58 for (var name in attrs) {
60 s.push(' ', name, '="', attrs[name], '"');
66 function expandValue(val) {
67 if (val.match(/^!param!/)) {
68 var param = val.replace(/^!param!/, '');
69 val = mkws.getParameterByName(param);
70 that.log("obtained val '" + val + "' from param '" + param + "'");
72 alert("This page has a MasterKey widget that needs a val specified by the '" + param + "' parameter");
74 } else if (val.match(/^!path!/)) {
75 var index = val.replace(/^!path!/, '');
76 var path = window.location.pathname.split('/');
77 val = path[path.length - index];
78 that.log("obtained val '" + val + "' from path-component '" + index + "'");
80 alert("This page has a MasterKey widget that needs a val specified by the path-component " + index);
82 } else if (val.match(/^!var!/)) {
83 var name = val.replace(/^!var!/, '');
84 val = window[name]; // It's ridiculous that this works
85 that.log("obtained val '" + val + "' from variable '" + name + "'");
87 alert("This page has a MasterKey widget that needs a val specified by the '" + name + "' variable");
93 // Utility function for use by all widgets that can invoke autosearch.
94 that.autosearch = function() {
96 var query = this.config.autosearch;
98 var old = this.team.config.query;
100 // Stash this for subsequent inspection
101 this.team.config.query = query;
102 } else if (old === query) {
103 this.log("duplicate autosearch: '" + query + "': ignoring");
106 this.log("conflicting autosearch: '" + query + "' vs '" + old + "': ignoring");
110 this.team.queue("ready").subscribe(function() {
111 // Postpone testing for the configuration items: these are not
112 // yet set for Record subclass widgets that fill them in in the
113 // subclass, as widget.autosearch is called in the superclass,
114 // before the subclass fiddles with the configuration.
115 var sortOrder = that.config.sort;
116 var maxrecs = that.config.maxrecs;
117 var perpage = that.config.perpage;
118 var limit = that.config.limit;
119 var targets = that.config.targets;
120 var targetfilter = that.config.targetfilter;
121 var target = that.config.target;
122 if (target) targetfilter = 'udb=="' + target + '"';
124 var s = "running auto search: '" + query + "'";
125 if (sortOrder) s += " sorted by '" + sortOrder + "'";
126 if (maxrecs) s += " restricted to " + maxrecs + " records";
127 if (perpage) s += " with " + perpage + " per page";
128 if (limit) s += " limited by '" + limit + "'";
129 if (targets) s += " in targets '" + targets + "'";
130 if (targetfilter) s += " constrained by targetfilter '" + targetfilter + "'";
133 that.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
138 // Utility function for all widgets that want to hide in narrow windows
139 that.hideWhenNarrow = function() {
141 this.team.queue("resize-narrow").subscribe(function(n) {
144 this.team.queue("resize-wide").subscribe(function(n) {
150 for (var i = 0; i < node.attributes.length; i++) {
151 var a = node.attributes[i];
152 var val = expandValue(a.value);
153 if (a.name === 'data-mkws-config') {
154 // Treat as a JSON fragment configuring just this widget
155 this.log(node + ": parsing config fragment '" + val + "'");
158 data = $.parseJSON(val);
159 for (var key in data) {
160 this.log(node + ": adding config element " + key + "='" + data[key] + "'");
161 that.config[key] = data[key];
164 alert("Can't parse " + node + " data-mkws-config as JSON: " + val);
166 } else if (a.name.match (/^data-mkws-/)) {
167 var name = a.name.replace(/^data-mkws-/, '')
168 that.config[name] = val;
169 this.log(that + ": set data-mkws attribute " + name + "='" + val + "'");
170 } else if (!ignoreAttrs[a.name]) {
171 that.config[a.name] = val;
172 this.log(that + ": set regular attribute " + a.name + "='" + val + "'");
176 var fn = mkws.promotionFunction(type);
179 this.log("made " + type + " widget(node=" + node + ")");
180 } else if (type.match(/-Container-(narrow|wide)$/)) {
181 // Not really a widget: no need to log its lack of promotion
183 this.log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");