Local function log() is now a simple alias to team.log(), rather than invoking it.
[mkws-moved-to-github.git] / src / mkws-widget.js
1 // Factory function for widget objects.
2 mkws.makeWidget = function($, team, type, node) {
3   // Static register of attributes that do not contribute to config
4   var ignoreAttrs = {
5     id:1, 'class':1, style:1, name:1, action:1, type:1, size:1,
6     value:1, width:1, valign:1
7   };
8
9   var that = {
10     team: team,
11     type: type,
12     node: $(node),
13     config: mkws.objectInheritingFrom(team.config)
14   };
15
16   var log = team.log;
17   that.log = log;
18
19   that.toString = function() {
20     return '[Widget ' + team.name() + ':' + type + ']';
21   };
22
23   that.value = function() {
24     return node.value;
25   };
26
27   // Returns the HTML of a subwidget of the specified type. It gets
28   // the same attributes at the parent widget that invokes this
29   // function, except where overrides are passed in. If defaults are
30   // also provided, then these are used when the parent widget
31   // provides no values.
32   that.subwidget = function(type, overrides, defaults) {
33     var attrs = { _team: team.name() };
34     
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");
40       }
41     }
42     
43     for (var name in overrides) {
44       log(this + " overrode property " + name + "='" + overrides[name] + "' (was '" + attrs[name] + "') for " + type + " subwidget");
45       attrs[name] = overrides[name];
46     }
47
48     if (defaults) {
49       for (var name in defaults) {
50         if (!attrs[name]) {
51           attrs[name] = defaults[name];
52           log(this + " fell back to default property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
53         }
54       }
55     }
56
57     var s = [];
58     s.push('<div class="mkws', type, ' mkws-team-', attrs._team, '"');
59     for (var name in attrs) {    
60       if (name !== '_team')
61         s.push(' ', name, '="', attrs[name], '"');
62     }
63     s.push('></div>');
64     return s.join('');
65   };
66
67   function expandValue(val) {
68     if (val.match(/^!param!/)) {
69       var param = val.replace(/^!param!/, '');
70       val = mkws.getParameterByName(param);
71       log("obtained val '" + val + "' from param '" + param + "'");
72       if (!val) {
73         alert("This page has a MasterKey widget that needs a val specified by the '" + param + "' parameter");
74       }
75     } else if (val.match(/^!path!/)) {
76       var index = val.replace(/^!path!/, '');
77       var path = window.location.pathname.split('/');
78       val = path[path.length - index];
79       log("obtained val '" + val + "' from path-component '" + index + "'");
80       if (!val) {
81         alert("This page has a MasterKey widget that needs a val specified by the path-component " + index);
82       }
83     } else if (val.match(/^!var!/)) {
84       var name = val.replace(/^!var!/, '');
85       val = window[name]; // It's ridiculous that this works
86       log("obtained val '" + val + "' from variable '" + name + "'");
87       if (!val) {
88         alert("This page has a MasterKey widget that needs a val specified by the '" + name + "' variable");
89       }
90     }
91     return val;
92   };
93
94   // Utility function for use by all widgets that can invoke autosearch.
95   that.autosearch = function() {
96     var that = this;
97     var query = this.config.autosearch;
98     if (query) {
99       var old = this.team.config.query;
100       if (!old) {
101         // Stash this for subsequent inspection
102         this.team.config.query = query;
103       } else if (old === query) {
104         this.log("duplicate autosearch: '" + query + "': ignoring");
105         return;
106       } else {
107         this.log("conflicting autosearch: '" + query + "' vs '" + old + "': ignoring");
108         return;
109       }
110
111       this.team.queue("ready").subscribe(function() {
112         // Postpone testing for the configuration items: these are not
113         // yet set for Record subclass widgets that fill them in in the
114         // subclass, as widget.autosearch is called in the superclass,
115         // before the subclass fiddles with the configuration.
116         var sortOrder = that.config.sort;
117         var maxrecs = that.config.maxrecs;
118         var perpage = that.config.perpage;
119         var limit = that.config.limit;
120         var targets = that.config.targets;
121         var targetfilter = that.config.targetfilter;
122         var target = that.config.target;
123         if (target) targetfilter = 'udb=="' + target + '"';
124
125         var s = "running auto search: '" + query + "'";
126         if (sortOrder) s += " sorted by '" + sortOrder + "'";
127         if (maxrecs) s += " restricted to " + maxrecs + " records";
128         if (perpage) s += " with " + perpage + " per page";
129         if (limit) s += " limited by '" + limit + "'";
130         if (targets) s += " in targets '" + targets + "'";
131         if (targetfilter) s += " constrained by targetfilter '" + targetfilter + "'";
132         that.log(s);
133
134         that.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
135       });
136     }
137   };
138
139   // Utility function for all widgets that want to hide in narrow windows
140   that.hideWhenNarrow = function() {
141     var that = this;
142     this.team.queue("resize-narrow").subscribe(function(n) {
143       that.node.hide();
144     });
145     this.team.queue("resize-wide").subscribe(function(n) {
146       that.node.show();
147     });
148   };
149
150
151   for (var i = 0; i < node.attributes.length; i++) {
152     var a = node.attributes[i];
153     var val = expandValue(a.value);
154     if (a.name === 'data-mkws-config') {
155       // Treat as a JSON fragment configuring just this widget
156       log(node + ": parsing config fragment '" + val + "'");
157       var data;
158       try {
159         data = $.parseJSON(val);
160         for (var key in data) {
161           log(node + ": adding config element " + key + "='" + data[key] + "'");
162           that.config[key] = data[key];
163         }
164       } catch (err) {
165         alert("Can't parse " + node + " data-mkws-config as JSON: " + val);
166       }
167     } else if (a.name.match (/^data-mkws-/)) {
168       var name = a.name.replace(/^data-mkws-/, '')
169       that.config[name] = val;
170       log(that + ": set data-mkws attribute " + name + "='" + val + "'");
171     } else if (!ignoreAttrs[a.name]) {
172       that.config[a.name] = val;
173       log(that + ": set regular attribute " + a.name + "='" + val + "'");
174     }
175   }
176
177   var fn = mkws.promotionFunction(type);
178   if (fn) {
179     fn.call(that);
180     log("made " + type + " widget(node=" + node + ")");
181   } else if (type.match(/-Container-(narrow|wide)$/)) {
182     // Not really a widget: no need to log its lack of promotion
183   } else {
184     log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
185   }
186
187   return that;
188 };