<commit_before>'use strict';

angular.module('dbe', ['jsonRpc', 'ui.bootstrap', 'bellows.services', 'palaso.ui.dc.entry', 'palaso.ui.dc.comments', 'ngAnimate', 'truncate', 'lexicon.services', 'palaso.ui.scroll'])
.controller('editCtrl', ['$scope', 'userService', 'sessionService', 'lexEntryService', 'lexConfigService', '$window', '$timeout', '$filter', 'lexLinkService', 
                        function ($scope, userService, sessionService, lexService, configService, $window, $timeout, $filter, linkService) {
	// see http://alistapart.com/article/expanding-text-areas-made-elegant
	// for an idea on expanding text areas
	
	/* this is what an entry looks like
	$scope.currentEntry = {
		'id': '1234',
		'lexeme': { 'en': '', 'th': '' },
		'senses': [
			{
				'meaning': { 'en': '', 'th': '' },
			}
		]
	};
	*/
	
	
	
	var pristineEntry = {};
	$scope.lastSavedDate = new Date();
	$scope.currentEntry = {};
	$scope.entries = [];
	$scope.config = {};
	
	$scope.currentEntryIsDirty = function() {
		if ($scope.entryLoaded()) {
			return !angular.equals($scope.currentEntry, pristineEntry);
		}
		return false;
	};
	
	$scope.saveButtonTitle = function() {
		if ($scope.currentEntryIsDirty()) {
			return "Save Now";
		} else {
			return "Saved " + moment($scope.lastSavedDate).fromNow();
		}
	};

	//$timeout($scope.saveButtonTitle,60000);
	//TODO set a watch on currentEntry
	// when currentEntry changes, if it can be saved, then
	// set a new 30 second timer, and delete the old timer
	// when timer goes off, execute the save now method and delete the timer
	
	$scope.saveCurrentEntry = function() {
		if ($scope.currentEntryIsDirty()) {
			var foundLexeme = false;
			angular.forEach($scope.config.entry.fields.lexeme.inputSystems, function(ws) {
				if($scope.currentEntry.lexeme[ws].value != '') {
					foundLexeme = true;
				};
			});
			if (foundLexeme) {
				lexService.update($scope.currentEntry, function(result) {
					//$scope.updateListWithEntry(result.data);
					$scope.lastSavedDate = new Date();
					pristineEntry = angular.copy($scope.currentEntry);
					$scope.refreshView($scope.load.iEntryStart, $scope.load.numberOfEntries);
				});
				return true;
			} else {
				return false;
			}
		}
		return true;
	};
	
	$scope.updateListWithEntry = function(entry) {
		var isNew = true;
		for (var i=0; i<$scope.entries.length; i++) {
			var e = $scope.entries[i];
			if (e.id == entry.id) {
				$scope.entries[i] = entry;
				isNew = false;
				break;
			}
		}
		if (isNew) {
			$scope.entries.unshift(entry);
		}
	};
	
	$scope.getEntryIndexById = function(id) {
		var index = undefined;
		for (var i=0; i<$scope.entries.length; i++) {
			var e = $scope.entries[i];
			if (e.id == id) {
				index = i;
				break;
			}
		}
		return index;
	};
	
	$scope.getMeaning = function(entry) {
		var meaning = '';
		if (angular.isDefined($scope.config.entry) && angular.isDefined(entry.definition)) {
			var ws = $scope.config.entry.fields.senses.fields.definition.inputSystems[0];
			if (angular.isDefined(entry.definition[ws])) {
				meaning = entry.definition[ws].value;
			}
		}
		return meaning;
	};
	
	$scope.setCurrentEntry = function(entry) {
		entry = entry || {};
		$scope.lexemeFormRequired = false;
		$scope.currentEntry = entry;
		pristineEntry = angular.copy(entry);
	};
	
	$scope.editEntry = function(id) {
		$scope.lexemeFormRequired = false;
		if (angular.isUndefined(id) || $scope.currentEntry.id != id) {
			if ($scope.saveCurrentEntry()) {
				if (arguments.length == 0) {
					//if ($scope.currentEntry.id != '') {
						var newEntry = {id:''};
						$scope.setCurrentEntry(newEntry);
						$scope.updateListWithEntry(newEntry);
					//}
				} else {
					lexService.read(id, function(result) {
						$scope.setCurrentEntry(result.data);
					});
				}
			} else {
				$scope.lexemeFormRequired = true;
			}
		}
	};

	$scope.newEntry = function() {
		$scope.editEntry();
	};
	
	$scope.getTitle = function(entry) {
		entry = entry || $scope.currentEntry;
		var title = "[empty]";
		if (entry.lexeme && $scope.config && $scope.config.entry) {
			var lexemeInputSystem = $scope.config.entry.fields.lexeme.inputSystems[0];
			if (angular.isDefined(entry.lexeme[lexemeInputSystem]) && entry.lexeme[lexemeInputSystem].value != '') {
				title = entry.lexeme[lexemeInputSystem].value;
			}
		}
		return title;
	};

	$scope.entryLoaded = function() {
		return angular.isDefined($scope.currentEntry.id);
	};
	
	$scope.deleteEntry = function(entry) {
		if ($window.confirm("Are you sure you want to delete '" + $scope.getTitle(entry) + "'?")) {
			if ($scope.entryHasComments(entry)) {
				if ($window.confirm("Are you sure you want to delete '" + $scope.getTitle(entry) + "'?")) {
					$scope.entries.splice($scope.getEntryIndexById(entry.id), 1);
					if (entry.id != '') {
						lexService.remove(entry.id, function(){});
					}
					$scope.setCurrentEntry({});
				}
			} else {
					$scope.entries.splice($scope.getEntryIndexById(entry.id), 1);
					if (entry.id != '') {
						lexService.remove(entry.id, function(){});
					}
					$scope.setCurrentEntry({});
			}
		}
	};
	
	$scope.entryHasComments = function(entry) {
		return false;
	};
	
	
	$scope.load = {
		iEntryStart: 0,
		numberOfEntries: null<commit_msg>On save, update DBE entry locally before update from Dto<commit_after>'use strict';

angular.module('dbe', ['jsonRpc', 'ui.bootstrap', 'bellows.services', 'palaso.ui.dc.entry', 'palaso.ui.dc.comments', 'ngAnimate', 'truncate', 'lexicon.services', 'palaso.ui.scroll'])
.controller('editCtrl', ['$scope', 'userService', 'sessionService', 'lexEntryService', 'lexConfigService', '$window', '$timeout', '$filter', 'lexLinkService', 
                        function ($scope, userService, sessionService, lexService, configService, $window, $timeout, $filter, linkService) {
	// see http://alistapart.com/article/expanding-text-areas-made-elegant
	// for an idea on expanding text areas
	
	/* this is what an entry looks like
	$scope.currentEntry = {
		'id': '1234',
		'lexeme': { 'en': '', 'th': '' },
		'senses': [
			{
				'meaning': { 'en': '', 'th': '' },
			}
		]
	};
	*/
	
	
	
	var pristineEntry = {};
	$scope.lastSavedDate = new Date();
	$scope.currentEntry = {};
	$scope.entries = [];
	$scope.config = {};
	
	$scope.currentEntryIsDirty = function() {
		if ($scope.entryLoaded()) {
			return !angular.equals($scope.currentEntry, pristineEntry);
		}
		return false;
	};
	
	$scope.saveButtonTitle = function() {
		if ($scope.currentEntryIsDirty()) {
			return "Save Now";
		} else {
			return "Saved " + moment($scope.lastSavedDate).fromNow();
		}
	};

	//$timeout($scope.saveButtonTitle,60000);
	//TODO set a watch on currentEntry
	// when currentEntry changes, if it can be saved, then
	// set a new 30 second timer, and delete the old timer
	// when timer goes off, execute the save now method and delete the timer
	
	$scope.saveCurrentEntry = function() {
		if ($scope.currentEntryIsDirty()) {
			var foundLexeme = false;
			angular.forEach($scope.config.entry.fields.lexeme.inputSystems, function(ws) {
				if($scope.currentEntry.lexeme[ws].value != '') {
					foundLexeme = true;
				};
			});
			if (foundLexeme) {
				lexService.update($scope.currentEntry, function(result) {
					$scope.updateListWithEntry(result.data);
					$scope.lastSavedDate = new Date();
					pristineEntry = angular.copy($scope.currentEntry);
					$scope.refreshView($scope.load.iEntryStart, $scope.load.numberOfEntries);
				});
				return true;
			} else {
				return false;
			}
		}
		return true;
	};
	
	$scope.updateListWithEntry = function(entry) {
		var isNew = true;
		for (var i=0; i<$scope.entries.length; i++) {
			var e = $scope.entries[i];
			if (e.id == entry.id) {
				$scope.show.entries[i] = entry;
				$scope.entries[i] = entry;
				isNew = false;
				break;
			}
		}
		if (isNew) {
			$scope.show.entries.unshift(entry);
			$scope.entries.unshift(entry);
		}
	};
	
	$scope.getEntryIndexById = function(id) {
		var index = undefined;
		for (var i=0; i<$scope.entries.length; i++) {
			var e = $scope.entries[i];
			if (e.id == id) {
				index = i;
				break;
			}
		}
		return index;
	};
	
	$scope.getMeaning = function(entry) {
		var meaning = '';
		if (angular.isDefined($scope.config.entry) && angular.isDefined(entry.definition)) {
			var ws = $scope.config.entry.fields.senses.fields.definition.inputSystems[0];
			if (angular.isDefined(entry.definition[ws])) {
				meaning = entry.definition[ws].value;
			}
		}
		return meaning;
	};
	
	$scope.setCurrentEntry = function(entry) {
		entry = entry || {};
		$scope.lexemeFormRequired = false;
		$scope.currentEntry = entry;
		pristineEntry = angular.copy(entry);
	};
	
	$scope.editEntry = function(id) {
		$scope.lexemeFormRequired = false;
		if (angular.isUndefined(id) || $scope.currentEntry.id != id) {
			if ($scope.saveCurrentEntry()) {
				if (arguments.length == 0) {
					//if ($scope.currentEntry.id != '') {
						var newEntry = {id:''};
						$scope.setCurrentEntry(newEntry);
						$scope.updateListWithEntry(newEntry);
					//}
				} else {
					lexService.read(id, function(result) {
						$scope.setCurrentEntry(result.data);
					});
				}
			} else {
				$scope.lexemeFormRequired = true;
			}
		}
	};

	$scope.newEntry = function() {
		$scope.editEntry();
	};
	
	$scope.getTitle = function(entry) {
		entry = entry || $scope.currentEntry;
		var title = "[empty]";
		if (entry.lexeme && $scope.config && $scope.config.entry) {
			var lexemeInputSystem = $scope.config.entry.fields.lexeme.inputSystems[0];
			if (angular.isDefined(entry.lexeme[lexemeInputSystem]) && entry.lexeme[lexemeInputSystem].value != '') {
				title = entry.lexeme[lexemeInputSystem].value;
			}
		}
		return title;
	};

	$scope.entryLoaded = function() {
		return angular.isDefined($scope.currentEntry.id);
	};
	
	$scope.deleteEntry = function(entry) {
		if ($window.confirm("Are you sure you want to delete '" + $scope.getTitle(entry) + "'?")) {
			if ($scope.entryHasComments(entry)) {
				if ($window.confirm("Are you sure you want to delete '" + $scope.getTitle(entry) + "'?")) {
					$scope.entries.splice($scope.getEntryIndexById(entry.id), 1);
					if (entry.id != '') {
						lexService.remove(entry.id, function(){});
					}
					$scope.setCurrentEntry({});
				}
			} else {
					$scope.entries.splice($scope.getEntryIndexById(entry.id), 1);
					if (entry.id != '') {
						lexService.remove(entry.id, function(){});
					}
					$scope.setCurrentEntry({});
			}
		}
	};
	
	$scope.entryHasComments = function(entry) {
		return false;
	};
	
	$scope.load = {
		iEntryStart: 0,
		numberOfEntries: null
{'fraction_non_alphanumeric': 0.17305513595166164, 'fraction_numerical': 0.0035876132930513596, 'mean_word_length': 3.421118530884808, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '4886925', 'n_tokens_mistral': 4245, 'n_tokens_neox': 3586, 'n_words': 671}