www

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

overlay.js (7587B)


      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 /*
     27  * This object contains the various functions for the interface
     28  */
     29 var ZoteroOverlay = new function()
     30 {
     31 	const DEFAULT_ZPANE_HEIGHT = 300;
     32 	var toolbarCollapseState;
     33 	var zoteroPane, zoteroSplitter;
     34 	var _stateBeforeReload = false;
     35 	
     36 	this.isTab = false;
     37 	
     38 	this.onLoad = function () {
     39 		Zotero.spawn(function* () {
     40 			try {
     41 				//
     42 				// Code that runs in both full and connector mode
     43 				//
     44 				zoteroPane = document.getElementById('zotero-pane-stack');
     45 				zoteroSplitter = document.getElementById('zotero-splitter');
     46 				
     47 				var iconLoaded = false;
     48 				
     49 				if (!Zotero) {
     50 					throw new Error("No Zotero object");
     51 				}
     52 				if (Zotero.skipLoading) {
     53 					throw new Error("Skipping loading");
     54 				}
     55 				
     56 				ZoteroPane_Overlay = ZoteroPane;
     57 				
     58 				// Close pane before reload
     59 				ZoteroPane_Local.addBeforeReloadListener(function(newMode) {
     60 					if(newMode == "connector") {
     61 						// save current state
     62 						_stateBeforeReload = !zoteroPane.hidden && !zoteroPane.collapsed;
     63 						// ensure pane is closed
     64 						if(!zoteroPane.collapsed) ZoteroOverlay.toggleDisplay(false, true);
     65 					}
     66 				});
     67 				
     68 				// Close pane if connector is enabled
     69 				ZoteroPane_Local.addReloadListener(function() {
     70 					if(!Zotero.isConnector) {
     71 						// reopen pane if it was open before
     72 						ZoteroOverlay.toggleDisplay(_stateBeforeReload, true);
     73 					}
     74 				});
     75 				
     76 				// TODO: Add only when progress window is open
     77 				document.getElementById('appcontent').addEventListener('mousemove', Zotero.ProgressWindowSet.updateTimers, false);
     78 				
     79 				// Hide browser chrome on Zotero tab
     80 				XULBrowserWindow.inContentWhitelist.push("chrome://zotero/content/tab.xul");
     81 				
     82 				// Perform additional initialization for full mode
     83 				if (!Zotero.isConnector) {
     84 					yield _onLoadFull();
     85 				}
     86 			}
     87 			catch (e) {
     88 				Zotero.debug(e, 1);
     89 				
     90 				// Add toolbar icon if still necessary
     91 				if (!iconLoaded) {
     92 					try {
     93 						Services.scriptloader.loadSubScript("chrome://zotero/content/icon.js", {}, "UTF-8");
     94 					}
     95 					catch (e) {
     96 						Zotero.logError(e);
     97 					}
     98 				}
     99 				
    100 				throw e;
    101 			}
    102 		});
    103 	}
    104 	
    105 	
    106 	/**
    107 	 * Initialize overlay in new windows in full mode
    108 	 *
    109 	 * This is never run in Zotero for Firefox if Standalone is open first and Z4Fx is opened
    110 	 * second, but we don't care.
    111 	 */
    112 	var _onLoadFull = function () {
    113 		return Zotero.spawn(function* () {
    114 			yield Zotero.Promise.all([Zotero.initializationPromise, Zotero.unlockPromise]);
    115 			
    116 			Zotero.debug("Initializing overlay");
    117 			
    118 			if (Zotero.skipLoading) {
    119 				throw new Error("Skipping loading");
    120 			}
    121 			
    122 			ZoteroPane.init();
    123 			
    124 			// Clear old Zotero icon pref
    125 			var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
    126 								.getService(Components.interfaces.nsIPrefService)
    127 								.getBranch('extensions.zotero.');
    128 			prefBranch.clearUserPref('statusBarIcon');
    129 			
    130 			// Add toolbar icon
    131 			try {
    132 				iconLoaded = true;
    133 				Services.scriptloader.loadSubScript("chrome://zotero/content/icon.js", {}, "UTF-8");
    134 			}
    135 			catch (e) {
    136 				Zotero.logError(e);
    137 			}
    138 			
    139 			// Used for loading pages from upgrade wizard
    140 			if (Zotero.initialURL) {
    141 				setTimeout(function () {
    142 					gBrowser.selectedTab = gBrowser.addTab(Zotero.initialURL);
    143 					Zotero.initialURL = null;
    144 				}, 1);
    145 			}
    146 		}, this);
    147 	}
    148 	
    149 	
    150 	this.onUnload = function() {
    151 		ZoteroPane.destroy();
    152 	}
    153 	
    154 	this.onBeforeUnload = function() {
    155 		// close Zotero as a tab, so it won't be pinned
    156 		var zoteroTab = ZoteroOverlay.findZoteroTab();
    157 		if(zoteroTab) gBrowser.removeTab(zoteroTab);
    158 	}
    159 	
    160 	/**
    161 	 * Hides/displays the Zotero interface
    162 	 * @param {Boolean} makeVisible Whether or not Zotero interface should be visible
    163 	 * @param {Boolean} dontRefocus If true, don't focus content when closing Zotero pane. Used
    164 	 *     when closing pane because Zotero Standalone is being opened, to avoid pulling Firefox to 
    165 	 *     the foreground.
    166 	 */
    167 	this.toggleDisplay = function(makeVisible, dontRefocus)
    168 	{
    169 		if (!Zotero || Zotero.startupError || Zotero.skipLoading) {
    170 			ZoteroPane.displayStartupError();
    171 			return;
    172 		}
    173 		
    174 		// Don't do anything if pane is already showing
    175 		if (makeVisible && ZoteroPane.isShowing()) {
    176 			return;
    177 		}
    178 		
    179 		if(makeVisible || makeVisible === undefined) {
    180 			if(Zotero.isConnector) {
    181 				// If in connector mode, bring Zotero Standalone to foreground
    182 				Zotero.activateStandalone();
    183 				return;
    184 			} else if(this.isTab) {
    185 				// If in separate tab mode, just open the tab
    186 				this.loadZoteroTab();
    187 				return;
    188 			}
    189 		}
    190 		
    191 		if(makeVisible === undefined) makeVisible = zoteroPane.hidden || zoteroPane.collapsed;
    192 		
    193 		/*
    194 		Zotero.debug("zoteroPane.boxObject.height: " + zoteroPane.boxObject.height);
    195 		Zotero.debug("zoteroPane.getAttribute('height'): " + zoteroPane.getAttribute('height'));
    196 		Zotero.debug("zoteroPane.getAttribute('minheight'): " + zoteroPane.getAttribute('minheight'));
    197 		Zotero.debug("savedHeight: " + savedHeight);
    198 		*/
    199 		
    200 		if(makeVisible) {
    201 			zoteroSplitter.setAttribute('hidden', false);
    202 			zoteroPane.setAttribute('hidden', false);
    203 			zoteroPane.setAttribute('collapsed', false);
    204 			
    205 			// Get saved height (makeVisible() may change it)
    206 			if (zoteroPane.hasAttribute('savedHeight')) {
    207 				var savedHeight = zoteroPane.getAttribute('savedHeight');
    208 			}
    209 			else {
    210 				var savedHeight = DEFAULT_ZPANE_HEIGHT;
    211 			}
    212 			
    213 			// Restore height
    214 			var max = document.getElementById('appcontent').boxObject.height
    215 						- zoteroSplitter.boxObject.height;
    216 			zoteroPane.setAttribute('height', Math.min(savedHeight, max));
    217 			
    218 			// Make visible
    219 			ZoteroPane.makeVisible();
    220 			
    221 			// Warn about unsafe data directory on first display
    222 			Zotero.DataDirectory.checkForUnsafeLocation(Zotero.DataDirectory.dir); // async
    223 
    224 			// Make sure tags splitter isn't missing for people upgrading from <2.0b7
    225 			document.getElementById('zotero-tags-splitter').collapsed = false;
    226 		} else {
    227 			ZoteroPane.makeHidden();
    228 			
    229 			// Collapse pane
    230 			zoteroSplitter.setAttribute('hidden', true);
    231 			zoteroPane.setAttribute('collapsed', true);
    232 			zoteroPane.height = 0;
    233 			
    234 			document.getElementById('content').setAttribute('collapsed', false);
    235 			
    236 			if(!dontRefocus) {
    237 				// Return focus to the browser content pane
    238 				window.content.window.focus();
    239 			}
    240 		}
    241 	}
    242 }
    243 
    244 window.addEventListener("load", function(e) {
    245 	try {
    246 		ZoteroOverlay.onLoad(e);
    247 	}
    248 	catch (e) {
    249 		Components.utils.reportError(e);
    250 		if (Zotero) {
    251 			Zotero.debug(e, 1);
    252 		}
    253 		else {
    254 			dump(e + "\n\n");
    255 		}
    256 	}
    257 }, false);
    258 window.addEventListener("unload", function(e) { ZoteroOverlay.onUnload(e); }, false);
    259 window.addEventListener("beforeunload", function(e) { ZoteroOverlay.onBeforeUnload(e); }, false);