Merge branch 'master' of ssh://git.indexdata.com:222/home/git/private/mkws
[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             var id_popup = config.id_popup || "#mkwsPopup";
61             id_popup = id_popup.replace(/^#/, "");
62
63             // simple layout
64             var div = '\
65 <div id="mkwsSwitch"></div>\
66 <div id="mkwsLang"></div>\
67 <div id="mkwsSearch"></div>\
68 <div id="mkwsResults"></div>\
69 <div id="mkwsTargets"></div>\
70 <div id="mkwsStat"></div>';
71
72             // new table layout
73             var table = '\
74 <style type="text/css">\
75   #mkwsTermlists div.facet {\
76   float:left;\
77   width: 30%;\
78   margin: 0.3em;\
79   }\
80   #mkwsStat {\
81   text-align: right;\
82   }\
83 </style>\
84     \
85 <table width="100%" border="0">\
86   <tr>\
87     <td>\
88       <div id="mkwsSwitch"></div>\
89       <div id="mkwsLang"></div>\
90       <div id="mkwsSearch"></div>\
91     </td>\
92   </tr>\
93   <tr>\
94     <td>\
95       <div style="height:500px; overflow: auto">\
96         <div id="mkwsPager"></div>\
97         <div id="mkwsNavi"></div>\
98         <div id="mkwsRecords"></div>\
99         <div id="mkwsTargets"></div>\
100         <div id="mkwsRanking"></div>\
101       </div>\
102     </td>\
103   </tr>\
104   <tr>\
105     <td>\
106       <div style="height:300px; overflow: hidden">\
107         <div id="mkwsTermlists"></div>\
108       </div>\
109     </td>\
110   </tr>\
111   <tr>\
112     <td>\
113       <div id="mkwsStat"></div>\
114     </td>\
115   </tr>\
116 </table>';
117
118             var popup = '\
119 <div id="mkwsSearch"></div>\
120 <div id="' + id_popup + '">\
121   <div id="mkwsSwitch"></div>\
122   <div id="mkwsLang"></div>\
123   <div id="mkwsResults"></div>\
124   <div id="mkwsTargets"></div>\
125   <div id="mkwsStat"></div>\
126 </div>'
127
128             if (config && config.layout == 'div') {
129                 debug("jquery plugin layout: div");
130                 document.write(div);
131             } else if (config && config.layout == 'popup') {
132                 debug("jquery plugin layout: popup with id: " + id_popup);
133                 document.write(popup);
134                 $(document).ready(function() { init_popup(config); });
135             } else {
136                 debug("jquery plugin layout: table");
137                 document.write(table);
138             }
139         }
140     });
141 };
142
143
144 (function (j) {
145     // enable before page load, so we could call it before mkws() runs
146     _mkws_jquery_plugin(j);
147 })(jQuery);