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