UNPKG

4.21 kBJavaScriptView Raw
1import $ from './jquery';
2import '../../js-vendor/jquery/jquery.tablesorter';
3import globalize from './internal/globalize';
4
5const DEFAULT_SORT_OPTIONS = {
6 sortMultiSortKey: '',
7 headers: {},
8 debug: false,
9 tabIndex: false
10};
11
12/**
13 * @param {jQuery} $table - element to be sortable
14 * @param {Object} [extraOptions] - tablesorter.js options
15 */
16function sortTable($table, extraOptions) {
17 let options = Object.assign({}, DEFAULT_SORT_OPTIONS, extraOptions);
18 $table.find('th').each(function (index, header) {
19 const $header = $(header);
20 options.headers[index] = {};
21 if ($header.hasClass('aui-table-column-unsortable')) {
22 options.headers[index].sorter = false;
23 } else {
24 $header.attr('tabindex', '0');
25 $header.wrapInner("<span class='aui-table-header-content'/>");
26 if ($header.hasClass('aui-table-column-issue-key')) {
27 options.headers[index].sorter = 'issue-key';
28 }
29 }
30 });
31 return $table.tablesorter(options);
32}
33
34let tablessortable = {
35 setup: function () {
36
37 /*
38 This parser is used for issue keys in the format <PROJECT_KEY>-<ISSUE_NUMBER>, where <PROJECT_KEY> is a maximum
39 10 character string with characters(A-Z). Assumes that issue number is no larger than 999,999. e.g. not more
40 than a million issues.
41 This pads the issue key to allow for proper string sorting so that the project key is always 10 characters and the
42 issue number is always 6 digits. e.g. it appends the project key '.' until it is 10 characters long and prepends 0
43 so that the issue number is 6 digits long. e.g. CONF-102 == CONF......000102. This is to allow proper string sorting.
44 */
45 $.tablesorter.addParser({
46 id: 'issue-key',
47 is: function () {
48 return false;
49 },
50
51 format: function (s) {
52 const keyComponents = s.split('-');
53 const projectKey = keyComponents[0];
54 const issueNumber = keyComponents[1];
55
56 const PROJECT_KEY_TEMPLATE = '..........';
57 const ISSUE_NUMBER_TEMPLATE = '000000';
58 let stringRepresentation = (projectKey + PROJECT_KEY_TEMPLATE).slice(0, PROJECT_KEY_TEMPLATE.length);
59 stringRepresentation += (ISSUE_NUMBER_TEMPLATE + issueNumber).slice(-ISSUE_NUMBER_TEMPLATE.length);
60
61 return stringRepresentation;
62 },
63
64 type: 'text'
65 });
66
67 /*
68 Text parser that uses the data-sort-value attribute for sorting if it is set and data-sort-type is not set
69 or set to 'text'.
70 */
71 $.tablesorter.addParser({
72 id: 'textSortAttributeParser',
73 is: function (nodeValue, table, node) {
74 return node.hasAttribute('data-sort-value') && (!node.hasAttribute('data-sort-type') || node.getAttribute('data-sort-type') === 'text');
75 },
76 format: function (nodeValue, table, node) {
77 return node.getAttribute('data-sort-value');
78 },
79 type: 'text'
80 });
81
82 /*
83 Numeric parser that uses the data-sort-value attribute for sorting if it is set and data-sort-type is set
84 to 'numeric'.
85 */
86 $.tablesorter.addParser({
87 id: 'numericSortAttributeParser',
88 is: function (nodeValue, table, node) {
89 return node.getAttribute('data-sort-type') === 'numeric' && node.hasAttribute('data-sort-value');
90 },
91 format: function (nodeValue, table, node) {
92 return node.getAttribute('data-sort-value');
93 },
94 type: 'numeric'
95 });
96
97 $('.aui-table-sortable').each(function () {
98 sortTable($(this));
99 });
100 },
101 /**
102 * @param {jQuery} $table - element to be sortable
103 * @param {Object} [options] - tablesorter.js options
104 */
105 setTableSortable: function ($table, options) {
106 return sortTable($table, options);
107 }
108};
109
110$(tablessortable.setup);
111
112globalize('tablessortable', tablessortable);
113
114export default tablessortable;