Ignore vim swap files.
[mkws-moved-to-github.git] / doc / mkws-developer.txt
1 INTRODUCTION
2 ============
3
4 Development with MKWS consists primarily of defining new types of
5 widgets. These can interact with the core functionality is several
6 defined ways.
7
8 You create a new widget type by calling the mkws.registerWidgetType
9 function, passing in the widget name and a function. The name is used
10 to recognise HTML elements as being widgets of this type -- for
11 example, if you register a "Foo" widget, elements like <div
12 class="mkwsFoo"> will be widgets of this type.
13
14 The function promotes a bare widget object (passed as `this') into a
15 widget of the appropriate type. MKWS doesn't use classes or explicit
16 prototypes: it just makes objects that have the necessary
17 behaviours. Widgets have *no* behaviours that they have to provide:
18 you can make a doesn't-do-anything-at-all widget if you like:
19
20         mkws.registerWidgetType('Sluggard', function() {});
21
22 More commonly, widgets will subscribe to one or more events, so that
23 they're notified when something interesting happens. For example, the
24 "Log" widget asks to be notified when a "log" event happens, and
25 appends the logged message to its node, as follows:
26
27         mkws.registerWidgetType('Log', function() {
28             var that = this;
29
30             this.team.queue("log").subscribe(function(teamName, timestamp, message) {
31                 $(that.node).append(teamName + ": " + timestamp + message + "<br/>");
32             });
33         });
34
35 This simple widget illustrates several important points:
36
37 * The base widget object (`this') has several baked-in properties and
38   methods that are available to individual widgets. These include
39   this.team (the team that this widget is a part of) and this.node
40   (the DOM element of the widget).
41
42 * The team object (`this.team') also has baked-in properties and
43   methods. These include the queue function, which takes an event-name
44   as its argument. It's possible to subscribe to an event's queue
45   using this.team.queue("EVENT").subscribe. The argument is a function
46   which is called whenever the event is published. The arguments to
47   the function are different for different events.
48
49 * The value of `this' is lost inside the subscribe callback, so it
50   must be saved if it's to be used inside that callback (typically as
51   a local variable named `that').
52
53
54 SPECIALISATION (INHERITANCE)
55 ============================
56
57 Many widgets are simple specialisations of existing widgets. For
58 example, the "Record" widget is the same as the "Records" widget
59 except that it defaults to displaying a single record. It's defined as
60 follows:
61
62         mkws.registerWidgetType('Record', function() {
63             mkws.promotionFunction('Records').call(this);
64             if (!this.config.maxrecs) this.config.maxrecs = 1;
65         });
66
67 Remember that when a promotion function is called, it's passed a base
68 widget object that's not specialised for any particular task. To make
69 a specialised widget, first promote that base widget into the type
70 that you want to specialise from -- in this case, "Records" -- using
71 the promotion function that's been registered for that type.
72
73 Once this has been done, the specialisations can be introduced. In
74 this case, it's a very simple matter of changing the "maxrecs"
75 configuration setting to 1 unless it's already been given an explicit
76 value. (That would occur if the HTML used an element like <div
77 class="mkwsRecord" maxrecs="2">, though it's not obvious why anyone
78 would do that.)
79
80
81 WIDGET PROPERTIES AND METHODS
82 =============================
83
84 String this.type
85         A string containing the type of the widget.
86
87 Team this.team
88         The team object to which this widget belongs. The team has
89         several additional important properties and methods, described
90         below.
91
92 DOMElement this.node
93         The DOM element of the widget
94
95 Hash this.config
96         A table of configuration values for the widget. This table
97         inherits missing values from the team's configuration, which
98         in turn inherits from the top-level MKWS configuration, which
99         inherits from the default configuration. Instances of widgets
100         in HTML can set configuration items as HTML attributes, as in
101         <div class="mkwsRecords" maxrecs="2">.
102
103 String this.toString()
104         A function returning a string that briefly names this
105         widget. Can be useful in logging.
106
107 Void this.log(string)
108         A function to log a string for debugging purposes. The string
109         is written on the browser console, and also published to any
110         "log" subcribers.
111
112 String this.value()
113         A function returning the value of the widget's HTML element.
114
115
116 TEAM METHODS
117 ============
118
119 Since the team object is supposed to be opaque to widgets, all access
120 is via the following API methods rather than direct access to
121 properties.
122
123 String team.name()
124 Bool team.submitted()
125 Num team.perpage()
126 Num team.totalRecordCount()
127 Num team.currentPage();
128 String team.currentRecordId()
129 String team.currentRecordData()
130         Simple accessor functions that provide the ability to read
131         properties of the team.
132
133 Array team.filters()
134         Another accessor function, providing access to the array of
135         prevailing filters (which narrow the search results by means
136         of Pazpar2 filters and limits). This is really too complicated
137         an object for the widgets to be given access to, but it's
138         convenient to do it this way. See the "Navi" widget, which is
139         the only place it's used.
140
141 Hash team.config()
142         Access to the team's configuration settings. There is almost
143         certainly no reason to use this: the settings that haven't
144         been overridden are accessible via this.config.
145
146 Void team.set_sortOrder(string)
147 Void team.set_perpage(number)
148         "Setter" functions for the team's sortOrder and perpage
149         functions. Unlikely to be needed outside of the "Sort" and
150         "Perpage" widgets.
151
152 Queue team.queue(eventName)
153         Returns the queue associated with the named event: this can be
154         used to subscribe to the event (or more rarely to publish it).
155
156 Bool team.targetFiltered(targetId)
157         Indicates whether the specified target has been filtered by
158         selection as a facet.
159
160 Void team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter)
161         Starts a new search with the specified parameters. All but the
162         query may be omitted, in which case the prevailing defaults
163         are used.
164
165 Void team.reShow()
166         Using the existing search, re-shows the result records after a
167         change in sort-order, per-page count, etc.
168
169 String team.recordElementId(recordId)
170         Utility function for converting a record identifer (returned
171         from Pazpar2) into a version suitable for use as an HTML
172         element ID.
173
174 String team.renderDetails(recordData)
175         Utility function returns an HTML rendering of the record
176         represented by the specified data.
177
178 Template team.loadTemplate(templateName)
179         Loads (or retrieves from cache) the named Handlebars template,
180         and returns it in a form that can be invoked as a function,
181         passed a data-set.
182
183 Some of these methods either (A) are really too low-level and should
184 not be exposed, or (B) should be widget-level methods. The present
185 infelicities reflect the fact that some code that rightly belongs in
186 widgets is still in the team. When we finish migrating it, the widget
187 API should get simpler.
188