All files / builds/1000i100/quiz-maker/generated/maintainability/assets/scripts plato-file.js

0% Statements 0/176
0% Branches 0/1
0% Functions 0/1
0% Lines 0/176

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177                                                                                                                                                                                                                                                                                                                                                                 
/*global $:false, _:false, Morris:false, CodeMirror:false, __report:false, __history:false */
/*jshint browser:true*/

$(function() {
	'use strict';

  // bootstrap popover
	$('[rel=popover]').popover();

	_.templateSettings = {
		interpolate: /\{\{(.+?)\}\}/g
	};

	function focusFragment() {
		$('.plato-mark').removeClass('focus');
		var markId = window.location.hash.substr(1);
		if (markId) $('.' + markId).addClass('focus');
		return focusFragment;
	}

	window.onhashchange = focusFragment();

	var srcEl = document.getElementById('file-source');

	var options = {
		lineNumbers: true,
		gutters: ['plato-gutter-jshint', 'plato-gutter-complexity'],
		readOnly: 'nocursor'
	};


	var cm = CodeMirror.fromTextArea(srcEl, options);

	var byComplexity = [],
		bySloc = [];

	var popoverTemplate = _.template($('#complexity-popover-template').text());
	var gutterIcon = $('<a><i class="plato-gutter-icon icon-cog"></i></a>');

	var popovers = cm.operation(function() {
		var queuedPopovers = [];
		__report.complexity.functions.forEach(function(fn, i) {
			byComplexity.push({
				label: fn.name,
				value: fn.complexity.cyclomatic
			});
			bySloc.push({
				label: fn.name,
				value: fn.complexity.sloc.physical,
				formatter: function(x) {
					return x + ' lines';
				}
			});

			var name = fn.name === '<anonymous>' ? 'function\\s*\\([^)]*\\)' : fn.name;
			var line = fn.line - 1;
			var className = 'plato-mark-fn-' + i;
			var gutter = {
				gutterId: 'plato-gutter-complexity',
				el: gutterIcon.clone().attr('name', className)[0]
			};
			var popover = {
				type: 'popover',
				title: fn.name === '<anonymous>' ? '&lt;anonymous&gt;' : 'function ' + fn.name + '',
				content: popoverTemplate(fn)
			};

			console.log(fn);

			console.log(line,name, className, gutter, popover);

			queuedPopovers.push(cm.markPopoverText({
				line: line,
				ch: 0
			}, name, className, gutter, popover));
		});
		return queuedPopovers;
	});

	popovers.forEach(function(fn) {
		fn();
	});

	var scrollToLine = function(i) {
		var origScroll = [window.pageXOffset, window.pageYOffset];
		window.location.hash = '#plato-mark-fn-' + i;
		window.scrollTo(origScroll[0], origScroll[1]);
		var line = __report.complexity.functions[i].line;
		var coords = cm.charCoords({
			line: line,
			ch: 0
		});
		$('body,html').animate({
			scrollTop: coords.top - 50
		}, 250);
	};

  // yield to the browser
	setTimeout(function() {
		drawFunctionCharts([{
			element: 'fn-by-complexity',
			data: byComplexity
		}, {
			element: 'fn-by-sloc',
			data: bySloc
		}]);
		drawHistoricalCharts(__history);
	}, 0);

	cm.operation(function() {
		addLintMessages(__report);
	});


	function drawFunctionCharts(charts) {
		charts.forEach(function(chart) {
			Morris.Donut(chart).on('click', scrollToLine);
		});
	}

	function drawHistoricalCharts(history) {
		$('.historical.chart').empty();
		var data = _.map(history, function(record) {
			var date = new Date(record.date);
			return {
				date: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(),
				maintainability: parseFloat(record.maintainability).toFixed(2),
				sloc: record.sloc
			};
		}).slice(-20);
		Morris.Line({
			element: 'chart_historical_sloc',
			data: data,
			xkey: 'date',
			ykeys: ['sloc'],
			labels: ['Lines of Code'],
			parseTime: false
		});
		Morris.Line({
			element: 'chart_historical_maint',
			data: data,
			xkey: 'date',
			ykeys: ['maintainability'],
			labels: ['Maintainability'],
			ymax: 100,
			parseTime: false
		});
	}

	function addLintMessages(report) {
		var lines = {};

		console.log(report);

		report.eslint.messages.forEach(function(message) {
			var text = 'Column: ' + message.column + ' "' + message.message + '"';
			if (_.isArray(message.line)) {
				message.line.forEach(function(line) {
					if (!lines[line]) lines[line] = '';
					lines[line] += '<div class="plato-jshint-message text-' + message.severity + '">' + text + '</div>';
				});
			} else {
				if (!lines[message.line]) lines[message.line] = '';
				lines[message.line] += '<div class="plato-jshint-message text-' + message.severity + '">' + text + '</div>';
			}
		});
		var marker = document.createElement('a');
		marker.innerHTML = '<i class="plato-gutter-icon icon-eye-open"></i>';
		Object.keys(lines).forEach(function(line) {
			var lineWidget = document.createElement('div');
			lineWidget.innerHTML = lines[line];
			cm.setGutterMarker(line - 1, 'plato-gutter-jshint', marker.cloneNode(true));
			cm.addLineWidget(line - 1, lineWidget);
		});
	}
});