1 % Embedded metasearching with the MasterKey Widget Set
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.
17 Index Data provides several different toolkits for communicating with
18 its metasearching middleware, trading off varying degrees of
19 flexibility against convenience:
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.
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.
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/
34 * MKDru -- a toolkit for embedding MasterKey-like searching into
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.
49 The following is a complete MKWS-based searching application:
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" />
59 <div id="mkwsSearch"></div>
60 <div id="mkwsResults"></div>
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
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.
79 These fall into two categories. First, the prerequisites in the HTML
80 header, which are loaded from the tool site mkws.indexdata.com:
83 contains all the JavaScript needed by the widget-set.
86 provides the default CSS styling
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:
94 * `mkwsSearch` -- provides the search box and button.
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.
101 * `mkwsLang` -- provides links to switch between one of several
102 different UI languages. By default, English, Danish and German are
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.
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.
113 * `mkwsStat` --provides a status line summarising the statistics of
116 To see all of these working together, just put them all into the HTML
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>
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
135 <script type="text/javascript">
138 sort_default: "title",
142 <script type="text/javascript" src="http://mkws.indexdata.com/mkws-complete.js"></script>
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.
149 The full set of supported configuration items is described in the
150 reference guide below.
153 Control over HTML and CSS
154 -------------------------
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.
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:
166 * `mkwsTermlists` -- provides the facets
168 * `mkwsRanking` -- provides the options for how records are sorted and
169 how many are included on each page of results.
171 * `mkwsPager` -- provides the links for navigating back and forth
172 through the pages of records.
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.
178 * `mkwsRecords` -- lists the actual result records.
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.
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.
197 ### Message of the day
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
207 ### Customised display using Handlebars templates
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:
213 * `mkwsTemplate_Summary` -- used for each summary record in a list of
216 * `mkwsTemplate_Record` -- used when displaying a full record.
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.
225 For example, an application can install a simple author+title summary
226 record in place of the usual one providing the following template:
228 <script class="mkwsTemplate_Summary" type="text/x-handlebars-template">
230 <span>{{md-author}}</span>
232 <a href="#" id="{{_id}}" onclick="{{_onclick}}">
237 For details of Handlebars template syntax, see
238 [the online documentation](http://handlebarsjs.com/).
241 ### Responsive design
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.
249 To turn on this behaviour, set the `responsive_design_width` to the desired
250 threshhold width in pixels. For example:
252 <script type="text/javascript">
254 responsive_design_width: 990
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.
267 ### Popup results with jQuery UI
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:
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>
284 The necessary scaffolding can be seen in an example application,
285 http://example.indexdata.com/index-popup.html
288 ### Authentication and target configuration
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.
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). But in order to gain access to those resources, the
301 authentication tokens have to be available to the widgets in some way,
302 and simple embedding them in the JavaScript configuration is not
303 acceptable because they are easy to read from there.
305 The solution to this problem is in three steps.
308 the application's web-server creates a rewriting rule that takes an
310 http://example.indexdata.com/service-proxy-auth/
311 and rewrites it as an access to Index Data's authentication service
312 with authentication credentials embedded. This can be done using
313 Apache2 directives such as
316 RewriteRule /service-proxy-auth/
317 http://mkws.indexdata.com/service-proxy/?command=auth&action=login&username=U&password=PW [P]
319 Because the credentials appear only in the application's web-server
320 configuration, they are not visible to malicious users.
322 <b>Second</b>, the broader application that includes MKWS widgets must
323 protect access to the authentication URL on its own web-server. This
324 can be done using IP authentication, a local username/password scheme,
325 Kerberos or any other means.
327 <b>Third</b>, the MKWS application must be configured to use the
328 application-hosted authentication URL instead of the default one. This
329 is done by means of the `service_proxy_auth` configuration element,
330 which should be set to the authentication URL.
332 Once these three steps are taken, the MKWS application will
333 authenticate by means of a special URL on the application's web
334 server, which the application prevents unauthorised access to, and the
335 underlying credentials are hidden.
341 ### Configuration object
343 The configuration object `mkws_config` may be created before including
344 the MKWS JavaScript code to modify default behaviour. This structure
345 is a key-value lookup table, whose entries are described in the table
346 below. All entries are optional, but if specified must be given values
347 of the specified type. If ommitted, each setting takes the indicated
348 default value; long default values are in footnotes to keep the table
352 Element Type Default Description
353 -------- ----- --------- ------------
354 debug_level int 1 Level of debugging output to emit. 0 = none, 1 = messages, 2 = messages with
355 datestamps, 3 = messages with datestamps and stack-traces.
357 facets array *Note 1* Ordered list of names of facets to display. Supported facet names are
358 `xtargets`, `subject` and `author`.
360 lang string en Code of the default language to display the UI in. Supported language codes are `en` =
361 English, `de` = German, `da` = Danish, and whatever additional languages are configured
362 using `language_*` entries (see below).
364 lang_options array [] A list of the languages to offer as options. If empty (the default), then all
365 configured languages are listed.
367 language_* hash Support for any number of languages can be added by providing entries whose name is
368 `language_` followed by the code of the language. See the separate section below for
371 pazpar2_url string *Note 2* The URL used to access the metasearch middleware. This service must be configured to
372 provide search results, facets, etc. It may be either unmediated or Pazpar2 the
373 MasterKey Service Proxy, which mediates access to an underlying Pazpar2 instance. In
374 the latter case, `service_proxy_auth` must be provided.
376 perpage_default string 20 The initial value for the number of records to show on each page.
378 perpage_options array *Note 3* A list of candidate page sizes. Users can choose between these to determine how many
379 records are displayed on each page of results.
381 query_width int 50 The width of the query box, in characters.
383 responsive_design_width int If defined, then the facets display moves between two locations as the screen-width
384 varies, as described above. The specified number is the threshhold width, in pixels,
385 at which the facets move between their two locations.
387 service_proxy_auth url *Note 4* A URL which, when `use_service_proxy` is true, is fetched once at the beginning of each
388 session to authenticate the user and establish a session that encompasses a defined set
389 of targets to search in.
391 service_proxy_auth_domain domain Can be set to the domain for which `service_proxy_auth` proxies authentication, so
392 that cookies are rewritten to appear to be from this domain. In general, this is not
393 necessary, as this setting defaults to the domain of `pazpar2_url`.
395 show_lang bool true Indicates whether or not to display the language menu.
397 show_perpage bool true Indicates whether or not to display the perpage menu.
399 show_sort bool true Indicates whether or not to display the sort menu.
401 show_switch bool true Indicates whether or not to display the switch menu, for switching between showing
402 retrieved records and target information.
404 sort_default string relevance The label of the default sort criterion to use. Must be one of those in the `sort`
407 sort_options array *Note 6* List of supported sort criteria. Each element of the list is itself a two-element list:
408 the first element of each sublist is a pazpar2 sort-expression such as `data:0` and
409 the second is a human-readable label such as `newest`.
411 use_service_proxy bool true If true, then a Service Proxy is used to deliver searching services rather than raw
415 Perhaps we should get rid of the `show_lang`, `show_perpage`,
416 `show_sort` and `show_switch` configuration items, and simply display the relevant menus
417 only when their containers are provided -- e.g. an `mkwsLang` element
418 for the language menu. But for now we retain these, as an easier route
419 to lightly customise the display than my changing providing a full HTML
424 1. ["sources", "subjects", "authors"]
426 2. /pazpar2/search.pz2
430 4. http://mkws.indexdata.com/service-proxy-auth
432 5. http://mkws.indexdata.com/service-proxy/
434 6. [["relevance"], ["title:1", "title"], ["date:0", "newest"], ["date:1", "oldest"]]
437 ### Language specification
439 Support for another UI language can be added by providing an entry in
440 the `mkws_config` object whose name is `language_` followed by the
441 name of the language: for example, `language_French` to support
442 French. Then value of this entry must be a key-value lookup table,
443 mapping the English-language strings of the UI into their equivalents
444 in the specified language. For example:
448 "Authors": "Auteurs",
449 "Subjects": "Sujets",
450 // ... and others ...
454 The following strings occurring in the UI can be translated:
470 In addition, facet names can be translated:
476 Finally, the names of fields in the full-record display can be
477 translated. These include, but may not be limited to:
487 ### jQuery UI popup invocation
489 The MasterKey Widget Set can be invoked in a popup window on top of the page.
491 Note that when using the `popup` layout, facilities from the jQuery UI
492 toolkit are used, so it's necessary to include both CSS and JavaScript
493 from that toolkit. The relevant lines are:
495 <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
496 <link rel="stylesheet" type="text/css"
497 href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
499 <div class="mkwsSearch"></div>
500 <div class="mkwsPopup" popup_width="1024" popup_height="650" popup_modal="0" popup_autoOpen="0" popup_button="input.mkwsButton">
501 <div class="mkwsSwitch"></div>
502 <div class="mkwsLang"></div>
503 <div class="mkwsResults"></div>
504 <div class="mkwsTargets"></div>
505 <div class="mkwsStat"></div>
509 Element Type Default Description
510 -------- ----- --------- ------------
511 popup_width string 880 Width of the popup window (if used), in
514 popup_height string 760 Height of the popup window (if used), in
517 popup_button string input.mkwsButton (Never change this.)
519 popup_modal string 0 Modal confirmation mode. Valid values are 0 or 1
521 popup_autoOpen string 1 Open popup window on load. Valid values are 0 or 1
526 ### The structure of the HTML generated by the MKWS widgets
528 In order to override the default CSS styles provided by the MasterKey Widget
529 Set, it's necessary to understand that structure of the HTML elements that are
530 generated within the components. This knowledge make it possible, for example,
531 to style each `<div>` with class `term` but only when it occurs inside an
532 element with ID `#mkwsTermlists`, so as to avoid inadvertently styling other
533 elements using the same class in the non-MKWS parts of the page.
535 The HTML structure is as follows. As in CSS, #ID indicates a unique identifier
536 and .CLASS indicates an instance of a class.
546 input#mkwsQuery type=text
547 input#mkwsButton type=submit
550 (no contents -- used only for masking)
571 span (for sequence number)
573 span (for other information such as author)
574 div.details (sometimes)
597 Copyright (C) 2013-2014 by IndexData ApS, <http://www.indexdata.com>