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