Remove (redundant and outdated) section on authentication.
[mkws-moved-to-github.git] / doc / whitepaper.markdown
1 % Embedded metasearching with the MasterKey Widget Set
2 % Mike Taylor
3
4
5 Introduction
6 ------------
7
8 There are lots of practical problems in building resource discovery
9 solutions. One of the biggest, and most ubiquitous is incorporating
10 metasearching functionality into existing web-sites -- for example,
11 content-management systems, library catalogues or intranets. In
12 general, even when access to core metasearching functionality is
13 provided by simple web-services such as
14 [Pazpar2](http://www.indexdata.com/pazpar2), integration work is seen
15 as a major part of most projects.
16
17 Index Data provides several different toolkits for communicating with
18 its metasearching middleware, trading off varying degrees of
19 flexibility against convenience:
20
21 * pz2.js -- a low-level JavaScript library for interrogating the
22   Service Proxy and Pazpar2. It allows the HTML/JavaScript programmer
23   to create JavaScript applications display facets, records, etc. that
24   are fetched from the metasearching middleware.
25
26 * masterkey-ui-core -- a higher-level, complex JavaScript library that
27   uses pz2.js to provide the pieces needed for building a
28   full-featured JavaScript application.
29
30 * MasterKey Demo UI -- an example of a searching application built on
31   top of masterkey-ui-core. Available as a public demo at
32   http://mk2.indexdata.com/
33
34 * MKDru -- a toolkit for embedding MasterKey-like searching into
35   Drupal sites.
36
37 All of these approaches require programming to a greater or lesser
38 extent. Against this backdrop, we introduced MKWS (the MasterKey
39 Widget Set) -- a set of simple, very high-level HTML+CSS+JavaScript
40 components that can be incorporated into any web-site to provide
41 MasterKey searching facilities. By placing `<div>`s with well-known
42 identifiers in any HTML page, the various components of an application
43 can be embedded: search-boxes, results areas, target information, etc.
44
45
46 Simple Example
47 --------------
48
49 The following is a complete MKWS-based searching application:
50
51     <html>
52       <head>
53         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
54         <title>MKWS demo client</title>
55         <script type="text/javascript" src="http://mkws.indexdata.com/mkws-complete.js"></script>
56         <link rel="stylesheet" href="http://mkws.indexdata.com/mkws.css" />
57       </head>
58       <body>
59         <div id="mkwsSearch"></div>
60         <div id="mkwsResults"></div>
61       </body>
62     </html>
63
64 Go ahead, try it! You don't even need a web-server. Just copy and
65 paste this HTML into a file on your computer -- `/tmp/magic.html`,
66 say -- and point your web-browser at it:
67 `file:///tmp/magic.html`. Just like that, you have working
68 metasearching.
69
70
71 How the example works
72 ---------------------
73
74 If you know any HTML, the structure of the file will be familar to
75 you: the `<html>` element at the top level contains a `<head>` and a
76 `<body>`. In addition to whatever else you might want to put on your
77 page, you can add MKWS elements.
78
79 These fall into two categories. First, the prerequisites in the HTML
80 header, which are loaded from the tool site mkws.indexdata.com:
81
82 * `mkws-complete.js`
83   contains all the JavaScript needed by the widget-set.
84
85 * `mkws.css`
86   provides the default CSS styling
87
88 Second, within the HTML body, `<div>` elements with special IDs that
89 begin `mkws` can be provided. These are filled in by the MKWS code,
90 and provide the components of the searching UI. The very simple
91 application above has only two such components: a search box and a
92 results area. But more are supported. The main `<div>`s are:
93
94 * `mkwsSearch` -- provides the search box and button.
95
96 * `mkwsResults` -- provides the results area, including a list of
97    brief records (which open out into full versions when clicked),
98    paging for large results sets, facets for refining a search,
99    sorting facilities, etc.
100
101 * `mkwsLang` -- provides links to switch between one of several
102    different UI languages. By default, English, Danish and German are
103    provided.
104
105 * `mkwsSwitch` -- provides links to switch between a view of the
106    result records and of the targets that provide them. Only
107    meaningful when `mkwsTargets` is also provided.
108
109 * `mkwsTargets` -- the area where per-target information will appear
110    when selected by the link in the `mkwsSwitch` area. Of interest
111    mostly for fault diagnosis rather than for end-users.
112
113 * `mkwsStat` --provides a status line summarising the statistics of
114    the various targets.
115
116 To see all of these working together, just put them all into the HTML
117 `<body>` like so:
118
119         <div id="mkwsSwitch"></div>
120         <div id="mkwsLang"></div>
121         <div id="mkwsSearch"></div>
122         <div id="mkwsResults"></div>
123         <div id="mkwsTargets"></div>
124         <div id="mkwsStat"></div>
125
126 Configuration
127 -------------
128
129 Many aspects of the behaviour of MKWS can be modified by setting
130 parameters into the `mkws_config` object. **This must be done *before*
131 including the MKWS JavaScript** so that when that code is executed it
132 can refer to the configuration values. So the HTML header looks like
133 this:
134
135         <script type="text/javascript">
136           var mkws_config = {
137             lang: "da",
138             sort_default: "title",
139             query_width: 60
140           };
141         </script>
142         <script type="text/javascript" src="http://mkws.indexdata.com/mkws-complete.js"></script>
143
144 This configuration sets the UI language to Danish (rather than the
145 default of English), initially sorts search results by title rather
146 than relevance (though as always this can be changed in the UI) and
147 makes the search box a bit wider than the default.
148
149 The full set of supported configuration items is described in the
150 reference guide below.
151
152
153 Control over HTML and CSS
154 -------------------------
155
156 More sophisticated applications will not simply place the `<div>`s
157 together, but position them carefully within an existing page
158 framework -- such as a Drupal template, an OPAC or a SharePoint page.
159
160 While it's convenient for simple applications to use a monolithic
161 `mkwsResults` area which contains record, facets, sorting options,
162 etc., customised layouts may wish to treat each of these components
163 separately. In this case, `mkwsResults` can be omitted, and the
164 following lower-level components provided instead:
165
166 * `mkwsTermlists` -- provides the facets
167
168 * `mkwsRanking` -- provides the options for how records are sorted and
169    how many are included on each page of results.
170
171 * `mkwsPager` -- provides the links for navigating back and forth
172    through the pages of records.
173
174 * `mkwsNavi` -- when a search result has been narrowed by one or more
175    facets, this area shows the names of those facets, and allows the
176    selected values to be clicked in order to remove them.
177
178 * `mkwsRecords` -- lists the actual result records.
179
180 Customisation of MKWS searching widgets can also be achieved by
181 overriding the styles set in the toolkit's CSS stylesheet. The default
182 styles can be inspected in `mkws.css` and overridden in any
183 styles that appears later in the HTML than that file. At the simplest
184 level, this might just mean changing fonts, sizes and colours, but
185 more fundamental changes are also possible.
186
187 To properly apply styles, it's necessary to understand how the HTML is
188 structured, e.g. which elements are nested within which
189 containers. The structures used by the widget-set are described in the
190 reference guide below.
191
192
193 Refinements
194 -----------
195
196
197 ### Message of the day
198
199 Some applications might like to open with content in the area that
200 will subsequently be filled with result-records -- a message of the
201 day, a welcome message or a help page. This can be done by placing an
202 `mkwsMOTD` division anywhere on the page. It will be moved into the
203 `mkwsResults` area and initially displayed, but will be hidden when a
204 search is made.
205
206
207 ### Customised display using Handlebars templates
208
209 Certain aspects of the widget-set's display can be customised by
210 providing Handlebars templates with well-known classes that begin with
211 the string `mkwsTemplate_`. At present, the supported templates are:
212
213 * `mkwsTemplate_Summary` -- used for each summary record in a list of
214   results.
215
216 * `mkwsTemplate_Record` -- used when displaying a full record.
217
218 For both of these the metadata record is passed in, and its fields can
219 be referenced in the template. As well as the metadata fields
220 (`md-*`), two special fields are provided to the `mkwsTemplate_Summary`
221 template, for creating popup links for full records. These are `_id`,
222 which must be provided as the `id` attribute of a link tag, and
223 `_onclick`, which must be provided as the `onclick` attribute.
224
225 For example, an application can install a simple author+title summary
226 record in place of the usual one providing the following template:
227
228         <script class="mkwsTemplate_Summary" type="text/x-handlebars-template">
229           {{#if md-author}}
230             <span>{{md-author}}</span>
231           {{/if}}
232           <a href="#" id="{{_id}}" onclick="{{_onclick}}">
233             <b>{{md-title}}</b>
234           </a>
235         </script>
236
237 For details of Handlebars template syntax, see
238 [the online documentation](http://handlebarsjs.com/).
239
240
241 ### Responsive design
242
243 Metasearching applications may need to appear differently on
244 small-screened mobile devices, or change their appearance when
245 screen-width changes (as when a small device is rotated). To achieve
246 this, MKWS supports responsive design which will move the termlists to
247 the bottom on narrow screens and to the sidebar on wide screens.
248
249 To turn on this behaviour, set the `responsive_design_width` to the desired
250 threshhold width in pixels. For example:
251
252         <script type="text/javascript">
253             var mkws_config = {
254                 responsive_design_width: 990
255             };
256         </script>
257
258 If individual result-related components are in use in place of the
259 all-in-one mkwsResults, then the redesigned application needs to
260 specify the locations where the termlists should appear in both
261 cases. In this case, wrap the wide-screen `mkwsTermlists` element in a
262 `mkwsTermlists-Container-wide` element; and provide an
263 `mkwsTermlists-Container-narrow` element in the place where the narrow-screen
264 termlists should appear.
265
266
267 ### Popup results with jQuery UI
268
269 The [jQuery UI library](http://en.wikipedia.org/wiki/JQuery_UI)
270 can be used to construct MKWS applications in which the only component
271 generally visible on the page is a search box, and the results appear
272 in a popup. The key part of such an application is this invocation of
273 the MKWS jQuery plugin:
274
275         <div class="mkwsSearch"></div>
276         <div class="mkwsPopup" popup_width="1024" popup_height="650" popup_modal="0" popup_autoOpen="0" popup_button="input.mkwsButton">
277           <div class="mkwsSwitch"></div>
278           <div class="mkwsLang"></div>
279           <div class="mkwsResults"></div>
280           <div class="mkwsTargets"></div>
281           <div class="mkwsStat"></div>
282         </div>
283
284 The necessary scaffolding can be seen in an example application,
285 http://example.indexdata.com/index-popup.html
286
287
288 ### Authentication and target configuration
289
290 By default, MKWS configures itself to use a demonstration account on a
291 service hosted by mkws.indexdata.com. This account (username `demo`,
292 password `demo`) provides access to about a dozen free data
293 sources. Authentication onto this service is via an authentication URL
294 on the same MKWS server, so no explicit configuration is needed.
295
296 In order to search in a customised set of targets, including
297 subscription resources, it's necessary to create an account with
298 Index Data's hosted service proxy, and protect that account with
299 authentication tokens (to prevent unauthorised use of subscription
300 resources). For information on how to do this, see
301 [MKWS Target Selection](library-configuration.html)
302
303
304 Reference Guide
305 ---------------
306
307 ### Configuration object
308
309 The configuration object `mkws_config` may be created before including
310 the MKWS JavaScript code to modify default behaviour. This structure
311 is a key-value lookup table, whose entries are described in the table
312 below. All entries are optional, but if specified must be given values
313 of the specified type. If ommitted, each setting takes the indicated
314 default value; long default values are in footnotes to keep the table
315 reasonably narrow.
316
317 ---
318 Element                   Type    Default   Description
319 --------                  -----   --------- ------------
320 debug_level               int     1         Level of debugging output to emit. 0 = none, 1 = messages, 2 = messages with
321                                             datestamps, 3 = messages with datestamps and stack-traces.
322
323 facets                    array   *Note 1*  Ordered list of names of facets to display. Supported facet names are
324                                             `xtargets`, `subject` and `author`.
325
326 lang                      string  en        Code of the default language to display the UI in. Supported language codes are `en` =
327                                             English, `de` = German, `da` = Danish, and whatever additional languages are configured
328                                             using `language_*` entries (see below).
329
330 lang_options              array   []        A list of the languages to offer as options. If empty (the default), then all
331                                             configured languages are listed.
332
333 language_*                hash              Support for any number of languages can be added by providing entries whose name is
334                                             `language_` followed by the code of the language. See the separate section below for
335                                             details.
336
337 pazpar2_url               string  *Note 2*  The URL used to access the metasearch middleware. This service must be configured to
338                                             provide search results, facets, etc. It may be either unmediated or Pazpar2 the
339                                             MasterKey Service Proxy, which mediates access to an underlying Pazpar2 instance. In
340                                             the latter case, `service_proxy_auth` must be provided.
341
342 perpage_default           string  20        The initial value for the number of records to show on each page.
343
344 perpage_options           array   *Note 3*  A list of candidate page sizes. Users can choose between these to determine how many
345                                             records are displayed on each page of results.
346
347 query_width               int     50        The width of the query box, in characters.
348
349 responsive_design_width   int               If defined, then the facets display moves between two locations as the screen-width
350                                             varies, as described above. The specified number is the threshhold width, in pixels,
351                                             at which the facets move between their two locations.
352
353 service_proxy_auth        url     *Note 4*  A URL which, when `use_service_proxy` is true, is fetched once at the beginning of each
354                                             session to authenticate the user and establish a session that encompasses a defined set
355                                             of targets to search in.
356
357 service_proxy_auth_domain domain            Can be set to the domain for which `service_proxy_auth` proxies authentication, so
358                                             that cookies are rewritten to appear to be from this domain. In general, this is not
359                                             necessary, as this setting defaults to the domain of `pazpar2_url`.
360
361 show_lang                 bool    true      Indicates whether or not to display the language menu.
362
363 show_perpage              bool    true      Indicates whether or not to display the perpage menu.
364
365 show_sort                 bool    true      Indicates whether or not to display the sort menu.
366
367 show_switch               bool    true      Indicates whether or not to display the switch menu, for switching between showing
368                                             retrieved records and target information.
369
370 sort_default              string  relevance The label of the default sort criterion to use. Must be one of those in the `sort`
371                                             array.
372
373 sort_options              array   *Note 6*  List of supported sort criteria. Each element of the list is itself a two-element list:
374                                             the first element of each sublist is a pazpar2 sort-expression such as `data:0` and
375                                             the second is a human-readable label such as `newest`.
376
377 use_service_proxy         bool    true      If true, then a Service Proxy is used to deliver searching services rather than raw
378                                             Pazpar2.
379 ---
380
381 Perhaps we should get rid of the `show_lang`, `show_perpage`,
382 `show_sort` and `show_switch` configuration items, and simply display the relevant menus
383 only when their containers are provided -- e.g. an `mkwsLang` element
384 for the language menu. But for now we retain these, as an easier route
385 to lightly customise the display than my changing providing a full HTML
386 structure.
387
388 #### Notes
389
390 1. ["sources", "subjects", "authors"]
391
392 2. /pazpar2/search.pz2
393
394 3. [10, 20, 30, 50]
395
396 4. http://mkws.indexdata.com/service-proxy-auth
397
398 5. http://mkws.indexdata.com/service-proxy/
399
400 6. [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]]
401
402
403 ### Language specification
404
405 Support for another UI language can be added by providing an entry in
406 the `mkws_config` object whose name is `language_` followed by the
407 name of the language: for example, `language_French` to support
408 French. Then value of this entry must be a key-value lookup table,
409 mapping the English-language strings of the UI into their equivalents
410 in the specified language. For example:
411
412             var mkws_config = {
413               language_French: {
414                 "Authors": "Auteurs",
415                 "Subjects": "Sujets",
416                 // ... and others ...
417               }
418             }
419
420 The following strings occurring in the UI can be translated:
421 `Displaying`,
422 `Next`,
423 `Prev`,
424 `Records`,
425 `Search`,
426 `Sort by`,
427 `Targets`,
428 `Termlists`,
429 `and show`,
430 `found`,
431 `of`,
432 `per page`
433 and
434 `to`.
435
436 In addition, facet names can be translated:
437 `Authors`,
438 `Sources`
439 and
440 `Subjects`.
441
442 Finally, the names of fields in the full-record display can be
443 translated. These include, but may not be limited to:
444 `Author`,
445 `Date`,
446 `Location`,
447 `Subject`
448 and
449 `Title`.
450
451
452
453 ### jQuery UI popup invocation
454
455 The MasterKey Widget Set can be invoked in a popup window on top of the page.
456
457 Note that when using the `popup` layout, facilities from the jQuery UI
458 toolkit are used, so it's necessary to include both CSS and JavaScript
459 from that toolkit. The relevant lines are:
460
461     <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
462     <link rel="stylesheet" type="text/css"
463           href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
464
465     <div class="mkwsSearch"></div>
466     <div class="mkwsPopup" popup_width="1024" popup_height="650" popup_modal="0" popup_autoOpen="0" popup_button="input.mkwsButton">
467       <div class="mkwsSwitch"></div>
468       <div class="mkwsLang"></div>
469       <div class="mkwsResults"></div>
470       <div class="mkwsTargets"></div>
471       <div class="mkwsStat"></div>
472     </div
473
474 ---
475 Element    Type    Default           Description
476 --------   -----   ---------         ------------
477 popup_width     string     880       Width of the popup window (if used), in
478                                      pixels.
479
480 popup_height    string     760       Height of the popup window (if used), in
481                                      pixels.
482
483 popup_button    string      input.mkwsButton  (Never change this.)
484
485 popup_modal     string      0       Modal confirmation mode. Valid values are 0 or 1
486
487 popup_autoOpen  string      1       Open popup window on load. Valid values are 0 or 1
488
489 ---
490
491
492 ### The structure of the HTML generated by the MKWS widgets
493
494 In order to override the default CSS styles provided by the MasterKey Widget
495 Set, it's necessary to understand that structure of the HTML elements that are
496 generated within the components. This knowledge make it possible, for example,
497 to style each `<div>` with class `term` but only when it occurs inside an
498 element with ID `#mkwsTermlists`, so as to avoid inadvertently styling other
499 elements using the same class in the non-MKWS parts of the page.
500
501 The HTML structure is as follows. As in CSS, #ID indicates a unique identifier
502 and .CLASS indicates an instance of a class.
503
504     #mkwsSwitch
505       a*
506
507     #mkwsLang
508       ( a | span )*
509
510     #mkwsSearch
511       form
512         input#mkwsQuery type=text
513         input#mkwsButton type=submit
514
515     #mkwsBlanket
516       (no contents -- used only for masking)
517
518     #mkwsResults
519       table
520         tbody
521           tr
522             td
523               #mkwsTermlists
524                 div.title
525                 div.facet*
526                   div.termtitle
527                   ( a span br )*
528             td
529               div#mkwsRanking
530                 form#mkwsSelect
531                   select#mkwsSort
532                   select#mkwsPerpage
533               #mkwsPager
534               #mkwsNavi
535               #mkwsRecords
536                 div.record*
537                   span (for sequence number)
538                   a (for title)
539                   span (for other information such as author)
540                   div.details (sometimes)
541                     table
542                       tbody
543                         tr*
544                           th
545                           td
546     #mkwsTargets
547       #mkwsBytarget
548         table
549           thead
550             tr*
551               td*
552           tbody
553             tr*
554               td*
555
556     #mkwsStat
557       span.head
558       span.clients
559       span.records
560
561 - - -
562
563 Copyright (C) 2013-2014 by IndexData ApS, <http://www.indexdata.com>