Use the LGPL instead of the GPL.
[mkws-moved-to-github.git] / doc / mkws-developer.markdown
1 % The MasterKey Widget Set developer's guide
2 % Mike Taylor
3 % 11 August 2014
4
5
6 Overview
7 ========
8
9 Core concepts
10 -------------
11
12 Development with MKWS consists primarily of defining new types of
13 widgets. These can interact with the core functionality is several
14 defined ways.
15
16 You create a new widget type by calling the `mkws.registerWidgetType`
17 function, passing in the widget name and a function. The name is used
18 to recognise HTML elements as being widgets of this type -- for
19 example, if you register a `Foo` widget, elements like
20 `<div class="mkwsFoo">` will be widgets of this type.
21
22 The function promotes a bare widget object (passed as `this`) into a
23 widget of the appropriate type. MKWS doesn't use classes or explicit
24 prototypes: it just makes objects that have the necessary
25 behaviours. There are _no_ behaviours that Widgets are obliged to
26 provide: you can make a doesn't-do-anything-at-all widget if you like:
27
28         mkws.registerWidgetType('Sluggard', function() {});
29
30 More commonly, widgets will subscribe to one or more events, so that
31 they're notified when something interesting happens. For example, the
32 `Log` widget asks to be notified when a `log` event happens, and
33 appends the logged message to its node, as follows:
34
35         mkws.registerWidgetType('Log', function() {
36           var that = this;
37
38           this.team.queue("log").subscribe(function(teamName, timestamp, message) {
39             $(that.node).append(teamName + ": " + timestamp + message + "<br/>");
40           });
41         });
42
43 This simple widget illustrates several important points:
44
45 * The base widget object (`this`) has several baked-in properties and
46   methods that are available to individual widgets. These include
47   `this.team` (the team that this widget is a part of) and `this.node`
48   (the DOM element of the widget). See below for a full list.
49
50 * The team object (`this.team`) also has baked-in properties and
51   methods. These include the `queue` function, which takes an event-name
52   as its argument. See below for a full list.
53
54 * You can add functionality to a widget by subscribing it to an
55   event's queue using `this.team.queue("EVENT").subscribe`. The
56   argument is a function which is called whenever the event is
57   published. The arguments to the function are different for different
58   events.
59
60 * As with so much JavaScript programming, the value of the special
61   variable `this` is lost inside the `subscribez` callback function,
62   so it must be saved if it's to be used inside that callback
63   (typically as a local variable named `that`).
64
65
66 Widget specialisation (inheritance)
67 -----------------------------------
68
69 Many widgets are simple specialisations of existing widgets. For
70 example, the `Record` widget is the same as the `Records` widget
71 except that it defaults to displaying a single record. It's defined as
72 follows:
73
74         mkws.registerWidgetType('Record', function() {
75           mkws.promotionFunction('Records').call(this);
76           if (!this.config.maxrecs) this.config.maxrecs = 1;
77         });
78
79 Remember that when a promotion function is called, it's passed a base
80 widget object that's not specialised for any particular task. To make
81 a specialised widget, you first promote that base widget into the type
82 that you want to specialise from -- in this case, `Records` -- using
83 the promotion function that's been registered for that type.
84
85 Once this has been done, the specialisations can be introduced. In
86 this case, it's a very simple matter of changing the `maxrecs`
87 configuration setting to 1 unless it's already been given an explicit
88 value. (That would occur if the HTML used an element like `<div
89 class="mkwsRecord" maxrecs="2">`, though it's not obvious why anyone
90 would do that.)
91
92
93 Reference Guide
94 ===============
95
96
97 Widget properties and methods
98 -----------------------------
99
100 The following properties and methods exist in the bare widget object
101 that is passed into `registerWidgetType`'s callback function, and can
102 be used by the derived widget.
103
104 * `String this.type` --
105         A string containing the type of the widget.
106
107 * `Team this.team` --
108         The team object to which this widget belongs. The team has
109         several additional important properties and methods, described
110         below.
111
112 * `DOMElement this.node` --
113         The DOM element of the widget
114
115 * `Hash this.config` --
116         A table of configuration values for the widget. This table
117         inherits missing values from the team's configuration, which
118         in turn inherits from the top-level MKWS configuration, which
119         inherits from the default configuration. Instances of widgets
120         in HTML can set configuration items as HTML attributes: for
121         example, the HTML element
122         `<div class="mkwsRecords" maxrecs="10">`.
123         creates a widget for which `this.config.maxrecs` is set to 10.
124
125 * `String this.toString()` --
126         A function returning a string that briefly names this
127         widget. Can be useful in logging.
128
129 * `Void this.log(string)` --
130         A function to log a string for debugging purposes. The string
131         is written on the browser console, and also published to any
132         subcribers to the `log` event.
133
134 * `String this.value()` --
135         A function returning the value of the widget's HTML element.
136
137 * `VOID autosearch()` --
138         Registers that this kind of widget is one that requires an
139         automatic search to be run for it if an `autosearch` attribute
140         is provided on the HTML element. This is appropriate for
141         widgets such as `Records` and `Facet` that display some part
142         of a search result.
143
144 * `VOID hideWhenNarrow()` --
145         Registers that this widget should hide itself when the page
146         becomes "narrow" -- that is, fewer pixels in width that the
147         threshhold value specified by the top-level configuration item
148         `responsive_design_width`. Should be used for "unimportant"
149         widgets that can be omitted from the mobile version of a site.
150
151 * `expandValue()` --
152         TODO: either document this or remove it from the API.
153
154 * `subwidget(type, overrides, defaults)` --
155         Returns the HTML of a subwidget of the specified type, which
156         can then be inserted into the widget using the
157         `this.node.html` function. The subwidget is given the same
158         attributes at the parent widget that invokes this function,
159         except where overrides are passed in. If defaults are also
160         provided, then these are used when the parent widget provides
161         no values. Both the `overrides` and `defaults` arguments are
162         hashes: the latter is optional.
163   
164         See for example the `Credo` widget defined in the example
165         area's `mkws-widget-credo.js` file. This uses several
166         invocations of `subwidget` to create a complex compound widget
167         with numerous text, facet and image panes. TODO: rename this
168         widget and everything related to it.
169
170 In addition to these properties and methods of the bare widget object,
171 some kinds of specific widget add other properties of their own. For
172 example, the `Builder` widget uses a `callback` property as the
173 function that it use to publish the widget definition that it
174 constructs. This defaults to the builtin function `alert`, but can be
175 overridden by derived widgets such as `ConsoleBuilder`.
176
177
178 Team methods
179 ------------
180
181 Since the team object is supposed to be opaque to widgets, all access
182 is via the following API methods rather than direct access to
183 properties.
184
185 * `String team.name()`
186 * `Bool team.submitted()`
187 * `Num team.perpage()`
188 * `Num team.totalRecordCount()`
189 * `Num team.currentPage();`
190 * `String team.currentRecordId()`
191 * `String team.currentRecordData()`
192
193 These are all simple accessor functions that provide the ability to
194 read properties of the team.
195
196 * `Array team.filters()` --
197         Another accessor function, providing access to the array of
198         prevailing filters (which narrow the search results by means
199         of Pazpar2 filters and limits). This is really too complicated
200         an object for the widgets to be given access to, but it's
201         convenient to do it this way. If you must insist on using
202         this, see the `Navi` widget, which is the only place it's used.
203
204 * `Bool team.targetFiltered(targetId)` --
205         Indicates whether the specified target has been filtered by
206         selection as a facet. This is used only by the `Facet` widget,
207         and there is probably no reason for you to use it.
208
209 * `Hash team.config()` --
210         Access to the team's configuration settings. There is almost
211         certainly no reason to use this: the settings that haven't
212         been overridden are accessible via `this.config`.
213
214 * `Void team.set_sortOrder(string)`, `Void team.set_perpage(number)` --
215         "Setter" functions for the team's sortOrder and perpage
216         functions. Unlikely to be needed outside of the `Sort` and
217         `Perpage` widgets.
218
219 * `Queue team.queue(eventName)` --
220         Returns the queue associated with the named event: this can be
221         used to subscribe to the event (or more rarely to publish it).
222
223 * `Void team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter)` --
224         Starts a new search with the specified parameters. All but the
225         query may be omitted, in which case the prevailing defaults
226         are used.
227
228 * `Void team.reShow()` --
229         Using the existing search, re-shows the result records after a
230         change in sort-order, per-page count, etc.
231
232 * `String team.recordElementId(recordId)` --
233         Utility function for converting a record identifer (returned
234         from Pazpar2) into a version suitable for use as an HTML
235         element ID.
236
237 * `String team.renderDetails(recordData)` --
238         Utility function returns an HTML rendering of the record
239         represented by the specified data.
240
241 * `Template team.loadTemplate(templateName)` --
242         Loads (or retrieves from cache) the named Handlebars template,
243         and returns it in a form that can be invoked as a function,
244         passed a data-set.
245
246 Some of these methods either (A) are really too low-level and should
247 not be exposed, or (B) should be widget-level methods. The present
248 infelicities reflect the fact that some code that rightly belongs in
249 widgets is still in the team. When we finish migrating it, the widget
250 API should get simpler.
251
252
253 Events
254 ------
255
256 TODO: list of events that can be usefully subscribed to.
257
258
259 - - -
260
261 Copyright (C) 2013-2014 by IndexData ApS, <http://www.indexdata.com>