Commit | Line | Data |
---|---|---|
7c653aa3 JD |
1 | // Generated by CoffeeScript 1.8.0 |
2 | define(['jquery-noconflict', 'lodash', 'json', 'od_api', 'jquery-ui'], function($, _, json, od) { | |
3 | var Labels, brief_name, responseMessage; | |
4 | (function(url) { | |
5 | var link; | |
6 | link = document.createElement('link'); | |
7 | link.type = 'text/css'; | |
8 | link.rel = 'stylesheet'; | |
9 | link.href = url; | |
10 | return document.getElementsByTagName('head')[0].appendChild(link); | |
11 | })('//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.min.css'); | |
12 | brief_name = function() { | |
13 | var xs; | |
14 | xs = window.location.pathname.match(/eg\/opac\/(.+)/); | |
15 | if (xs) { | |
16 | return xs[1].replace(/\/\d+/, ''); | |
17 | } else { | |
18 | return ''; | |
19 | } | |
20 | }; | |
21 | responseMessage = function(x) { | |
22 | return (json.parse(x.responseText)).message; | |
23 | }; | |
24 | $.widget('ui.dialogAction', $.ui.dialog, { | |
25 | options: { | |
26 | draggable: false, | |
27 | resizable: true, | |
28 | modal: true, | |
29 | buttons: [ | |
30 | { | |
31 | text: 'Yes', | |
32 | click: function(ev) { | |
33 | ev.stopPropagation(); | |
34 | $(this).dialogAction('yes_action'); | |
35 | } | |
36 | }, { | |
37 | text: 'No', | |
38 | click: function(ev) { | |
39 | ev.stopPropagation(); | |
40 | $(this).dialogAction('autoclose'); | |
41 | } | |
42 | } | |
43 | ] | |
44 | }, | |
45 | _create: function() { | |
46 | var f, ib, intent, ob, position, _ref, _ref1, _ref2, _ref3; | |
47 | intent = (_ref = this.options._scenario) != null ? _ref.intent : void 0; | |
48 | position = (_ref1 = this.options._action) != null ? _ref1._of : void 0; | |
49 | if (intent) { | |
50 | ob = this.options.buttons; | |
51 | ib = intent.buttons; | |
52 | ob[0].text = (ib != null ? ib[0] : void 0) || 'Yes'; | |
53 | ob[1].text = (ib != null ? ib[1] : void 0) || 'No'; | |
54 | } | |
55 | if (position) { | |
56 | this.options.position = { | |
57 | of: position, | |
58 | at: 'top', | |
59 | my: 'top' | |
60 | }; | |
61 | } | |
62 | this._super(); | |
63 | f = (_ref2 = this.options._scenario.intent) != null ? _ref2.body : void 0; | |
64 | this.set_message('intent', !intent, $.isFunction(f) ? f((_ref3 = this.options._action) != null ? _ref3._name : void 0) : void 0); | |
65 | return this._on({ | |
66 | 'dialogactionclose': function() { | |
67 | return this._destroy(); | |
68 | } | |
69 | }); | |
70 | }, | |
71 | set_message: function(scenario, close, body, title) { | |
72 | var s; | |
73 | this._close_button(close); | |
74 | s = this.options._scenario[scenario]; | |
75 | this.element.empty().append(body || (s != null ? s.body : void 0) || $('<div>').progressbar({ | |
76 | value: false | |
77 | })); | |
78 | this.option('title', title || (s != null ? s.title : void 0) || this.options._action._of._etitle()); | |
79 | return this; | |
80 | }, | |
81 | autoclose: function() { | |
82 | var _ref, _ref1; | |
83 | this._on({ | |
84 | 'dialogactionclose': ((_ref = this.options._scenario) != null ? (_ref1 = _ref.intent) != null ? _ref1.reroute : void 0 : void 0) || function() {} | |
85 | }); | |
86 | this.close(); | |
87 | return this; | |
88 | }, | |
89 | yes_action: function() { | |
90 | var action, close, onclose_maybe_reroute, progress; | |
91 | this.option('buttons', []); | |
92 | onclose_maybe_reroute = (function(_this) { | |
93 | return function() { | |
94 | var _ref, _ref1; | |
95 | return _this._on({ | |
96 | 'dialogactionclose': ((_ref = _this.options._scenario) != null ? (_ref1 = _ref.done) != null ? _ref1.reroute : void 0 : void 0) || function() {} | |
97 | }); | |
98 | }; | |
99 | })(this); | |
100 | close = (function(_this) { | |
101 | return function() { | |
102 | onclose_maybe_reroute(); | |
103 | return _this.close(); | |
104 | }; | |
105 | })(this); | |
106 | action = this.options._action; | |
107 | progress = (function(_this) { | |
108 | return function() { | |
109 | return _this.set_message('progress', true); | |
110 | }; | |
111 | })(this); | |
112 | od.api(action.href, action.method, { | |
113 | fields: $('form', this.element).serializeArray() | |
114 | }, progress).then((function(_this) { | |
115 | return function(x) { | |
116 | var f, _ref, _ref1; | |
117 | f = (_ref = _this.options._scenario.done) != null ? _ref.body : void 0; | |
118 | _this.set_message('done', true, $.isFunction(f) ? f(x) : void 0); | |
119 | return window.setTimeout(close, ((_ref1 = _this.options._scenario.done) != null ? _ref1.timeout : void 0) || 2000); | |
120 | }; | |
121 | })(this), (function(_this) { | |
122 | return function(x) { | |
123 | return _this.set_message('fail', true, responseMessage(x)); | |
124 | }; | |
125 | })(this)).done(onclose_maybe_reroute); | |
126 | return this; | |
127 | }, | |
128 | _close_button: function(show) { | |
129 | var $button; | |
130 | $button = this.element.parent().find('.ui-dialog-titlebar-close'); | |
131 | if (show) { | |
132 | $button.show().focus(); | |
133 | } else { | |
134 | $button.hide(); | |
135 | } | |
136 | return this; | |
137 | } | |
138 | }); | |
139 | Labels = { | |
140 | hold: 'Place hold', | |
141 | addSuspension: 'Suspend', | |
142 | releaseSuspension: 'Activate', | |
143 | removeHold: 'Cancel', | |
144 | checkout: 'Check out', | |
145 | earlyReturn: 'Return title', | |
146 | format: 'Select format' | |
147 | }; | |
148 | return $.fn.extend({ | |
149 | _hold: function(action) { | |
150 | var scenario; | |
151 | scenario = { | |
152 | intent: { | |
153 | body: $('<div>')._action_fields(action.fields), | |
154 | buttons: ['Place hold', 'Cancel'], | |
155 | reroute: function() { | |
156 | return window.history.back(); | |
157 | } | |
158 | }, | |
159 | done: { | |
160 | body: 'Hold was successfully placed. Close this box to be redirected to your holds list.', | |
161 | reroute: function() { | |
162 | return window.location.replace('/eg/opac/myopac/holds?e_items'); | |
163 | } | |
164 | }, | |
165 | fail: { | |
166 | body: 'Hold was not placed. There may have been a network or server problem. Please try again.' | |
167 | } | |
168 | }; | |
169 | return this.dialogAction({ | |
170 | _scenario: scenario, | |
171 | _action: action | |
172 | }); | |
173 | }, | |
174 | _removeHold: function(action) { | |
175 | var scenario; | |
176 | scenario = { | |
177 | intent: { | |
178 | body: 'Are you sure you want to cancel this hold?' | |
179 | }, | |
180 | done: { | |
181 | body: 'Hold was successfully cancelled.' | |
182 | }, | |
183 | fail: { | |
184 | body: 'Hold was not cancelled. There may have been a network or server problem. Please try again.' | |
185 | } | |
186 | }; | |
187 | return this.dialogAction({ | |
188 | _scenario: scenario, | |
189 | _action: action | |
190 | }); | |
191 | }, | |
192 | _addSuspension: function(action) { | |
193 | var scenario; | |
194 | scenario = { | |
195 | intent: { | |
196 | body: $('<div>')._action_fields(action.fields), | |
197 | buttons: ['Suspend', 'Cancel'] | |
198 | }, | |
199 | done: { | |
200 | body: 'Hold was successfully suspended' | |
201 | }, | |
202 | fail: { | |
203 | body: 'Hold was not suspended. There may have been a network or server problem. Please try again.' | |
204 | } | |
205 | }; | |
206 | return this.dialogAction({ | |
207 | _scenario: scenario, | |
208 | _action: action | |
209 | }); | |
210 | }, | |
211 | _releaseSuspension: function(action) { | |
212 | var scenario; | |
213 | scenario = { | |
214 | intent: { | |
215 | body: 'Are you sure you want this hold to activate again?' | |
216 | }, | |
217 | done: { | |
218 | body: 'Suspension was successfully released. The page will reload to update your account status.', | |
219 | reroute: function() { | |
220 | return window.location.reload(true); | |
221 | } | |
222 | }, | |
223 | fail: { | |
224 | body: 'Suspension was not released. There may have been a network or server problem. Please try again.' | |
225 | } | |
226 | }; | |
227 | return this.dialogAction({ | |
228 | _scenario: scenario, | |
229 | _action: action | |
230 | }); | |
231 | }, | |
232 | _checkout: function(action) { | |
233 | var scenario; | |
234 | scenario = { | |
235 | intent: { | |
236 | body: $('<div>')._action_fields(action.fields), | |
237 | buttons: ['Check out', 'Cancel'], | |
238 | reroute: function() { | |
239 | if (brief_name() === 'place_hold') { | |
240 | return window.history.back(); | |
241 | } | |
242 | } | |
243 | }, | |
244 | done: { | |
245 | body: 'Title was successfully checked out. Close this page to be redirected to your checkouts list.', | |
246 | reroute: function() { | |
247 | return window.location.replace('/eg/opac/myopac/circs?e_items'); | |
248 | } | |
249 | }, | |
250 | fail: { | |
251 | body: 'Title was not checked out. There may have been a network or server problem. Please try again.' | |
252 | } | |
253 | }; | |
254 | return this.dialogAction({ | |
255 | _scenario: scenario, | |
256 | _action: action | |
257 | }); | |
258 | }, | |
259 | _format: function(action) { | |
260 | var scenario; | |
261 | scenario = { | |
262 | intent: { | |
263 | body: $('<div>')._action_fields(action.fields), | |
264 | buttons: ['Select format', 'Cancel'] | |
265 | }, | |
266 | done: { | |
267 | body: 'Format was successfully selected.' | |
268 | }, | |
269 | fail: { | |
270 | body: 'Format was not selected. There may have been a network or server problem. Please try again.' | |
271 | } | |
272 | }; | |
273 | return this.dialogAction({ | |
274 | _scenario: scenario, | |
275 | _action: action | |
276 | }); | |
277 | }, | |
278 | _earlyReturn: function(action) { | |
279 | var scenario; | |
280 | scenario = { | |
281 | intent: { | |
282 | body: 'Are you sure you want to return this title before it expires?' | |
283 | }, | |
284 | done: { | |
285 | body: 'Title was successfully returned.' | |
286 | }, | |
287 | fail: { | |
288 | body: 'Title was not returned. There may have been a network or server problem. Please try again.' | |
289 | } | |
290 | }; | |
291 | return this.dialogAction({ | |
292 | _scenario: scenario, | |
293 | _action: action | |
294 | }); | |
295 | }, | |
296 | _downloadLink: function(action) { | |
297 | var scenario; | |
298 | scenario = { | |
299 | intent: { | |
300 | body: function(formatType) { | |
301 | if (formatType === 'ebook-overdrive') { | |
302 | return 'Download content to read in the browser?'; | |
303 | } else { | |
304 | return 'Download content for an e-book reader?'; | |
305 | } | |
306 | } | |
307 | }, | |
308 | done: { | |
309 | body: function(x) { | |
310 | return $("<div>Content is ready.</div>\n<a href=" + x.links.contentlink.href + ">Right-click to download in a new tab or window.</a>\n<div>The link is valid for the next 60 seconds.</div>"); | |
311 | }, | |
312 | timeout: 59000 | |
313 | }, | |
314 | fail: { | |
315 | body: 'could not download' | |
316 | } | |
317 | }; | |
318 | return this.dialogAction({ | |
319 | _scenario: scenario, | |
320 | _action: action | |
321 | }); | |
322 | }, | |
323 | _actions: function(actions, id, suspended) { | |
324 | var $buttons, $tr, action, n, tpl; | |
325 | tpl = _.template("<div>\n <a href=\"<%= href %>\" class=\"opac-button <%= action_name %>\" style=\"margin-top: 0px; margin-bottom: 0px\"><%= label %></a>\n</div>"); | |
326 | $tr = this.closest('tr'); | |
327 | $buttons = (function() { | |
328 | var _results; | |
329 | _results = []; | |
330 | for (n in actions) { | |
331 | action = actions[n]; | |
332 | if (!((suspended != null) && n === 'addSuspension')) { | |
333 | $.extend(action, { | |
334 | _of: $tr, | |
335 | _name: n, | |
336 | _id: id | |
337 | }); | |
338 | _results.push($(tpl({ | |
339 | href: action.href, | |
340 | action_name: n, | |
341 | label: (Labels != null ? Labels[n] : void 0) || n | |
342 | })).on('click', action, function(ev) { | |
343 | ev.preventDefault(); | |
344 | $('<div>')['_' + ev.data._name](ev.data); | |
345 | return false; | |
346 | })); | |
347 | } else { | |
348 | _results.push(void 0); | |
349 | } | |
350 | } | |
351 | return _results; | |
352 | })(); | |
353 | return this.empty().append($buttons); | |
354 | }, | |
355 | _action_fields: function(fields) { | |
356 | return $('<form>')._action_field_hidden(_.where(fields, { | |
357 | name: 'reserveId' | |
358 | })[0])._action_field_email(_.where(fields, { | |
359 | name: 'emailAddress' | |
360 | })[0])._action_field_radio(_.where(fields, { | |
361 | name: 'formatType' | |
362 | })[0])._action_field_suspend(_.where(fields, { | |
363 | name: 'suspensionType' | |
364 | })[0])._action_field_date(_.where(fields, { | |
365 | name: 'numberOfDays' | |
366 | })[0]).on('click', '[name=suspensionType]', function(ev) { | |
367 | var $input; | |
368 | $input = $('[name=numberOfDays]'); | |
369 | switch (this.defaultValue) { | |
370 | case 'limited': | |
371 | return $input.show(); | |
372 | case 'indefinite': | |
373 | return $input.hide(); | |
374 | } | |
375 | }).on('submit', function(ev) { | |
376 | var $input; | |
377 | return $input = $('[name=numberOfDays]'); | |
378 | }); | |
379 | }, | |
380 | _action_field_date: function(field) { | |
381 | var $input; | |
382 | if (!field) { | |
383 | return this; | |
384 | } | |
385 | $input = $("<input type=\"date\" name=\"" + field.name + "\" value=\"" + field.value + "\" />"); | |
386 | return this.append($input.hide()); | |
387 | }, | |
388 | _action_field_hidden: function(field) { | |
389 | if (!field) { | |
390 | return this; | |
391 | } | |
392 | return this.append("<input type=\"hidden\" name=\"" + field.name + "\" value=\"" + field.value + "\" />"); | |
393 | }, | |
394 | _action_field_email: function(field) { | |
395 | var $input; | |
396 | if (!field) { | |
397 | return this; | |
398 | } | |
399 | $input = $("<div>\n You will be notified by email when a copy becomes available\n</div>\n<div>\n <label>Email address: <input type=\"email\" name=\"" + field.name + "\" value=\"" + field.value + "\" />\n </label>\n</div>"); | |
400 | if (!Boolean(field.optional)) { | |
401 | $input.find('input').prop('required', true); | |
402 | } | |
403 | return this.append($input); | |
404 | }, | |
405 | _action_field_radio: function(field) { | |
406 | var $x, $y, hint, inputs, v; | |
407 | if (!field) { | |
408 | return this; | |
409 | } | |
410 | _.remove(field.options, function(f) { | |
411 | return f === 'ebook-overdrive'; | |
412 | }); | |
413 | hint = (function() { | |
414 | switch (false) { | |
415 | case field.options.length !== 1: | |
416 | return "<div>Only one " + field.name + " is available and it has been selected for you</div>"; | |
417 | case !Boolean(field.optional): | |
418 | return "<div>You may select a " + field.name + " at this time</div>"; | |
419 | default: | |
420 | return "<div>Please select one of the available " + field.name + "s</div>"; | |
421 | } | |
422 | })(); | |
423 | inputs = (function() { | |
424 | var _i, _len, _ref, _results; | |
425 | _ref = field.options; | |
426 | _results = []; | |
427 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
428 | v = _ref[_i]; | |
429 | $x = $("<div>\n <input type=\"radio\" name=\"" + field.name + "\" value=\"" + v + "\" />" + (od.labels(v)) + "\n</div>"); | |
430 | $y = $x.find('input'); | |
431 | if (!Boolean(field.optional)) { | |
432 | $y.prop('required', true); | |
433 | } | |
434 | if (!(field.options.length > 1)) { | |
435 | $y.prop('checked', true); | |
436 | } | |
437 | _results.push($x); | |
438 | } | |
439 | return _results; | |
440 | })(); | |
441 | return this.append(hint).append(inputs); | |
442 | }, | |
443 | _action_field_suspend: function(field) { | |
444 | var $x, $y, inputs, label, v; | |
445 | if (!field) { | |
446 | return this; | |
447 | } | |
448 | label = { | |
449 | indefinite: 'Suspend this hold indefinitely', | |
450 | limited: 'Suspend this hold for a limited time' | |
451 | }; | |
452 | inputs = (function() { | |
453 | var _i, _len, _ref, _results; | |
454 | _ref = field.options; | |
455 | _results = []; | |
456 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
457 | v = _ref[_i]; | |
458 | $x = $("<div><label>\n <input type=\"radio\" name=\"" + field.name + "\" value=\"" + v + "\" /> " + label[v] + "\n</label></div>"); | |
459 | $y = $x.find('input'); | |
460 | if (!Boolean(field.optional)) { | |
461 | $y.prop('required', true); | |
462 | } | |
463 | if (v === 'indefinite') { | |
464 | $y.prop('checked', true); | |
465 | } | |
466 | _results.push($x); | |
467 | } | |
468 | return _results; | |
469 | })(); | |
470 | return this.append(inputs); | |
471 | }, | |
472 | _formats: function(formats, id) { | |
473 | var $buttons, $tr, action, format, href, n, tpl, type; | |
474 | if (!formats) { | |
475 | return this; | |
476 | } | |
477 | tpl = _.template("<div>\n <a href=\"<%= href %>\" class=\"opac-button\" style=\"margin-top: 0px; margin-bottom: 0px\"><%= label %></a>\n</div>"); | |
478 | $tr = this.closest('tr'); | |
479 | $buttons = (function() { | |
480 | var _i, _len, _ref, _ref1, _results; | |
481 | _results = []; | |
482 | for (_i = 0, _len = formats.length; _i < _len; _i++) { | |
483 | format = formats[_i]; | |
484 | n = format.formatType, (_ref = format.linkTemplates, (_ref1 = _ref.downloadLink, href = _ref1.href, type = _ref1.type)); | |
485 | href = href.replace(/\{errorpageurl\}/, encodeURIComponent(window.location.href)).replace(/\{odreadauthurl\}/, encodeURIComponent("" + window.location.href + "&reserveid=[RESERVE_ID]&read_error=[READ_ERROR]")); | |
486 | action = format.linkTemplates.downloadLink; | |
487 | $.extend(action, { | |
488 | _of: $tr, | |
489 | _name: n, | |
490 | _id: id, | |
491 | href: href | |
492 | }); | |
493 | _results.push($(tpl({ | |
494 | href: href, | |
495 | label: "Download " + (od.labels(n)) | |
496 | })).on('click', action, function(ev) { | |
497 | ev.preventDefault(); | |
498 | $('<div>')['_downloadLink'](ev.data); | |
499 | return false; | |
500 | })); | |
501 | } | |
502 | return _results; | |
503 | })(); | |
504 | return this.empty().append($buttons); | |
505 | }, | |
506 | _notify: function(title, text) { | |
507 | return this.dialog({ | |
508 | minHeight: 0, | |
509 | autoOpen: true, | |
510 | draggable: false, | |
511 | resizable: true, | |
512 | show: { | |
513 | effect: 'slideDown' | |
514 | }, | |
515 | hide: { | |
516 | effect: 'slideUp' | |
517 | }, | |
518 | close: function() { | |
519 | return $(this).dialog('destroy'); | |
520 | }, | |
521 | title: title | |
522 | }).html(text); | |
523 | }, | |
524 | _etitle: function() { | |
525 | return this.find('.title a').text(); | |
526 | } | |
527 | }); | |
528 | }); |