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