<commit_before><?xml version="1.0" encoding="utf-8"?>
<!--
/*
    Weave (Web-based Analysis and Visualization Environment)
    Copyright (C) 2008-2011 University of Massachusetts Lowell

    This file is a part of Weave.

    Weave is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License, Version 3,
    as published by the Free Software Foundation.

    Weave is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Weave.  If not, see <http://www.gnu.org/licenses/>.
*/
-->
<mx:VBox 
	xmlns:mx="http://www.adobe.com/2006/mxml"
	xmlns="weave.editors.*"
	verticalScrollPolicy="off"
	horizontalScrollPolicy="off"
	creationPolicy="all"
	implements="weave.api.ui.ILinkableObjectEditor" xmlns:ui="weave.ui.*"
	>
	<mx:Label id="editorTitle" fontWeight="bold" text="{lang((_csvDataSource ? 'Edit' : 'Add') + ' CSV Data Source')}"/>
	<ui:Indent group="{indentGroup}" label="{lang('Source Name *')}">
		<mx:TextInput id="sourceName" focusOut="keyTypeSelector.addKeyTypeToMenu(sourceName.text)" width="100%"/>
	</ui:Indent>
	<ui:Indent group="{indentGroup}" label="{lang('Source URL')}">
		<ui:TextInputWithPrompt change="handleURLTextChange()" id="url" width="100%" prompt="http://www.example.com/example.csv"/>
	</ui:Indent>
	<mx:VBox width="100%" enabled="{url.text?false:true}">
		<mx:Label width="100%" textAlign="center" text="OR" fontWeight="bold"/>
		<ui:Indent group="{indentGroup}" label="{lang('Open local CSV file')}">
			<mx:Button label="{lang('Open file')}" click="append = false; browseForFiles()"/>
			<mx:Button label="{lang('Append file')}" click="append = true; browseForFiles()"/>
			<mx:Label id="uploadMessage" color="0xFF0000" text="{lang('loaded')}" visible="false"/>
		</ui:Indent>
		<mx:Label width="100%" textAlign="center" text="{lang('OR')}" fontWeight="bold"/>
		<ui:Indent group="{indentGroup}" label="{lang('Paste/Edit CSV Data')}">
			<mx:TextArea id="csvTextArea" change="csvParser.parseCSV(csvTextArea.text)" width="100%" height="100"/>
		</ui:Indent>
		<mx:Button label="{lang('View data in Grid')}" id="showGridButton" click="showGrid()" enabled="{!arrayIsEmpty(csvRows)}"/>
	</mx:VBox>
	<ui:Indent group="{indentGroup}" label="{lang('Key Column')}">
		<mx:TextInput id="keyColumnName" width="100%" toolTip="{lang('Enter the name of the column that uniquely identifies each row in the data set')}"/>
	</ui:Indent>
	<ui:Indent label="{lang('Key Type *')}">
		<ui:KeyTypesMenu id="keyTypeSelector"/>
	</ui:Indent>
	<mx:Spacer/>
	<mx:Label text="{lang('* = required')}"/>
	<mx:Script>
		<![CDATA[
			import mx.controls.dataGridClasses.DataGridColumn;
			import mx.managers.PopUpManager;
			
			import weave.Weave;
			import weave.api.WeaveAPI;
			import weave.api.core.ILinkableObject;
			import weave.api.getCallbackCollection;
			import weave.api.getSessionState;
			import weave.api.reportError;
			import weave.compiler.StandardLib;
			import weave.data.CSVParser;
			import weave.data.DataSources.CSVDataSource;
			import weave.editors.managers.EditDataGridPanel;
			import weave.ui.BusyIndicator;
			import weave.ui.IndentGroup;
			import weave.utils.VectorUtils;
			
			[Bindable] private var indentGroup:IndentGroup = new IndentGroup();
			
			private var busyIndicator:BusyIndicator = new BusyIndicator();
			
			override protected function createChildren():void
			{
				super.createChildren();
				addChild(busyIndicator);
				hideBusy();
				sourceName.text = Weave.root.generateUniqueName("CSVDataSource");
				keyTypeSelector.addKeyTypeToMenu(sourceName.text);
				getCallbackCollection(csvParser).addImmediateCallback(this, handleCSVParser);
			}
			
			
			[Bindable] private var _csvDataSource:CSVDataSource = null;
			public function setTarget(object:ILinkableObject):void
			{
				_csvDataSource = object as CSVDataSource;
				
				if (!parent)
				{
					callLater(setTarget, arguments);
					return;
				}
				
				if (_csvDataSource)
				{
					sourceName.text = Weave.root.getName(_csvDataSource);
					url.text = _csvDataSource.url.value;
					keyColumnName.text = _csvDataSource.keyColName.value;
					keyTypeSelector.addKeyTypeToMenu(_csvDataSource.keyType.value);
					keyTypeSelector.selectedKeyType = _csvDataSource.keyType.value;
					sourceName.editable = false;
					sourceName.toolTip = lang("Source name is not editable once added to the session. If needed, delete the source and add it again with the new name.")
					handleURLTextChange();
					if (!url.text)
					{
						var rows:Array = _csvDataSource.csvData.getSessionState() as Array;
						setCSVRows(rows);
					}
				}
			}
			
			public function hasPendingChanges():Boolean
			{
				return StandardLib.asString(_csvDataSource.url.value) != StandardLib.asString(url.text)
					|| StandardLib.asString(_csvDataSource.keyColName.value) != StandardLib.asString(keyColumnName.text)
					|| StandardLib.asString(_csvDataSource.keyType.value) != StandardLib.asString(keyTypeSelector.selectedKeyType)
					|| !!WeaveAPI.SessionManager.computeDiff(getSessionState(_csvDataSource.csvData), csvRows);
			}

			public function applyChanges():void
			{
				_csvDataSource.url.value = url.text;
				_csvDataSource.keyColName.value = keyColumnName.text;
				_csvDataSource.keyType.value = keyTypeSelector.selectedKeyType;
				_csvDataSource.csvData.setSessionState(csvRows);
			}
			
			private var append:Boolean = false;
			private var _fileLoader:FileReference = null;
			/**
			* browseForfiles: opens up a file browser window to upload CSV files 
			**/
			private function browseForFiles():void
			{
				try
				{
					if(_fileLoader == null)
					{
						_fileLoader = new FileReference();
						_fileLoader.addEventListener(Event.SELECT, csvFileSelectedHandler);
						_fileLoader.addEventListener(ProgressEvent.PROGRESS, handleProgress);
						_fileLoader.addEventListener(Event.COMPLETE, csvFileLoadCompleteHandler);
						_fileLoader.addEventListener(IOErrorEvent.IO_ERROR, fileErrorHandler);
					}
					
					_fileLoader.browse([new FileFilter("CSV", "*.csv")]);
				}
				catch (e:Error)
				{
					reportError(e);
				}
			}
			
			
			// called when parser finishes
			private function handleCSVParser():void
			{
				if (append)
				{
					setCSVRows(
						WeaveAPI.CSVParser.convertRecordsToRows(
							VectorUtils.flatten(
								WeaveAPI.CSVParser.convertRowsToRecords(csvParser.parseResult),
								WeaveAPI.CSVParser.convertRowsToRecords(csvRows)
							),
							csvRows[0]
						)
					);
				}
				else
					setCSVRows(csvParser.parseResult);
			}
			private function setCSVRows(rows:Array):void
			{
				csvRows = rows;
				headerArray = rows ? rows[0] : [];
				
				//this is set to false because this function might be called from Text Area change.
				uploadMessage.visible = false;
				
				//clearing the Source URL text input
				url.text = "";
				
				hideBusy();
			}
			<commit_msg>Fixed bug where CSVDataSourceEditor's append file feature wouldn't add new columns.<commit_after><?xml version="1.0" encoding="utf-8"?>
<!--
/*
    Weave (Web-based Analysis and Visualization Environment)
    Copyright (C) 2008-2011 University of Massachusetts Lowell

    This file is a part of Weave.

    Weave is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License, Version 3,
    as published by the Free Software Foundation.

    Weave is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Weave.  If not, see <http://www.gnu.org/licenses/>.
*/
-->
<mx:VBox 
	xmlns:mx="http://www.adobe.com/2006/mxml"
	xmlns="weave.editors.*"
	verticalScrollPolicy="off"
	horizontalScrollPolicy="off"
	creationPolicy="all"
	implements="weave.api.ui.ILinkableObjectEditor" xmlns:ui="weave.ui.*"
	>
	<mx:Label id="editorTitle" fontWeight="bold" text="{lang((_csvDataSource ? 'Edit' : 'Add') + ' CSV Data Source')}"/>
	<ui:Indent group="{indentGroup}" label="{lang('Source Name *')}">
		<mx:TextInput id="sourceName" focusOut="keyTypeSelector.addKeyTypeToMenu(sourceName.text)" width="100%"/>
	</ui:Indent>
	<ui:Indent group="{indentGroup}" label="{lang('Source URL')}">
		<ui:TextInputWithPrompt change="handleURLTextChange()" id="url" width="100%" prompt="http://www.example.com/example.csv"/>
	</ui:Indent>
	<mx:VBox width="100%" enabled="{url.text?false:true}">
		<mx:Label width="100%" textAlign="center" text="OR" fontWeight="bold"/>
		<ui:Indent group="{indentGroup}" label="{lang('Open local CSV file')}">
			<mx:Button label="{lang('Open file')}" click="append = false; browseForFiles()"/>
			<mx:Button label="{lang('Append file')}" click="append = true; browseForFiles()"/>
			<mx:Label id="uploadMessage" color="0xFF0000" text="{lang('loaded')}" visible="false"/>
		</ui:Indent>
		<mx:Label width="100%" textAlign="center" text="{lang('OR')}" fontWeight="bold"/>
		<ui:Indent group="{indentGroup}" label="{lang('Paste/Edit CSV Data')}">
			<mx:TextArea id="csvTextArea" change="csvParser.parseCSV(csvTextArea.text)" width="100%" height="100"/>
		</ui:Indent>
		<mx:Button label="{lang('View data in Grid')}" id="showGridButton" click="showGrid()" enabled="{!arrayIsEmpty(csvRows)}"/>
	</mx:VBox>
	<ui:Indent group="{indentGroup}" label="{lang('Key Column')}">
		<mx:TextInput id="keyColumnName" width="100%" toolTip="{lang('Enter the name of the column that uniquely identifies each row in the data set')}"/>
	</ui:Indent>
	<ui:Indent label="{lang('Key Type *')}">
		<ui:KeyTypesMenu id="keyTypeSelector"/>
	</ui:Indent>
	<mx:Spacer/>
	<mx:Label text="{lang('* = required')}"/>
	<mx:Script>
		<![CDATA[
			import mx.controls.dataGridClasses.DataGridColumn;
			import mx.managers.PopUpManager;
			
			import weave.Weave;
			import weave.api.WeaveAPI;
			import weave.api.core.ILinkableObject;
			import weave.api.getCallbackCollection;
			import weave.api.getSessionState;
			import weave.api.reportError;
			import weave.compiler.StandardLib;
			import weave.data.CSVParser;
			import weave.data.DataSources.CSVDataSource;
			import weave.editors.managers.EditDataGridPanel;
			import weave.ui.BusyIndicator;
			import weave.ui.IndentGroup;
			import weave.utils.VectorUtils;
			
			[Bindable] private var indentGroup:IndentGroup = new IndentGroup();
			
			private var busyIndicator:BusyIndicator = new BusyIndicator();
			
			override protected function createChildren():void
			{
				super.createChildren();
				addChild(busyIndicator);
				hideBusy();
				sourceName.text = Weave.root.generateUniqueName("CSVDataSource");
				keyTypeSelector.addKeyTypeToMenu(sourceName.text);
				getCallbackCollection(csvParser).addImmediateCallback(this, handleCSVParser);
			}
			
			
			[Bindable] private var _csvDataSource:CSVDataSource = null;
			public function setTarget(object:ILinkableObject):void
			{
				_csvDataSource = object as CSVDataSource;
				
				if (!parent)
				{
					callLater(setTarget, arguments);
					return;
				}
				
				if (_csvDataSource)
				{
					sourceName.text = Weave.root.getName(_csvDataSource);
					url.text = _csvDataSource.url.value;
					keyColumnName.text = _csvDataSource.keyColName.value;
					keyTypeSelector.addKeyTypeToMenu(_csvDataSource.keyType.value);
					keyTypeSelector.selectedKeyType = _csvDataSource.keyType.value;
					sourceName.editable = false;
					sourceName.toolTip = lang("Source name is not editable once added to the session. If needed, delete the source and add it again with the new name.")
					handleURLTextChange();
					if (!url.text)
					{
						var rows:Array = _csvDataSource.csvData.getSessionState() as Array;
						setCSVRows(rows);
					}
				}
			}
			
			public function hasPendingChanges():Boolean
			{
				return StandardLib.asString(_csvDataSource.url.value) != StandardLib.asString(url.text)
					|| StandardLib.asString(_csvDataSource.keyColName.value) != StandardLib.asString(keyColumnName.text)
					|| StandardLib.asString(_csvDataSource.keyType.value) != StandardLib.asString(keyTypeSelector.selectedKeyType)
					|| !!WeaveAPI.SessionManager.computeDiff(getSessionState(_csvDataSource.csvData), csvRows);
			}

			public function applyChanges():void
			{
				_csvDataSource.url.value = url.text;
				_csvDataSource.keyColName.value = keyColumnName.text;
				_csvDataSource.keyType.value = keyTypeSelector.selectedKeyType;
				_csvDataSource.csvData.setSessionState(csvRows);
			}
			
			private var append:Boolean = false;
			private var _fileLoader:FileReference = null;
			/**
			* browseForfiles: opens up a file browser window to upload CSV files 
			**/
			private function browseForFiles():void
			{
				try
				{
					if(_fileLoader == null)
					{
						_fileLoader = new FileReference();
						_fileLoader.addEventListener(Event.SELECT, csvFileSelectedHandler);
						_fileLoader.addEventListener(ProgressEvent.PROGRESS, handleProgress);
						_fileLoader.addEventListener(Event.COMPLETE, csvFileLoadCompleteHandler);
						_fileLoader.addEventListener(IOErrorEvent.IO_ERROR, fileErrorHandler);
					}
					
					_fileLoader.browse([new FileFilter("CSV", "*.csv")]);
				}
				catch (e:Error)
				{
					reportError(e);
				}
			}
			
			
			// called when parser finishes
			private function handleCSVParser():void
			{
				if (append)
				{
					var newRows:Array = csvParser.parseResult;
					setCSVRows(
						WeaveAPI.CSVParser.convertRecordsToRows(
							VectorUtils.flatten(
								WeaveAPI.CSVParser.convertRowsToRecords(newRows),
								WeaveAPI.CSVParser.convertRowsToRecords(csvRows)
							),
							VectorUtils.union(headerArray, newRows[0])
						)
					);
				}
				else
					setCSVRows(csvParser.parseResult);
			}
			private function setCSVRows(rows:Array):void
			{
				csvRows = rows;
				headerArray = rows ? rows[0] : [];
				
				//this is set to false because this function might be called from Text Area change.
				uploadMessage.visible = false;
				
				//clearing the Source URL text input
				url.text = "";
				
				hideBusy();
			}
			
{'fraction_non_alphanumeric': 0.12898309781102799, 'fraction_numerical': 0.006511499030202272, 'mean_word_length': 4.070951879171057, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 2, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 10}, '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': '2884608', 'n_tokens_mistral': 5444, 'n_tokens_neox': 4404, 'n_words': 946}