note.js (2866B)
1 /* 2 ***** BEGIN LICENSE BLOCK ***** 3 4 Copyright © 2009 Center for History and New Media 5 George Mason University, Fairfax, Virginia, USA 6 http://zotero.org 7 8 This file is part of Zotero. 9 10 Zotero is free software: you can redistribute it and/or modify 11 it under the terms of the GNU Affero General Public License as published by 12 the Free Software Foundation, either version 3 of the License, or 13 (at your option) any later version. 14 15 Zotero is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU Affero General Public License for more details. 19 20 You should have received a copy of the GNU Affero General Public License 21 along with Zotero. If not, see <http://www.gnu.org/licenses/>. 22 23 ***** END LICENSE BLOCK ***** 24 */ 25 26 var noteEditor; 27 var notifierUnregisterID; 28 29 async function onLoad() { 30 noteEditor = document.getElementById('zotero-note-editor'); 31 noteEditor.mode = 'edit'; 32 33 // Set font size from pref 34 Zotero.setFontSize(noteEditor); 35 36 if (window.arguments) { 37 var io = window.arguments[0]; 38 } 39 40 var itemID = parseInt(io.itemID); 41 var collectionID = parseInt(io.collectionID); 42 var parentItemKey = io.parentItemKey; 43 44 if (itemID) { 45 var ref = await Zotero.Items.getAsync(itemID); 46 noteEditor.item = ref; 47 document.title = ref.getNoteTitle(); 48 } 49 else { 50 if (parentItemKey) { 51 var ref = Zotero.Items.getByLibraryAndKey(parentItemKey); 52 noteEditor.parentItem = ref; 53 } 54 else { 55 if (collectionID && collectionID != '' && collectionID != 'undefined') { 56 noteEditor.collection = Zotero.Collections.get(collectionID); 57 } 58 } 59 noteEditor.refresh(); 60 } 61 62 noteEditor.focus(); 63 notifierUnregisterID = Zotero.Notifier.registerObserver(NotifyCallback, 'item', 'noteWindow'); 64 } 65 66 // If there's an error saving a note, close the window and crash the app 67 function onError() { 68 try { 69 window.opener.ZoteroPane.displayErrorMessage(); 70 } 71 catch (e) { 72 Zotero.logError(e); 73 } 74 window.close(); 75 } 76 77 78 function onUnload() { 79 Zotero.Notifier.unregisterObserver(notifierUnregisterID); 80 81 if (noteEditor.item) { 82 window.opener.ZoteroPane.onNoteWindowClosed(noteEditor.item.id, noteEditor.value); 83 } 84 } 85 86 var NotifyCallback = { 87 notify: function(action, type, ids){ 88 if (noteEditor.item && ids.includes(noteEditor.item.id)) { 89 var noteTitle = noteEditor.item.getNoteTitle(); 90 if (!document.title && noteTitle != '') { 91 document.title = noteTitle; 92 } 93 94 // Update the window name (used for focusing) in case this is a new note 95 window.name = 'zotero-note-' + noteEditor.item.id; 96 } 97 } 98 } 99 100 addEventListener("load", function(e) { onLoad(e); }, false); 101 addEventListener("unload", function(e) { onUnload(e); }, false);