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

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

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                                                                                                                                                                                                                                                           
/* global $:false, _:false */
/* jshint browser:true */

/*
  author: david linse <davidlinse@gmail.com>
  version: 0.0.1

  A very first draft to add the ability to sort
  the "file-list" by the displayed 'numbers' for:

   + lint-errors
   + complexity
   + lines of code
   + estimated errors

  A group of buttons is added to the template above
  to trigger the update of the file-list.
*/

$(function sortable_file_list() {

	'use strict';

	var file_list = $('ul.file-list');

	var files = file_list.find('li');

  // work-horse
  // @param:  key  The 'data-<key>' to sort by
  // @return: descending sorted array of <li> elements
  //
	var _sortBy = function(key) {
		return _.sortBy(files, function(el) {
			return Number($(el).find('span[data-lint]').attr(key)) * -1;
		});
	};

  // sorter

	var _sortByLintErr = function _sortByLintErr() {
		return _sortBy('data-lint');
	};

	var _sortBySLOC = function _sortBySLOC() {
		return _sortBy('data-sloc');
	};

	var _sortByBugs = function _sortByBugs() {
		return _sortBy('data-bugs');
	};

	var _sortByComplexity = function _sortByComplexity() {
		return _sortBy('data-complexity');
	};

  // appends the 'list' of '<li>' elements
  // to its parent '<ul>'.
  // @param: a list of '<li>'' elements
  //
	var _update_list = function _update_list(list) {
		file_list.append($(list));
	};


	var _update_metrics_order = function _update_metrics_order(metric_name) {

		var reorder = function reorder() {

			var metric = $(this).children().find('label').filter(function() {
				return $(this).text() === metric_name;
			}).parent();

			$(metric).prependTo($(this));
		};

		$('div [class*=\'js-file-chart\']').each(reorder);
	};

  // button event-handler

	var _byComplexity = function() {
		_update_list(_sortByComplexity());
		_update_metrics_order('complexity');
	};

	var _byBugs = function() {
		_update_list(_sortByBugs());
		_update_metrics_order('est errors');
	};

	var _bySLOC = function() {
		_update_list(_sortBySLOC());
		_update_metrics_order('sloc');
	};

	var _byLint = function() {
		_update_list(_sortByLintErr());
		_update_metrics_order('lint errors');
	};

  // styling

	var _update_state = function _update_state(target) {

		var prev = $('button.on');
		prev.removeClass('on');

		var current = $(target);
		current.addClass('on');
	};

  // setup button events

	$('button#button-complexity').on('click', _byComplexity);
	$('button#button-bugs').on('click', _byBugs);
	$('button#button-sloc').on('click', _bySLOC);
	$('button#button-lint').on('click', _byLint);

  // styling update for buttons

	var all = $('button.btn');
	all.on('click', function(evt) {
		_update_state(evt.target);
	});
});