summaryrefslogtreecommitdiff
path: root/National Library of Australia (new catalog).js
blob: e311f379bf8190d25d5bf3db736d69dea3ab770a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
{
	"translatorID": "54ac4ec1-9d07-45d3-9d96-48bed3411fb6",
	"label": "National Library of Australia (new catalog)",
	"creator": "Mark Triggs, Steve McPhillips and Matt Burton",
	"target": "^https?://catalogue\\.nla\\.gov\\.au",
	"minVersion": "1.0.0b4.r5",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2013-04-17 19:42:02"
}

function detectWeb(doc, url) {
	if (url.match("/Record/[0-9]+")) {
		var format = doc.getElementById("myformat").textContent;
		return computeFormat(format);
		
	} else if (url.match ("/Search/Home") && doc.getElementById ("resultItemLine1")) {
		return "multiple";
	}
}

// map the nla formats to zotero formats
function computeFormat(format){
	// clean up whitespace and remove commas from items with multiple formats
	format = Zotero.Utilities.trimInternal(format.replace(',', ''));

	if (format == "Audio") {
		return "audioRecording";
	}
	else if (format == "Book") {
		return "book";
	}
	else if (format == "Journal/Newspaper") {
		return "journalArticle";
	}
	else if (format == "Manuscript") {
		return "manuscript";
	}
	else if (format == "Map") {
		return "map";
	}
	else if (format == "Music") {
		return "audioRecording";
	}
	else if (format == "Online") {
		return "webpage";
	}
	else if (format == "Picture") {
		return "artwork";
	}
	else if (format == "Video") {
		return "videoRecording";
	}
	else {
		return "book";
	}

}

// TODO: Remove this when we drop support for Fx3
if (!JSON) {
	var JSON = new function() {
		this.parse = function (arg) {
			var j;
			if (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/.test(arg.
					replace(/\\./g, '@').
					replace(/"[^"\\\n\r]*"/g, ''))) {
				// Friendly AMO reviewer: This is the official json.org library and is safe.
				j = eval('(' + arg + ')');
				return j;
			}
			throw new SyntaxError('parseJSON');
		}
	}
}

function load_item(responseText, url, format) {
	var metadata = JSON.parse(Zotero.Utilities.trimInternal(responseText));
	var bibid = url.match("^.*\/Record/([0-9]+)")[1];
	var newItem = new Zotero.Item(format[bibid]);

	/* load in our authors */
	if (metadata.authors) {
		for (var i=0; i< metadata.authors.length ; i++) {
			newItem.creators.push(Zotero.Utilities.cleanAuthor
								  (metadata.authors[i], "author", true));
		}
	}

	/* and our tags */
	if (metadata.tags) {
		for (var i=0; i< metadata.tags.length ; i++) {
			newItem.tags.push(metadata.tags[i]);
		}
	}
	
	/* and our summary */
	if (metadata.notes) {
		newItem.notes.push ({"note": metadata.notes});
	}

	/* and everything else */
	for (var attr in metadata) {
		if (!newItem[attr]) {
			newItem[attr] = metadata[attr];
		}
	}
	newItem.repository = "National Library of Australia";
	newItem.complete();
}

function doWeb(doc, url) {
	var format = detectWeb(doc, url);
	var items = {};
	// does javascript have an easy way to test if object has properties?
	var itemsHasProperties = false;
	if (format == "multiple") {
		for (var url in Zotero.selectItems((Zotero.Utilities.getItemArray
											(doc, doc, "/Record/[0-9]+")))) {
			var bibid = url.match("^.*\/Record/([0-9]+)")[1];
			// grab the item type for that bibid so we don't have to make a processDocuments call for each
			var xpath = "//div[contains(./div/a/@href, '"+bibid+"')]/div[@class = 'resultItemLine3']/span/text()";
			var nlaFormat = doc.evaluate(xpath, doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
			// populate an associative array with bibid -> format for the doGet
			items[bibid] = computeFormat(nlaFormat);
			itemsHasProperties = true; // items has stuff
		}
	} else {
		var bibid = url.match("^.*\/Record/([0-9]+)")[1];
		items[bibid] = format;
		itemsHasProperties = true; // items has stuff
	}
	// only continue if there are items to create
	if (itemsHasProperties) {
		var urls = [];
		for (var bibid in items) {
			urls.push("http://catalogue.nla.gov.au/Record/" + bibid + "/Export?style=zotero");
		}
		// the bibid->format associative array prevents the need for a doGet nested in processDocs
		Zotero.Utilities.HTTP.doGet(urls, 
			function(text, obj, url) { load_item(text, url, items);},
			function(){ Zotero.done();});
		Zotero.wait();
	}
}
/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://catalogue.nla.gov.au/Record/773336?lookfor=labor&offset=10&max=65985",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Richard Allen",
						"lastName": "Lester",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [
					"Working class",
					"United States.",
					"Labor unions"
				],
				"seeAlso": [],
				"attachments": [],
				"title": "Labor : readings on major issues / [edited by] Richard A. Lester",
				"publisher": "Random House",
				"callNumber": "331.082 L642",
				"place": "New York",
				"date": "New York : Random House, [1967]",
				"authors": "Lester, Richard Allen, 1908-",
				"extra": "Bibliographical footnotes.",
				"libraryCatalog": "National Library of Australia",
				"shortTitle": "Labor"
			}
		]
	},
	{
		"type": "web",
		"url": "http://catalogue.nla.gov.au/Search/Home?lookfor=labor&type=all&limit%5B%5D=&submit=Find&filter[]=language:%22eng%22",
		"items": "multiple"
	}
]
/** END TEST CASES **/