<commit_before>// Marker
var MK = MK || {};

MK.app = {
	init: function () {
		l('MK.app.init()');
		MK.events.stickyPane();
	},
	clearSession: function () {
		Session.set("docId", null);
		Session.set("content", '');
		Session.set("currentPage", 1);
	},
	converter: new Showdown.converter(),
	setDoc: function (context) {
		var doc = Documents.findOne({uri : context.params.docUri});
		if (doc) {
			if (Session.get("docId") !== doc._id) {
				Session.set("docId", doc._id);
				Session.set("content", doc.content);
			}
		}
	},
	setAnalytics: function () {
		_gaq.push(['_trackPageview']);
	},
	pageSize: 12,
	hideLoader: function () {
		$('#header .loading').addClass('hidden');
	},
	showLoader: function () {
		$('#header .loading').removeClass('hidden');
	}
};

// Window

window.onresize = function (e) {
	MK.events.resizeCanvas();
};

window.onclick = function (e) {
	$('#search').addClass('hide');
};

// Header

Template.header.canDelete = function () {
	var doc = Documents.findOne({_id: Session.get('docId')});
	return (doc && doc.owner === Meteor.userId()) ? "" : "hide";
};

Template.header.events({
	'click #new-doc': function () {
		Meteor.go(Meteor.newPath());
	},
	'click #edit-doc': function () {
		var doc = Documents.findOne({_id: Session.get('docId')});
		if (doc)
			Meteor.go(Meteor.editPath({docUri: doc.uri}));
	},
	'click #cancel-edit': function () {
		var doc = Documents.findOne({_id: Session.get('docId')});
		if (doc)
			Meteor.go(Meteor.docPath({docUri: doc.uri}));
		else
			Meteor.go(Meteor.rootPath());
	},
	'click #delete-doc': function () {
		Meteor.call("deleteDocument", Session.get('docId'), function (error) {
			if (! error)
				Meteor.go(Meteor.rootPath());
			else
				alert(error.reason);
		});
	},
	'click #save-doc': function () {
		if (! Session.get('docId')) {
			var title = Session.get('content').replace(/^\n*/, '').split('\n').first().replace('#', '');
			var doc = {
				title: title,
				content: Session.get('content'),
				public: true,
				uri: title.replace(/\s/g, '-')
			};
			Meteor.call("createDocument", doc, function (error, docId) {
				if (! error)
					Meteor.go(Meteor.editPath({docUri: doc.uri}));
				else
					alert(error.reason);
			});
		} else {
			Meteor.call("updateDocument", Session.get('docId'), Session.get('content'), function (error) {
				if (error) alert(error.reason);
			});
		}
	},
	'keyup #search-text': function (e) {
		if (e.which === 27) {
			// 27 = escape
			$('#search').addClass('hide');
		} else {
			$('#header .loading').removeClass('hidden');
			Session.set('searchText', e.target.value);<commit_msg>Add loading spinner on save for basic feedback.<commit_after>// Marker
var MK = MK || {};

MK.app = {
	init: function () {
		l('MK.app.init()');
		MK.events.stickyPane();
	},
	clearSession: function () {
		Session.set("docId", null);
		Session.set("content", '');
		Session.set("currentPage", 1);
	},
	converter: new Showdown.converter(),
	setDoc: function (context) {
		var doc = Documents.findOne({uri : context.params.docUri});
		if (doc) {
			if (Session.get("docId") !== doc._id) {
				Session.set("docId", doc._id);
				Session.set("content", doc.content);
			}
		}
	},
	setAnalytics: function () {
		_gaq.push(['_trackPageview']);
	},
	pageSize: 12,
	hideLoader: function () {
		$('#header .loading').addClass('hidden');
	},
	showLoader: function () {
		$('#header .loading').removeClass('hidden');
	}
};

// Window

window.onresize = function (e) {
	MK.events.resizeCanvas();
};

window.onclick = function (e) {
	$('#search').addClass('hide');
};

// Header

Template.header.canDelete = function () {
	var doc = Documents.findOne({_id: Session.get('docId')});
	return (doc && doc.owner === Meteor.userId()) ? "" : "hide";
};

Template.header.events({
	'click #new-doc': function () {
		Meteor.go(Meteor.newPath());
	},
	'click #edit-doc': function () {
		var doc = Documents.findOne({_id: Session.get('docId')});
		if (doc)
			Meteor.go(Meteor.editPath({docUri: doc.uri}));
	},
	'click #cancel-edit': function () {
		var doc = Documents.findOne({_id: Session.get('docId')});
		if (doc)
			Meteor.go(Meteor.docPath({docUri: doc.uri}));
		else
			Meteor.go(Meteor.rootPath());
	},
	'click #delete-doc': function () {
		Meteor.call("deleteDocument", Session.get('docId'), function (error) {
			if (! error)
				Meteor.go(Meteor.rootPath());
			else
				alert(error.reason);
		});
	},
	'click #save-doc': function () {
		MK.app.showLoader();
		if (! Session.get('docId')) {
			var title = Session.get('content').replace(/^\n*/, '').split('\n').first().replace('#', '');
			var doc = {
				title: title,
				content: Session.get('content'),
				public: true,
				uri: title.replace(/\s/g, '-')
			};
			Meteor.call("createDocument", doc, function (error, docId) {
				MK.app.hideLoader();
				if (! error)
					Meteor.go(Meteor.editPath({docUri: doc.uri}));
				else
					alert(error.reason);
			});
		} else {
			Meteor.call("updateDocument", Session.get('docId'), Session.get('content'), function (error) {
				MK.app.hideLoader();
				if (error) alert(error.reason);
			});
		}
	},
	'keyup #search-text': function (e) {
		if (e.which === 27) {
			// 27 = escape
			$('#search').addClass('hide');
		} else {
			$('#header .loading').removeClass('hidden');
			Session.set('searchText', e.target.value);
{'fraction_non_alphanumeric': 0.2235626767200754, 'fraction_numerical': 0.002639019792648445, 'mean_word_length': 3.7122557726465364, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6838837', 'n_tokens_mistral': 2156, 'n_tokens_neox': 1901, 'n_words': 339}