Ticket #817: csledit.xul

File csledit.xul, 6.8 KB (added by codec, 9 years ago)

CSL Editor

Line 
1<?xml version="1.0"?>
2<!--
3    ***** BEGIN LICENSE BLOCK *****
4   
5    Copyright (c) 2006  Center for History and New Media
6                        George Mason University, Fairfax, Virginia, USA
7                        http://chnm.gmu.edu
8   
9    Licensed under the Educational Community License, Version 1.0 (the "License");
10    you may not use this file except in compliance with the License.
11    You may obtain a copy of the License at
12   
13    http://www.opensource.org/licenses/ecl1.php
14   
15    Unless required by applicable law or agreed to in writing, software
16    distributed under the License is distributed on an "AS IS" BASIS,
17    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18    See the License for the specific language governing permissions and
19    limitations under the License.
20   
21    ***** END LICENSE BLOCK *****
22-->
23<?xml-stylesheet href="chrome://global/skin/global.css"?>
24<?xml-stylesheet href="chrome://zotero/skin/zotero.css" type="text/css"?>
25
26<window
27        id="csl-edit"
28        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
29        onload="Zotero_CSL_Editor.init();"
30        title="Zotero Reference Test pane">
31       
32        <script src="chrome://zotero/content/include.js"/>
33       
34        <script>
35        <![CDATA[
36                var Zotero_CSL_Editor = new function() {
37                        var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
38                                .getInterface(Components.interfaces.nsIWebNavigation)
39                                .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
40                                .rootTreeItem
41                                .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
42                                .getInterface(Components.interfaces.nsIDOMWindow);
43                       
44                        this.init = init;
45                        this.handleKeyPress = handleKeyPress;
46                        this.loadCSL = loadCSL;
47                        this.generateBibliography = generateBibliography;
48                        this.refresh = refresh;
49                       
50                        function init() {
51                                var cslList = document.getElementById('zotero-csl-list');
52                                if (cslList.getAttribute('initialized') == 'true') {
53                                        return;
54                                }
55                               
56                                var csls = Zotero.DB.query("SELECT title, cslID FROM csl ORDER BY title");
57                                for (var i=0; i<csls.length; i++) {
58                                        cslList.appendItem(csls[i].title, csls[i].cslID);
59                                }
60                                var pageList = document.getElementById('zotero-csl-page-type');
61                                var locators = Zotero.CSL.Global.getLocatorStrings();
62                                for each(var type in locators) {
63                                        var locator = type;
64                                        locator = locator[0].toUpperCase()+locator.substr(1);
65                                        pageList.appendItem(locator, type);
66                                }
67                               
68                                pageList.selectedIndex = 0;
69                                cslList.setAttribute('initialized', true)
70                        }
71                        function refresh() {
72                                var editor = document.getElementById('zotero-csl-editor')
73                                generateBibliography(editor.value)
74
75                        }
76                       
77                        function handleKeyPress(event) {
78                                if (event.keyCode == 9 &&
79                                                (!event.shiftKey && !event.metaKey && !event.altKey && !event.ctrlKey)) {
80                                        _insertText("\t");
81                                        event.preventDefault();
82                                }
83                        }
84                       
85                       
86                        function loadCSL(cslID) {
87                                var editor = document.getElementById('zotero-csl-editor')
88                                editor.value = Zotero.DB.valueQuery("SELECT csl FROM csl WHERE cslID=?", cslID);
89                                editor.doCommand();
90                                document.getElementById('zotero-csl-list').value = cslID;
91                        }
92                       
93                       
94                        function generateBibliography(str) {
95                                var iframe = document.getElementById('zotero-csl-preview-box');
96                               
97                                var items = mainWindow.ZoteroPane.getSelectedItems();
98                                if (items.length == 0) {
99                                        iframe.contentDocument.documentElement.innerHTML = '<html><head><title></title></head><body><p style="color: red">No references selected in Zotero.</p></body></html>';
100                                        return;
101                                }
102                               
103                                if (str.indexOf("<defaults") != -1) {
104                                        csl = new Zotero.CSL.Compat(str);
105                                }
106                                else {
107                                        csl = new Zotero.CSL(str);
108                                }
109                               
110                                var itemSet = csl.createItemSet(items);
111                               
112                                // Generate multiple citations
113                                var citation = csl.createCitation(itemSet.items);
114                                // Generate single citations
115                                var author = document.getElementById("preview-suppress-author").checked;
116                                var search = document.getElementById('preview-pages');
117                                var loc = document.getElementById('zotero-csl-page-type');
118                                var pos = document.getElementById('preview-subsequent').checked ? Zotero.CSL.POSITION_SUBSEQUENT : Zotero.CSL.POSITION_FIRST;
119                                var citations = '<h1>Single Citations</h1>';
120                                for (var i=0; i<citation.citationItems.length; i++) {
121                                        citation.citationItems[i].suppressAuthor = author;
122                                        if (search.value != '') {
123                                                citation.citationItems[i].locator = search.value;
124                                                citation.citationItems[i].locatorType = loc.selectedItem.value;
125                                        }
126                                        citation.citationItems[i].position = pos;
127                                        citations += csl.formatCitation(csl.createCitation([citation.citationItems[i]]), "HTML") + '<br />';
128                                }
129                                var multCitations = '<hr><h1>Multi Citations</h1>' +
130                                        csl.formatCitation(citation, "HTML");
131
132                                // Generate bibliography
133                                var bibliography = '<hr/><h1>Bibliography</h1>' +
134                                        csl.formatBibliography(itemSet, "HTML");
135                                iframe.contentDocument.documentElement.innerHTML =
136                                        citations +
137                                        multCitations + bibliography;
138                        }
139                       
140                       
141                        // From http://kb.mozillazine.org/Inserting_text_at_cursor
142                        function _insertText(text) {
143                                var command = "cmd_insertText";
144                                var controller = document.commandDispatcher.getControllerForCommand(command);
145                                if (controller && controller.isCommandEnabled(command)) {
146                                        controller = controller.QueryInterface(Components.interfaces.nsICommandController);
147                                        var params = Components.classes["@mozilla.org/embedcomp/command-params;1"];
148                                        params = params.createInstance(Components.interfaces.nsICommandParams);
149                                        params.setStringValue("state_data", "\t");
150                                        controller.doCommandWithParams(command, params);
151                                }
152                        }
153                }
154        ]]>
155        </script>
156       
157        <vbox flex="1">
158                <hbox align="center">
159                    <button id="preview-refresh-button" label="Refresh" oncommand="Zotero_CSL_Editor.refresh()"/>
160                    <menulist id="zotero-csl-page-type" style="min-height: 1.6em; min-width: 50px" oncommand="Zotero_CSL_Editor.refresh()" />
161                    <label value=":" />
162                    <textbox size="5" id="preview-pages" type="timed" timeout="250" oncommand="Zotero_CSL_Editor.refresh()"/>
163                    <checkbox oncommand="Zotero_CSL_Editor.refresh()" id="preview-suppress-author" label="Suppress author" />
164                    <checkbox oncommand="Zotero_CSL_Editor.refresh()" id="preview-subsequent" label="Subsequent citation" />
165                    <menulist id="zotero-csl-list" style="min-height: 1.6em; min-width: 100px" initialized="false" flex="1" oncommand="Zotero_CSL_Editor.loadCSL(this.selectedItem.value)"/>
166                </hbox>
167                <textbox id="zotero-csl-editor" type="timed" timeout="250" multiline="true"
168                        flex="1"
169                        onkeypress="Zotero_CSL_Editor.handleKeyPress(event)"
170                        oncommand="document.getElementById('zotero-csl-list').selectedIndex = -1; Zotero_CSL_Editor.generateBibliography(this.value)"/>
171                <splitter/>
172                <iframe id="zotero-csl-preview-box" flex="1" style="padding: 0 1em;background:white" overflow="auto"/>
173        </vbox>
174       
175</window>