Conditions | 2 |
Paths | 128 |
Total Lines | 162 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | (function(window, document) { |
||
3 | var factory = function($, DataTable) { |
||
4 | "use strict"; |
||
5 | |||
6 | /* Set the defaults for DataTables initialisation */ |
||
7 | $.extend(true, DataTable.defaults, { |
||
8 | dom: "<'hiddensearch'f'>" + |
||
9 | "tr" + |
||
10 | "<'table-footer'lip'>", |
||
11 | renderer: 'material' |
||
12 | }); |
||
13 | |||
14 | /* Default class modification */ |
||
15 | $.extend(DataTable.ext.classes, { |
||
16 | sWrapper: "dataTables_wrapper", |
||
17 | sFilterInput: "form-control input-sm", |
||
18 | sLengthSelect: "form-control input-sm" |
||
19 | }); |
||
20 | |||
21 | /* Bootstrap paging button renderer */ |
||
22 | DataTable.ext.renderer.pageButton.material = function(settings, host, idx, buttons, page, pages) { |
||
23 | var api = new DataTable.Api(settings); |
||
24 | var classes = settings.oClasses; |
||
25 | var lang = settings.oLanguage.oPaginate; |
||
26 | var btnDisplay, btnClass, counter = 0; |
||
27 | |||
28 | var attach = function(container, buttons) { |
||
29 | var i, ien, node, button; |
||
30 | var clickHandler = function(e) { |
||
31 | e.preventDefault(); |
||
32 | if (!$(e.currentTarget).hasClass('disabled')) { |
||
33 | api.page(e.data.action).draw(false); |
||
34 | } |
||
35 | }; |
||
36 | |||
37 | for (i = 0, ien = buttons.length; i < ien; i++) { |
||
38 | button = buttons[i]; |
||
39 | |||
40 | if ($.isArray(button)) { |
||
41 | attach(container, button); |
||
42 | } else { |
||
43 | btnDisplay = ''; |
||
44 | btnClass = ''; |
||
45 | |||
46 | switch (button) { |
||
47 | case 'first': |
||
48 | btnDisplay = lang.sFirst; |
||
49 | btnClass = button + (page > 0 ? |
||
50 | '' : ' disabled'); |
||
51 | break; |
||
52 | |||
53 | case 'previous': |
||
54 | btnDisplay = '<i class="material-icons">chevron_left</i>'; |
||
55 | btnClass = button + (page > 0 ? |
||
56 | '' : ' disabled'); |
||
57 | break; |
||
58 | |||
59 | case 'next': |
||
60 | btnDisplay = '<i class="material-icons">chevron_right</i>'; |
||
61 | btnClass = button + (page < pages - 1 ? |
||
62 | '' : ' disabled'); |
||
63 | break; |
||
64 | |||
65 | case 'last': |
||
66 | btnDisplay = lang.sLast; |
||
67 | btnClass = button + (page < pages - 1 ? |
||
68 | '' : ' disabled'); |
||
69 | break; |
||
70 | |||
71 | default: |
||
72 | break; |
||
73 | } |
||
74 | |||
75 | if (btnDisplay) { |
||
|
|||
76 | node = $('<li>', { |
||
77 | 'class': classes.sPageButton + ' ' + btnClass, |
||
78 | 'id': idx === 0 && typeof button === 'string' ? |
||
79 | settings.sTableId + '_' + button : null |
||
80 | }) |
||
81 | .append($('<a>', { |
||
82 | 'href': '#', |
||
83 | 'aria-controls': settings.sTableId, |
||
84 | 'data-dt-idx': counter, |
||
85 | 'tabindex': settings.iTabIndex |
||
86 | }) |
||
87 | .html(btnDisplay) |
||
88 | ) |
||
89 | .appendTo(container); |
||
90 | |||
91 | settings.oApi._fnBindAction( |
||
92 | node, { |
||
93 | action: button |
||
94 | }, clickHandler |
||
95 | ); |
||
96 | |||
97 | counter++; |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | }; |
||
102 | |||
103 | // IE9 throws an 'unknown error' if document.activeElement is used |
||
104 | // inside an iframe or frame. |
||
105 | var activeEl; |
||
106 | |||
107 | try { |
||
108 | // Because this approach is destroying and recreating the paging |
||
109 | // elements, focus is lost on the select button which is bad for |
||
110 | // accessibility. So we want to restore focus once the draw has |
||
111 | // completed |
||
112 | activeEl = $(document.activeElement).data('dt-idx'); |
||
113 | } catch (e) { |
||
114 | // console.log(e); |
||
115 | } |
||
116 | |||
117 | attach( |
||
118 | $(host).empty().html('<ul class="material-pagination"/>').children('ul'), |
||
119 | buttons |
||
120 | ); |
||
121 | |||
122 | if (activeEl) { |
||
123 | $(host).find('[data-dt-idx=' + activeEl + ']').focus(); |
||
124 | } |
||
125 | }; |
||
126 | |||
127 | /* |
||
128 | * TableTools Bootstrap compatibility |
||
129 | * Required TableTools 2.1+ |
||
130 | */ |
||
131 | if (DataTable.TableTools) { |
||
132 | // Set the classes that TableTools uses to something suitable for Bootstrap |
||
133 | $.extend(true, DataTable.TableTools.classes, { |
||
134 | "container": "DTTT btn-group", |
||
135 | "buttons": { |
||
136 | "normal": "btn btn-default", |
||
137 | "disabled": "disabled" |
||
138 | }, |
||
139 | "collection": { |
||
140 | "container": "DTTT_dropdown dropdown-menu", |
||
141 | "buttons": { |
||
142 | "normal": "", |
||
143 | "disabled": "disabled" |
||
144 | } |
||
145 | }, |
||
146 | "print": { |
||
147 | "info": "DTTT_print_info" |
||
148 | }, |
||
149 | "select": { |
||
150 | "row": "active" |
||
151 | } |
||
152 | }); |
||
153 | |||
154 | // Have the collection use a material compatible drop down |
||
155 | $.extend(true, DataTable.TableTools.DEFAULTS.oTags, { |
||
156 | "collection": { |
||
157 | "container": "ul", |
||
158 | "button": "li", |
||
159 | "liner": "a" |
||
160 | } |
||
161 | }); |
||
162 | } |
||
163 | |||
164 | }; // /factory |
||
165 | |||
178 | })(window, document); |