<commit_before>/*!
 * Annotations Support for the Viewer - v0.3
 *
 * Copyright (c) 2013 <NAME>, http://bradfrostweb.com & <NAME>, http://dmolsen.com
 * Licensed under the MIT license
 *
 */

var annotationsViewer = {
	
	commentsActive: false,
	
	onReady: function() {
		
		$('body').addClass('comments-ready');
		$('#sg-t-annotations').click(function(e) {
			
			e.preventDefault();
			
			// make sure the code view overlay is off
			$('#sg-t-code').removeClass('active');
			codeViewer.codeActive = false;
			var targetOrigin = (window.location.protocol === "file:") ? "*" : window.location.protocol+"//"+window.location.host;
			document.getElementById('sg-viewport').contentWindow.postMessage({ "codeToggle": "off" },targetOrigin);
			codeViewer.slideCode(999);
			
			$('#sg-t-toggle').removeClass('active');
				
			annotationsViewer.toggleComments();
			annotationsViewer.commentContainerInit();
			
		});
	},
	
	toggleComments: function() {
		
		var targetOrigin = (window.location.protocol === "file:") ? "*" : window.location.protocol+"//"+window.location.host;
		
		if (!annotationsViewer.commentsActive) {
			
			annotationsViewer.commentsActive = true;
			document.getElementById('sg-viewport').contentWindow.postMessage({ "commentToggle": "on" },targetOrigin);
			$('#sg-t-annotations').addClass('active');
			
		} else {
			
			annotationsViewer.commentsActive = false;
			document.getElementById('sg-viewport').contentWindow.postMessage({ "commentToggle": "off" },targetOrigin);
			annotationsViewer.slideComment($('#sg-annotation-container').outerHeight());
			$('#sg-t-annotations').removeClass('active');
			
		}
		
	},
	
	commentContainerInit: function() {
		
		if (document.getElementById("sg-annotation-container") === null) {
			$('<div id="sg-annotation-container" class="sg-view-container"></div>').html($("#annotations-template").html()).appendTo('body').css('bottom',-$(document).outerHeight());
			
			setTimeout(function(){ $('#sg-annotation-container').addClass('anim-ready'); },50); //Add animation class once container is positioned out of frame
		}
		
		//Close Annotation View Button
		$('body').delegate('#sg-annotation-close-btn','click',function() {
			annotationsViewer.commentsActive = false;
			$('#sg-t-annotations').removeClass('active');
			annotationsViewer.slideComment($('#sg-annotation-container').outerHeight());
			var targetOrigin = (window.location.protocol === "file:") ? "*" : window.location.protocol+"//"+window.location.host;
			document.getElementById('sg-viewport').contentWindow.postMessage({ "commentToggle": "off" },targetOrigin);
			return false;
		});
	},
	
	slideComment: function(pos) {
		$('#sg-annotation-container').css('bottom',-pos);
	},
	
	updateComments: function(comments) {
		
		var commentsContainer = document.getElementById("sg-comments-container");
		
		if (commentsContainer.innerHTML !== "") {
			commentsContainer.innerHTML = "";
		}
		
		var count = Object.keys(comments).length;
		if (count > 0) {
			
			for (i = 1; i <= count; i++) {
				
				var displayNum = comments[i].number;
				
				var span = document.createElement("span");
				span.id = "annotation-state-" + displayNum;
				span.style.fontSize = "0.8em";
				span.style.color    = "#666";
				if (comments[i].state === false) {
					span.innerHTML  = " hidden";
				}
				
				var h2 = document.createElement("h2");
				h2.innerHTML  = displayNum + ". " + comments[i].title;
				h2.appendChild(span);
				
				var div = document.createElement("div");
				div.innerHTML = comments[i].comment;
				
				var commentDiv = document.createElement("div");
				commentDiv.classList.add("sg-comment-container");
				commentDiv.id = "annotation-" + displayNum;
				commentDiv.appendChild(h2);
				commentDiv.appendChild(div);
				
				commentsContainer.appendChild(commentDiv);
				
			}
			
		} else {
			
			var h2        = document.createElement("h2");
			h2.innerHTML  = "No Annotations";
			
			var div       = document.createElement("div");
			div.innerHTML = "There are no annotations for this pattern.";
			
			var commentDiv = document.createElement("div");
			commentDiv.classList.add("sg-comment-container");
			commentDiv.appendChild(h2);
			commentDiv.appendChild(div);
			
			commentsContainer.appendChild(commentDiv);
			
		}
		
		
		annotationsViewer.slideComment(0);
		
	},
	
	/**
	* toggle the comment pop-up based on a user clicking on the pattern
	* based on the great MDN docs at https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage
	* @param  {Object}      event info
	*/
	receiveIframeMessage: function(event) {
		
		// does the origin sending the message match the current host? if not dev/null the request
		if ((window.location.protocol !== "file:") && (event.origin !== window.location.protocol+"//"+window.location.host)) {
			return;
		}
		
		if (event.data.commentOverlay !== undefined) {
			if (event.data.commentOverlay === "on") {
				annotationsViewer.updateComments(event.data.comments);
			} else {
				annotationsViewer.slideComment($('#sg-annotation-container').outerHeight());
			}
		} else if (event.data.annotationState !== undefined) {
			document.getElementById("annotation-state-"+event.data.displayNumber).innerHTML = (event.data.annotationState == true) ? "" : " hidden";
		} else if (event.data.displaynumber !== undefined) {
			var top = document.getElementById("annotation-"+event.data.displaynumber).offsetTop;
			$('#sg-annotation-container').animate({scrollTop: top - 10}, 600);
		}
		
	}
	
};

$(document).ready(function() {
	annotationsViewer.onReady();
});

window.addEventListener("message", annotationsViewer.receiveIframeMessage, false);

// make sure if a new pattern or view-all is loaded that comments are turned on as appropriate
$('#sg-viewport').load(function() {
	if (annotationsViewer.commentsActive) {
		var targetOrigin = (window.location.protocol === "file:") ? "*" : window.location.protocol+"//"+window.location.host;
		document.getElementById('sg-viewport').contentWindow.postMessage({ "commentToggle": "on" },targetOrigin);
	}
});

// no idea why this has to be outside. there's something funky going on with the JS pattern
$('#sg-view li a').click(function() {
	$(this).parent().parent().removeClass('active');
	$(this).parent().parent().parent().parent().removeClass('active');<commit_msg>added comments and code clean-up<commit_after>/*!
 * Annotations Support for the Viewer - v0.3
 *
 * Copyright (c) 2013 <NAME>, http://bradfrostweb.com & <NAME>, http://dmolsen.com
 * Licensed under the MIT license
 *
 */

var annotationsViewer = {
	
	// set-up default sections
	commentsActive: false,
	targetOrigin: (window.location.protocol === "file:") ? "*" : window.location.protocol+"//"+window.location.host,
	
	/**
	* add the onclick handler to the annotations link in the main nav
	*/
	onReady: function() {
		
		// not sure this is used anymore...
		$('body').addClass('comments-ready');
		$('#sg-t-annotations').click(function(e) {
			
			e.preventDefault();
			
			// make sure the code view overlay is off before showing the annotations view
			$('#sg-t-code').removeClass('active');
			codeViewer.codeActive = false;
			document.getElementById('sg-viewport').contentWindow.postMessage({ "codeToggle": "off" },annotationsViewer.targetOrigin);
			codeViewer.slideCode(999);
			
			// remove the class from the "eye" nav item
			$('#sg-t-toggle').removeClass('active');
			
			// turn the annotations section on and off
			annotationsViewer.toggleComments();
			
		});
		
		// initialize the annotations viewer
		annotationsViewer.commentContainerInit();
		
	},
	
	/**
	* decide on if the annotations panel should be open or closed
	*/
	toggleComments: function() {
		
		if (!annotationsViewer.commentsActive) {
			
			annotationsViewer.commentsActive = true;
			document.getElementById('sg-viewport').contentWindow.postMessage({ "commentToggle": "on" },annotationsViewer.targetOrigin);
			$('#sg-t-annotations').addClass('active');
			
		} else {
			
			annotationsViewer.commentsActive = false;
			document.getElementById('sg-viewport').contentWindow.postMessage({ "commentToggle": "off" },annotationsViewer.targetOrigin);
			annotationsViewer.slideComment($('#sg-annotation-container').outerHeight());
			$('#sg-t-annotations').removeClass('active');
			
		}
		
	},
	
	/**
	* add the basic mark-up and events for the annotations container
	*/
	commentContainerInit: function() {
		
		// the bulk of this template is in core/templates/index.mustache
		if (document.getElementById("sg-annotation-container") === null) {
			$('<div id="sg-annotation-container" class="sg-view-container"></div>').html($("#annotations-template").html()).appendTo('body').css('bottom',-$(document).outerHeight());
			setTimeout(function(){ $('#sg-annotation-container').addClass('anim-ready'); },50); //Add animation class once container is positioned out of frame
		}
		
		// make sure the close button handles the click
		$('body').delegate('#sg-annotation-close-btn','click',function() {
			annotationsViewer.commentsActive = false;
			$('#sg-t-annotations').removeClass('active');
			annotationsViewer.slideComment($('#sg-annotation-container').outerHeight());
			document.getElementById('sg-viewport').contentWindow.postMessage({ "commentToggle": "off" },annotationsViewer.targetOrigin);
			return false;
		});
		
	},
	
	/**
	* slides the panel
	*/
	slideComment: function(pos) {
		$('#sg-annotation-container').css('bottom',-pos);
	},
	
	/**
	* when turning on or switching between patterns with annotations view on make sure we get
	* the annotations from from the pattern via post message
	*/
	updateComments: function(comments) {
		
		var commentsContainer = document.getElementById("sg-comments-container");
		
		// clear out the comments container
		if (commentsContainer.innerHTML !== "") {
			commentsContainer.innerHTML = "";
		}
		
		// see how many comments this pattern might have. if more than zero write them out. if not alert the user to the fact their aren't any
		var count = Object.keys(comments).length;
		if (count > 0) {
			
			for (i = 1; i <= count; i++) {
				
				var displayNum = comments[i].number;
				
				var span = document.createElement("span");
				span.id = "annotation-state-" + displayNum;
				span.style.fontSize = "0.8em";
				span.style.color    = "#666";
				if (comments[i].state === false) {
					span.innerHTML  = " hidden";
				}
				
				var h2 = document.createElement("h2");
				h2.innerHTML  = displayNum + ". " + comments[i].title;
				h2.appendChild(span);
				
				var div = document.createElement("div");
				div.innerHTML = comments[i].comment;
				
				var commentDiv = document.createElement("div");
				commentDiv.classList.add("sg-comment-container");
				commentDiv.id = "annotation-" + displayNum;
				commentDiv.appendChild(h2);
				commentDiv.appendChild(div);
				
				commentsContainer.appendChild(commentDiv);
				
			}
			
		} else {
			
			var h2        = document.createElement("h2");
			h2.innerHTML  = "No Annotations";
			
			var div       = document.createElement("div");
			div.innerHTML = "There are no annotations for this pattern.";
			
			var commentDiv = document.createElement("div");
			commentDiv.classList.add("sg-comment-container");
			commentDiv.appendChild(h2);
			commentDiv.appendChild(div);
			
			commentsContainer.appendChild(commentDiv);
			
		}
		
		// slide the comment section into view
		annotationsViewer.slideComment(0);
		
	},
	
	/**
	* toggle the comment pop-up based on a user clicking on the pattern
	* based on the great MDN docs at https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage
	* @param  {Object}      event info
	*/
	receiveIframeMessage: function(event) {
		
		// does the origin sending the message match the current host? if not dev/null the request
		if ((window.location.protocol !== "file:") && (event.origin !== window.location.protocol+"//"+window.location.host)) {
			return;
		}
		
		if (event.data.commentOverlay !== undefined) {
			if (event.data.commentOverlay === "on") {
				annotationsViewer.updateComments(event.data.comments);
			} else {
				annotationsViewer.slideComment($('#sg-annotation-container').outerHeight());
			}
		} else if (event.data.annotationState !== undefined) {
			document.getElementById("annotation-state-"+event.data.displayNumber).innerHTML = (event.data.annotationState == true) ? "" : " hidden";
		} else if (event.data.displaynumber !== undefined) {
			var top = document.getElementById("annotation-"+event.data.displaynumber).offsetTop;
			$('#sg-annotation-container').animate({scrollTop: top - 10}, 600);
		}
		
	}
	
};

$(document).ready(function() { annotationsViewer.onReady(); });
window.addEventListener("message", annotationsViewer.receiveIframeMessage, false);

// make sure if a new pattern or view-all is loaded that comments are turned on as appropriate
$('#sg-viewport').load(function() {
	if (annotationsViewer.commentsActive) {
		var targetOrigin = (window.location.protocol === "file:") ? "*" : window.location.protocol+"//"+window.location.host;
		document.getElementById('sg-viewport').contentWindow.postMessage({ "commentToggle": "on" },targetOrigin);
	}
});

// no idea why this has to be outside. there's something funky going on with the JS pattern
$('#sg-view li a').click(function() {
	$(this).parent().parent().removeClass('active');
	$(this).parent().parent().parent().parent().removeClass('active');
{'fraction_non_alphanumeric': 0.1611243662391888, 'fraction_numerical': 0.004920966298836862, 'mean_word_length': 3.9769944341372914, 'pattern_counts': {'":': 10, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, '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': '1072783', 'n_tokens_mistral': 4445, 'n_tokens_neox': 3762, 'n_words': 810}