www

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

preferences_cite.js (6323B)


      1 /*
      2     ***** BEGIN LICENSE BLOCK *****
      3     
      4     Copyright © 2006–2013 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 "use strict";
     27 
     28 Zotero_Preferences.Cite = {
     29 	wordPluginIDs: new Set([
     30 		'zoteroOpenOfficeIntegration@zotero.org',
     31 		'zoteroMacWordIntegration@zotero.org',
     32 		'zoteroWinWordIntegration@zotero.org'
     33 	]),
     34 
     35 	init: Zotero.Promise.coroutine(function* () {
     36 		Components.utils.import("resource://gre/modules/AddonManager.jsm");
     37 		this.updateWordProcessorInstructions();
     38 		yield this.refreshStylesList();
     39 	}),
     40 	
     41 	
     42 	/**
     43 	 * Determines if any word processors are disabled and if so, shows a message in the pref pane
     44 	 */
     45 	updateWordProcessorInstructions: async function () {
     46 		var someDisabled = false;
     47 		await new Promise(function(resolve) {
     48 			AddonManager.getAllAddons(function(addons) {
     49 				for (let addon of addons) {
     50 					if (Zotero_Preferences.Cite.wordPluginIDs.has(addon.id) && addon.userDisabled) {
     51 						someDisabled = true;
     52 					}
     53 				}
     54 				resolve();
     55 			});
     56 		});
     57 		if (someDisabled) {
     58 			document.getElementById("wordProcessors-somePluginsDisabled").hidden = undefined;
     59 		}
     60 	},
     61 	
     62 	enableWordPlugins: function () {
     63 		AddonManager.getAllAddons(function(addons) {
     64 			for (let addon of addons) {
     65 				if (Zotero_Preferences.Cite.wordPluginIDs.has(addon.id) && addon.userDisabled) {
     66 					addon.userDisabled = false;
     67 				}
     68 			}
     69 			return Zotero.Utilities.Internal.quit(true);
     70 		});
     71 	},
     72 	
     73 	
     74 	/**
     75 	 * Refreshes the list of styles in the styles pane
     76 	 * @param {String} cslID Style to select
     77 	 * @return {Promise}
     78 	 */
     79 	refreshStylesList: Zotero.Promise.coroutine(function* (cslID) {
     80 		Zotero.debug("Refreshing styles list");
     81 		
     82 		var treechildren = document.getElementById('styleManager-rows');
     83 		while (treechildren.hasChildNodes()) {
     84 			treechildren.removeChild(treechildren.firstChild);
     85 		}
     86 		
     87 		yield Zotero.Styles.init();
     88 		var styles = Zotero.Styles.getVisible();
     89 		var selectIndex = false;
     90 		styles.forEach(function (style, i) {
     91 			var treeitem = document.createElement('treeitem');
     92 			var treerow = document.createElement('treerow');
     93 			var titleCell = document.createElement('treecell');
     94 			var updatedCell = document.createElement('treecell');
     95 			
     96 			if (style.updated) {
     97 				var updatedDate = Zotero.Date.formatDate(Zotero.Date.strToDate(style.updated), true);
     98 			}
     99 			else {
    100 				var updatedDate = '';
    101 			}
    102 			
    103 			treeitem.setAttribute('id', 'zotero-csl-' + style.styleID);
    104 			titleCell.setAttribute('label', style.title);
    105 			updatedCell.setAttribute('label', updatedDate);
    106 			
    107 			treerow.appendChild(titleCell);
    108 			treerow.appendChild(updatedCell);
    109 			treeitem.appendChild(treerow);
    110 			treechildren.appendChild(treeitem);
    111 			
    112 			if (cslID == style.styleID) {
    113 				document.getElementById('styleManager').view.selection.select(i);
    114 			}
    115 		});
    116 	}),
    117 	
    118 	
    119 	openStylesPage: function () {
    120 		Zotero.openInViewer("https://www.zotero.org/styles/", function (doc) {
    121 			// Hide header, intro paragraph, Link, and Source
    122 			//
    123 			// (The first two aren't sent to the client normally, but hide anyway in case they are.)
    124 			var style = doc.createElement('style');
    125 			style.type = 'text/css';
    126 			style.innerHTML = 'h1, #intro, .style-individual-link, .style-view-source { display: none !important; }';
    127 			Zotero.debug(doc.documentElement.innerHTML);
    128 			doc.getElementsByTagName('head')[0].appendChild(style);
    129 		});
    130 	},
    131 	
    132 	
    133 	/**
    134 	 * Adds a new style to the style pane
    135 	 **/
    136 	addStyle: function () {	
    137 		const nsIFilePicker = Components.interfaces.nsIFilePicker;
    138 		var fp = Components.classes["@mozilla.org/filepicker;1"]
    139 				.createInstance(nsIFilePicker);
    140 		fp.init(window, Zotero.getString("zotero.preferences.styles.addStyle"), nsIFilePicker.modeOpen);
    141 		
    142 		fp.appendFilter("CSL Style", "*.csl");
    143 		
    144 		var rv = fp.show();
    145 		if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
    146 			Zotero.Styles.install({ file: fp.file }, fp.file.path, true)
    147 			.catch(function (e) {
    148 				(new Zotero.Exception.Alert("styles.install.unexpectedError",
    149 					fp.file.path, "styles.install.title", e)).present()
    150 			});
    151 		}
    152 	},
    153 	
    154 	
    155 	/**
    156 	 * Deletes selected styles from the styles pane
    157 	 **/
    158 	deleteStyle: Zotero.Promise.coroutine(function* () {
    159 		// get selected cslIDs
    160 		var tree = document.getElementById('styleManager');
    161 		var treeItems = tree.lastChild.childNodes;
    162 		var cslIDs = [];
    163 		var start = {};
    164 		var end = {};
    165 		var nRanges = tree.view.selection.getRangeCount();
    166 		for(var i=0; i<nRanges; i++) {
    167 			tree.view.selection.getRangeAt(i, start, end);
    168 			for(var j=start.value; j<=end.value; j++) {
    169 				cslIDs.push(treeItems[j].getAttribute('id').substr(11));
    170 			}
    171 		}
    172 		
    173 		if(cslIDs.length == 0) {
    174 			return;
    175 		} else if(cslIDs.length == 1) {
    176 			var selectedStyle = Zotero.Styles.get(cslIDs[0])
    177 			var text = Zotero.getString('styles.deleteStyle', selectedStyle.title);
    178 		} else {
    179 			var text = Zotero.getString('styles.deleteStyles');
    180 		}
    181 		
    182 		var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
    183 			.getService(Components.interfaces.nsIPromptService);
    184 		if(ps.confirm(null, '', text)) {
    185 			// delete if requested
    186 			if(cslIDs.length == 1) {
    187 				yield selectedStyle.remove();
    188 			} else {
    189 				for(var i=0; i<cslIDs.length; i++) {
    190 					yield Zotero.Styles.get(cslIDs[i]).remove();
    191 				}
    192 			}
    193 			
    194 			yield this.refreshStylesList();
    195 			document.getElementById('styleManager-delete').disabled = true;
    196 		}
    197 	}),
    198 	
    199 	
    200 	/**
    201 	 * Shows an error if import fails
    202 	 **/
    203 	styleImportError: function () {
    204 		alert(Zotero.getString('styles.installError', "This"));
    205 	}
    206 }