<commit_before><html>
  <head>
	<script type="text/javascript" src="scripts/jquery-3.0.0.js"></script>
	<script type="text/javascript" src="scripts/handlebars-v4.0.5.js"></script>
	<script type="text/javascript" src="scripts/categorizeus.js"></script>
	<script type="text/javascript" src="scripts/mainui.js"></script>
	<link rel="stylesheet" type="text/css" href="css/categorizeus.css"></script>
	<link rel="stylesheet" type="text/css" href="css/mainui.css"></script>
	<script id="tmplBasicDocument" type="text/x-handlebars-template">
		<div class="basicDocument" id="{{id}}">
			<h1>{{title}}</h1>
			{{#if link}}
				<a href="{{link}}" target="_blank">
					<img src="{{link}}" width="200px" height="200px"/>
				</a>
			{{/if}}
			<div class="postedBy">
				posted by {{postedBy.userName}}
			</div>
			<div class="body">
				{{body}}
			</div>
		</div>
	</script>
	<script id="tmplBasicDocumentEdit" type="text/x-handlebars-template">
		<div class="basicDocumentEdit">
			<input class="inputMsgId" type="hidden" name="messageId" value="{{id}}"/>
			<p>Title <input class="inputMsgTitle" type="text" value="{{title}}"/></p>
			<p>Body <textarea class="inputMsgBody" rows="4" cols="87" type="text">{{body}}</textarea></p>
			<p>Attachment <input class="inputFileAttachment" type="file"/></p>
			<input type="button" class="inputMsgBtn" value="Submit"/>
		</div>
	</script>
	<script id="tmplLogin" type="text/x-handlebars-template">
		<div class="login">
			<p>Username <input class="txtUsername" type="text"/></p>
			<p>Password <input class="txtPassword" type="password"/></p>
			<input type="button" class="btnLogin" value="Login"/>
		</div>
	</script>
		<script type="text/javascript">
			var tmplBasicDocument;
			var tmplBasicDocumentEdit;
			var tmplLogin;
			var tmplRegister;
			var displayEditForm = function(container, sourceMsg){//#TODO don't just replace
				var controls = $(container).html(tmplBasicDocumentEdit(sourceMsg));
				controls.find(".inputMsgBtn").click(dynamicEditSubmit(controls));
			}

			var displayLoginForm = function(container){ //#TODO hey we are seeing a template pattern here, let's generalize it
				var controls = $(container).html(tmplLogin({}));
				controls.find(".btnLogin").click(dynamicLogin(controls));
			}

			var displayRegisterForm = function(container){ //#TODO hey we are seeing a template pattern here, let's generalize it
				var controls = $(container).html(tmplRegister({}));
				controls.find(".btnRegister").click(dynamicRegister(controls));
			}
			var displayMessages = function(err, messages){
				$("#content").empty();
				for(var i=0; i<messages.length;i++){
					console.log(messages[i]);
					var appliedTemplate = $(tmplBasicDocument(messages[i]));
					var newMessage = $("#content").append(appliedTemplate);
					appliedTemplate.bind('click',
					   (function(template, message){ 
						return function(event){
							console.log(message);
							if(event.target.tagName != "IMG"){
								template.toggleClass('selected');
							}

						}
					   })(appliedTemplate, messages[i])
					);
				}
			};
			var dynamicLogin = function(el){
				return function(){
					var username = el.find(".txtUsername").val();
					var password = el.find(".txtPassword").val();
					loginUser(username, password, function(err, user){
						if(err!=null){
							$("#status").html("<h1>"+err+"</h1>");
						}else{
							$("#status").html("<h1>Logged in successfully, welcome!</h1>");
						}
					});
				};
			}

			var dynamicRegister = function(el){
				return function(){
					var username = el.find(".txtUsername").val();
					var password = el.find(".txtPassword").val();
					registerUser(username, password,function(err, user){
						if(err!=null){
							$("#status").html("<h1>"+err+"</h1>");
						}else{
							$("#status").html("<h1>Registered successfully, welcome!</h1>");
						}
					});
				};
			}

			var dynamicEditSubmit = function(el){
	
				return function(){
					console.log("Dynamically bound control OK");
					var title = el.find(".inputMsgTitle").val();
					var body = el.find(".inputMsgBody").val();
					var id = el.find(".inputMsgId").val();
					var file = el.find(".inputFileAttachment");
					var isNew = (!id) || id.length==0;
					console.log(id + " is new? " + isNew + " " + title + " & " + body);
					if(isNew){
						var newMessage = {
							body:body,
							title:title
						};
						if(file.val()!==''){//file[0].files.length?
							console.log("Found an attached file");
							console.log(file[0].files);
							uploadMessage(newMessage, file[0].files, function(err, response){
								if(err!=null){
									$("#status").append("<p>Error: " + err + "</p>");
								}else{
									$("#status").append("<p>Created new document with id " + response + "</p>");
								}
							});
							return;
						}
						createMessage(newMessage, function(err, response){
							if(err!=null){
								$("#status").append("<p>Error: " + err + "</p>");
							}else{
								$("#status").append("<p>Created new document with id " + response + "</p>");
							}
						});
			
					}else{
						$("#status").append("<p>Currently, editing existing docs not supported. Clear and try again.</p>");
					}

				};
			}
			$(document).ready(function(){
				console.log("We're up");
				tmplBasicDocument = Handlebars.compile($("#tmplBasicDocument").html());
				tmplBasicDocumentEdit = Handlebars.compile($("#tmplBasicDocumentEdit").html());//notice the pattern
				tmplLogin = Handlebars.compile($("#tmplLogin").html());
				displayLoginForm(".userInfo");
				fetchCurrentUser(function(err, user){
					if(err!=null){
						console.log("Nobody is logged in");
						console.log(err);
						return; 
					}
					$("#status").html("<h1>Welcome back "+user.userName+"</h1>");
					console.log(user);
				});
				tagSearch([], displayMessages);
				$("#btnPost").click(function(){
					displayEditForm("#editor", {});
				});

				$("#btnSearch").click(function(){
					var tags = $("#txtTagSearch").val();
					var allTags = tags.split(" ");
					var tagArray = [];
					for(var i=0; i<allTags.length;i++){
						if(allTags[i].length>0){
							tagArray.push(allTags[i]);
						}
					}
					tagSearch(tagArray, displayMessages);	
				});

				$("#btnTag").click(function(){

					var tags = $("#txtTagSearch").val();
					var allTags = tags.split(" ");
					var tagArray = [];
					for(var i=0; i<allTags.length;i++){
						if(allTags[i].length>0){
							tagArray.push(allTags[i]);
						}		}

					var whichTagged = [];
					$('input:checkbox.chkTag').each(function () {
						if(this.checked){
							whichTagged.push(this.name);
						}
					});
					console.log(whichTagged);
					if(tagArray.length==0){
						$("#status").html("<h1>Please provide tags when tagging</h1>");
						return;
					}
					if(whichTagged.length==0){
						$("#status").html("<h1>Please select messages when tagging</h1>");
						return;
					}
					tagMessages(tagArray, whichTagged,function(err, message){
						if(err!=null){
							$("#status").html(err);
						}else{
							$("#status").html("Tagged Messages Successfully");	
						}
					});
				});


			});<commit_msg>restore tag functionality with new ui<commit_after><html>
  <head>
	<script type="text/javascript" src="scripts/jquery-3.0.0.js"></script>
	<script type="text/javascript" src="scripts/handlebars-v4.0.5.js"></script>
	<script type="text/javascript" src="scripts/categorizeus.js"></script>
	<script type="text/javascript" src="scripts/mainui.js"></script>
	<link rel="stylesheet" type="text/css" href="css/categorizeus.css"></script>
	<link rel="stylesheet" type="text/css" href="css/mainui.css"></script>
	<script id="tmplBasicDocument" type="text/x-handlebars-template">
		<div class="basicDocument" id="{{id}}">
			<h1>{{title}}</h1>
			{{#if link}}
				<a href="{{link}}" target="_blank">
					<img src="{{link}}" width="200px" height="200px"/>
				</a>
			{{/if}}
			<div class="postedBy">
				posted by {{postedBy.userName}}
			</div>
			<div class="body">
				{{body}}
			</div>
		</div>
	</script>
	<script id="tmplBasicDocumentEdit" type="text/x-handlebars-template">
		<div class="basicDocumentEdit">
			<input class="inputMsgId" type="hidden" name="messageId" value="{{id}}"/>
			<p>Title <input class="inputMsgTitle" type="text" value="{{title}}"/></p>
			<p>Body <textarea class="inputMsgBody" rows="4" cols="87" type="text">{{body}}</textarea></p>
			<p>Attachment <input class="inputFileAttachment" type="file"/></p>
			<input type="button" class="inputMsgBtn" value="Submit"/>
		</div>
	</script>
	<script id="tmplLogin" type="text/x-handlebars-template">
		<div class="login">
			<p>Username <input class="txtUsername" type="text"/></p>
			<p>Password <input class="txtPassword" type="password"/></p>
			<input type="button" class="btnLogin" value="Login"/>
		</div>
	</script>
		<script type="text/javascript">
			var tmplBasicDocument;
			var tmplBasicDocumentEdit;
			var tmplLogin;
			var tmplRegister;
			var displayEditForm = function(container, sourceMsg){//#TODO don't just replace
				var controls = $(container).html(tmplBasicDocumentEdit(sourceMsg));
				controls.find(".inputMsgBtn").click(dynamicEditSubmit(controls));
			}

			var displayLoginForm = function(container){ //#TODO hey we are seeing a template pattern here, let's generalize it
				var controls = $(container).html(tmplLogin({}));
				controls.find(".btnLogin").click(dynamicLogin(controls));
			}

			var displayRegisterForm = function(container){ //#TODO hey we are seeing a template pattern here, let's generalize it
				var controls = $(container).html(tmplRegister({}));
				controls.find(".btnRegister").click(dynamicRegister(controls));
			}
			var displayMessages = function(err, messages){
				$("#content").empty();
				for(var i=0; i<messages.length;i++){
					console.log(messages[i]);
					var appliedTemplate = $(tmplBasicDocument(messages[i]));
					var newMessage = $("#content").append(appliedTemplate);
					appliedTemplate.bind('click',
					   (function(template, message){ 
						return function(event){
							console.log(message);
							if(event.target.tagName != "IMG"){
								template.toggleClass('selected');
							}

						}
					   })(appliedTemplate, messages[i])
					);
				}
			};
			var dynamicLogin = function(el){
				return function(){
					var username = el.find(".txtUsername").val();
					var password = el.find(".txtPassword").val();
					loginUser(username, password, function(err, user){
						if(err!=null){
							$("#status").html("<h1>"+err+"</h1>");
						}else{
							$("#status").html("<h1>Logged in successfully, welcome!</h1>");
						}
					});
				};
			}

			var dynamicRegister = function(el){
				return function(){
					var username = el.find(".txtUsername").val();
					var password = el.find(".txtPassword").val();
					registerUser(username, password,function(err, user){
						if(err!=null){
							$("#status").html("<h1>"+err+"</h1>");
						}else{
							$("#status").html("<h1>Registered successfully, welcome!</h1>");
						}
					});
				};
			}

			var dynamicEditSubmit = function(el){
	
				return function(){
					console.log("Dynamically bound control OK");
					var title = el.find(".inputMsgTitle").val();
					var body = el.find(".inputMsgBody").val();
					var id = el.find(".inputMsgId").val();
					var file = el.find(".inputFileAttachment");
					var isNew = (!id) || id.length==0;
					console.log(id + " is new? " + isNew + " " + title + " & " + body);
					if(isNew){
						var newMessage = {
							body:body,
							title:title
						};
						if(file.val()!==''){//file[0].files.length?
							console.log("Found an attached file");
							console.log(file[0].files);
							uploadMessage(newMessage, file[0].files, function(err, response){
								if(err!=null){
									$("#status").append("<p>Error: " + err + "</p>");
								}else{
									$("#status").append("<p>Created new document with id " + response + "</p>");
								}
							});
							return;
						}
						createMessage(newMessage, function(err, response){
							if(err!=null){
								$("#status").append("<p>Error: " + err + "</p>");
							}else{
								$("#status").append("<p>Created new document with id " + response + "</p>");
							}
						});
			
					}else{
						$("#status").append("<p>Currently, editing existing docs not supported. Clear and try again.</p>");
					}

				};
			}
			$(document).ready(function(){
				console.log("We're up");
				tmplBasicDocument = Handlebars.compile($("#tmplBasicDocument").html());
				tmplBasicDocumentEdit = Handlebars.compile($("#tmplBasicDocumentEdit").html());//notice the pattern
				tmplLogin = Handlebars.compile($("#tmplLogin").html());
				displayLoginForm(".userInfo");
				fetchCurrentUser(function(err, user){
					if(err!=null){
						console.log("Nobody is logged in");
						console.log(err);
						return; 
					}
					$("#status").html("<h1>Welcome back "+user.userName+"</h1>");
					console.log(user);
				});
				tagSearch([], displayMessages);
				$("#btnPost").click(function(){
					displayEditForm("#editor", {});
				});

				$("#btnSearch").click(function(){
					var tags = $("#txtTagSearch").val();
					var allTags = tags.split(" ");
					var tagArray = [];
					for(var i=0; i<allTags.length;i++){
						if(allTags[i].length>0){
							tagArray.push(allTags[i]);
						}
					}
					tagSearch(tagArray, displayMessages);	
				});

				$("#btnTag").click(function(){

					var tags = $("#txtTagSearch").val();
					var allTags = tags.split(" ");
					var tagArray = [];
					for(var i=0; i<allTags.length;i++){
						if(allTags[i].length>0){
							tagArray.push(allTags[i]);
						}		}

					var whichTagged = [];
					$('.basicDocument.selected').each(function () {
						whichTagged.push(this.id);
					});
					
					if(tagArray.length==0){
						$("#status").html("<h1>Please provide tags when tagging</h1>");
						return;
					}
					if(whichTagged.length==0){
						$("#status").html("<h1>Please select messages when tagging</h1>");
						return;
					}
					tagMessages(tagArray, whichTagged,function(err, message){
						if(err!=null){
							$("#status").html(err);
						}else{
							$("#status").html("Tagged Messages Successfully");	
						}
					});
				});


			});
{'fraction_non_alphanumeric': 0.20155636567582727, 'fraction_numerical': 0.005888951205832866, 'mean_word_length': 3.317493946731235, 'pattern_counts': {'":': 0, '<': 167, '<?xml version=': 0, '>': 165, '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': '7439799', 'n_tokens_mistral': 5958, 'n_tokens_neox': 4468, 'n_words': 807}