Completed
Push — master ( ee422a...74f940 )
by Ismail
51s
created

datatable-materialize.js ➔ factory   B

Complexity

Conditions 2
Paths 128

Size

Total Lines 162

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 162
rs 8.1463
nc 128
nop 2

How to fix   Long Method   

Long Method

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:

1
(function(window, document) {
2
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) {
0 ignored issues
show
Bug introduced by
The variable btnDisplay is changed as part of the for loop for example by "" on line 43. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
76
                            node = $('<li>', {
77
                                    'class': classes.sPageButton + ' ' + btnClass,
0 ignored issues
show
Bug introduced by
The variable btnClass is changed as part of the for loop for example by button + page > 0 ? "": " disabled" on line 55. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable counter is changed as part of the for loop for example by counter++ on line 97. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
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
166
    // Define as an AMD module if possible
167
    /** global: define */
168
    if (typeof define === 'function' && define.amd) {
169
        define(['jquery', 'datatables'], factory);
170
    } else if (typeof exports === 'object') {
171
        // Node/CommonJS
172
        factory(require('jquery'), require('datatables'));
173
    } else if (jQuery) {
174
        // Otherwise simply initialise as normal, stopping multiple evaluation
175
        factory(jQuery, jQuery.fn.dataTable);
176
    }
177
178
})(window, document);