1 % The MasterKey Widget Set developer's guide
9 This manual is for people who want to build the widget set from
10 source, develop the widget set's core code, or (more likely) create
11 their own widgets as extensions to the main set.
13 Those who want to use existing widgets should read
14 [The MKWS manual: embedded metasearching with the MasterKey Widget
15 Set](mkws-manual.html) instead.
18 Required development tools
19 ==========================
21 If you are building the widget set, you will need the following Debian
22 packages (or their equivalents on your operating system):
24 $ sudo apt-get install curl git make unzip apache2 \
25 pandoc yui-compressor libbsd-resource-perl
27 You also need Node.js, but unfortunately the `node-js` package is not
28 available for Debian wheezy. You can either get it from
29 wheezy-backports or download the source from
30 http://nodejs.org/download/ and build it yourself. You need both Node
31 itself and its package manager NPM: `make install` puts them into
41 The code of the widget set is in four main layers, described here from
44 1. The core code, which manages the set of widget teams, default
45 options, authentication onto the Service Proxy, and the creation of
46 widgets from HTML elements.
47 This code is in `mkws-core.js`
49 2. The team code, which manages teams of widgets. This is responsible
50 for the collections of widgets that make up teams, event queues, and
51 handling search-and-retrieval events
52 This code is in `mkws-team.js`
54 3. The generic widget code, which handles the creation of widget
55 objects, parsing configuration attributes from their HTML elements,
56 and firing off automatic searches.
58 4. The code for individual widgets, which is specific to those
59 widgets. It often involves subscribing to events and responding to
60 them by setting the HTML of the widget element, but need not do
61 so. The code for many of the most important widgets is in
62 `mkws-widget-main.js`, but certain other widgets are defined in other
63 files beginning with the prefix `mkws-widget-`.
65 In addition to this code, there are several source files containing
68 * `mkws-filter.js` contains support routine implementing the
69 filter-set data structure, which contains information about which
70 filters (e.g. by target, or by facet) are in force.
72 * `mkws-handlebars.js` contains Handlebars helpers which can be used
73 by the HTML templates.
75 * `mkws-popup.js` defines a special widget for creating popup
76 windows. These may, but need not, contain other MKWS widgets,
77 forming a popup searching application.
79 The final component of the source code is the set of Handlebars
80 templates, in the `templates` directory, which are used to emit the
81 HTML of the various widgets' contents. These are compiled into the
82 file `mkws-templates.js`.
86 Defining new types of widget
87 ----------------------------
89 Development with MKWS consists primarily of defining new types of
90 widgets. This is done using exactly the same API as the the widgets
91 that come as part of the set: they have no privileged access.
93 You create a new widget type by calling the `mkws.registerWidgetType`
94 function, passing in the widget name and a function. The name is used
95 to recognise HTML elements as being widgets of this type -- for
96 example, if you register a `foo` widget, elements like
97 `<div class="mkws-foo">` will become widgets of this type.
99 The function promotes a bare widget object (which is created by the
100 core widget code and passed in as `this`) into a
101 widget of the appropriate type. MKWS doesn't use classes or explicit
102 prototypes: it just makes objects that have the necessary
103 behaviours. There are _no_ behaviours that Widgets are obliged to
104 provide: you can make a doesn't-do-anything-at-all widget if you like:
106 mkws.registerWidgetType('sluggard', function() {});
108 More commonly, widgets will subscribe to one or more events, so that
109 they're notified when something interesting happens. For example, the
110 `log` widget asks to be notified when a `log` event happens, and
111 appends the logged message to its node, as follows:
113 mkws.registerWidgetType('log', function() {
116 this.team.queue("log").subscribe(function(teamName, timestamp, message) {
117 $(that.node).append(teamName + ": " + timestamp + message + "<br/>");
121 This simple widget illustrates several important points:
123 * The base widget object (`this`) has several baked-in properties and
124 methods that are available to individual widgets. These include
125 `this.team` (the team that this widget is a part of) and `this.node`
126 (the DOM element of the widget). See below for a full list.
128 * The team object (`this.team`) also has baked-in properties and
129 methods. These include the `queue` function, which takes an event-name
130 as its argument. See below for a full list.
132 * You can add functionality to a widget by subscribing it to an
133 event's queue using `this.team.queue("EVENT").subscribe`. The
134 argument is a function which is called whenever the event is
135 published. The arguments to the event-callback function are
136 different for different events.
138 * As with so much JavaScript programming, the value of the special
139 variable `this` is lost inside the `subscribe` callback function,
140 so it must be saved if it's to be used inside that callback
141 (typically as a local variable named `that`).
144 Widget specialisation (inheritance)
145 -----------------------------------
147 Many widgets are simple specialisations of existing widgets. For
148 example, the `images` widget is the same as the `records` widget
149 except that it defaults to using the `images` template for displaying
150 its result list. It's defined as follows:
152 mkws.registerWidgetType('images', function() {
153 mkws.promotionFunction('records').call(this);
154 if (!this.config.template) this.config.template = 'images';
157 Remember that when a promotion function is called, it's passed a base
158 widget object that's not specialised for any particular task. To make
159 a specialised widget, you first promote that base widget into the type
160 that you want to specialise from -- in this case, `Records` -- using
161 the promotion function that's been registered for that type.
163 Once this has been done, the specialisations can be introduced. In
164 this case, it's a very simple matter of changing the `template`
165 configuration setting to `'images'` unless it's already been given an
166 explicit value. (That would occur if the HTML used an element like
167 `<div class="mkws-images" template="my-images">` to use a customised
175 Widget properties and methods
176 -----------------------------
178 The following properties and methods exist in the bare widget object
179 that is passed into `registerWidgetType`'s callback function, and can
180 be used by the derived widget.
182 * `String this.type` --
183 A string containing the type of the widget (`search`,
186 * `Team this.team` --
187 The team object to which this widget belongs. The team has
188 several additional important properties and methods, described
191 * `DOMElement this.node` --
192 The DOM element of the widget. Most often used for inserting
193 HTML into the widget element.
195 * `Hash this.config` --
196 A table of configuration values for the widget. This table
197 inherits missing values from the team's configuration, which
198 in turn inherits from the top-level MKWS configuration, which
199 inherits from the default configuration. Instances of widgets
200 in HTML can set configuration items as HTML attributes: for
201 example, the HTML element
202 `<div class="mkwsRecords" maxrecs="10">`
203 creates a widget for which `this.config.maxrecs` is set to 10.
205 * `String this.toString()` --
206 A function returning a string that briefly names this
207 widget. Can be useful in logging.
209 * `Void this.log(string)` --
210 A function to log a string for debugging purposes. The string
211 is written on the browser console, and also published to any
212 subcribers to the `log` event.
214 * `String this.value()` --
215 A function returning the value of the widget's HTML element.
217 * `VOID autosearch()` --
218 Registers that this kind of widget is one that requires an
219 automatic search to be run for it if an `autosearch` attribute
220 is provided on the HTML element. This is appropriate for
221 widgets such as `Records` and `Facet` that display some part
224 * `subwidget(type, overrides, defaults)` --
225 Returns the HTML of a subwidget of the specified type, which
226 can then be inserted into the widget using the
227 `this.node.html` function. The subwidget is given the same
228 attributes at the parent widget that invokes this function,
229 except where overrides are passed in. If defaults are also
230 provided, then these are used when the parent widget provides
231 no values. Both the `overrides` and `defaults` arguments are
232 hashes: the latter is optional.
234 See for example the `Credo` widget defined in the example
235 area's `mkws-widget-credo.js` file. This uses several
236 invocations of `subwidget` to create a complex compound widget
237 with numerous text, facet and image panes. FIXME: rename this
238 widget and everything related to it.
240 In addition to these properties and methods of the bare widget object,
241 some kinds of specific widget add other properties of their own. For
242 example, the `Builder` widget uses a `callback` property as the
243 function that it use to publish the widget definition that it
244 constructs. This defaults to the builtin function `alert`, but can be
245 overridden by derived widgets such as `ConsoleBuilder`.
251 Since the team object is supposed to be opaque to widgets, all access
252 is via the following API methods rather than direct access to
255 * `String team.name()`
256 * `Bool team.submitted()`
257 * `Num team.perpage()`
258 * `Num team.totalRecordCount()`
259 * `Num team.currentPage();`
260 * `String team.currentRecordId()`
261 * `String team.currentRecordData()`
263 These are all simple accessor functions that provide the ability to
264 read properties of the team.
266 * `Array team.filters()` --
267 Another accessor function, providing access to the array of
268 prevailing filters (which narrow the search results by means
269 of Pazpar2 filters and limits). This is really too complicated
270 an object for the widgets to be given access to, but it's
271 convenient to do it this way. If you must insist on using
272 this, see the `Navi` widget, which is the only place it's used.
274 * `Bool team.targetFiltered(targetId)` --
275 Indicates whether the specified target has been filtered by
276 selection as a facet. This is used only by the `Facet` widget,
277 and there is probably no reason for you to use it.
279 * `Hash team.config()` --
280 Access to the team's configuration settings. There is almost
281 certainly no reason to use this: the settings that haven't
282 been overridden are accessible via `this.config`.
284 * `Void team.set_sortOrder(string)`, `Void team.set_perpage(number)` --
285 "Setter" functions for the team's sortOrder and perpage
286 functions. Unlikely to be needed outside of the `Sort` and
289 * `Queue team.queue(eventName)` --
290 Returns the queue associated with the named event: this can be
291 used to subscribe to the event (or more rarely to publish it).
293 * `Void team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter)` --
294 Starts a new search with the specified parameters. All but the
295 query may be omitted, in which case the prevailing defaults
298 * `Void team.reShow()` --
299 Using the existing search, re-shows the result records after a
300 change in sort-order, per-page count, etc.
302 * `String team.recordElementId(recordId)` --
303 Utility function for converting a record identifer (returned
304 from Pazpar2) into a version suitable for use as an HTML
307 * `String team.renderDetails(recordData)` --
308 Utility function returns an HTML rendering of the record
309 represented by the specified data.
311 * `Template team.loadTemplate(templateName)` --
312 Loads (or retrieves from cache) the named Handlebars template,
313 and returns it in a form that can be invoked as a function,
316 Some of these methods either (A) are really too low-level and should
317 not be exposed, or (B) should be widget-level methods. The present
318 infelicities reflect the fact that some code that rightly belongs in
319 widgets is still in the team. When we finish migrating it, the widget
320 API should get simpler.
326 FIXME: list of events that can be usefully subscribed to.
331 Copyright (C) 2013-2014 Index Data ApS. <http://indexdata.com>