longTagFixer.js (5175B)
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 var Zotero_Long_Tag_Fixer = new function () { 28 var _oldTag = window.arguments[0]; 29 var _dataOut = window.arguments[1]; 30 31 this.init = function () { 32 document.getElementById('zotero-old-tag').value = _oldTag; 33 document.getElementById('zotero-old-tag-delimiter').nextSibling.value = Zotero.getString('general.character.singular'); 34 35 var delimiter = Zotero.Prefs.get('lastLongTagDelimiter'); 36 document.getElementById('zotero-old-tag-delimiter').value = delimiter; 37 38 var lastMode = Zotero.Prefs.get('lastLongTagMode'); 39 if (!lastMode) { 40 lastMode = 0; 41 } 42 this.switchMode(lastMode); 43 } 44 45 this.switchMode = function (index) { 46 var dialog = document.getElementById('zotero-long-tag-fixer'); 47 48 document.getElementById('zotero-new-tag-actions').selectedIndex = index; 49 50 switch (index) { 51 case 0: 52 var buttonLabel = "saveTags"; 53 this.updateTagList(); 54 document.getElementById('zotero-old-tag-delimiter').select(); 55 break; 56 57 case 1: 58 var buttonLabel = "saveTag"; 59 document.getElementById('zotero-new-tag-editor').value = _oldTag; 60 this.updateEditLength(_oldTag.length) 61 break; 62 63 case 2: 64 var buttonLabel = "deleteTag"; 65 dialog.getButton('accept').disabled = false; 66 break; 67 } 68 69 document.getElementById('zotero-long-tag-fixer').getButton('accept').label = Zotero.getString('sync.longTagFixer.' + buttonLabel); 70 window.sizeToContent(); 71 Zotero.Prefs.set('lastLongTagMode', index); 72 } 73 74 75 /** 76 * Split tags and populate list 77 */ 78 this.updateTagList = function () { 79 var listbox = document.getElementById('zotero-new-tag-list'); 80 while (listbox.childNodes.length) { 81 listbox.removeChild(listbox.lastChild); 82 } 83 84 var delimiter = document.getElementById('zotero-old-tag-delimiter').value; 85 if (delimiter) { 86 Zotero.Prefs.set('lastLongTagDelimiter', delimiter); 87 var re = new RegExp("\\s*" + delimiter.replace(/([\.\-\[\]\(\)\?\*\+])/g, "\\$1") + "\\s*"); 88 var tags = _oldTag.split(re); 89 } 90 91 var acceptButton = document.getElementById('zotero-long-tag-fixer').getButton('accept'); 92 if (!delimiter || tags.length < 2) { 93 acceptButton.disabled = true; 94 return; 95 } 96 else { 97 acceptButton.disabled = false; 98 } 99 100 tags.sort(); 101 for (var i=0; i<tags.length; i++) { 102 if (i != 0 && tags[i] == tags[i-1]) { 103 continue; 104 } 105 if (!tags[i]) { 106 continue; 107 } 108 var li = listbox.appendItem(tags[i]); 109 li.setAttribute('type', 'checkbox'); 110 li.setAttribute('checked', 'true'); 111 } 112 113 window.sizeToContent(); 114 } 115 116 117 this.deselectAll = function () { 118 var lis = document.getElementById('zotero-new-tag-list').getElementsByTagName('listitem'); 119 for (var i=0; i<lis.length; i++) { 120 lis[i].checked = false; 121 } 122 } 123 124 125 this.selectAll = function () { 126 var lis = document.getElementById('zotero-new-tag-list').getElementsByTagName('listitem'); 127 for (var i=0; i<lis.length; i++) { 128 lis[i].checked = true; 129 } 130 } 131 132 133 this.updateEditLength = function (len) { 134 document.getElementById('zotero-new-tag-character-count').value = len; 135 var invalid = len == 0 || len > Zotero.Tags.MAX_SYNC_LENGTH; 136 document.getElementById('zotero-new-tag-characters').setAttribute('invalid', invalid); 137 document.getElementById('zotero-long-tag-fixer').getButton('accept').disabled = invalid; 138 } 139 140 141 this.cancel = function () { 142 _dataOut.result = false; 143 } 144 145 146 this.save = function () { 147 try { 148 149 var result = {}; 150 151 var index = document.getElementById('zotero-new-tag-actions').selectedIndex; 152 153 switch (index) { 154 // Split 155 case 0: 156 // Get checked tags 157 var listbox = document.getElementById('zotero-new-tag-list'); 158 var len = listbox.childElementCount; 159 var newTags = []; 160 for (var i=0; i<len; i++) { 161 var li = listbox.childNodes[i]; 162 if (li.getAttribute('checked') == 'true') { 163 newTags.push(li.getAttribute('label')); 164 } 165 } 166 167 result.op = 'split'; 168 result.tags = newTags; 169 break; 170 171 // Edit 172 case 1: 173 result.op = 'edit'; 174 result.tag = document.getElementById('zotero-new-tag-editor').value; 175 break; 176 177 // Delete 178 case 2: 179 result.op = 'delete'; 180 break; 181 } 182 183 _dataOut.result = result; 184 185 } 186 catch (e) { 187 Zotero.debug(e); 188 throw (e); 189 } 190 } 191 }