overdrive-eg-opac: initial commit of compiled JS
[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() {
163 return od.api(session.links.libraries.href);
164 };
165 ok = function(x) {
166 session.links.update(x);
167 session.labels.update(x);
168 od.$.triggerHandler('od.libraryinfo', x);
169 return x;
170 };
171 retry = function(jqXHR) {
172 if (jqXHR.status === 401) {
173 if (session.token.is_patron_access()) {
174 return od.$.triggerHandler('od.logout', 'od');
175 } else {
176 return od.apiDiscAccess().then(get, logError).then(ok);
177 }
178 }
179 };
180 return get().then(ok, retry);
181 },
182 login: function(credentials) {
183 var login, ok;
184 if (credentials) {
185 session.creds.update(credentials);
186 session.token.update();
187 od.$.triggerHandler('od.login');
188 return;
189 }
190 if (session.token.is_patron_access()) {
191 return $.Deferred().resolve().promise();
192 }
193 login = function(prefs) {
194 var pw, scope, un, x;
195 x = function(label) {
196 var r, _ref;
197 r = new RegExp("" + label + "<\\/td>\\s+<td.+>(.*?)<\\/td>", 'i');
198 return ((_ref = prefs.match(r)) != null ? _ref[1] : void 0) || '';
199 };
200 session.prefs.update({
201 barcode: x('barcode'),
202 email_address: x('email address'),
203 home_library: x('home library')
204 });
205 un = session.prefs.barcode || session.creds.un();
206 pw = session.creds.pw(config.password_required(session.prefs.home_library));
207 session.creds.update();
208 scope = "websiteid:" + config.websiteID + " authorizationname:" + (config.authorizationname(session.prefs.home_library));
209 return _api(session.links.patrontoken.href, {
210 grant_type: 'password',
211 username: un,
212 password: pw,
213 password_required: config.password_required(session.prefs.home_library),
214 scope: scope
215 });
216 };
217 ok = function(x) {
218 session.token.update(x);
219 od.$.triggerHandler('od.patronaccess', x);
220 return x;
221 };
222 return $.get('/eg/opac/myopac/prefs').then(login).then(ok).then(od.apiPatronInfo).fail(log);
223 },
224 apiSearch: function(x) {
225 if (!x) {
226 return;
227 }
228 return od.api(session.links.products.href, get, x);
229 },
230 apiMetadata: function(x) {
231 if (!x.id) {
232 return;
233 }
234 return od.api("" + session.links.products.href + "/" + x.id + "/metadata").then(function(y) {
235 y = new D.Metadata(y);
236 od.$.triggerHandler('od.metadata', y);
237 return y;
238 }).fail(function() {
239 return od.$.triggerHandler('od.metadata', x);
240 });
241 },
242 apiAvailability: function(x) {
243 var alink, url, _ref;
244 if (!x.id) {
245 return;
246 }
247 url = (alink = (_ref = session.links.availability) != null ? _ref.href : void 0) ? alink.replace('{crId}', x.id) : "" + session.links.products.href + "/" + x.id + "/availability";
248 return od.api(url).then(function(y) {
249 y = new D.Availability(y, session.prefs.email_address);
250 od.$.triggerHandler('od.availability', y);
251 return y;
252 }).fail(function() {
253 return od.$.triggerHandler('od.availability', x);
254 });
255 },
256 apiPatronInfo: function() {
257 var ok;
258 ok = function(x) {
259 session.links.update(x);
260 od.$.triggerHandler('od.patroninfo', x);
261 return x;
262 };
263 return od.api(session.links.patrons.href).then(ok, logError);
264 },
265 apiHoldsGet: function(x) {
266 if (!session.token.is_patron_access()) {
267 return;
268 }
269 return od.api("" + session.links.holds.href + ((x != null ? x.productID : void 0) ? x.productID : '')).then(function(y) {
270 y = new D.Holds(y);
271 od.$.triggerHandler('od.holds', y);
272 return y;
273 });
274 },
275 apiCheckoutsGet: function(x) {
276 if (!session.token.is_patron_access()) {
277 return;
278 }
279 return od.api("" + session.links.checkouts.href + ((x != null ? x.reserveID : void 0) ? x.reserveID : '')).then(function(y) {
280 y = new D.Checkouts(y);
281 od.$.triggerHandler('od.checkouts', y);
282 return y;
283 });
284 },
285 apiInterestsGet: function() {
286 return $.when(od.apiHoldsGet(), od.apiCheckoutsGet()).then(function(h, c) {
287 var y;
288 y = new D.Interests(h, c);
289 od.$.triggerHandler('od.interests', y);
290 return y;
291 });
292 }
293 };
294 return od;
295});