From 20f38fa773fea00b9170ac56f559ac2394e83173 Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Mon, 14 Feb 2011 16:56:44 +0000 Subject: [PATCH] restore live updating in csledit.xul --- chrome/content/zotero/tools/csledit.xul | 44 +++++++++++++++---------- chrome/content/zotero/xpcom/style.js | 30 ++++++++++++----- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/chrome/content/zotero/tools/csledit.xul b/chrome/content/zotero/tools/csledit.xul index dc2033c5a7..100b0e9cf6 100644 --- a/chrome/content/zotero/tools/csledit.xul +++ b/chrome/content/zotero/tools/csledit.xul @@ -76,7 +76,7 @@ } function refresh() { var editor = document.getElementById('zotero-csl-editor') - generateBibliography(editor.cslID) + generateBibliography(editor.value); } @@ -117,9 +117,14 @@ return; } else { - var cslFile = Zotero.Styles.get(editor.cslID).file; - styleObject = new Zotero.Style(cslFile); - styleEngine = styleObject.csl; + try { + styleObject = new Zotero.Style(str); + styleEngine = styleObject.csl; + } catch(e) { + iframe.contentDocument.documentElement.innerHTML = '
Error parsing '+ + 'style:
'+e+'
'; + throw e; + } } var itemIds = [items[i].id for (i in items)]; @@ -152,19 +157,24 @@ citations += styleEngine.makeCitationCluster(subcitation) + '
'; } - var multCitations = '

Multi Citations (all with position "first")

' + - styleEngine.previewCitationCluster(citation, [], [], "html"); - - // Generate bibliography - styleEngine.updateItems(itemIds); - var bibData = styleEngine.makeBibliography(); - var bibliography = '

Bibliography

' + - bibData[0].bibstart + bibData[1].join("") + bibData[0].bibend; - - iframe.contentDocument.documentElement.innerHTML = - '
' - + citations + multCitations + bibliography - + '
'; + try { + var multCitations = '

Multi Citations (all with position "first")

' + + styleEngine.previewCitationCluster(citation, [], [], "html"); + + // Generate bibliography + styleEngine.updateItems(itemIds); + var bibliography = '

Bibliography

' + + Zotero.Cite.makeFormattedBibliography(styleEngine, "html"); + + iframe.contentDocument.documentElement.innerHTML = + '
' + + citations + multCitations + bibliography + + '
'; + } catch(e) { + iframe.contentDocument.documentElement.innerHTML = '
Error generating citations '+ + 'and bibliography:
'+e+'
'; + throw e; + } } diff --git a/chrome/content/zotero/xpcom/style.js b/chrome/content/zotero/xpcom/style.js index 6d2aea000b..d493aef789 100644 --- a/chrome/content/zotero/xpcom/style.js +++ b/chrome/content/zotero/xpcom/style.js @@ -337,16 +337,22 @@ Zotero.Styles = new function() { * @property {Boolean} hidden True if this style is hidden in style selection dialogs, false if it * is not */ -Zotero.Style = function(file) { - this.file = file; +Zotero.Style = function(arg) { + if(typeof arg === "string") { + this.string = arg; + } else if(typeof arg === "object") { + this.file = arg; + } else { + throw "Invalid argument passed to Zotero.Style"; + } - var extension = file.leafName.substr(-4).toLowerCase(); + var extension = (typeof arg === "string" ? ".csl" : arg.leafName.substr(-4).toLowerCase()); if(extension == ".ens") { this.type = "ens"; this.styleID = Zotero.Styles.ios.newFileURI(this.file).spec; - this.title = file.leafName.substr(0, file.leafName.length-4); - this.updated = Zotero.Date.dateToSQL(new Date(file.lastModifiedTime)); + this.title = this.file.leafName.substr(0, this.file.leafName.length-4); + this.updated = Zotero.Date.dateToSQL(new Date(this.file.lastModifiedTime)); this._version = "0.8"; } else if(extension == ".csl") { // "with ({});" needed to fix default namespace scope issue @@ -355,7 +361,7 @@ Zotero.Style = function(file) { this.type = "csl"; - var xml = Zotero.Styles.cleanXML(Zotero.File.getContents(file)); + var xml = Zotero.Styles.cleanXML(typeof arg === "string" ? arg : Zotero.File.getContents(arg)); this.styleID = xml.info.id.toString(); this.title = xml.info.title.toString(); @@ -480,8 +486,10 @@ function() { Zotero.Styles.ios.newFileURI(this.file).spec, null)); } return formatCSL.file; + } else if(this.file) { + return this.file; } - return this.file; + return null; }); /** @@ -503,7 +511,9 @@ Zotero.Style.prototype.getXML = function() { return XML.toXMLString(); } else { - return Zotero.File.getContents(this.independentFile); + var indepFile = this.independentFile; + if(indepFile) return Zotero.File.getContents(indepFile); + return this.string; } }; @@ -511,6 +521,10 @@ Zotero.Style.prototype.getXML = function() { * Deletes a style */ Zotero.Style.prototype.remove = function() { + if(!this.file) { + throw "Cannot delete a style with no associated file." + } + // make sure no styles depend on this one var dependentStyles = false; var styles = Zotero.Styles.getAll();