From 06e6168cb4f439d06ccfc782339dedcc853406b6 Mon Sep 17 00:00:00 2001 From: rmzelle Date: Mon, 2 Feb 2015 21:23:59 -0500 Subject: [PATCH] Implement locale drop-down menus --- chrome/content/zotero/bibliography.js | 90 +++++++++--- chrome/content/zotero/bibliography.xul | 8 +- chrome/content/zotero/fileInterface.js | 17 ++- .../integration/integrationDocPrefs.xul | 9 +- chrome/content/zotero/locale/csl | 2 +- .../zotero/preferences/preferences_export.js | 117 +++++++++++---- .../zotero/preferences/preferences_export.xul | 14 +- .../preferences_export_firefox.xul | 1 + .../preferences/quickCopySiteEditor.xul | 30 +++- chrome/content/zotero/rtfScan.js | 8 +- chrome/content/zotero/rtfScan.xul | 7 +- chrome/content/zotero/tools/csledit.js | 15 +- chrome/content/zotero/tools/csledit.xul | 1 + chrome/content/zotero/tools/cslpreview.js | 9 +- chrome/content/zotero/tools/cslpreview.xul | 2 + chrome/content/zotero/xpcom/integration.js | 13 +- chrome/content/zotero/xpcom/itemTreeView.js | 10 +- chrome/content/zotero/xpcom/quickCopy.js | 134 +++++++++++------- chrome/content/zotero/xpcom/style.js | 102 ++++++++++++- chrome/content/zotero/zoteroPane.js | 17 ++- chrome/locale/en-US/zotero/preferences.dtd | 2 + chrome/locale/en-US/zotero/zotero.dtd | 1 + defaults/preferences/zotero.js | 1 - 23 files changed, 461 insertions(+), 149 deletions(-) diff --git a/chrome/content/zotero/bibliography.js b/chrome/content/zotero/bibliography.js index c61be77be..e279e0240 100644 --- a/chrome/content/zotero/bibliography.js +++ b/chrome/content/zotero/bibliography.js @@ -34,16 +34,14 @@ var Zotero_File_Interface_Bibliography = new function() { var _io, _saveStyle; - - this.init = init; - this.styleChanged = styleChanged; - this.acceptSelection = acceptSelection; + var selectedLocale = ""; + var defaultStyleLocale = ""; /* * Initialize some variables and prepare event listeners for when chrome is done * loading */ - function init() { + this.init = function () { // Set font size from pref // Affects bibliography.xul and integrationDocPrefs.xul var bibContainer = document.getElementById("zotero-bibliography-container"); @@ -87,10 +85,18 @@ var Zotero_File_Interface_Bibliography = new function() { selectIndex = 0; } + // add locales to list + if(!_io.locale) { + _io.locale = Zotero.Prefs.get("export.lastLocale"); + } + var menulist = document.getElementById("locale-menu"); + selectedLocale = Zotero.Styles.populateLocaleList(menulist, _io.locale); + // Has to be async to work properly window.setTimeout(function () { listbox.ensureIndexIsVisible(selectIndex); listbox.selectedIndex = selectIndex; + Zotero_File_Interface_Bibliography.styleChanged(); }, 0); // ONLY FOR bibliography.xul: export options @@ -149,22 +155,24 @@ var Zotero_File_Interface_Bibliography = new function() { // set style to false, in case this is cancelled _io.style = false; - } - + }; + + /* + * Called when locale is changed + */ + this.localeChanged = function (selectedValue) { + selectedLocale = selectedValue; + }; + /* * Called when style is changed */ - function styleChanged(index) { - // When called from init(), selectedItem isn't yet set - if (index != undefined) { - var selectedItem = document.getElementById("style-listbox").getItemAtIndex(index); - } - else { - var selectedItem = document.getElementById("style-listbox").selectedItem; - } + this.styleChanged = function () { + var selectedItem = document.getElementById("style-listbox").selectedItem; + var selectedStyle = selectedItem.getAttribute('value'); + var selectedStyleObj = Zotero.Styles.get(selectedStyle); - var selectedStyle = selectedItem.getAttribute('value'), - selectedStyleObj = Zotero.Styles.get(selectedStyle); + updateLocaleMenu(selectedStyleObj); // // For integrationDocPrefs.xul @@ -195,20 +203,55 @@ var Zotero_File_Interface_Bibliography = new function() { // Change label to "Citation" or "Note" depending on style class if(document.getElementById("citations")) { + let label = ""; if(Zotero.Styles.get(selectedStyle).class == "note") { - var label = Zotero.getString('citation.notes'); + label = Zotero.getString('citation.notes'); } else { - var label = Zotero.getString('citation.citations'); + label = Zotero.getString('citation.citations'); } document.getElementById("citations").label = label; } window.sizeToContent(); + }; + + /* + * Update locale menulist when style is changed + */ + function updateLocaleMenu(selectedStyle) { + // For styles with a default-locale, disable locale menulist and show locale + var menulist = document.getElementById("locale-menu"); + + // If not null, then menulist is extended with the default-locale value + // of the previously selected style + if (defaultStyleLocale) { + // Reset menulist + menulist.removeItemAt(0); + defaultStyleLocale = ""; + } + + if (selectedStyle.locale) { + defaultStyleLocale = selectedStyle.locale; + + //add default-locale to menulist + let localeLabel = defaultStyleLocale; + if (Zotero.Styles.locales[defaultStyleLocale] !== undefined) { + localeLabel = Zotero.Styles.locales[defaultStyleLocale]; + } + + menulist.insertItemAt(0, localeLabel, defaultStyleLocale); + menulist.selectedIndex = 0; + menulist.disabled = true; + } else { + menulist.value = selectedLocale; + menulist.disabled = false; + } } - function acceptSelection() { + this.acceptSelection = function () { // collect code _io.style = document.getElementById("style-listbox").selectedItem.value; + _io.locale = document.getElementById("locale-menu").selectedItem.value; if(document.getElementById("output-method-radio")) { // collect settings _io.mode = document.getElementById("output-mode-radio").selectedItem.id; @@ -235,5 +278,8 @@ var Zotero_File_Interface_Bibliography = new function() { if(_saveStyle) { Zotero.Prefs.set("export.lastStyle", _io.style); } - } -} \ No newline at end of file + + // save locale + Zotero.Prefs.set("export.lastLocale", selectedLocale); + }; +} diff --git a/chrome/content/zotero/bibliography.xul b/chrome/content/zotero/bibliography.xul index 32a91c2e5..365789bfa 100644 --- a/chrome/content/zotero/bibliography.xul +++ b/chrome/content/zotero/bibliography.xul @@ -16,6 +16,12 @@ + + + + + + @@ -33,4 +39,4 @@ - \ No newline at end of file + diff --git a/chrome/content/zotero/fileInterface.js b/chrome/content/zotero/fileInterface.js index 67dd69056..0681c1de3 100644 --- a/chrome/content/zotero/fileInterface.js +++ b/chrome/content/zotero/fileInterface.js @@ -420,15 +420,15 @@ var Zotero_File_Interface = new function() { * * Does not check that items are actual references (and not notes or attachments) */ - function copyItemsToClipboard(items, style, asHTML, asCitations) { + function copyItemsToClipboard(items, style, locale, asHTML, asCitations) { // copy to clipboard var transferable = Components.classes["@mozilla.org/widget/transferable;1"]. createInstance(Components.interfaces.nsITransferable); var clipboardService = Components.classes["@mozilla.org/widget/clipboard;1"]. getService(Components.interfaces.nsIClipboard); var style = Zotero.Styles.get(style); - var cslEngine = style.getCiteProc(); - + var cslEngine = style.getCiteProc(locale); + // add HTML var bibliography = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, items, "html", asCitations); var str = Components.classes["@mozilla.org/supports-string;1"]. @@ -458,14 +458,14 @@ var Zotero_File_Interface = new function() { * * if |asHTML| is true, copy HTML source as text */ - function copyCitationToClipboard(items, style, asHTML) { + function copyCitationToClipboard(items, style, locale, asHTML) { // copy to clipboard var transferable = Components.classes["@mozilla.org/widget/transferable;1"]. createInstance(Components.interfaces.nsITransferable); var clipboardService = Components.classes["@mozilla.org/widget/clipboard;1"]. getService(Components.interfaces.nsIClipboard); - var style = Zotero.Styles.get(style).getCiteProc(); + var style = Zotero.Styles.get(style).getCiteProc(locale); var citation = {"citationItems":[{id:item.id} for each(item in items)], properties:{}}; // add HTML @@ -518,14 +518,17 @@ var Zotero_File_Interface = new function() { format = "rtf"; } + // determine locale preference + var locale = io.locale; + // generate bibliography try { if(io.method == 'copy-to-clipboard') { - copyItemsToClipboard(items, io.style, false, io.mode === "citations"); + copyItemsToClipboard(items, io.style, locale, false, io.mode === "citations"); } else { var style = Zotero.Styles.get(io.style); - var cslEngine = style.getCiteProc(); + var cslEngine = style.getCiteProc(locale); var bibliography = Zotero.Cite.makeFormattedBibliographyOrCitationList(cslEngine, items, format, io.mode === "citations"); } diff --git a/chrome/content/zotero/integration/integrationDocPrefs.xul b/chrome/content/zotero/integration/integrationDocPrefs.xul index 8ac6bdbc3..050626355 100644 --- a/chrome/content/zotero/integration/integrationDocPrefs.xul +++ b/chrome/content/zotero/integration/integrationDocPrefs.xul @@ -48,6 +48,13 @@ + + + + + + + @@ -77,4 +84,4 @@ &zotero.integration.prefs.storeReferences.caption; - \ No newline at end of file + diff --git a/chrome/content/zotero/locale/csl b/chrome/content/zotero/locale/csl index d2b612f8a..c1679e2d7 160000 --- a/chrome/content/zotero/locale/csl +++ b/chrome/content/zotero/locale/csl @@ -1 +1 @@ -Subproject commit d2b612f8a6f764cbd66e67238636fac3888a7736 +Subproject commit c1679e2d7ec0819f73a486507c0c2d2a29cef3ed diff --git a/chrome/content/zotero/preferences/preferences_export.js b/chrome/content/zotero/preferences/preferences_export.js index 1f134e88d..7746a2ff4 100644 --- a/chrome/content/zotero/preferences/preferences_export.js +++ b/chrome/content/zotero/preferences/preferences_export.js @@ -44,10 +44,17 @@ Zotero_Preferences.Export = { populateQuickCopyList: function () { // Initialize default format drop-down var format = Zotero.Prefs.get("export.quickCopy.setting"); + format = Zotero.QuickCopy.unserializeSetting(format); var menulist = document.getElementById("zotero-quickCopy-menu"); - this.buildQuickCopyFormatDropDown(menulist, Zotero.QuickCopy.getContentType(format), format); + this.buildQuickCopyFormatDropDown(menulist, format.contentType, format); menulist.setAttribute('preference', "pref-quickCopy-setting"); - this.updateQuickCopyHTMLCheckbox(); + + // Initialize locale drop-down + var localeMenulist = document.getElementById("zotero-quickCopy-locale-menu"); + this.populateQuickCopyLocaleList(localeMenulist); + localeMenulist.setAttribute('preference', "pref-quickCopy-locale"); + + this.updateQuickCopyUI(); if (!Zotero.isStandalone) { this.refreshQuickCopySiteList(); @@ -58,12 +65,12 @@ Zotero_Preferences.Export = { /* * Builds a Quick Copy drop-down */ - buildQuickCopyFormatDropDown: function (menulist, contentType, currentFormat) { - if (!currentFormat) { - currentFormat = menulist.value; + buildQuickCopyFormatDropDown: function (menulist, contentType, format) { + if (!format) { + format = menulist.value; } - // Strip contentType from mode - currentFormat = Zotero.QuickCopy.stripContentType(currentFormat); + + format = Zotero.QuickCopy.unserializeSetting(format); menulist.selectedItem = null; menulist.removeAllItems(); @@ -86,15 +93,14 @@ Zotero_Preferences.Export = { // add styles to list var styles = Zotero.Styles.getVisible(); for each(var style in styles) { - var baseVal = 'bibliography=' + style.styleID; var val = 'bibliography' + (contentType == 'html' ? '/html' : '') + '=' + style.styleID; var itemNode = document.createElement("menuitem"); itemNode.setAttribute("value", val); itemNode.setAttribute("label", style.title); - itemNode.setAttribute("oncommand", 'Zotero_Preferences.Export.updateQuickCopyHTMLCheckbox()'); + itemNode.setAttribute("oncommand", 'Zotero_Preferences.Export.updateQuickCopyUI()'); popup.appendChild(itemNode); - if (baseVal == currentFormat) { + if (format.mode == 'bibliography' && format.id == style.styleID) { menulist.selectedItem = itemNode; } } @@ -115,14 +121,14 @@ Zotero_Preferences.Export = { case '14763d24-8ba0-45df-8f52-b8d1108e7ac9': continue; } - var val = 'export=' + translators[i].translatorID; + var val = 'export=' + translators[i].translatorID; var itemNode = document.createElement("menuitem"); itemNode.setAttribute("value", val); itemNode.setAttribute("label", translators[i].label); - itemNode.setAttribute("oncommand", 'Zotero_Preferences.Export.updateQuickCopyHTMLCheckbox()'); + itemNode.setAttribute("oncommand", 'Zotero_Preferences.Export.updateQuickCopyUI()'); popup.appendChild(itemNode); - if (val == currentFormat) { + if (format.mode == 'export' && format.id == translators[i].translatorID) { menulist.selectedItem = itemNode; } } @@ -133,16 +139,49 @@ Zotero_Preferences.Export = { }, - updateQuickCopyHTMLCheckbox: function () { + updateQuickCopyUI: function () { var format = document.getElementById('zotero-quickCopy-menu').value; + var mode, contentType; - var checkbox = document.getElementById('zotero-quickCopy-copyAsHTML'); [mode, format] = format.split('='); [mode, contentType] = mode.split('/'); + var checkbox = document.getElementById('zotero-quickCopy-copyAsHTML'); checkbox.checked = contentType == 'html'; checkbox.disabled = mode != 'bibliography'; + + var menulist = document.getElementById('zotero-quickCopy-locale-menu'); + var menulistLabel = document.getElementById('zotero-quickCopy-locale-menu-label'); + + var lastSelectedLocale = menulist.value; + var localeLabel = ""; + if (menulist.disabled === true) { + menulist.removeItemAt(0); + } + + if (mode == 'bibliography') { + var defaultStyleLocale = Zotero.Styles.get(format).locale; + if (defaultStyleLocale) { + if (Zotero.Styles.locales[defaultStyleLocale] !== undefined) { + localeLabel = Zotero.Styles.locales[defaultStyleLocale]; + } else { + localeLabel = defaultStyleLocale; + } + } + menulistLabel.disabled = false; + } else { + menulistLabel.disabled = true; + } + + if (mode == 'bibliography' && !defaultStyleLocale) { + menulist.value = lastSelectedLocale; + menulist.disabled = false; + } else { + menulist.insertItemAt(0, localeLabel, lastSelectedLocale); + menulist.selectedIndex = 0; + menulist.disabled = true; + } }, /** @@ -164,19 +203,24 @@ Zotero_Preferences.Export = { showQuickCopySiteEditor: function (index) { var treechildren = document.getElementById('quickCopy-siteSettings-rows'); - if (index != undefined && index > -1 && index < treechildren.childNodes.length) { + var formattedName = document.getElementById('zotero-quickCopy-menu').label; + var locale = document.getElementById('zotero-quickCopy-locale-menu').value; + var asHTML = document.getElementById('zotero-quickCopy-copyAsHTML').checked; + + if (index !== undefined && index > -1 && index < treechildren.childNodes.length) { var treerow = treechildren.childNodes[index].firstChild; var domain = treerow.childNodes[0].getAttribute('label'); - var format = treerow.childNodes[1].getAttribute('label'); - var asHTML = treerow.childNodes[2].getAttribute('label') != ''; + formattedName = treerow.childNodes[1].getAttribute('label'); + locale = treerow.childNodes[2].getAttribute('label'); + asHTML = treerow.childNodes[3].getAttribute('label') !== ''; } - var format = Zotero.QuickCopy.getSettingFromFormattedName(format); + var format = Zotero.QuickCopy.getSettingFromFormattedName(formattedName); if (asHTML) { format = format.replace('bibliography=', 'bibliography/html='); } - var io = {domain: domain, format: format, ok: false}; + var io = {domain: domain, format: format, locale: locale, asHTML: asHTML, ok: false}; window.openDialog('chrome://zotero/content/preferences/quickCopySiteEditor.xul', "zotero-preferences-quickCopySiteEditor", "chrome,modal,centerscreen", io); @@ -188,7 +232,10 @@ Zotero_Preferences.Export = { Zotero.DB.query("DELETE FROM settings WHERE setting='quickCopySite' AND key=?", [domain]); } - Zotero.DB.query("REPLACE INTO settings VALUES ('quickCopySite', ?, ?)", [io.domain, io.format]); + var quickCopysetting = Zotero.QuickCopy.unserializeSetting(io.format); + quickCopysetting.locale = io.locale; + + Zotero.DB.query("REPLACE INTO settings VALUES ('quickCopySite', ?, ?)", [io.domain, JSON.stringify(quickCopysetting)]); this.refreshQuickCopySiteList(); }, @@ -213,17 +260,21 @@ Zotero_Preferences.Export = { var treerow = document.createElement('treerow'); var domainCell = document.createElement('treecell'); var formatCell = document.createElement('treecell'); + var localeCell = document.createElement('treecell'); var HTMLCell = document.createElement('treecell'); domainCell.setAttribute('label', siteData[i].domainPath); - var formatted = Zotero.QuickCopy.getFormattedNameFromSetting(siteData[i].format); - formatCell.setAttribute('label', formatted); - var copyAsHTML = Zotero.QuickCopy.getContentType(siteData[i].format) == 'html'; - HTMLCell.setAttribute('label', copyAsHTML ? ' ✓ ' : ''); + var formattedName = Zotero.QuickCopy.getFormattedNameFromSetting(siteData[i].format); + formatCell.setAttribute('label', formattedName); + + var format = Zotero.QuickCopy.unserializeSetting(siteData[i].format); + localeCell.setAttribute('label', format.locale); + HTMLCell.setAttribute('label', format.contentType == 'html' ? ' ✓ ' : ''); treerow.appendChild(domainCell); treerow.appendChild(formatCell); + treerow.appendChild(localeCell); treerow.appendChild(HTMLCell); treeitem.appendChild(treerow); treechildren.appendChild(treeitem); @@ -241,6 +292,16 @@ Zotero_Preferences.Export = { this.refreshQuickCopySiteList(); }, + /* + * Builds the Quick Copy locale drop-down + */ + populateQuickCopyLocaleList: function (menulist, quickCopyLocale) { + if (!quickCopyLocale) { + quickCopyLocale = Zotero.Prefs.get("export.quickCopy.locale"); + } + + Zotero.Styles.populateLocaleList(menulist, quickCopyLocale); + }, updateQuickCopyInstructions: function () { var prefix = Zotero.isMac ? Zotero.getString('general.keys.cmdShift') : Zotero.getString('general.keys.ctrlShift'); @@ -253,9 +314,9 @@ Zotero_Preferences.Export = { } instr.appendChild(document.createTextNode(str)); - var key = Zotero.Prefs.get('keys.copySelectedItemCitationsToClipboard'); - var str = Zotero.getString('zotero.preferences.export.quickCopy.citationInstructions', prefix + key); - var instr = document.getElementById('quickCopy-citationInstructions'); + key = Zotero.Prefs.get('keys.copySelectedItemCitationsToClipboard'); + str = Zotero.getString('zotero.preferences.export.quickCopy.citationInstructions', prefix + key); + instr = document.getElementById('quickCopy-citationInstructions'); while (instr.hasChildNodes()) { instr.removeChild(instr.firstChild); } diff --git a/chrome/content/zotero/preferences/preferences_export.xul b/chrome/content/zotero/preferences/preferences_export.xul index 378533509..8040c0be0 100644 --- a/chrome/content/zotero/preferences/preferences_export.xul +++ b/chrome/content/zotero/preferences/preferences_export.xul @@ -33,6 +33,7 @@ + @@ -49,10 +50,15 @@ - + + + + + + diff --git a/chrome/content/zotero/tools/csledit.js b/chrome/content/zotero/tools/csledit.js index 1bcbc6a91..209763693 100644 --- a/chrome/content/zotero/tools/csledit.js +++ b/chrome/content/zotero/tools/csledit.js @@ -30,6 +30,11 @@ var Zotero_CSL_Editor = new function() { this.generateBibliography = generateBibliography; this.refresh = refresh; function init() { + var quickCopyLocale = Zotero.Prefs.get("export.quickCopy.locale"); + var menulist = document.getElementById("locale-menu"); + + Zotero.Styles.populateLocaleList(menulist, quickCopyLocale); + var cslList = document.getElementById('zotero-csl-list'); if (cslList.getAttribute('initialized') == 'true') { if (currentStyle) { @@ -39,8 +44,8 @@ var Zotero_CSL_Editor = new function() { return; } - var rawDefaultStyle = Zotero.Prefs.get('export.quickCopy.setting'); - var defaultStyle = Zotero.QuickCopy.stripContentType(rawDefaultStyle); + var quickCopyFormat = Zotero.Prefs.get('export.quickCopy.setting'); + quickCopyFormat = Zotero.QuickCopy.unserializeSetting(quickCopyFormat); var styles = Zotero.Styles.getAll(); var currentStyle = null; @@ -50,7 +55,7 @@ var Zotero_CSL_Editor = new function() { continue; } var item = cslList.appendItem(style.title, style.styleID); - if (!currentStyle || defaultStyle == ('bibliography=' + style.styleID)) { + if (!currentStyle || (quickCopyFormat.mode == 'bibliography' && quickCopyFormat.id == style.styleID)) { currentStyle = style.styleID; cslList.selectedIndex = listPos; } @@ -130,10 +135,12 @@ var Zotero_CSL_Editor = new function() { iframe.contentDocument.documentElement.innerHTML = '

' + Zotero.getString('styles.editor.warning.noItems') + '

'; return; } + + var locale = document.getElementById("locale-menu").selectedItem.value; var styleObject, styleEngine; try { styleObject = new Zotero.Style(str); - styleEngine = styleObject.getCiteProc(); + styleEngine = styleObject.getCiteProc(locale); } catch(e) { iframe.contentDocument.documentElement.innerHTML = '
' + Zotero.getString('styles.editor.warning.parseError') + '
'+e+'
'; throw e; diff --git a/chrome/content/zotero/tools/csledit.xul b/chrome/content/zotero/tools/csledit.xul index f646d266f..879406d38 100644 --- a/chrome/content/zotero/tools/csledit.xul +++ b/chrome/content/zotero/tools/csledit.xul @@ -58,6 +58,7 @@ + '; @@ -86,7 +90,10 @@ var Zotero_CSL_Preview = new function() { Zotero.debug("CSL IGNORE: citation format is " + style.categories); return ''; } - var styleEngine = style.getCiteProc(); + + var locale = document.getElementById("locale-menu").selectedItem.value; + + var styleEngine = style.getCiteProc(locale); // Generate multiple citations var citations = styleEngine.previewCitationCluster( diff --git a/chrome/content/zotero/tools/cslpreview.xul b/chrome/content/zotero/tools/cslpreview.xul index 0b984577a..b9e8420ab 100644 --- a/chrome/content/zotero/tools/cslpreview.xul +++ b/chrome/content/zotero/tools/cslpreview.xul @@ -56,6 +56,8 @@ + +