Line 0
Link Here
|
|
|
1 |
if ( typeof KOHA == "undefined" || !KOHA ) { |
2 |
var KOHA = {}; |
3 |
} |
4 |
|
5 |
KOHA.OneClickDigital = new function() { |
6 |
var svc_url = '/cgi-bin/koha/svc/oneclickdigital'; |
7 |
|
8 |
var error_div = $('<div class="oneclickdigital-error">'); |
9 |
function display_error ( error ) { |
10 |
error_div.text(error); |
11 |
} |
12 |
|
13 |
var details = null; |
14 |
|
15 |
function is_identified() { |
16 |
return details ? details.is_identified : false; |
17 |
} |
18 |
|
19 |
var checkout_popup = null; |
20 |
$( document ).ready(function() { |
21 |
checkout_popup = $("#oneclickdigital-checkout"); |
22 |
}); |
23 |
|
24 |
function display_account (container, data) { |
25 |
if (!data.is_identified) { |
26 |
return; |
27 |
} |
28 |
|
29 |
if (data.checkouts) { |
30 |
var checkouts_div = $('<div class="oneclickdigital-div">').html('<h3>' + _("Checkouts") + '</h3>'); |
31 |
var checkouts_list = $('<ul class="oneclickdigital-list">'); |
32 |
data.checkouts.items.forEach(function(item) { |
33 |
item_line(checkouts_list, item); |
34 |
}); |
35 |
checkouts_div.append(checkouts_list); |
36 |
$(container).append(checkouts_div); |
37 |
} |
38 |
|
39 |
if (data.holds) { |
40 |
var holds_div = $('<div class="oneclickdigital-div">').html('<h3>' + _("Holds") + '</h3>'); |
41 |
var holds_list = $('<ul class="oneclickdigital-list">'); |
42 |
data.holds.items.forEach(function(item) { |
43 |
item_line(holds_list, item); |
44 |
}); |
45 |
holds_div.append(holds_list); |
46 |
$(container).append(holds_div); |
47 |
} |
48 |
} |
49 |
|
50 |
function item_line(ul_el, item) { |
51 |
var line = $('<li class="oneclickdigital-item">'); |
52 |
if (item.images) { |
53 |
var thumb_url = item.images.thumbnail; |
54 |
if (thumb_url) { |
55 |
$('<img class="oneclickdigital-item-thumbnail">') |
56 |
.attr("src", thumb_url) |
57 |
.appendTo(line); |
58 |
} |
59 |
} |
60 |
$('<div class="oneclickdigital-item-title">') |
61 |
.text(item.title) |
62 |
.appendTo(line); |
63 |
$('<div class="oneclickdigital-item-subtitle">') |
64 |
.text(item.subtitle) |
65 |
.appendTo(line); |
66 |
$('<div class="oneclickdigital-item-author">') |
67 |
.text(item.author) |
68 |
.appendTo(line); |
69 |
var actions = $('<span class="actions">'); |
70 |
display_actions(actions, item.isbn); |
71 |
$('<div id="action_'+item.isbn+'" class="actions-menu">') |
72 |
.append(actions) |
73 |
.appendTo(line); |
74 |
|
75 |
$(ul_el).append(line); |
76 |
} |
77 |
|
78 |
function svc_ajax ( method, params, success_callback ) { |
79 |
// remove when jquery is upgraded |
80 |
for (var key in params) { |
81 |
if (params[key] === null) delete params[key]; |
82 |
} |
83 |
return $.ajax({ |
84 |
method: method, |
85 |
dataType: "json", |
86 |
url: svc_url, |
87 |
data: params, |
88 |
success: function (data) { |
89 |
if (data.error) { |
90 |
display_error(data.error); |
91 |
} |
92 |
success_callback(data); |
93 |
}, |
94 |
error: function(jqXHR, textStatus, errorThrown) { |
95 |
display_error(errorThrown); |
96 |
} |
97 |
}); |
98 |
} |
99 |
|
100 |
function load_account_details ( callback ) { |
101 |
svc_ajax('get', { action: "account" }, function(data) { |
102 |
details = data; |
103 |
callback(data); |
104 |
}); |
105 |
} |
106 |
|
107 |
function item_action (params, el) { |
108 |
var isbn = params.isbn; |
109 |
svc_ajax('post', params, function(data) { |
110 |
if (data.checkouts) { |
111 |
details.checkouts = data.checkouts; |
112 |
} |
113 |
if (data.holds) { |
114 |
details.holds = data.holds; |
115 |
} |
116 |
display_actions(el, isbn); |
117 |
}); |
118 |
} |
119 |
|
120 |
function item_is_checked_out (isbn) { |
121 |
if ( !(details && details.checkouts) ) { |
122 |
return null; |
123 |
} |
124 |
var isbn_uc = isbn.toUpperCase(); |
125 |
var items = details.checkouts.items; |
126 |
for (var i = 0; i < items.length; i++) { |
127 |
if ( items[i].isbn.toUpperCase() == isbn_uc ) { |
128 |
return items[i]; |
129 |
} |
130 |
} |
131 |
return null; |
132 |
} |
133 |
|
134 |
function item_is_on_hold (isbn) { |
135 |
if ( !(details && details.holds) ) { |
136 |
return false; |
137 |
} |
138 |
var isbn_uc = isbn.toUpperCase(); |
139 |
var items = details.holds.items; |
140 |
for (var i = 0; i < items.length; i++) { |
141 |
if ( items[i].isbn.toUpperCase() == isbn_uc ) { |
142 |
return items[i]; |
143 |
} |
144 |
} |
145 |
return null; |
146 |
} |
147 |
|
148 |
function display_actions(el, isbn) { |
149 |
$(el).empty(); |
150 |
if (is_identified()) { |
151 |
|
152 |
var item = item_is_checked_out(isbn); |
153 |
if (item) { |
154 |
var expires = new Date(item.expires); |
155 |
$('<span class="oneclickdigital-item-status">') |
156 |
.text(_("Checked out until") + " " + expires.toLocaleString()) |
157 |
.appendTo(el); |
158 |
$(el).append(" "); |
159 |
|
160 |
if (item.url) { |
161 |
var download = $('<a href="'+item.url+'">').appendTo(el); |
162 |
decorate_button(download, _("Download")); |
163 |
$(el).append(" "); |
164 |
} |
165 |
|
166 |
$(el).append( ajax_button(_("Check in"), function() { |
167 |
if( confirm(_("Are you sure you want to return this item?")) ) { |
168 |
item_action({action: "return", isbn: isbn}, el); |
169 |
} |
170 |
}) ); |
171 |
|
172 |
return item; |
173 |
} |
174 |
|
175 |
item = item_is_on_hold(isbn); |
176 |
if (item) { |
177 |
$('<span class="oneclickdigital-status">') |
178 |
.text(_("On hold")) |
179 |
.appendTo(el); |
180 |
$(el).append(" "); |
181 |
} |
182 |
|
183 |
if(checkout_popup) { |
184 |
$(el).append( ajax_button(_("Check out"), function() { |
185 |
if( confirm(_("Are you sure you want to checkout this item?")) ) { |
186 |
svc_ajax('post', {action: "checkout", isbn: isbn}, function(data) { |
187 |
if (data.checkouts) { |
188 |
details.checkouts = data.checkouts; |
189 |
} |
190 |
if (data.holds) { |
191 |
details.holds = data.holds; |
192 |
} |
193 |
item = display_actions(el, isbn); |
194 |
}); |
195 |
} |
196 |
}) ); |
197 |
} |
198 |
if (!item) { |
199 |
$(el).append( ajax_button(_("Place hold"), function() { |
200 |
item_action({action: "place_hold", isbn: isbn}, el); |
201 |
}) ); |
202 |
} |
203 |
|
204 |
if (item) { |
205 |
$(el).append( ajax_button(_("Cancel"), function() { |
206 |
if( confirm(_("Are you sure you want to cancel this hold?")) ) { |
207 |
item_action({action: "return", isbn: isbn}, el); |
208 |
} |
209 |
}) ); |
210 |
} |
211 |
return item; |
212 |
} |
213 |
} |
214 |
|
215 |
function ajax_button(label, on_click) { |
216 |
var button = $('<a href="#">') |
217 |
.click(function(e) { |
218 |
e.preventDefault(); |
219 |
on_click(); |
220 |
}); |
221 |
decorate_button(button, label); |
222 |
return button; |
223 |
} |
224 |
|
225 |
function decorate_button(button, label) { |
226 |
$(button) |
227 |
.addClass("btn btn-primary btn-mini") |
228 |
.css("color","white") |
229 |
.text(label); |
230 |
} |
231 |
|
232 |
this.with_account_details = function( el, callback ) { |
233 |
$(el).append(error_div); |
234 |
load_account_details( callback ); |
235 |
} |
236 |
|
237 |
this.display_account_details = function( el ) { |
238 |
$(el).empty().append(error_div); |
239 |
load_account_details(function(data) { |
240 |
display_account(el, data); |
241 |
}); |
242 |
}; |
243 |
|
244 |
this.display_error = function( el, error ) { |
245 |
$(el).empty().append(error_div); |
246 |
display_error(error); |
247 |
}; |
248 |
|
249 |
this.is_identified = is_identified; |
250 |
|
251 |
this.add_actions = function(el, isbn) { |
252 |
var actions = $('<span class="actions">'); |
253 |
display_actions(actions, isbn); |
254 |
$('<div id="action_'+isbn+'" class="actions-menu">') |
255 |
.append(actions) |
256 |
.appendTo(el); |
257 |
}; |
258 |
|
259 |
this.search = function( q, page_size, page, callback ) { |
260 |
svc_ajax('get', { action: "search", q: q, page_size: page_size, page: page }, function (data) { |
261 |
error = data.error; |
262 |
if (!data.is_identified && !error) error = _("Invalid account"); |
263 |
|
264 |
var results; |
265 |
if (data.results) { |
266 |
results = data.results; |
267 |
if (!results.total) { |
268 |
var total = results.items.length; |
269 |
if ( total == result.page_size ) total = total + "+"; |
270 |
results.total = total; |
271 |
} |
272 |
} |
273 |
else results = {}; |
274 |
results.error = error; |
275 |
callback(results); |
276 |
}); |
277 |
}; |
278 |
} |