]>
Commit | Line | Data |
---|---|---|
06a3d686 BB |
1 | /* global $:false, _:false */ |
2 | /* jshint browser:true */ | |
3 | ||
4 | /* | |
5 | author: david linse <davidlinse@gmail.com> | |
6 | version: 0.0.1 | |
7 | ||
8 | A very first draft to add the ability to sort | |
9 | the "file-list" by the displayed 'numbers' for: | |
10 | ||
11 | + lint-errors | |
12 | + complexity | |
13 | + lines of code | |
14 | + estimated errors | |
15 | ||
16 | A group of buttons is added to the template above | |
17 | to trigger the update of the file-list. | |
18 | */ | |
19 | ||
20 | $(function sortable_file_list () { | |
21 | ||
22 | "use strict"; | |
23 | ||
24 | var file_list = $('ul.file-list'); | |
25 | ||
26 | var files = file_list.find('li'); | |
27 | ||
28 | // work-horse | |
29 | // @param: key The 'data-<key>' to sort by | |
30 | // @return: descending sorted array of <li> elements | |
31 | // | |
32 | var _sortBy = function (key) { | |
33 | return _.sortBy(files, function (el) { | |
34 | return Number($(el).find('span[data-lint]').attr(key)) * -1; | |
35 | }); | |
36 | }; | |
37 | ||
38 | // sorter | |
39 | ||
40 | var _sortByLintErr = function _sortByLintErr () { | |
41 | return _sortBy('data-lint'); | |
42 | }; | |
43 | ||
44 | var _sortBySLOC = function _sortBySLOC () { | |
45 | return _sortBy('data-sloc'); | |
46 | }; | |
47 | ||
48 | var _sortByBugs = function _sortByBugs () { | |
49 | return _sortBy('data-bugs'); | |
50 | }; | |
51 | ||
52 | var _sortByComplexity = function _sortByComplexity () { | |
53 | return _sortBy('data-complexity'); | |
54 | }; | |
55 | ||
56 | // appends the 'list' of '<li>' elements | |
57 | // to its parent '<ul>'. | |
58 | // @param: a list of '<li>'' elements | |
59 | // | |
60 | var _update_list = function _update_list (list) { | |
61 | file_list.append($(list)); | |
62 | }; | |
63 | ||
64 | // button event-handler | |
65 | ||
66 | var _byComplexity = function () { | |
67 | _update_list(_sortByComplexity()); | |
68 | }; | |
69 | ||
70 | var _byBugs = function () { | |
71 | _update_list(_sortByBugs()); | |
72 | }; | |
73 | ||
74 | var _bySLOC = function () { | |
75 | _update_list(_sortBySLOC()); | |
76 | }; | |
77 | ||
78 | var _byLint = function () { | |
79 | _update_list(_sortByLintErr()); | |
80 | }; | |
81 | ||
82 | // styling | |
83 | ||
84 | var _update_state = function _update_state (target) { | |
85 | ||
86 | var prev = $('button.on'); | |
87 | prev.removeClass('on'); | |
88 | ||
89 | var current = $(target); | |
90 | current.addClass('on'); | |
91 | }; | |
92 | ||
93 | // setup button events | |
94 | ||
95 | $('button#button-complexity').on('click', _byComplexity); | |
96 | $('button#button-bugs').on('click', _byBugs); | |
97 | $('button#button-sloc').on('click', _bySLOC); | |
98 | $('button#button-lint').on('click', _byLint); | |
99 | ||
100 | // styling update for buttons | |
101 | ||
102 | var all = $('button.btn'); | |
103 | all.on('click', function (evt) { | |
104 | _update_state(evt.target); | |
105 | }); | |
106 | }); |