The autosearch method now detects when multiple autosearches are
[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       attrs[name] = overrides[name];
47       log(this + " overrode property " + name + "='" + attrs[name] + "' for " + type + " subwidget");
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   // Utility function for use by all widgets that can invoke autosearch.
70   that.autosearch = function() {
71     var that = this;
72     var query = this.config.autosearch;
73     if (query) {
74       if (query.match(/^!param!/)) {
75         var param = query.replace(/^!param!/, '');
76         query = mkws.getParameterByName(param);
77         this.log("obtained query '" + query + "' from param '" + param + "'");
78         if (!query) {
79           alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
80         }
81       } else if (query.match(/^!path!/)) {
82         var index = query.replace(/^!path!/, '');
83         var path = window.location.pathname.split('/');
84         query = path[path.length - index];
85         this.log("obtained query '" + query + "' from path-component '" + index + "'");
86         if (!query) {
87           alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
88         }
89       } else if (query.match(/^!var!/)) {
90         var name = query.replace(/^!var!/, '');
91         query = window[name]; // It's ridiculous that this works
92         this.log("obtained query '" + query + "' from variable '" + name + "'");
93         if (!query) {
94           alert("This page has a MasterKey widget that needs a query specified by the '" + name + "' variable");
95         }
96       }
97
98       var old = this.team.config().query;
99       if (!old) {
100         // Stash this for subsequent inspection
101         this.team.config().query = query;
102       } else if (old === query) {
103         this.log("duplicate autosearch: '" + query + "': ignoring");
104         return;
105       } else {
106         this.log("conflicting autosearch: '" + query + "' vs '" + old + "': ignoring");
107         return;
108       }
109
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 + '"';
123
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 + "'";
131         that.log(s);
132
133         that.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
134       });
135     }
136   };
137
138   // Utility function for all widgets that want to hide in narrow windows
139   that.hideWhenNarrow = function() {
140     var that = this;
141     this.team.queue("resize-narrow").subscribe(function(n) {
142       that.node.hide();
143     });
144     this.team.queue("resize-wide").subscribe(function(n) {
145       that.node.show();
146     });
147   };
148
149
150   for (var i = 0; i < node.attributes.length; i++) {
151     var a = node.attributes[i];
152     if (a.name === 'data-mkws-config') {
153       // Treat as a JSON fragment configuring just this widget
154       log(node + ": parsing config fragment '" + a.value + "'");
155       var data;
156       try {
157         data = $.parseJSON(a.value);
158         for (var key in data) {
159           log(node + ": adding config element " + key + "='" + data[key] + "'");
160           that.config[key] = data[key];
161         }
162       } catch (err) {
163         alert("Can't parse " + node + " data-mkws-config as JSON: " + a.value);
164       }
165     } else if (a.name.match (/^data-mkws-/)) {
166       var name = a.name.replace(/^data-mkws-/, '')
167       that.config[name] = a.value;
168       log(that + ": set data-mkws attribute " + name + "='" + a.value + "'");
169     } else if (!ignoreAttrs[a.name]) {
170       that.config[a.name] = a.value;
171       log(that + ": set regular attribute " + a.name + "='" + a.value + "'");
172     }
173   }
174
175   var fn = mkws.promotionFunction(type);
176   if (fn) {
177     fn.call(that);
178     log("made " + type + " widget(node=" + node + ")");
179   } else if (type.match(/-Container-(narrow|wide)$/)) {
180     // Not really a widget: no need to log its lack of promotion
181   } else {
182     log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
183   }
184
185   return that;
186 }