<commit_before>/*
    ***** BEGIN LICENSE BLOCK *****
    
    Copyright (c) 2006  Center for History and New Media
                        George Mason University, Fairfax, Virginia, USA
                        http://chnm.gmu.edu
    
    Licensed under the Educational Community License, Version 1.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
    http://www.opensource.org/licenses/ecl1.php
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    
    ***** END LICENSE BLOCK *****
*/

var Zotero_Citation_Dialog = new function () {
	var _preserveData = {
		"prefix":"value",
		"suffix":"value",
		"locatorType":"selectedIndex",
		"locator":"value",
		"suppressAuthor":"checked"
	};
	
	var _itemData = new Object();
	var _multipleSourcesOn = false;
	var _lastSelected = null;
	var _previewShown = false;
	var _suppressNextTreeSelect = false;
	var _locatorIndexArray = {};
	var _autoRegeneratePref;
	var _acceptButton;
	var _sortCheckbox;
	var _originalHTML;
	var io;
	
	this.load = load;
	this.toggleMultipleSources = toggleMultipleSources;
	this.toggleEditor = toggleEditor;
	this.treeItemSelected = treeItemSelected;
	this.listItemSelected = listItemSelected;
	this.add = add;
	this.remove = remove;
	this.sortCitation = sortCitation;
	this.confirmRegenerate = confirmRegenerate;
	this.accept = accept;
	this.cancel = cancel;
	
	/*
	 * initialize add citation dialog
	 */
	function load() {
		document.getElementById("multiple-sources-button").label = Zotero.getString("citation.multipleSources");
		document.getElementById("show-editor-button").label = Zotero.getString("citation.showEditor");
		
		io = window.arguments[0].wrappedJSObject;
		
		// find accept button
		_acceptButton = document.getElementById("zotero-add-citation-dialog").getButton("accept");
		_autoRegeneratePref = Zotero.Prefs.get("integration.autoRegenerate");
		
		// if a style with sortable citations, present checkbox
		if(io.citation.sortable) {
			_sortCheckbox = document.getElementById("keepSorted");
			_sortCheckbox.hidden = false;
			if(io.citation.properties.sort === undefined) io.citation.properties.sort = true;
			_sortCheckbox.checked = io.citation.properties.sort;
		}
		
		// load locators
		var locators = Zotero.CSL.Global.getLocatorStrings();
		var menu = document.getElementById("locatorType");
		var popup = document.getElementById("locator-type-popup");
		var i = 0;
		for(var value in locators) {
			var locator = locators[value];
			locator = locator[0].toUpperCase()+locator.substr(1);
			// add to popup
			var child = document.createElement("menuitem");
			child.setAttribute("value", value);
			child.setAttribute("label", locator);
			popup.appendChild(child);
			// add to array
			_locatorIndexArray[value] = i;
			i++;
		}
		menu.selectedIndex = 0;
		
		// load (from selectItemsDialog.js)
		doLoad();
		
		// if we already have a citation, load data from it
		document.getElementById('editor').format = "Integration";
		if(io.citation.citationItems.length) {
			if(io.citation.citationItems.length == 1) {
				// single citation
				_suppressNextTreeSelect = true;
				itemsView.selectItem(io.citation.citationItems[0].itemID);	 // treeview from selectItemsDialog.js
				for(var property in _preserveData) {
					if(io.citation.citationItems[0][property]) {
						if(property == "locatorType") {
							document.getElementById(property)[_preserveData[property]] = _locatorIndexArray[io.citation.citationItems[0][property]];
						} else {
							document.getElementById(property)[_preserveData[property]] = io.citation.citationItems[0][property];
						}
					}
				}
			} else {
				// multiple citations
				toggleMultipleSources();
				for(var i=0; i<io.citation.citationItems.length; i++) {
					var item = Zotero.Items.get(io.citation.citationItems[i].itemID);
					if(item) {
						_addItem(item);
						_itemData[io.citation.citationItems[i].itemID] = io.citation.citationItems[i];
					}
				}
			}
			
			// show user-editable edited citation
			if(io.citation.properties.custom) {
				toggleEditor(io.citation.properties.custom);
				io.citation.properties.custom = undefined;
			}
			
			_updateAccept();
		}
		
		// Center citation popups manually after a delay when using a popup, since
		// popups aren't resizable and there might be persisted positions
		if (Zotero.Integration.usePopup) {
			document.getElementsByTagName("dialog")[0].style.border = "1px solid black";
			setTimeout(function () {
				window.centerWindowOnScreen();
			}, 1);
		}
	}
	
	/*
	 * turn on/off multiple sources item list
	 */
	function toggleMultipleSources() {
		_multipleSourcesOn = !_multipleSourcesOn;
		if(_multipleSourcesOn) {
			document.getElementById("multiple-sources").hidden = undefined;
			document.getElementById("zotero-add-citation-dialog").width = "750";
			document.getElementById("multiple-sources-button").label = Zotero.getString("citation.singleSource");			
			window.sizeToContent();
			window.moveTo((window.screenX-75), window.screenY);
			treeItemSelected();
			// disable adding info until citation added
			_itemSelected(false);
		} else {
			document.getElementById("multiple-sources").hidden = true;
			document.getElementById("zotero-add-citation-dialog").width = "600";
			document.getElementById("multiple-sources-button").label = Zotero.getString("citation.multipleSources");			
			window.sizeToContent();
			window.moveTo((window.screenX+75), window.screenY);
			
			// enable all fields
			for(var i in _preserveData) {
				document.getElementById(i).disabled = false;
			}
			
			// delete item list
			_itemData = new Object();
			
			// delete all items
			_clearCitationList();
		}
		_updateAccept();
		_updatePreview();
	}
	
	/*
	 * called when an item in the item selection tree is clicked
	 */
	function treeItemSelected() {
		if(_suppressNextTreeSelect) {
			_suppressNextTreeSelect = false;
			_updateAccept();
			return;
		}
		var items = itemsView.getSelectedItems(true); // treeview from selectItemsDialog.js
		var itemID = (items.length ? items[0] : false);
		
		if(_multipleSourcesOn) {
			// if item is also on right side, show info
			var hasBeenAdded = itemID && _itemData[itemID] !== undefined;<commit_msg>Checking in <NAME>'s single-to-multiple citation dialog fix.<commit_after>/*
    ***** BEGIN LICENSE BLOCK *****
    
    Copyright (c) 2006  Center for History and New Media
                        George Mason University, Fairfax, Virginia, USA
                        http://chnm.gmu.edu
    
    Licensed under the Educational Community License, Version 1.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
    http://www.opensource.org/licenses/ecl1.php
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    
    ***** END LICENSE BLOCK *****
*/

var Zotero_Citation_Dialog = new function () {
	var _preserveData = {
		"prefix":"value",
		"suffix":"value",
		"locatorType":"selectedIndex",
		"locator":"value",
		"suppressAuthor":"checked"
	};
	
	var _itemData = new Object();
	var _multipleSourcesOn = false;
	var _lastSelected = null;
	var _previewShown = false;
	var _suppressNextTreeSelect = false;
	var _locatorIndexArray = {};
	var _autoRegeneratePref;
	var _acceptButton;
	var _sortCheckbox;
	var _originalHTML;
	var io;
	
	this.load = load;
	this.toggleMultipleSources = toggleMultipleSources;
	this.toggleEditor = toggleEditor;
	this.treeItemSelected = treeItemSelected;
	this.listItemSelected = listItemSelected;
	this.add = add;
	this.remove = remove;
	this.sortCitation = sortCitation;
	this.confirmRegenerate = confirmRegenerate;
	this.accept = accept;
	this.cancel = cancel;
	
	/*
	 * initialize add citation dialog
	 */
	function load() {
		document.getElementById("multiple-sources-button").label = Zotero.getString("citation.multipleSources");
		document.getElementById("show-editor-button").label = Zotero.getString("citation.showEditor");
		
		io = window.arguments[0].wrappedJSObject;
		
		// find accept button
		_acceptButton = document.getElementById("zotero-add-citation-dialog").getButton("accept");
		_autoRegeneratePref = Zotero.Prefs.get("integration.autoRegenerate");
		
		// if a style with sortable citations, present checkbox
		if(io.citation.sortable) {
			_sortCheckbox = document.getElementById("keepSorted");
			_sortCheckbox.hidden = false;
			if(io.citation.properties.sort === undefined) io.citation.properties.sort = true;
			_sortCheckbox.checked = io.citation.properties.sort;
		}
		
		// load locators
		var locators = Zotero.CSL.Global.getLocatorStrings();
		var menu = document.getElementById("locatorType");
		var popup = document.getElementById("locator-type-popup");
		var i = 0;
		for(var value in locators) {
			var locator = locators[value];
			locator = locator[0].toUpperCase()+locator.substr(1);
			// add to popup
			var child = document.createElement("menuitem");
			child.setAttribute("value", value);
			child.setAttribute("label", locator);
			popup.appendChild(child);
			// add to array
			_locatorIndexArray[value] = i;
			i++;
		}
		menu.selectedIndex = 0;
		
		// load (from selectItemsDialog.js)
		doLoad();
		
		// if we already have a citation, load data from it
		document.getElementById('editor').format = "Integration";
		if(io.citation.citationItems.length) {
			if(io.citation.citationItems.length == 1) {
				// single citation
				_suppressNextTreeSelect = true;
				itemsView.selectItem(io.citation.citationItems[0].itemID);	 // treeview from selectItemsDialog.js
				for(var property in _preserveData) {
					if(io.citation.citationItems[0][property]) {
						if(property == "locatorType") {
							document.getElementById(property)[_preserveData[property]] = _locatorIndexArray[io.citation.citationItems[0][property]];
						} else {
							document.getElementById(property)[_preserveData[property]] = io.citation.citationItems[0][property];
						}
					}
				}
			} else {
				// multiple citations
				toggleMultipleSources();
				for(var i=0; i<io.citation.citationItems.length; i++) {
					var item = Zotero.Items.get(io.citation.citationItems[i].itemID);
					if(item) {
						_addItem(item);
						_itemData[io.citation.citationItems[i].itemID] = io.citation.citationItems[i];
					}
				}
			}
			
			// show user-editable edited citation
			if(io.citation.properties.custom) {
				toggleEditor(io.citation.properties.custom);
				io.citation.properties.custom = undefined;
			}
			
			_updateAccept();
		}
		
		// Center citation popups manually after a delay when using a popup, since
		// popups aren't resizable and there might be persisted positions
		if (Zotero.Integration.usePopup) {
			document.getElementsByTagName("dialog")[0].style.border = "1px solid black";
			setTimeout(function () {
				window.centerWindowOnScreen();
			}, 1);
		}
	}
	
	/*
	 * turn on/off multiple sources item list
	 */
	function toggleMultipleSources() {
		_multipleSourcesOn = !_multipleSourcesOn;
		if(_multipleSourcesOn) {
			var items = itemsView.getSelectedItems(true);
			var itemID = (items.length ? items[0] : false);
			// var itemDataID = itemID+"::"+0;
			document.getElementById("multiple-sources").hidden = undefined;
			document.getElementById("zotero-add-citation-dialog").width = "750";
			document.getElementById("multiple-sources-button").label = Zotero.getString("citation.singleSource");			
			// move user field content to multiple before adding XXXXX
			if (itemID) {
				// _itemData[itemDataID] = new Object();
				_itemData[itemID] = new Object();
				for (box in _preserveData) {
					element = document.getElementById(box);
					// _itemData[itemDataID][box] = element[_preserveData[box]];
					_itemData[itemID][box] = element[_preserveData[box]];
				}
			}
			window.sizeToContent();
			window.moveTo((window.screenX-75), window.screenY);
			treeItemSelected();
			// disable adding info until citation added
			_itemSelected(false);
			// add current selection
			if (itemID) {
				this.add();
			} else {
				_updateAccept();
				_updatePreview();
			}
		} else {
			document.getElementById("multiple-sources").hidden = true;
			document.getElementById("zotero-add-citation-dialog").width = "600";
			document.getElementById("multiple-sources-button").label = Zotero.getString("citation.multipleSources");			
			window.sizeToContent();
			window.moveTo((window.screenX+75), window.screenY);
			
			// enable all fields
			for(var i in _preserveData) {
				document.getElementById(i).disabled = false;
			}
			
			// delete item list
			_itemData = new Object();
			
			// delete all items
			_clearCitationList();
			_updateAccept();
			_updatePreview();
		}
	}
	
	/*
	 * called when an item in the item selection tree is clicked
	 */
	function treeItemSelected() {
		if(_suppressNextTreeSelect) {
			_suppressNextTreeSelect = false;
			_updateAccept();
			return;
		}
		var items = itemsView.getSelectedItems(true); // treeview from selectItemsDialog.js
		var itemID = (items.length ? items[0] : false);
		
		if(_multipleSourcesOn) {
			// if item is also on right side, show info
			var hasBeenAdded = itemID && _itemData[itemID] !== undefined;
{'fraction_non_alphanumeric': 0.12027616279069768, 'fraction_numerical': 0.004796511627906977, 'mean_word_length': 3.691783157176952, 'pattern_counts': {'":': 11, '<': 6, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2223054', 'n_tokens_mistral': 4807, 'n_tokens_neox': 3995, 'n_words': 930}