Project

General

Profile

Download (2.44 KB) Statistics
| Branch: | Revision:

hydrilla-fixes-bundle / src / worldcat-org-fix / worldcat.js @ 70022b58

1
/**
2
 * SPDX-License-Identifier: Apache-2.0
3
 *
4
 * Copyright © 2021 jahoti <jahoti@tilde.team>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *    http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18

    
19
var pathParts = location.pathname.split('/'), itemRef = pathParts[pathParts.length - 1];
20

    
21
// Generate a function which, when invoked, loads the catalog holdings starting at i (one-indexed) focused on loc
22
function generateGoTo(i, set_loc) {
23
	return function (e) {
24
		e.preventDefault();
25
		; // If this is a new search, "set_loc" won't be set; set it
26
		var xhr = new XMLHttpRequest(), loc = set_loc || encodeURIComponent(locInput.value);
27
		xhr.onreadystatechange = function () {
28
			if (this.readyState === 4) {
29
				if (this.status === 200) {
30
					retrieved.innerHTML = this.responseText;
31

    
32
					var i, node = document.getElementById('libslocator');
33
					node.parentNode.removeChild(node);
34
					for (node of retrieved.querySelectorAll('a[href^="javascript:findLibs(\'\', "]')) {
35
						i = parseInt(node.href.split(',', 2)[1]);
36
						node.onclick = generateGoTo(i, loc);
37
					}
38
				}
39
				else alert('Search failed: response code ' + this.status);
40
			}
41
		}
42

    
43
		xhr.open('GET', 'https://www.worldcat.org/wcpa/servlet/org.oclc.lac.ui.ajax.ServiceServlet?wcoclcnum=' + itemRef + '&start_holding='
44
				+ i + '&serviceCommand=holdingsdata&loc=' + loc, true);
45
		xhr.send();
46
	};
47
}
48

    
49

    
50
var retriever = document.querySelector('.retrieving'), retrieved = document.getElementById('donelocator');
51

    
52
var locForm = document.createElement('form'), locLabel = document.createElement('label'), locInput = document.createElement('input'),
53
	locSubmit = document.createElement('input');
54

    
55
locForm.appendChild(locLabel);
56
locForm.appendChild(locInput);
57
locForm.appendChild(locSubmit);
58

    
59
locInput.name = locLabel.htmlFor = 'cat_location';
60
locInput.type = 'text';
61
locInput.required = 'yes';
62
locLabel.innerText = 'Find copies closest to: ';
63
locSubmit.value = 'Go';
64
locSubmit.type = 'submit';
65
locForm.onsubmit = generateGoTo(1);
66

    
67
retriever.parentNode.replaceChild(locForm, retriever);
(2-2/2)