overdrive-eg-opac: update od_api.js with latest bugfixes
[sitka/sitka-tools.git] / overdrive-eg-opac / build / od_api.js
CommitLineData
7c653aa3
JD
1// Generated by CoffeeScript 1.8.0
2var __slice = [].slice;
3
4define(['jquery-noconflict', 'lodash', 'json', 'cookies', 'moment', 'od_config', 'od_session', 'od_data'], function($, _, json, C, M, config, Session, D) {
5 var $notify, eventList, eventObject, log, logError, od, serializeObject, session, _api;
6 log = function() {
7 var x, _i, _len;
8 try {
9 for (_i = 0, _len = arguments.length; _i < _len; _i++) {
10 x = arguments[_i];
11 dump("" + x + "\n");
12 }
13 } catch (_error) {
14 console.log(arguments);
15 }
16 };
17 $notify = $({});
18 logError = function(jqXHR, textStatus, errorThrown) {
19 log("" + textStatus + " " + jqXHR.status + " " + errorThrown);
20 return $notify.trigger('od.fail', arguments);
21 };
22 eventList = ['od.clientaccess', 'od.libraryinfo', 'od.metadata', 'od.availability', 'od.patronaccess', 'od.patroninfo', 'od.holds', 'od.checkouts', 'od.interests', 'od.action', 'od.hold.update', 'od.hold.delete', 'od.checkout.update', 'od.checkout.delete', 'od.prefs', 'od.login', 'od.logout', 'od.error'];
23 eventObject = $({}).on(eventList.join(' '), function() {
24 var e, x, y;
25 e = arguments[0], x = arguments[1], y = 3 <= arguments.length ? __slice.call(arguments, 2) : [];
26 });
27 session = new Session(window.name, Boolean(C('eg_loggedin') || window.IAMXUL));
28 $(window).on('unload', function() {
29 return window.name = session.store();
30 });
31 _api = function(url, data) {
32 return $.ajax($.extend({}, {
33 headers: {
34 Authorization: "Basic " + config.credentials
35 },
36 url: url,
37 type: 'POST',
38 data: data
39 }));
40 };
41 serializeObject = function(a) {
42 var o;
43 o = {};
44 $.each(a, function() {
45 var n, v;
46 v = this.value || '';
47 if ((n = o[this.name]) !== void 0) {
48 if (!n.push) {
49 o[this.name] = [n];
50 }
51 return o[this.name].push(v);
52 } else {
53 return o[this.name] = v;
54 }
55 });
56 return o;
57 };
58 $.fn.extend({
59 _serializeObject: function() {
60 return serializeObject(this.serializeArray());
61 },
62 _serializeX: function(X) {
63 switch (X) {
64 case 'j':
65 return json.stringify(this._serializeX('o'));
66 case 'k':
67 return json.stringify(this._serializeX('a'));
68 case 'p':
69 return $.param(this._serializeX('a'));
70 case 's':
71 return this.serialize();
72 case 'o':
73 return serializeObject(this._serializeX('a'));
74 case 'a':
75 return this.serializeArray();
76 default:
77 return this;
78 }
79 }
80 });
81 od = {
82 $: eventObject.on({
83 'od.prefs': function(ev, x) {
84 return session.prefs.update(x);
85 },
86 'od.logout': function(ev, x) {
87 if (x === 'eg') {
88 if (session.token.is_patron_access()) {
89 return session = new Session();
90 }
91 }
92 }
93 }),
94 log: log,
95 labels: function(id) {
96 return session.labels[id] || id;
97 },
98 api: function(url, method, data, beforeSend) {
99 var v, _i, _len, _ref;
100 if (method === 'post') {
101 _ref = data.fields;
102 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
103 v = _ref[_i];
104 if (v.name === 'numberOfDays') {
105 v.value = 1 + M(v.value).diff(M().toArray().slice(0, 3), 'days');
106 }
107 }
108 }
109 return $.ajax($.extend({}, {
110 headers: {
111 Authorization: "" + session.token.token_type + " " + session.token.access_token
112 },
113 url: url,
114 type: method,
115 contentType: 'application/json; charset=utf-8',
116 data: json.stringify(data),
117 beforeSend: beforeSend
118 })).done(function() {
119 var id, x;
120 if (method === 'post') {
121 if (/\/holds|\/suspension/.test(url)) {
122 x = new D.Holds({
123 holds: [arguments[0]]
124 });
125 od.$.triggerHandler('od.hold.update', x);
126 }
127 if (/\/checkouts/.test(url)) {
128 x = new D.Checkouts({
129 checkouts: [arguments[0]]
130 });
131 od.$.triggerHandler('od.checkout.update', x);
132 }
133 }
134 if (method === 'delete') {
135 if (id = url.match(/\/holds\/(.+)\/suspension$/)) {
136 return;
137 }
138 if (id = url.match(/\/holds\/(.+)$/)) {
139 od.$.triggerHandler('od.hold.delete', id[1]);
140 }
141 if (id = url.match(/\/checkouts\/(.+)$/)) {
142 return od.$.triggerHandler('od.checkout.delete', id[1]);
143 }
144 }
145 }).fail(function() {
146 return od.$.triggerHandler('od.error', [url, arguments[0]]);
147 });
148 },
149 apiDiscAccess: function() {
150 var ok;
151 ok = function(x) {
152 session.token.update(x);
153 od.$.triggerHandler('od.clientaccess', x);
154 return x;
155 };
156 return _api(session.links.token.href, {
157 grant_type: 'client_credentials'
158 }).then(ok, logError);
159 },
160 apiLibraryInfo: function() {
161 var get, ok, retry;
162 get = function() {
2a839518
JD
163 if (session.token.token_type != null) {
164 return od.api(session.links.libraries.href);
165 } else {
166 return od.apiDiscAccess().then(get, logError).then(ok);
167 }
7c653aa3
JD
168 };
169 ok = function(x) {
170 session.links.update(x);
171 session.labels.update(x);
172 od.$.triggerHandler('od.libraryinfo', x);
173 return x;
174 };
175 retry = function(jqXHR) {
176 if (jqXHR.status === 401) {
177 if (session.token.is_patron_access()) {
178 return od.$.triggerHandler('od.logout', 'od');
179 } else {
180 return od.apiDiscAccess().then(get, logError).then(ok);
181 }
182 }
183 };
184 return get().then(ok, retry);
185 },
186 login: function(credentials) {
187 var login, ok;
188 if (credentials) {
189 session.creds.update(credentials);
190 session.token.update();
191 od.$.triggerHandler('od.login');
192 return;
193 }
194 if (session.token.is_patron_access()) {
195 return $.Deferred().resolve().promise();
196 }
197 login = function(prefs) {
198 var pw, scope, un, x;
199 x = function(label) {
200 var r, _ref;
201 r = new RegExp("" + label + "<\\/td>\\s+<td.+>(.*?)<\\/td>", 'i');
202 return ((_ref = prefs.match(r)) != null ? _ref[1] : void 0) || '';
203 };
204 session.prefs.update({
205 barcode: x('barcode'),
206 email_address: x('email address'),
207 home_library: x('home library')
208 });
209 un = session.prefs.barcode || session.creds.un();
210 pw = session.creds.pw(config.password_required(session.prefs.home_library));
211 session.creds.update();
212 scope = "websiteid:" + config.websiteID + " authorizationname:" + (config.authorizationname(session.prefs.home_library));
213 return _api(session.links.patrontoken.href, {
214 grant_type: 'password',
215 username: un,
216 password: pw,
217 password_required: config.password_required(session.prefs.home_library),
218 scope: scope
219 });
220 };
221 ok = function(x) {
222 session.token.update(x);
223 od.$.triggerHandler('od.patronaccess', x);
224 return x;
225 };
226 return $.get('/eg/opac/myopac/prefs').then(login).then(ok).then(od.apiPatronInfo).fail(log);
227 },
228 apiSearch: function(x) {
229 if (!x) {
230 return;
231 }
232 return od.api(session.links.products.href, get, x);
233 },
234 apiMetadata: function(x) {
235 if (!x.id) {
236 return;
237 }
238 return od.api("" + session.links.products.href + "/" + x.id + "/metadata").then(function(y) {
239 y = new D.Metadata(y);
240 od.$.triggerHandler('od.metadata', y);
241 return y;
242 }).fail(function() {
243 return od.$.triggerHandler('od.metadata', x);
244 });
245 },
246 apiAvailability: function(x) {
247 var alink, url, _ref;
248 if (!x.id) {
249 return;
250 }
2a839518 251 url = (alink = (_ref = session.links.availability) != null ? _ref.href : void 0) ? alink.replace('{reserveId}', x.id) : "" + session.links.products.href + "/" + x.id + "/availability";
7c653aa3
JD
252 return od.api(url).then(function(y) {
253 y = new D.Availability(y, session.prefs.email_address);
254 od.$.triggerHandler('od.availability', y);
255 return y;
256 }).fail(function() {
257 return od.$.triggerHandler('od.availability', x);
258 });
259 },
260 apiPatronInfo: function() {
261 var ok;
262 ok = function(x) {
263 session.links.update(x);
264 od.$.triggerHandler('od.patroninfo', x);
265 return x;
266 };
267 return od.api(session.links.patrons.href).then(ok, logError);
268 },
269 apiHoldsGet: function(x) {
270 if (!session.token.is_patron_access()) {
271 return;
272 }
273 return od.api("" + session.links.holds.href + ((x != null ? x.productID : void 0) ? x.productID : '')).then(function(y) {
274 y = new D.Holds(y);
275 od.$.triggerHandler('od.holds', y);
276 return y;
277 });
278 },
279 apiCheckoutsGet: function(x) {
280 if (!session.token.is_patron_access()) {
281 return;
282 }
283 return od.api("" + session.links.checkouts.href + ((x != null ? x.reserveID : void 0) ? x.reserveID : '')).then(function(y) {
284 y = new D.Checkouts(y);
285 od.$.triggerHandler('od.checkouts', y);
286 return y;
287 });
288 },
289 apiInterestsGet: function() {
290 return $.when(od.apiHoldsGet(), od.apiCheckoutsGet()).then(function(h, c) {
291 var y;
292 y = new D.Interests(h, c);
293 od.$.triggerHandler('od.interests', y);
294 return y;
295 });
296 }
297 };
298 return od;
299});