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)
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 log(this + " overrode property " + name + "='" + overrides[name] + "' (was '" + attrs[name] + "') for " + type + " subwidget");
47 attrs[name] = overrides[name];
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, ' mkws-team-', attrs._team, '"');
61 for (var name in attrs) {
63 s.push(' ', name, '="', attrs[name], '"');
69 function expandValue(val) {
70 if (val.match(/^!param!/)) {
71 var param = val.replace(/^!param!/, '');
72 val = mkws.getParameterByName(param);
73 log("obtained val '" + val + "' from param '" + param + "'");
75 alert("This page has a MasterKey widget that needs a val specified by the '" + param + "' parameter");
77 } else if (val.match(/^!path!/)) {
78 var index = val.replace(/^!path!/, '');
79 var path = window.location.pathname.split('/');
80 val = path[path.length - index];
81 log("obtained val '" + val + "' from path-component '" + index + "'");
83 alert("This page has a MasterKey widget that needs a val specified by the path-component " + index);
85 } else if (val.match(/^!var!/)) {
86 var name = val.replace(/^!var!/, '');
87 val = window[name]; // It's ridiculous that this works
88 log("obtained val '" + val + "' from variable '" + name + "'");
90 alert("This page has a MasterKey widget that needs a val specified by the '" + name + "' variable");
96 // Utility function for use by all widgets that can invoke autosearch.
97 that.autosearch = function() {
99 var query = this.config.autosearch;
101 var old = this.team.config.query;
103 // Stash this for subsequent inspection
104 this.team.config.query = query;
105 } else if (old === query) {
106 this.log("duplicate autosearch: '" + query + "': ignoring");
109 this.log("conflicting autosearch: '" + query + "' vs '" + old + "': ignoring");
113 this.team.queue("ready").subscribe(function() {
114 // Postpone testing for the configuration items: these are not
115 // yet set for Record subclass widgets that fill them in in the
116 // subclass, as widget.autosearch is called in the superclass,
117 // before the subclass fiddles with the configuration.
118 var sortOrder = that.config.sort;
119 var maxrecs = that.config.maxrecs;
120 var perpage = that.config.perpage;
121 var limit = that.config.limit;
122 var targets = that.config.targets;
123 var targetfilter = that.config.targetfilter;
124 var target = that.config.target;
125 if (target) targetfilter = 'udb=="' + target + '"';
127 var s = "running auto search: '" + query + "'";
128 if (sortOrder) s += " sorted by '" + sortOrder + "'";
129 if (maxrecs) s += " restricted to " + maxrecs + " records";
130 if (perpage) s += " with " + perpage + " per page";
131 if (limit) s += " limited by '" + limit + "'";
132 if (targets) s += " in targets '" + targets + "'";
133 if (targetfilter) s += " constrained by targetfilter '" + targetfilter + "'";
136 that.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
141 // Utility function for all widgets that want to hide in narrow windows
142 that.hideWhenNarrow = function() {
144 this.team.queue("resize-narrow").subscribe(function(n) {
147 this.team.queue("resize-wide").subscribe(function(n) {
153 for (var i = 0; i < node.attributes.length; i++) {
154 var a = node.attributes[i];
155 var val = expandValue(a.value);
156 if (a.name === 'data-mkws-config') {
157 // Treat as a JSON fragment configuring just this widget
158 log(node + ": parsing config fragment '" + val + "'");
161 data = $.parseJSON(val);
162 for (var key in data) {
163 log(node + ": adding config element " + key + "='" + data[key] + "'");
164 that.config[key] = data[key];
167 alert("Can't parse " + node + " data-mkws-config as JSON: " + val);
169 } else if (a.name.match (/^data-mkws-/)) {
170 var name = a.name.replace(/^data-mkws-/, '')
171 that.config[name] = val;
172 log(that + ": set data-mkws attribute " + name + "='" + val + "'");
173 } else if (!ignoreAttrs[a.name]) {
174 that.config[a.name] = val;
175 log(that + ": set regular attribute " + a.name + "='" + val + "'");
179 var fn = mkws.promotionFunction(type);
182 log("made " + type + " widget(node=" + node + ")");
183 } else if (type.match(/-Container-(narrow|wide)$/)) {
184 // Not really a widget: no need to log its lack of promotion
186 log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");