<commit_before>'use strict';

angular.module('settings', ['jsonRpc', 'ui.bootstrap', 'bellows.services', 'palaso.ui.notice', 'palaso.ui.dc.entry', 'ngAnimate'])
.controller('SettingsCtrl', ['$scope', '$routeParams', 'userService', 'sessionService', 'silNoticeService', 'lexEntryService', '$window', '$timeout', 
							function($scope, $routeParams, userService, ss, notice, lexService, $window, $timeout) {
	$scope.project = {
		'id': $routeParams.projectId
	};
	
	$scope.showPre = true;		// TODO Remove. Set false to hide <pre>. Remove this and all <pre> IJH 2014-02

	$scope.config = {};
	$scope.languageCodes = {};
	$scope.lists = {
		inputSystems: {}
	};
	
	$scope.selects = {
		'special': {
			'options': [
				'none',
				'IPA transcription',
				'Voice',
				'Script / Region / Variant'
			]
		},
		'purpose': {
			'options': {
				'etic': 'Etic (raw phonetic transcription)',
				'emic': 'Emic (uses the phonology of the language)'
			}
		},
		'script': {
			'options': inputSystems.scripts()
		},
		'region': {
			'options': inputSystems.regions()
		},
	};
	
	$scope.currentInputSystemTag = '';
	$scope.currentInputSystem = {
		'name': '',
		'code': '',
		'abbreviation': '',
		'special': '',
		'purpose': '',
		'script': '',
		'region': '',
		'variant': ''
	};
	$scope.selectInputSystem = function(inputSystemTag) {
		// TODO Add. update old before loading new IJH 2014-02
		$scope.currentInputSystemTag = inputSystemTag;
		$scope.currentInputSystem.name = $scope.lists.inputSystems[inputSystemTag].name;
		$scope.currentInputSystem.code = $scope.lists.inputSystems[inputSystemTag].code;
		$scope.currentInputSystem.abbreviation = $scope.lists.inputSystems[inputSystemTag].abbreviation;
		$scope.currentInputSystem = convertSelects($scope.currentInputSystem, $scope.lists.inputSystems[inputSystemTag]);
	};
	
	$scope.queryProjectSettings = function() {
		lexService.projectSettings($scope.project.id, function(result) {
			if (result.ok) {
				$scope.languageCodes = inputSystems.languageCodes();
				$scope.config = result.data.config;
				$scope.lists.inputSystems = $scope.config.inputSystems;
				for (var tag in $scope.lists.inputSystems) {
					var code = inputSystems.getCode(tag);
					var script = inputSystems.getScript(tag);
					var region = inputSystems.getRegion(tag);
					var privateUse = inputSystems.getPrivateUse(tag);
					$scope.lists.inputSystems[tag].code = code;
					$scope.lists.inputSystems[tag].script = script;
					$scope.lists.inputSystems[tag].region = region;
					$scope.lists.inputSystems[tag].privateUse = privateUse;
					$scope.lists.inputSystems[tag].name = inputSystems.getName(code, script, region, privateUse);
				};
				$scope.currentFieldName = 'lexeme';
				$scope.currentTaskName = 'dashboard';
			}
		});
	};
	
	$scope.queryProjectSettings();

	$scope.settingsApply = function() {
//			console.log("settingsApply");
		lexService.updateProjectSettings($scope.project.id, $scope.config, function(result) {
			if (result.ok) {
				notice.push(notice.SUCCESS, "Project settings updated successfully");
				$scope.settingsForm.$setPristine();
			}
		});
	};
	
	// convert raw config inputSystems to use in selectors
	var convertSelects = function(selectorInputSystem, inputSystem) {
		selectorInputSystem.purpose = '';
		selectorInputSystem.script = '';
		selectorInputSystem.region = '';
		selectorInputSystem.variant = '';
		switch(inputSystem.script) {
			case '':
				selectorInputSystem.special = $scope.selects.special.options[0];
				break;
			case 'fonipa':
				selectorInputSystem.special = $scope.selects.special.options[1];
				selectorInputSystem.purpose = inputSystem.privateUse;
				break;
			case 'Zxxx':
				if (inputSystem.privateUse == 'audio') {
					selectorInputSystem.special = $scope.selects.special.options[2];
					break;
				}
			default:
				selectorInputSystem.special = $scope.selects.special.options[3];
				selectorInputSystem.script = inputSystem.script;
				selectorInputSystem.region = inputSystem.region;
				selectorInputSystem.variant = inputSystem.privateUse;
		}
		return selectorInputSystem;
	};
	
	$scope.$watch('config.inputSystems', function(newValue) {
//			console.log("config input systems watch: ", newValue);
		if (newValue != undefined) {
			for (var tag in newValue) {
				$scope.currentInputSystemTag = tag;
				break;
			}
		}
	});
	$scope.$watchCollection('currentInputSystem', function(newValue) {
//			console.log("current input system watch: ", newValue);
		if (newValue != undefined) {
			$scope.currentInputSystemTag = $scope.currentInputSystem.code;
			switch($scope.currentInputSystem.special) {
				case $scope.selects.special.options[1]:		// IPA transcription
					$scope.currentInputSystemTag += '-fonipa';
					$scope.currentInputSystemTag += ($scope.currentInputSystem.purpose) ? '-x-' + $scope.currentInputSystem.purpose : '';
					break;
				case $scope.selects.special.options[2]:		// Voice
					$scope.currentInputSystemTag += '-Zxxx-x-audio';
					break;
				case $scope.selects.special.options[3]:		// Script / Region / Variant
					$scope.currentInputSystemTag += ($scope.currentInputSystem.script) ? '-' + $scope.currentInputSystem.script : '';
					$scope.currentInputSystemTag += ($scope.currentInputSystem.region) ? '-' + $scope.currentInputSystem.region : '';
					$scope.currentInputSystemTag += ($scope.currentInputSystem.variant) ? '-x-' + $scope.currentInputSystem.variant : '';
					break;
			}
		}
	});
<commit_msg>Input Systems Settings - automatically select first item in the list<commit_after>'use strict';

angular.module('settings', ['jsonRpc', 'ui.bootstrap', 'bellows.services', 'palaso.ui.notice', 'palaso.ui.dc.entry', 'ngAnimate'])
.controller('SettingsCtrl', ['$scope', '$routeParams', 'userService', 'sessionService', 'silNoticeService', 'lexEntryService', '$window', '$timeout', '$filter', 
							function($scope, $routeParams, userService, ss, notice, lexService, $window, $timeout, $filter) {
	$scope.project = {
		'id': $routeParams.projectId
	};
	
	$scope.showPre = true;		// TODO Remove. Set false to hide <pre>. Remove this and all debug <pre> IJH 2014-02

	$scope.config = {};
	$scope.languageCodes = {};
	$scope.lists = {
		inputSystems: {}
	};
	
	$scope.selects = {
		'special': {
			'options': [
				'none',
				'IPA transcription',
				'Voice',
				'Script / Region / Variant'
			]
		},
		'purpose': {
			'options': {
				'etic': 'Etic (raw phonetic transcription)',
				'emic': 'Emic (uses the phonology of the language)'
			}
		},
		'script': {
			'options': inputSystems.scripts()
		},
		'region': {
			'options': inputSystems.regions()
		},
	};
	
	$scope.currentInputSystemTag = '';
	$scope.currentInputSystem = {
		'name': '',
		'code': '',
		'abbreviation': '',
		'special': '',
		'purpose': '',
		'script': '',
		'region': '',
		'variant': ''
	};
	$scope.selectInputSystem = function(inputSystemTag) {
		// TODO Add. update old before loading new IJH 2014-02
		$scope.currentInputSystemTag = inputSystemTag;
		$scope.currentInputSystem.name = $scope.lists.inputSystems[inputSystemTag].name;
		$scope.currentInputSystem.code = $scope.lists.inputSystems[inputSystemTag].code;
		$scope.currentInputSystem.abbreviation = $scope.lists.inputSystems[inputSystemTag].abbreviation;
		$scope.currentInputSystem = convertSelects($scope.currentInputSystem, $scope.lists.inputSystems[inputSystemTag]);
	};
	
	$scope.queryProjectSettings = function() {
		lexService.projectSettings($scope.project.id, function(result) {
			if (result.ok) {
				$scope.languageCodes = inputSystems.languageCodes();
				$scope.config = result.data.config;
				$scope.lists.inputSystems = $scope.config.inputSystems;
				for (var tag in $scope.lists.inputSystems) {
					var code = inputSystems.getCode(tag);
					var script = inputSystems.getScript(tag);
					var region = inputSystems.getRegion(tag);
					var privateUse = inputSystems.getPrivateUse(tag);
					$scope.lists.inputSystems[tag].code = code;
					$scope.lists.inputSystems[tag].script = script;
					$scope.lists.inputSystems[tag].region = region;
					$scope.lists.inputSystems[tag].privateUse = privateUse;
					$scope.lists.inputSystems[tag].name = inputSystems.getName(code, script, region, privateUse);
				};
				// select the first items
				$scope.selectInputSystem($filter('orderAsArray')($scope.config.inputSystems, 'tag')[0]['tag']);
				$scope.currentFieldName = 'lexeme';
				$scope.currentTaskName = 'dashboard';
			}
		});
	};

	$scope.settingsApply = function() {
//		console.log("settingsApply");
		lexService.updateProjectSettings($scope.project.id, $scope.config, function(result) {
			if (result.ok) {
				notice.push(notice.SUCCESS, "Project settings updated successfully");
				$scope.settingsForm.$setPristine();
			}
		});
	};
	
	// convert raw config inputSystems to use in selectors
	var convertSelects = function(selectorInputSystem, inputSystem) {
		selectorInputSystem.purpose = '';
		selectorInputSystem.script = '';
		selectorInputSystem.region = '';
		selectorInputSystem.variant = '';
		switch(inputSystem.script) {
			case '':
				selectorInputSystem.special = $scope.selects.special.options[0];
				break;
			case 'fonipa':
				selectorInputSystem.special = $scope.selects.special.options[1];
				selectorInputSystem.purpose = inputSystem.privateUse;
				break;
			case 'Zxxx':
				if (inputSystem.privateUse == 'audio') {
					selectorInputSystem.special = $scope.selects.special.options[2];
					break;
				}
			default:
				selectorInputSystem.special = $scope.selects.special.options[3];
				selectorInputSystem.script = inputSystem.script;
				selectorInputSystem.region = inputSystem.region;
				selectorInputSystem.variant = inputSystem.privateUse;
		}
		return selectorInputSystem;
	};
	
	$scope.queryProjectSettings();
	
	$scope.$watchCollection('currentInputSystem', function(newValue) {
		console.log("current input system watch: ", newValue);
		if (newValue != undefined) {
			$scope.currentInputSystemTag = $scope.currentInputSystem.code;
			switch($scope.currentInputSystem.special) {
				case $scope.selects.special.options[1]:		// IPA transcription
					$scope.currentInputSystemTag += '-fonipa';
					$scope.currentInputSystemTag += ($scope.currentInputSystem.purpose) ? '-x-' + $scope.currentInputSystem.purpose : '';
					break;
				case $scope.selects.special.options[2]:		// Voice
					$scope.currentInputSystemTag += '-Zxxx-x-audio';
					break;
				case $scope.selects.special.options[3]:		// Script / Region / Variant
					$scope.currentInputSystemTag += ($scope.currentInputSystem.script) ? '-' + $scope.currentInputSystem.script : '';
					$scope.currentInputSystemTag += ($scope.currentInputSystem.region) ? '-' + $scope.currentInputSystem.region : '';
					$scope.currentInputSystemTag += ($scope.currentInputSystem.variant) ? '-x-' + $scope.currentInputSystem.variant : '';
					break;
			}
		}
	});

{'fraction_non_alphanumeric': 0.1553612517257248, 'fraction_numerical': 0.0035895075931891393, 'mean_word_length': 4.171822941456449, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '10582', 'n_tokens_mistral': 3965, 'n_tokens_neox': 3377, 'n_words': 568}