www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

recognizePDFDialog.js (7156B)


      1 /*
      2     ***** BEGIN LICENSE BLOCK *****
      3     
      4     Copyright © 2018 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 /**
     27  * @fileOverview Tools for automatically retrieving a citation for the given PDF
     28  */
     29 
     30 /**
     31  * Front end for recognizing PDFs
     32  * @namespace
     33  */
     34 
     35 var Zotero_RecognizePDF_Dialog = new function () {
     36 	const SUCCESS_IMAGE = 'chrome://zotero/skin/tick.png';
     37 	const FAILURE_IMAGE = 'chrome://zotero/skin/cross.png';
     38 	const LOADING_IMAGE = 'chrome://zotero/skin/arrow_refresh.png';
     39 	
     40 	let _progressWindow = null;
     41 	let _progressIndicator = null;
     42 	let _rowIDs = [];
     43 	
     44 	this.open = function() {
     45 		if (_progressWindow) {
     46 			_progressWindow.focus();
     47 			return;
     48 		}
     49 		_progressWindow = window.openDialog('chrome://zotero/content/recognizePDFDialog.xul', '', 'chrome,close=yes,resizable=yes,dependent,dialog,centerscreen');
     50 		_progressWindow.addEventListener('pageshow', _onWindowLoaded.bind(this), false);
     51 	};
     52 	
     53 	function close() {
     54 		_progressWindow.close();
     55 	}
     56 	
     57 	function _getImageByStatus(status) {
     58 		if (status === Zotero.RecognizePDF.ROW_PROCESSING) {
     59 			return LOADING_IMAGE;
     60 		}
     61 		else if (status === Zotero.RecognizePDF.ROW_FAILED) {
     62 			return FAILURE_IMAGE;
     63 		}
     64 		else if (status === Zotero.RecognizePDF.ROW_SUCCEEDED) {
     65 			return SUCCESS_IMAGE;
     66 		}
     67 		return '';
     68 	}
     69 	
     70 	function _rowToTreeItem(row) {
     71 		let treeitem = _progressWindow.document.createElement('treeitem');
     72 		treeitem.setAttribute('id', 'item-' + row.id);
     73 		
     74 		let treerow = _progressWindow.document.createElement('treerow');
     75 		
     76 		let treecell = _progressWindow.document.createElement('treecell');
     77 		treecell.setAttribute('id', 'item-' + row.id + '-icon');
     78 		treecell.setAttribute('src', _getImageByStatus(row.status));
     79 		
     80 		treerow.appendChild(treecell);
     81 		
     82 		treecell = _progressWindow.document.createElement('treecell');
     83 		treecell.setAttribute('label', row.fileName);
     84 		treerow.appendChild(treecell);
     85 		
     86 		treecell = _progressWindow.document.createElement('treecell');
     87 		treecell.setAttribute('id', 'item-' + row.id + '-title');
     88 		treecell.setAttribute('label', row.message);
     89 		treerow.appendChild(treecell);
     90 		
     91 		treeitem.appendChild(treerow);
     92 		return treeitem;
     93 	}
     94 	
     95 	function _onWindowLoaded() {
     96 		let rows = Zotero.RecognizePDF.getRows();
     97 		_rowIDs = [];
     98 		let treechildren = _progressWindow.document.getElementById('treechildren');
     99 		
    100 		for (let row of rows) {
    101 			_rowIDs.push(row.id);
    102 			let treeitem = _rowToTreeItem(row);
    103 			treechildren.appendChild(treeitem);
    104 		}
    105 		
    106 		_progressWindow.document.getElementById('tree').addEventListener('dblclick',
    107 			function (event) {
    108 				_onDblClick(event, this);
    109 			}
    110 		);
    111 		
    112 		_progressIndicator = _progressWindow.document.getElementById('progress-indicator');
    113 		_progressWindow.document.getElementById('cancel-button')
    114 			.addEventListener('command', function () {
    115 				close();
    116 				Zotero.RecognizePDF.cancel();
    117 			}, false);
    118 		
    119 		_progressWindow.document.getElementById('minimize-button')
    120 			.addEventListener('command', function () {
    121 				close();
    122 			}, false);
    123 		
    124 		_progressWindow.document.getElementById('close-button')
    125 			.addEventListener('command', function () {
    126 				close();
    127 				Zotero.RecognizePDF.cancel();
    128 			}, false);
    129 		
    130 		_progressWindow.addEventListener('keypress', function (e) {
    131 			if (e.keyCode === KeyEvent.DOM_VK_ESCAPE) {
    132 				// If done processing, Esc is equivalent to Close rather than Minimize
    133 				if (Zotero.RecognizePDF.getTotal() == Zotero.RecognizePDF.getProcessedTotal()) {
    134 					Zotero.RecognizePDF.cancel();
    135 				}
    136 				close();
    137 			}
    138 		});
    139 		
    140 		_progressWindow.addEventListener('unload', function () {
    141 			Zotero.RecognizePDF.removeListener('rowadded');
    142 			Zotero.RecognizePDF.removeListener('rowupdated');
    143 			Zotero.RecognizePDF.removeListener('rowdeleted');
    144 			_progressWindow = null;
    145 			_progressIndicator = null;
    146 			_rowIDs = [];
    147 		});
    148 		
    149 		_updateProgress();
    150 		
    151 		Zotero.RecognizePDF.addListener('rowadded', function (row) {
    152 			_rowIDs.push(row.id);
    153 			let treeitem = _rowToTreeItem(row);
    154 			treechildren.appendChild(treeitem);
    155 			_updateProgress();
    156 		});
    157 		
    158 		Zotero.RecognizePDF.addListener('rowupdated', function (row) {
    159 			let itemIcon = _progressWindow.document.getElementById('item-' + row.id + '-icon');
    160 			let itemTitle = _progressWindow.document.getElementById('item-' + row.id + '-title');
    161 			
    162 			itemIcon.setAttribute('src', _getImageByStatus(row.status));
    163 			itemTitle.setAttribute('label', row.message);
    164 			_updateProgress();
    165 		});
    166 		
    167 		Zotero.RecognizePDF.addListener('rowdeleted', function (row) {
    168 			_rowIDs.splice(_rowIDs.indexOf(row.id), 1);
    169 			let treeitem = _progressWindow.document.getElementById('item-' + row.id);
    170 			treeitem.parentNode.removeChild(treeitem);
    171 			_updateProgress();
    172 		});
    173 	}
    174 	
    175 	function _updateProgress() {
    176 		if (!_progressWindow) return;
    177 		let total = Zotero.RecognizePDF.getTotal();
    178 		let processed = Zotero.RecognizePDF.getProcessedTotal();
    179 		_progressIndicator.value = processed * 100 / total;
    180 		if (processed === total) {
    181 			_progressWindow.document.getElementById("cancel-button").hidden = true;
    182 			_progressWindow.document.getElementById("minimize-button").hidden = true;
    183 			_progressWindow.document.getElementById("close-button").hidden = false;
    184 			_progressWindow.document.getElementById("label").value = Zotero.getString('recognizePDF.complete.label');
    185 		}
    186 		else {
    187 			_progressWindow.document.getElementById("cancel-button").hidden = false;
    188 			_progressWindow.document.getElementById("minimize-button").hidden = false;
    189 			_progressWindow.document.getElementById("close-button").hidden = true;
    190 			_progressWindow.document.getElementById("label").value = Zotero.getString('recognizePDF.recognizing.label');
    191 		}
    192 	}
    193 	
    194 	/**
    195 	 * Focus items in Zotero library when double-clicking them in the Retrieve
    196 	 * metadata window.
    197 	 * @param {Event} event
    198 	 * @param {tree} tree XUL tree object
    199 	 * @private
    200 	 */
    201 	async function _onDblClick(event, tree) {
    202 		if (event && tree && event.type === 'dblclick') {
    203 			let itemID = _rowIDs[tree.treeBoxObject.getRowAt(event.clientX, event.clientY)];
    204 			if (!itemID) return;
    205 			
    206 			let item = await Zotero.Items.getAsync(itemID);
    207 			if (!item) return;
    208 			
    209 			if (item.parentItemID) itemID = item.parentItemID;
    210 			
    211 			if (window.ZoteroOverlay) {
    212 				window.ZoteroOverlay.toggleDisplay(true);
    213 			}
    214 			
    215 			window.ZoteroPane.selectItem(itemID, false, true);
    216 			window.focus();
    217 		}
    218 	}
    219 };