proxyEditor.js (4811B)
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 Zotero_ProxyEditor = new function() { 27 var treechildren; 28 var tree; 29 var treecol; 30 var multiSite; 31 32 /** 33 * Called when this window is first opened. Sets values if necessary 34 */ 35 this.load = function() { 36 treechildren = document.getElementById("zotero-proxies-hostname-multiSite-tree-children"); 37 tree = document.getElementById("zotero-proxies-hostname-multiSite-tree"); 38 multiSite = document.getElementById("zotero-proxies-multiSite"); 39 40 if(window.arguments && window.arguments[0]) { 41 var proxy = window.arguments[0]; 42 document.getElementById("zotero-proxies-scheme").value = proxy.scheme; 43 document.getElementById("zotero-proxies-multiSite").checked = !!proxy.multiHost; 44 if(proxy.hosts) { 45 if(proxy.multiHost) { 46 this.multiSiteChanged(); 47 for (var i=0; i<proxy.hosts.length; i++) { 48 _addTreeElement(proxy.hosts[i]); 49 } 50 document.getElementById("zotero-proxies-autoAssociate").checked = proxy.autoAssociate; 51 } else { 52 document.getElementById("zotero-proxies-hostname-text").value = proxy.hosts[0]; 53 } 54 } 55 } 56 57 window.sizeToContent(); 58 } 59 60 /** 61 * Called when a user checks/unchecks the Multi-Site checkbox. Shows or hides multi-site 62 * hostname specification box as necessary. 63 */ 64 this.multiSiteChanged = function() { 65 document.getElementById("zotero-proxies-hostname-multiSite").hidden = !multiSite.checked; 66 document.getElementById("zotero-proxies-hostname-multiSite-description").hidden = !multiSite.checked; 67 document.getElementById("zotero-proxies-hostname").hidden = multiSite.checked; 68 window.sizeToContent(); 69 } 70 71 /** 72 * Called when a row is selected 73 */ 74 this.select = function() { 75 document.getElementById("zotero-proxies-delete").disabled = tree.selectedIndex == -1; 76 } 77 78 /** 79 * Adds a host when in multi-host mode 80 */ 81 this.addHost = function() { 82 _addTreeElement(); 83 tree.startEditing(treechildren.childNodes.length-1, tree.columns.getFirstColumn()); 84 } 85 86 /** 87 * Deletes a host when in multi-host mode 88 */ 89 this.deleteHost = function() { 90 if(tree.currentIndex == -1) return; 91 treechildren.removeChild(treechildren.childNodes[tree.currentIndex]); 92 document.getElementById("zotero-proxies-delete").disabled = true; 93 } 94 95 /** 96 * Called when the user clicks "OK." Updates proxy for Zotero.Proxy. 97 */ 98 this.accept = function() { 99 var proxy = window.arguments && window.arguments[0] ? window.arguments[0] : new Zotero.Proxy(); 100 101 proxy.scheme = document.getElementById("zotero-proxies-scheme").value; 102 proxy.multiHost = multiSite.checked; 103 if(proxy.multiHost) { 104 proxy.hosts = []; 105 var treecol = tree.columns.getFirstColumn(); 106 for(var i=0; i<tree.view.rowCount; i++) { 107 var host = tree.view.getCellText(i, treecol); 108 if(host) proxy.hosts.push(host); 109 } 110 proxy.autoAssociate = document.getElementById("zotero-proxies-autoAssociate").checked; 111 } else { 112 proxy.hosts = [document.getElementById("zotero-proxies-hostname-text").value]; 113 } 114 115 var hasErrors = proxy.validate(); 116 if(hasErrors) { 117 error = hasErrors.shift(); 118 119 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] 120 .getService(Components.interfaces.nsIPromptService); 121 promptService.alert( 122 window, Zotero.getString("proxies.error"), 123 Zotero.getString("proxies.error." + error, hasErrors) 124 ); 125 if(window.arguments && window.arguments[0]) proxy.revert(); // async 126 return false; 127 } 128 proxy.save(true); 129 return true; 130 } 131 132 /** 133 * Adds an element to the tree 134 */ 135 function _addTreeElement(label) { 136 var treeitem = document.createElement('treeitem'); 137 var treerow = document.createElement('treerow'); 138 var treecell = document.createElement('treecell'); 139 140 if(label) treecell.setAttribute('label', label); 141 142 treerow.appendChild(treecell); 143 treeitem.appendChild(treerow); 144 treechildren.appendChild(treeitem); 145 } 146 }