-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathprogressWindow_inject.js
More file actions
592 lines (510 loc) · 17.3 KB
/
Copy pathprogressWindow_inject.js
File metadata and controls
592 lines (510 loc) · 17.3 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2018 Center for History and New Media
George Mason University, Fairfax, Virginia, USA
http://zotero.org
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
(function() {
/**
* Only register progress window code in top window
*/
var isTopWindow = false;
if(window.top) {
try {
isTopWindow = window.top == window;
} catch(e) {};
}
if (isTopWindow) {
//
// Progress window initialization
//
// The progress window is created using React in an iframe, and we use the
// connector messaging system to communicate with.
//
var frameID = 'zotero-progress-window-frame';
var closeOnLeave = false;
var itemBoxOpen = false;
var lastSuccessfulTarget;
var frameReadyDeferred = Zotero.Promise.defer();
var frameInitialized;
var closeTimeoutID;
window.Zotero.progressWindowReady = frameReadyDeferred.promise;
var currentSessionID;
var createdSessions = new Set();
var updatingSession;
var nextSessionUpdateData;
var isReadOnly = false;
var isFilesEditable = false;
var syncDelayIntervalID;
var insideIframe = false;
var closeTimerDisabled = false;
var blurred = false;
var frameSrc;
var frameIsHidden = false;
frameSrc = Zotero.getExtensionURL('progressWindow/progressWindow.html');
var scrollX;
var scrollY;
async function sendMessage(name, data = {}) {
// frameId=null - send message to all frames
return Zotero.Messaging.sendMessage(name, data, null, null);
}
function addMessageListener(name, handler) {
return Zotero.Messaging.addMessageListener(name, handler);
}
// The progress window component is initialized asynchronously, so queue updates and send them
// to the iframe once the component is ready
function addEvent(name, data) {
frameReadyDeferred.promise.then(function() {
sendMessage(`progressWindowIframe.${name}`, data);
});
}
/**
* Transform a raw translator item into the format expected by the ItemBox component:
* { id, fields: [{name, label, value}], creators: [...], creatorTypes: [{value, label}] }
*
* Field and type labels come from Zotero.Schema.locale (en-US locale from schema.json),
* stored during Zotero.Schema.init() in schema.js.
*/
function formatItemMetadata(id, item) {
let locale = Zotero.Schema.locale || {};
let fieldLocale = locale.fields || {};
let itemTypeLocale = locale.itemTypes || {};
let creatorTypeLocale = locale.creatorTypes || {};
let itemTypeID = Zotero.ItemTypes.getID(item.itemType);
let fieldIDs = Zotero.ItemFields.getItemTypeFields(itemTypeID);
let fields = [
{
name: 'itemType',
label: Zotero.getString("progressWindow_itemBox_itemType"),
value: itemTypeLocale[item.itemType] || item.itemType
}
];
for (let fieldID of fieldIDs) {
let fieldName = Zotero.ItemFields.getName(fieldID);
let value = item[fieldName];
let field = {
name: fieldName,
label: fieldLocale[fieldName] || fieldName,
value: (value != null && value !== '') ? String(value) : ''
};
if (Zotero.ItemFields.isMultiline(fieldID)) {
field.multiline = true;
}
fields.push(field);
}
let creatorTypeObjs = Zotero.CreatorTypes.getTypesForItemType(itemTypeID);
let creatorTypes = creatorTypeObjs.map(ct => ({
value: ct.name,
label: creatorTypeLocale[ct.name] || ct.name
}));
return {
id,
fields,
creators: item.creators || [],
creatorTypes
};
}
function changeHeadline() {
isReadOnly = arguments.length <= 2;
addEvent("changeHeadline", Array.from(arguments));
}
function makeReadOnly() {
isReadOnly = true;
addEvent("makeReadOnly", [lastSuccessfulTarget]);
}
/**
* Get selected collection and collections list from client and update popup
*/
async function updateFromClient(prefix, retryOnReadOnly = true) {
try {
var response = await Zotero.Connector.callMethod("getSelectedCollection", { switchToReadableLibrary: true })
}
catch (e) {
// TODO: Shouldn't this be coupled to the actual save process?
changeHeadline("Saving to zotero.org");
return;
}
// If we're reshowing the current session's popup, override the selected location with the
// last successful tarGet, since the selected collection in the client might have changed
if (lastSuccessfulTarget) {
response.id = lastSuccessfulTarget.id;
response.name = lastSuccessfulTarget.name;
response.libraryEditable = true;
}
// The library will change to editable upon save to a read-only library,
// so the currently selected library information is wrong/irrelevant
if (response.libraryEditable === false) {
if (retryOnReadOnly) {
setTimeout(() => updateFromClient(prefix, false), 250);
}
return;
}
var id;
// Legacy response for libraries
if (!response.id) {
id = "L" + response.libraryID;
}
// Legacy response for collections
else if (typeof response.id != 'string') {
id = "C" + response.id;
}
else {
id = response.id;
}
if (!prefix) {
prefix = Zotero.getString('progressWindow_savingTo');
}
var target = {
id,
name: response.name,
filesEditable: response.filesEditable
};
if (response.libraryEditable) {
lastSuccessfulTarget = target;
}
let targets = response.targets.filter(t => !isFilesEditable || t.filesEditable);
// TEMP: Make sure libraries have levels (added to client in 5.0.46)
if (response.targets) {
for (let row of response.targets) {
if (!row.level) {
row.level = 0;
}
}
}
// Format tags for autocomplete
// Tags array contains objects {tag : ""} that may contain duplicate values due to different types
// Unwrap the tag objects and deduplicate tags values to keep this object format {libraryID: [tag1, tag2, ...]}
let tags = {};
Object.entries(response.tags || []).forEach(([libraryID, tagArr]) => {
tags[libraryID] = [...new Set(tagArr.map(item => item.tag))];
});
changeHeadline(prefix, target, targets, tags);
}
async function addError() {
await showFrame();
addEvent("addError", Array.from(arguments));
}
function hideFrame() {
insideIframe = false;
var frame = document.getElementById(frameID);
if (frame) {
frame.style.display = 'none';
addEvent("hidden");
}
// Stop delaying syncs when the frame closes
if (syncDelayIntervalID) {
clearInterval(syncDelayIntervalID);
}
}
function resetFrame() {
stopCloseTimer();
addEvent('reset');
}
async function destroyFrame() {
stopCloseTimer();
var frame = await frameReadyDeferred.promise;
document.body.removeChild(frame);
frameReadyDeferred = Zotero.Promise.defer();
}
function handleMouseEnter() {
insideIframe = true;
blurred = false;
stopCloseTimer();
// See scroll listener in initFrame()
scrollX = window.scrollX;
scrollY = window.scrollY;
}
function handleMouseLeave() {
insideIframe = false;
if (closeOnLeave) {
startCloseTimer(closeOnLeave);
}
}
function startCloseTimer(delay) {
// Don't start the timer if the mouse is over the popup or the tags box has focus
if (insideIframe) return;
if (closeTimerDisabled) return;
if (!delay) delay = 5000;
// Give the user more time if they have the item box open
if (itemBoxOpen) delay = Math.max(delay, 20000);
stopCloseTimer();
closeTimeoutID = setTimeout(hideFrame, delay);
}
function stopCloseTimer() {
if (closeTimeoutID) {
clearTimeout(closeTimeoutID);
}
}
async function initFrame() {
// Create the iframe
var iframe = document.createElement('iframe');
iframe.id = frameID;
iframe.src = frameSrc;
iframe.title = Zotero.getString('general_saveTo', 'Zotero');
iframe.setAttribute('data-single-file-hidden-frame', '');
var style = {
position: 'fixed',
top: '15px',
left: 'unset',
right: '8px',
width: '351px',
maxWidth: '95%',
height: '120px',
border: "none",
padding: "none",
margin: "initial",
zIndex: 2147483647,
display: 'none',
// frame becomes scrollable if the user zooms in (wcag 1.4.10), or half of it will be inaccessible
maxHeight: '90vh'
};
for (let i in style) iframe.style[i] = style[i];
window.top.document.body.appendChild(iframe);
// Keep track of clicks on the window so that when the iframe document is blurred we can
// distinguish between a click on the document and switching to another window
var lastClick;
window.addEventListener('click', function () {
lastClick = new Date();
}, true);
// Prevent scrolling of parent document when scrolling to bottom of target list in iframe
// From https://stackoverflow.com/a/32283373
//
// This is jittery and it would be nice to do better
document.addEventListener('scroll', function (event) {
if (insideIframe) {
window.scrollTo(scrollX, scrollY);
}
});
//
// Handle messages from the progress window iframe
//
addMessageListener('progressWindowIframe.registered', function() {
frameReadyDeferred.resolve(iframe);
});
// Adjust iframe height when inner document is resized
addMessageListener('progressWindowIframe.resized', function(data) {
iframe.style.height = (data.height + 33) + "px";
});
// Update the client or API with changes
var handleUpdated = async function (data) {
// If we're making changes, don't close the popup and keep delaying syncs
stopCloseTimer();
blurred = false;
// If target selector hasn't been used yet, data.target may be undefined.
// Use lastSuccessfulTarget as fallback.
if (!data.target && !lastSuccessfulTarget) return;
var target = data.target || lastSuccessfulTarget;
// If the session isn't yet registered or a session update is in progress,
// store the data to run after, overwriting any already-queued data
if (!createdSessions.has(currentSessionID) || updatingSession) {
nextSessionUpdateData = data;
return;
}
updatingSession = true;
try {
await sendMessage(
"updateSession",
{
target: target.id,
tags: data.tags,
updatedMetadata: data.updatedMetadata,
note: data.note.replace(/\n/g, "<br>"), // replace newlines with <br> for note-editor
resaveAttachments: !lastSuccessfulTarget.filesEditable && target.filesEditable,
removeAttachments: lastSuccessfulTarget.filesEditable && !target.filesEditable
}
);
}
// Collapse popup on error
catch (e) {
makeReadOnly();
throw e;
}
finally {
updatingSession = false;
}
if (nextSessionUpdateData) {
data = nextSessionUpdateData;
nextSessionUpdateData = null;
return handleUpdated(data);
}
// Keep track of last successful target to show on reopen and failure
lastSuccessfulTarget = target;
};
// Once a session is created in the client, send any queued session data
Zotero.Messaging.addMessageListener('progressWindow.sessionCreated', async function (args) {
var sessionID = args.sessionID;
createdSessions.add(sessionID);
if (nextSessionUpdateData) {
let data = nextSessionUpdateData;
nextSessionUpdateData = null;
handleUpdated(data)
}
});
// Sent by the progress window when changes are made in the target selector
addMessageListener('progressWindowIframe.updated', handleUpdated);
// Keep track of when the mouse is over the popup, for various purposes
addMessageListener('progressWindowIframe.mouseenter', handleMouseEnter);
addMessageListener('progressWindowIframe.mouseleave', handleMouseLeave);
addMessageListener('progressWindowIframe.disableCloseTimer', () => closeTimerDisabled = true);
addMessageListener('progressWindowIframe.enableCloseTimer', () => closeTimerDisabled = false);
addMessageListener('progressWindowIframe.itemBoxToggled', (data) => { itemBoxOpen = data.open; });
addMessageListener('progressWindowIframe.blurred', async function() {
blurred = true;
// Hide iframe if it loses focus and the user recently clicked on the main page
// (i.e., they didn't just switch to another window)
await Zotero.Promise.delay(150);
if (lastClick > new Date() - 500) {
if (itemBoxOpen) {
startCloseTimer(20000);
}
else {
hideFrame();
}
}
});
addMessageListener('progressWindowIframe.close', function() {
hideFrame();
window.focus();
});
await frameReadyDeferred.promise;
return iframe;
}
/**
* Shows the frame (initializing it if necessary)
* @returns {Promise<iframe>}
*/
async function showFrame() {
let iframe
if (!frameInitialized) {
frameInitialized = true;
iframe = await initFrame();
} else {
iframe = await frameReadyDeferred.promise;
}
// If the frame has been hidden since we started to open it, don't make it visible
if (!frameIsHidden) {
addEvent('shown');
iframe.style.display = 'block';
}
// Delay syncs by 10 seconds at a time (specified in server_connector.js) while
// the selector is open. Run every 7.5 seconds to make sure the request gets
// there in time.
if (syncDelayIntervalID) {
clearInterval(syncDelayIntervalID);
}
syncDelayIntervalID = setInterval(() => {
// Don't prevent syncing when read-only or when tab isn't visible.
// See note in ProgressWindow.jsx::handleVisibilityChange() for latter.
if (isReadOnly || document.hidden || blurred) return;
Zotero.Connector.callMethod("delaySync", {});
}, 7500);
return iframe;
}
/**
* This is called after an item has started to save in order to show the progress window
*/
Zotero.Messaging.addMessageListener("progressWindow.show", async function (args) {
// Mark frame as visible immediately, so that if it's hidden before it's done initializing
// (e.g., when displaying the Select Items window) we can skip displaying it
frameIsHidden = false;
var [sessionID, headline, readOnly, filesEditable] = args;
if (typeof filesEditable != "undefined") {
isFilesEditable = filesEditable;
}
// Reopening existing popup
if (currentSessionID) {
// If session has changed, reset state before reopening
if (currentSessionID != sessionID) {
resetFrame();
// Disable closing on mouseleave until save finishes. (This is disabled initially
// but is enabled when a save finishes, so we have to redisable it for a new session.)
closeOnLeave = false;
lastSuccessfulTarget = null;
}
}
currentSessionID = sessionID;
await showFrame();
if (readOnly) {
changeHeadline(headline);
}
else {
await updateFromClient(headline);
}
return true;
});
/**
* @param {String} sessionID
* @param {Integer} id
* @param {String} iconSrc
* @param {String} title
* @param {Integer|false} parentItem
* @param {Integer|false} progress
*/
Zotero.Messaging.addMessageListener("progressWindow.itemProgress", (data) => {
// Skip progress updates for a previous session
if (data.sessionID && data.sessionID != currentSessionID) return;
// Keep progress window open as long as we're receiving updates
addEvent("updateProgress", [data.id, data]);
});
Zotero.Messaging.addMessageListener("progressWindow.setItemMetadata", (data) => {
if (data.sessionID && data.sessionID != currentSessionID) return;
addEvent("setItemMetadata", [formatItemMetadata(data.id, data.item)]);
});
Zotero.Messaging.addMessageListener("progressWindow.close", function () {
// Mark frame as hidden so that if this is called after a progressWindow.show but before
// the popup has been initialized (e.g., when displaying the Select Items dialog) it's
// not made visible
frameIsHidden = true;
hideFrame();
});
Zotero.Messaging.addMessageListener("progressWindow.setSession", function (sessionID) {
currentSessionID = sessionID;
});
Zotero.Messaging.addMessageListener("progressWindow.reopen", function () {
showFrame();
});
Zotero.Messaging.addMessageListener("progressWindow.done", async (returnValue) => {
const isTabFocused = await Zotero.Connector_Browser.isTabFocused();
if (!isTabFocused) {
// Don't queue hiding the progress bar if the save finished when the tab was not focused,
// so that when the user switches to it they can review the save.
// However, we set closeOnLeave, because when you switch to the tab, it seems that
// iframe mouseLeave event is automatically fired, and either way we don't want the progress window
// to be displayed forever.
closeOnLeave = 8000;
return;
}
closeOnLeave = 2500;
if (document.location.href.startsWith(Zotero.getExtensionURL('confirm/confirm.html'))) {
// Handled in contentTypeHandler
return;
}
if (returnValue[0]) {
startCloseTimer(3000);
addEvent("willHide");
}
else {
if (returnValue.length < 2) {
returnValue.push('translationError');
}
addError(returnValue[1], ...returnValue.slice(2));
startCloseTimer(8000);
}
});
Zotero.Messaging.addMessageListener("progressWindow.error", (args) => {
addError(args.shift(), ...args);
})
}
})();