pazpar2() extension uses an empty config object if nothing is passed
[mkws-moved-to-github.git] / tools / htdocs / mkws-jquery.js
1 /*! jQuery plugin for MKWS, the MasterKey Widget Set.
2  *  Copyright (C) 2013-2014 Index Data
3  *  See the file LICENSE for details
4  */
5
6 "use strict";
7
8
9 /*
10  * implement jQuery plugin $.pazpar2({})
11  */
12 function _mkws_jquery_plugin ($) {
13     function debug (string) {
14         mkws.debug("jquery.pazpar2: " + string);
15     }
16
17     function init_popup(obj) {
18         var config = obj ? obj : {};
19
20         var height = config.height || 760;
21         var width = config.width || 880;
22         var id_button = config.id_button || "input.mkwsButton";
23         var id_popup = config.id_popup || "#mkwsPopup";
24
25         debug("popup height: " + height + ", width: " + width);
26
27         // make sure that jquery-ui was loaded afte jQuery core lib, e.g.:
28         // <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
29         if (!$.ui) {
30             debug("Error: jquery-ui.js is missing, did you include it after jQuery core in the HTML file?");
31             return;
32         }
33
34         $(id_popup).dialog({
35             closeOnEscape: true,
36             autoOpen: false,
37             height: height,
38             width: width,
39             modal: true,
40             resizable: true,
41             buttons: {
42                 Cancel: function() {
43                     $(this).dialog("close");
44                 }
45             },
46             close: function() { }
47         });
48
49         $(id_button)
50             .button()
51             .click(function() {
52                 $(id_popup).dialog("open");
53             });
54     };
55
56     $.extend({
57
58         // service-proxy or pazpar2
59         pazpar2: function(config) {
60             if (!config) config = {};
61             var id_popup = config.id_popup || "#mkwsPopup";
62             id_popup = id_popup.replace(/^#/, "");
63
64             // simple layout
65             var div = '\
66 <div id="mkwsSwitch"></div>\
67 <div id="mkwsLang"></div>\
68 <div id="mkwsSearch"></div>\
69 <div id="mkwsResults"></div>\
70 <div id="mkwsTargets"></div>\
71 <div id="mkwsStat"></div>';
72
73             // new table layout
74             var table = '\
75 <style type="text/css">\
76   #mkwsTermlists div.facet {\
77   float:left;\
78   width: 30%;\
79   margin: 0.3em;\
80   }\
81   #mkwsStat {\
82   text-align: right;\
83   }\
84 </style>\
85     \
86 <table width="100%" border="0">\
87   <tr>\
88     <td>\
89       <div id="mkwsSwitch"></div>\
90       <div id="mkwsLang"></div>\
91       <div id="mkwsSearch"></div>\
92     </td>\
93   </tr>\
94   <tr>\
95     <td>\
96       <div style="height:500px; overflow: auto">\
97         <div id="mkwsPager"></div>\
98         <div id="mkwsNavi"></div>\
99         <div id="mkwsRecords"></div>\
100         <div id="mkwsTargets"></div>\
101         <div id="mkwsRanking"></div>\
102       </div>\
103     </td>\
104   </tr>\
105   <tr>\
106     <td>\
107       <div style="height:300px; overflow: hidden">\
108         <div id="mkwsTermlists"></div>\
109       </div>\
110     </td>\
111   </tr>\
112   <tr>\
113     <td>\
114       <div id="mkwsStat"></div>\
115     </td>\
116   </tr>\
117 </table>';
118
119             var popup = '\
120 <div id="mkwsSearch"></div>\
121 <div id="' + id_popup + '">\
122   <div id="mkwsSwitch"></div>\
123   <div id="mkwsLang"></div>\
124   <div id="mkwsResults"></div>\
125   <div id="mkwsTargets"></div>\
126   <div id="mkwsStat"></div>\
127 </div>'
128
129             if (config && config.layout == 'div') {
130                 debug("jquery plugin layout: div");
131                 document.write(div);
132             } else if (config && config.layout == 'popup') {
133                 debug("jquery plugin layout: popup with id: " + id_popup);
134                 document.write(popup);
135                 $(document).ready(function() { init_popup(config); });
136             } else {
137                 debug("jquery plugin layout: table");
138                 document.write(table);
139             }
140         }
141     });
142 };
143
144
145 (function (j) {
146     // enable before page load, so we could call it before mkws() runs
147     _mkws_jquery_plugin(j);
148 })(jQuery);