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