-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkoha-kiosk.js
341 lines (318 loc) · 14.9 KB
/
koha-kiosk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*!
* This file is part of Koha Kiosk
* Copyright 2017 Hector Castro
* Copyright 2017 Universidad de El Salvador
* koha Kiosk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Koha Kiosk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Koha Kiosk. If not, see <http://www.gnu.org/licenses/>.
*/
//Remove branches links and tooltips
$( "div.branch-info-tooltip" ).remove();
$( "span.branch-info-tooltip-trigger a").attr( "class", "koha_url" );
$( "[href='/cgi-bin/koha/opac-shelves.pl?op=list&category=1']" ).attr( "class", "koha_url" );
$( "[href='opac-ics.pl']" ).attr( "class", "koha_url" );
//Selectors for elements that sent the outside of domain
let outsideDomainHandleClick = `
#export,
#moresearches_menu,
#opacnav a,
#other_resources,
#listsmenu,
#cartmenulink
.print-large,
.koha_url,
.addtocart,
.addtoshelf`;
let outsideDomainCSS = `
#export,
#moresearches_menu,
#opacnav a,
#other_resources,
#listsmenu,
#cartmenulink,
.koha_url,
.addtocart,
.addtoshelf,
.print-large`;
$( outsideDomainHandleClick ).on("click", function(e){
e.preventDefault;
});
$("#addto").attr("disabled", "disabled");
$( outsideDomainCSS ).css({
"pointer-events": "none",
"cursor": "default",
"color": "grey"
});
$( "#format, #furthersearches" ).css({
"color": "grey"
});
//Get localization messages
let alert_debarred_heading = browser.i18n.getMessage("snapAlertHeading"),
alert_debarred_content = browser.i18n.getMessage("snapAlertContent"),
extension_name = browser.i18n.getMessage("extensionName"),
print_slip_button = browser.i18n.getMessage("printSlipButton"),
heads_up = browser.i18n.getMessage("headsUp"),
sign_off_message = browser.i18n.getMessage("signOffMessage"),
popover_message = browser.i18n.getMessage("popoverMessage"),
modal_title = browser.i18n.getMessage("modalTitle"),
card_number = browser.i18n.getMessage("cardNumber"),
help_block_input = browser.i18n.getMessage("helpBlockInput"),
logout_checkbox = browser.i18n.getMessage("logoutCheckBox"),
print_button = browser.i18n.getMessage("printButton"),
cancel_button = browser.i18n.getMessage("cancelButton");
// Test if user is loggedin, If so, present button to print/ckeckout items else show a popover message
if ( $( "span.loggedinusername" ).length ) {
if ( $( "li#userdebarred" ).length ) {
browser.runtime.sendMessage({"button": 0});
let userdebarred = `<button type="button" class="closebtn" data-dismiss="alert">×</button>
<h4 class="alert-heading">${alert_debarred_heading}</h4>
<p>${alert_debarred_content}</p>`;
$( "div#notesaved" ).append( userdebarred );
$( "div#notesaved" ).css({ "display": "block" });
$( "div#notesaved" ).attr( "class", "alert alert-block alert-error fade in" );
}
//Get Boolean if true show button else don't show it
let gettingItem = browser.storage.local.get( "set_button_print" );
gettingItem.then( ( val ) => {
if ( val.set_button_print == 1 ) {
let append_button_print = `<button class="btn btn-mini button-added" type="button">
${print_slip_button}</button>`;
$( ".item-status.available" ).append( append_button_print );
//Add printSlip() as a listener to click events on the class "button-added".
//listener added at this point due timing in promises.
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#Timing
let buttonAdded = document.getElementsByClassName( "button-added" ), i;
for ( i = 0; i < buttonAdded.length; i++ ) {
buttonAdded[i].addEventListener( "click", printSlip );
}
}
}, onError);
if ( $( "#userdetails" ).length ) {
let warning_alert = `<div class="alert alert-info">
<button type="button" class="closebtn" data-dismiss="alert">×</button>
<p class="text-center lead">${heads_up} ${sign_off_message}</p></div>`;
$( "#header-region" ).append( warning_alert );
}
} else {
$( "[href='/cgi-bin/koha/opac-user.pl']" ).attr( "data-toggle", "popover" );
$( "[href='/cgi-bin/koha/opac-user.pl']" ).attr( "data-placement", "bottom" );
$( "[href='/cgi-bin/koha/opac-user.pl']" ).attr( "data-original-title", extension_name );
$( "[href='/cgi-bin/koha/opac-user.pl']" ).attr( "data-content", popover_message );
$( "[href='/cgi-bin/koha/opac-user.pl']" ).attr( "data-trigger", "manual" );
$( "[href='/cgi-bin/koha/opac-user.pl']" ).popover( "show" );
browser.runtime.sendMessage({"button": 1});
}
$( "#logout" ).on( "click", function(){
browser.runtime.sendMessage({"logout": 1});
});
//Generic error logger
function onError(e) {
console.error(e);
}
//Send a message to the page script
function printSlip() {
//look up if modal exist, if true then remove it
if ( $( "#print_slip_modal" ).length ) {
$( "#print_slip_modal" ).remove();
}
// Take tile, author of book and borrower name
let book_title_resp_stmt = $( "h1.title" ).text(),
borrowername = $( "span.loggedinusername" ).text();
//take the index of the element clicked
let click_row = $( this ).parent().parent().parent().index(),
click_col = $( this ).parent().parent().index(),
table = $( "#DataTables_Table_0" );
//iterating through table
table.find( "tr" ).each(function () {
var tds = $( this ).find( "td" ),
barcode = tds.eq( click_col + 2 ),
col = barcode.index(),
row = barcode.parent().index();
if ( $( "#item_ccode" ).length ) {
if ( $( "#item_enumchron" ).length && $( "#item_copy" ).length ) {
var item_type = tds.eq(0).text(),
item_current_location = tds.eq(1).text(),
item_ccode = tds.eq(2).text(),
item_call_number = tds.eq(3).text(),
item_enum_chron = tds.eq(4).text(),
item_copy = tds.eq(5).text(),
item_status = tds.eq(6).text(),
item_date_due = tds.eq(7).text(),
item_barcode = tds.eq(8).text();
} else if ( $( "#item_copy" ).length ) {
var item_type = tds.eq(0).text(),
item_current_location = tds.eq(1).text(),
item_ccode = tds.eq(2).text(),
item_call_number = tds.eq(3).text(),
item_copy = tds.eq(4).text(),
item_status = tds.eq(5).text(),
item_date_due = tds.eq(6).text(),
item_barcode = tds.eq(7).text(),
item_enum_chron = "";
} else if ( $( "#item_enumchron" ).length ) {
var item_type = tds.eq(0).text(),
item_current_location = tds.eq(1).text(),
item_ccode = tds.eq(2).text(),
item_call_number = tds.eq(3).text(),
item_enum_chron = tds.eq(4).text(),
item_status = tds.eq(5).text(),
item_date_due = tds.eq(6).text(),
item_barcode = tds.eq(7).text(),
item_copy = "";
} else {
var item_type = tds.eq(0).text(),
item_current_location = tds.eq(1).text(),
item_ccode = tds.eq(2).text(),
item_call_number = tds.eq(3).text(),
item_status = tds.eq(4).text(),
item_date_due = tds.eq(5).text(),
item_barcode = tds.eq(6).text(),
item_enum_chron = "",
item_copy = "";
}
} else {
if ( $( "#item_enumchron" ).length && $( "#item_copy" ).length ) {
var item_type = tds.eq(0).text(),
item_current_location = tds.eq(1).text(),
item_call_number = tds.eq(2).text(),
item_enum_chron = tds.eq(3).text(),
item_copy = tds.eq(4).text(),
item_status = tds.eq(5).text(),
item_date_due = tds.eq(6).text(),
item_barcode = tds.eq(7).text(),
item_ccode = "";
} else if ( $( "#item_copy" ).length ) {
var item_type = tds.eq(0).text(),
item_current_location = tds.eq(1).text(),
item_call_number = tds.eq(2).text(),
item_copy = tds.eq(3).text(),
item_status = tds.eq(4).text(),
item_date_due = tds.eq(5).text(),
item_barcode = tds.eq(6).text(),
item_enum_chron = "",
item_ccode = "";
} else if ( $( "#item_enumchron" ).length ) {
var item_type = tds.eq(0).text(),
item_current_location = tds.eq(1).text(),
item_call_number = tds.eq(2).text(),
item_enum_chron = tds.eq(3).text(),
item_status = tds.eq(4).text(),
item_date_due = tds.eq(5).text(),
item_barcode = tds.eq(6).text(),
item_copy = "",
item_ccode = "";
} else {
var item_type = tds.eq(0).text(),
item_current_location = tds.eq(1).text(),
item_call_number = tds.eq(2).text(),
item_status = tds.eq(3).text(),
item_date_due = tds.eq(4).text(),
item_barcode = tds.eq(5).text(),
item_enum_chron = "",
item_ccode = "",
item_copy = "";
}
}
//trim double, triple and so on... spaces between words
let item_type_trimed = item_type.replace( /\s\s+/g, ' ' ),
item_current_location_trimed = item_current_location.replace( /\s\s+/g, ' ' ),
item_ccode_trimed = item_ccode.replace( /\s\s+/g, ' ' );
//NOTE: Testing... to know how to get bardcode, and position clicked
// alert("barcode: " + barcode.text() + " colindex: " + col + " rowindex: " + row + " clicking row: " + click_row);
if ( click_row == row ) {
//insert modal to take borrower card number
let print_slip_modal = `<div id="print_slip_modal" class="modal hide fade">
<div class="modal-header">
<button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">
×</button>
<h3>${modal_title}</h3></div>
<form id="print_slip_form" onsubmit="return printSlipForm()">
<fieldset><div class="modal-body">
<label>${card_number}</label>
<input type="text" id="borrowercard" placeholder="26345000012941" required>
<span class="help-block">${help_block_input}</span>
<label class="checkbox"><input id="logout_checkbox" type="checkbox" checked>
${logout_checkbox}</label>
</div><div class="modal-footer">
<button type="submit" class="btn btn-primary"><i class="icon-print icon-white"></i>
${print_button}</button>
<a href="#" class="close" data-dismiss="modal" aria-hidden="true">${cancel_button}</a>
</div></fieldset></form></div>`;
$( "body" ).append( print_slip_modal );
//Pass data to input hidden values, show modal and stop the each bucle
$("<input>").attr({
type: 'hidden',
id: "borrowername",
value: borrowername
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_title",
value: book_title_resp_stmt.trim()
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_type",
value: item_type_trimed.trim()
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_current_location",
value: item_current_location_trimed.trim()
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_ccode",
value: item_ccode_trimed.trim()
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_call_number",
value: item_call_number.trim()
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_enum_chron",
value: item_enum_chron.trim()
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_copy",
value: item_copy.trim()
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_status",
value: item_status.trim()
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_date_due",
value: item_date_due.trim()
}).appendTo('#print_slip_form');
$("<input>").attr({
type: 'hidden',
id: "resource_barcode",
value: item_barcode.trim()
}).appendTo('#print_slip_form');
$( "#print_slip_modal" ).modal( "show" );
return false;
}
});
}
window.addEventListener("message", (event) => {
if (event.source == window &&
event.data.message == "1" &&
event.data.direction == "from-page-script") {
browser.runtime.sendMessage({"logout": 1});
}
});