<commit_before>'use strict';

define(["github/github", "app/helpers", "angulartics"], function() {

	return ['$scope', '$routeParams', 'Repo', 'github', '$location', '$rootScope', 'FileLoader', 'FigureUploader', 'SnippetLoader', '$analytics', function(scope, routeParams, repo, github, location, $rootScope, fileLoader, figureUploader, snippetLoader, $analytics) {

		scope.createFile = function(relativePath) {
			console.log("Creating file", relativePath);

			var fullPath = scope.dirPath + "/" + relativePath;

			var doCreate = function(initialContent) {
				github.createFile(repo.owner, repo.name, fullPath, initialContent).then(function(f) {
					location.url("/edit/" + scope.branch + "/" + fullPath);
					$rootScope.$apply();
				}).catch(function(e) {
					console.error("Couldn't create file. Perhaps it already exists.", e);
				})				
			}

			if (fullPath.endsWith(".json")) {
				$rootScope.modal.show("Choose Page Type", "What type of page would you like to create?", "", [
					{
						caption: "Question Page",
						value: "isaacQuestionPage"
					},
					{
						caption: "Concept Page",
						value: "isaacConceptPage"
					}
				]).then(function(r) {

					snippetLoader.loadPageTemplate(r.value).then(function(t) {
						t = JSON.stringify(t, null, 2);
						doCreate(t);
					})
				}).catch(function(e) {
					if (e == "cancel") {
						console.log("File creation cancelled.");
					} else {
						console.error("Something went wrong choosing new file type:", e);
					}
				});
			} else {
				doCreate("");
			}
		}
		scope.openFile = function(fullPath) {
			location.url("/edit/" + scope.branch + "/" + fullPath);
		}
		scope.editedFile = function(newContent) {

			if (scope.file.decodedContent == newContent)
				return;

			scope.file.editedContent = newContent;

			scope.fileIsEdited = true;
			scope.$apply();
		}


		scope.saveFile = function() {
			console.log("Saving file", scope.file.path);

			// Do actual saving here.
			return new Promise(function(resolve, reject) {

				if (scope.file == null)
					return resolve();

				if (!scope.fileIsEdited)
					return resolve();

				$analytics.eventTrack("save", {category: "git", label: scope.file.path});

				var msg = $('<input type="text" value="' + "Edited " + scope.file.name + '" />');

				$rootScope.modal.show("Enter commit message", "Enter commit message, or accept default suggestion.", msg, [{caption: "Save", value: "save"}]).then(function() {

					github.commitChange(scope.file, scope.file.editedContent, msg.val()).then(function(f) {

			            // Merge the new git attributes of the file after save. This includes the updated SHA, so that the next save is correctly based on the new commit.
			            for (var attr in f.content) {
			                scope.file[attr] = f.content[attr];
			            }
			            scope.file.decodedContent = scope.file.editedContent;
			            delete scope.file.editedContent;

						scope.fileIsEdited = false;
						scope.$apply();

						return resolve();
					}).catch(function(e) {
						console.error("Could not save file:", e);
						return reject();
					})

				})

			});
		}

		var deleteFile = function() {
		    if (confirm("Do you really want to delete " + scope.file.name + "?")) {
		        console.log("Deleting", scope.file.path);

		        github.deleteFile(repo.owner, repo.name, scope.file.path, scope.file.sha).then(function(f){
		            location.url("/edit/" + scope.branch + "/" + scope.dirPath);
		            $rootScope.$apply();
		        }).catch(function(e) {
		        	console.error("Unable to delete file.", e);<commit_msg>Warn when saving JSON files without title or id.<commit_after>'use strict';

define(["github/github", "app/helpers", "angulartics"], function() {

	return ['$scope', '$routeParams', 'Repo', 'github', '$location', '$rootScope', 'FileLoader', 'FigureUploader', 'SnippetLoader', '$analytics', function(scope, routeParams, repo, github, location, $rootScope, fileLoader, figureUploader, snippetLoader, $analytics) {

		scope.createFile = function(relativePath) {
			console.log("Creating file", relativePath);

			var fullPath = scope.dirPath + "/" + relativePath;

			var doCreate = function(initialContent) {
				github.createFile(repo.owner, repo.name, fullPath, initialContent).then(function(f) {
					location.url("/edit/" + scope.branch + "/" + fullPath);
					$rootScope.$apply();
				}).catch(function(e) {
					console.error("Couldn't create file. Perhaps it already exists.", e);
				})				
			}

			if (fullPath.endsWith(".json")) {
				$rootScope.modal.show("Choose Page Type", "What type of page would you like to create?", "", [
					{
						caption: "Question Page",
						value: "isaacQuestionPage"
					},
					{
						caption: "Concept Page",
						value: "isaacConceptPage"
					}
				]).then(function(r) {

					snippetLoader.loadPageTemplate(r.value).then(function(t) {
						t = JSON.stringify(t, null, 2);
						doCreate(t);
					})
				}).catch(function(e) {
					if (e == "cancel") {
						console.log("File creation cancelled.");
					} else {
						console.error("Something went wrong choosing new file type:", e);
					}
				});
			} else {
				doCreate("");
			}
		}
		scope.openFile = function(fullPath) {
			location.url("/edit/" + scope.branch + "/" + fullPath);
		}
		scope.editedFile = function(newContent) {

			if (scope.file.decodedContent == newContent)
				return;

			scope.file.editedContent = newContent;

			scope.fileIsEdited = true;
			scope.$apply();
		}


		scope.saveFile = function() {
			console.log("Saving file", scope.file.path);

			// Do actual saving here.
			return new Promise(function(resolve, reject) {

				if (scope.file == null)
					return resolve();

				if (!scope.fileIsEdited)
					return resolve();

				var doSave = function() {

					$analytics.eventTrack("save", {category: "git", label: scope.file.path});

					var msg = $('<input type="text" value="' + "Edited " + scope.file.name + '" />');

					$rootScope.modal.show("Enter commit message", "Enter commit message, or accept default suggestion.", msg, [{caption: "Save", value: "save"}]).then(function() {

						github.commitChange(scope.file, scope.file.editedContent, msg.val()).then(function(f) {

				            // Merge the new git attributes of the file after save. This includes the updated SHA, so that the next save is correctly based on the new commit.
				            for (var attr in f.content) {
				                scope.file[attr] = f.content[attr];
				            }
				            scope.file.decodedContent = scope.file.editedContent;
				            delete scope.file.editedContent;

							scope.fileIsEdited = false;
							scope.$apply();

							return resolve();
						}).catch(function(e) {
							console.error("Could not save file:", e);
							return reject();
						})

					});
				};

				var buttons = [];

				buttons.push({
					caption: "Save anyway",
					value: "save"
				});

				var doc = null;
				try {
					doc = JSON.parse(scope.file.editedContent);
				} catch (e) { /* File is not a valid JSON document. Probably not a big deal, it may not even be a JSON file. */ }

				if (doc && (!doc.title || !doc.id || doc.title == "" || doc.id == "")) {
					return $rootScope.modal.show("Missing Metadata", "The content you are trying to save is missing an ID, or a title, or both. All pages should have both.", "", buttons).then(function(r) {
						debugger;
						if (r.value == "save") {
							r.closedPromise.then(doSave);
						} else {
							// Cancel the save operation.
						}
					});						
				} else {
					doSave();
				}
				
			});
		}

		var deleteFile = function() {
		    if (confirm("Do you really want to delete " + scope.file.name + "?")) {
		        console.log("Deleting", scope.file.path);

		        github.deleteFile(repo.owner, repo.name, scope.file.path, scope.file.sha).then(function(f){
		            location.url("/edit/" + scope.branch + "/" + scope.dirPath);
		            $rootScope.$apply();
		        }).catch(function(e) {
		        	console.error("Unable to delete file.", e);
{'fraction_non_alphanumeric': 0.15923960004937662, 'fraction_numerical': 0.0002468831008517467, 'mean_word_length': 2.725057471264368, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2965101', 'n_tokens_mistral': 3090, 'n_tokens_neox': 2392, 'n_words': 591}