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 |
return $.ajax({ |
80 |
method: method, |
81 |
dataType: "json", |
82 |
url: svc_url, |
83 |
data: params, |
84 |
success: function (data) { |
85 |
if (data.error) { |
86 |
display_error(data.error); |
87 |
} |
88 |
success_callback(data); |
89 |
}, |
90 |
error: function(jqXHR, textStatus, errorThrown) { |
91 |
display_error(errorThrown); |
92 |
} |
93 |
}); |
94 |
} |
95 |
|
96 |
function load_account_details ( callback ) { |
97 |
svc_ajax('get', { action: "account" }, function(data) { |
98 |
details = data; |
99 |
callback(data); |
100 |
}); |
101 |
} |
102 |
|
103 |
function item_action (params, el) { |
104 |
var isbn = params.isbn; |
105 |
svc_ajax('post', params, function(data) { |
106 |
if (data.checkouts) { |
107 |
details.checkouts = data.checkouts; |
108 |
} |
109 |
if (data.holds) { |
110 |
details.holds = data.holds; |
111 |
} |
112 |
display_actions(el, isbn); |
113 |
}); |
114 |
} |
115 |
|
116 |
function item_is_checked_out (isbn) { |
117 |
if ( !(details && details.checkouts) ) { |
118 |
return null; |
119 |
} |
120 |
var isbn_uc = isbn.toUpperCase(); |
121 |
var items = details.checkouts.items; |
122 |
for (var i = 0; i < items.length; i++) { |
123 |
if ( items[i].isbn.toUpperCase() == isbn_uc ) { |
124 |
return items[i]; |
125 |
} |
126 |
} |
127 |
return null; |
128 |
} |
129 |
|
130 |
function item_is_on_hold (isbn) { |
131 |
if ( !(details && details.holds) ) { |
132 |
return false; |
133 |
} |
134 |
var isbn_uc = isbn.toUpperCase(); |
135 |
var items = details.holds.items; |
136 |
for (var i = 0; i < items.length; i++) { |
137 |
if ( items[i].isbn.toUpperCase() == isbn_uc ) { |
138 |
return items[i]; |
139 |
} |
140 |
} |
141 |
return null; |
142 |
} |
143 |
|
144 |
function display_actions(el, isbn) { |
145 |
$(el).empty(); |
146 |
if (is_identified()) { |
147 |
|
148 |
var item = item_is_checked_out(isbn); |
149 |
if (item) { |
150 |
var expires = new Date(item.expires); |
151 |
$('<span class="oneclickdigital-item-status">') |
152 |
.text(_("Checked out until") + " " + expires.toLocaleString()) |
153 |
.appendTo(el); |
154 |
$(el).append(" "); |
155 |
|
156 |
if (item.url) { |
157 |
var download = $('<a href="'+item.url+'">').appendTo(el); |
158 |
decorate_button(download, _("Download")); |
159 |
$(el).append(" "); |
160 |
} |
161 |
|
162 |
$(el).append( ajax_button(_("Check in"), function() { |
163 |
if( confirm(_("Are you sure you want to return this item?")) ) { |
164 |
item_action({action: "return", isbn: isbn}, el); |
165 |
} |
166 |
}) ); |
167 |
|
168 |
return item; |
169 |
} |
170 |
|
171 |
item = item_is_on_hold(isbn); |
172 |
if (item) { |
173 |
$('<span class="oneclickdigital-status">') |
174 |
.text(_("On hold")) |
175 |
.appendTo(el); |
176 |
$(el).append(" "); |
177 |
} |
178 |
|
179 |
if(checkout_popup) { |
180 |
$(el).append( ajax_button(_("Check out"), function() { |
181 |
if( confirm(_("Are you sure you want to checkout this item?")) ) { |
182 |
svc_ajax('post', {action: "checkout", isbn: isbn}, function(data) { |
183 |
if (data.checkouts) { |
184 |
details.checkouts = data.checkouts; |
185 |
} |
186 |
if (data.holds) { |
187 |
details.holds = data.holds; |
188 |
} |
189 |
item = display_actions(el, isbn); |
190 |
}); |
191 |
} |
192 |
}) ); |
193 |
} |
194 |
if (!item) { |
195 |
$(el).append( ajax_button(_("Place hold"), function() { |
196 |
item_action({action: "place_hold", isbn: isbn}, el); |
197 |
}) ); |
198 |
} |
199 |
|
200 |
if (item) { |
201 |
$(el).append( ajax_button(_("Cancel"), function() { |
202 |
if( confirm(_("Are you sure you want to cancel this hold?")) ) { |
203 |
item_action({action: "return", isbn: isbn}, el); |
204 |
} |
205 |
}) ); |
206 |
} |
207 |
return item; |
208 |
} |
209 |
} |
210 |
|
211 |
function ajax_button(label, on_click) { |
212 |
var button = $('<a href="#">') |
213 |
.click(function(e) { |
214 |
e.preventDefault(); |
215 |
on_click(); |
216 |
}); |
217 |
decorate_button(button, label); |
218 |
return button; |
219 |
} |
220 |
|
221 |
function decorate_button(button, label) { |
222 |
$(button) |
223 |
.addClass("btn btn-primary btn-mini") |
224 |
.css("color","white") |
225 |
.text(label); |
226 |
} |
227 |
|
228 |
this.with_account_details = function( el, callback ) { |
229 |
$(el).append(error_div); |
230 |
load_account_details( callback ); |
231 |
} |
232 |
|
233 |
this.display_account_details = function( el ) { |
234 |
$(el).empty().append(error_div); |
235 |
load_account_details(function(data) { |
236 |
display_account(el, data); |
237 |
}); |
238 |
}; |
239 |
|
240 |
this.display_error = function( el, error ) { |
241 |
$(el).empty().append(error_div); |
242 |
display_error(error); |
243 |
}; |
244 |
|
245 |
this.is_identified = is_identified; |
246 |
|
247 |
this.add_actions = function(el, isbn) { |
248 |
var actions = $('<span class="actions">'); |
249 |
display_actions(actions, isbn); |
250 |
$('<div id="action_'+isbn+'" class="actions-menu">') |
251 |
.append(actions) |
252 |
.appendTo(el); |
253 |
}; |
254 |
|
255 |
this.search = function( q, page_size, page, callback ) { |
256 |
svc_ajax('get', { action: "search", q: q, page_size: page_size, page: page }, function (data) { |
257 |
error = data.error; |
258 |
if (!data.is_identified && !error) error = _("Invalid account"); |
259 |
|
260 |
var results; |
261 |
if (data.results) { |
262 |
results = data.results; |
263 |
if (!results.total) { |
264 |
var total = results.items.length; |
265 |
if ( total == result.page_size ) total = total + "+"; |
266 |
results.total = total; |
267 |
} |
268 |
} |
269 |
else results = {}; |
270 |
results.error = error; |
271 |
callback(results); |
272 |
}); |
273 |
}; |
274 |
} |