GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 414922...a9bc98 )
by butschster
12:35
created

resources/assets/js/libs/dependent-dropdown.js   D

Complexity

Total Complexity 69
Complexity/F 2.56

Size

Lines of Code 305
Function Count 27

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 3096576
dl 0
loc 305
rs 4.1944
c 0
b 0
f 0
wmc 69
mnd 2
bc 56
fnc 27
bpm 2.074
cpm 2.5555
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
B dependent-dropdown.js ➔ ?!? 0 250 1

How to fix   Complexity   

Complexity

Complex classes like resources/assets/js/libs/dependent-dropdown.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*!
2
 * @copyright © Kartik Visweswaran, Krajee.com, 2013 - 2016
3
 * http://plugins.krajee.com/dependent-dropdown
4
 *
5
 * Author: Kartik Visweswaran
6
 * Copyright: 2014 - 2016, Kartik Visweswaran, Krajee.com
7
 *
8
 * Licensed under the BSD 3-Clause
9
 * https://github.com/kartik-v/dependent-dropdown/blob/master/LICENSE.md
10
 */
11
(function (factory) {
12
    "use strict";
13
    //noinspection JSUnresolvedVariable
14
    if (typeof define === 'function' && define.amd) { // jshint ignore:line
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
15
        // AMD. Register as an anonymous module.
16
        define(['jquery'], factory); // jshint ignore:line
17
    } else { // noinspection JSUnresolvedVariable
18
        if (typeof module === 'object' && module.exports) { // jshint ignore:line
19
            // Node/CommonJS
20
            // noinspection JSUnresolvedVariable
21
            module.exports = factory(require('jquery')); // jshint ignore:line
22
        } else {
23
            // Browser globals
24
            factory(window.jQuery);
25
        }
26
    }
27
}(function ($) {
28
    "use strict";
29
30
    $.fn.depdropLocales = {};
31
32
    var isEmpty, createOption, setParams, DepDrop;
33
34
    isEmpty = function (value, trim) {
35
        return value === null || value === undefined || value.length === 0 || (trim && $.trim(value) === '');
36
    };
37
38
    createOption = function ($el, id, name, sel, opts) {
39
        var settings = {value: id, text: name}, strId = id.toString();
40
        $.extend(true, settings, (opts || {}));
41
        if (sel !== null && sel.length && (strId === sel ||
42
            ($el.attr('multiple') && (sel instanceof Array) && ($.inArray(strId, sel) > -1)))) {
43
            settings.selected = "selected";
44
        }
45
        $("<option/>", settings).appendTo($el);
46
    };
47
48
    setParams = function (props, vals) {
49
        var out = {};
50
        if (props.length === 0) {
51
            return {};
52
        }
53
        $.each(props, function (key, val) {
54
            out[val] = vals[key];
55
        });
56
        return out;
57
    };
58
59
    DepDrop = function (element, options) {
60
        var self = this;
61
        self.$element = $(element);
62
        $.each(options, function (key, value) {
63
            self[key] = value;
64
        });
65
        self.initData();
66
        self.init();
67
    };
68
69
    DepDrop.prototype = {
70
        constructor: DepDrop,
71
        initData: function () {
72
            var self = this, $el = self.$element;
73
            self.initVal = $el.val();
74
            $el.data('url', self.url)
75
                .data('placeholder', self.placeholder)
76
                .data('loading', self.loading)
77
                .data('loadingClass', self.loadingClass)
78
                .data('loadingText', self.loadingText)
79
                .data('emptyMsg', self.emptyMsg)
80
                .data('params', self.params);
81
        },
82
        init: function () {
83
            var self = this, i, depends = self.depends, $el = self.$element, len = depends.length,
84
                chkOptions = $el.find('option').length, initDepends = self.initDepends || self.depends;
85
            if (chkOptions === 0 || $el.find('option[value=""]').length === chkOptions) {
86
                $el.attr('disabled', 'disabled');
87
            }
88
            for (i = 0; i < len; i++) {
89
                self.listen(i, depends, len);
90
            }
91
            if (self.initialize === true) {
92
                for (i = 0; i < initDepends.length; i++) {
93
                    $('#' + initDepends[i]).trigger('depdrop.change');
94
                }
95
            }
96
            $el.trigger('depdrop.init');
97
        },
98
        parseDisabled: function () {
99
            var self = this;
100
            if (self.isDisabled) {
101
                self.$element.attr('disabled', 'disabled');
102
            }
103
        },
104
        listen: function (i, depends, len) {
105
            var self = this;
106
            $('#' + depends[i]).on('depdrop.change change select2:select krajeeselect2:cleared', function (e) {
107
                var $select = $(this);
108
                if (!isEmpty($select.data('select2')) && e.type === 'change') {
109
                    return;
110
                }
111
                self.setDep($select, depends, len);
112
            });
113
            self.parseDisabled();
114
        },
115
        setDep: function ($elCurr, depends, len) {
116
            var self = this, $el, type, value = {};
117
            for (var j = 0; j < len; j++) {
118
                $el = $('#' + depends[j]);
119
                type = $el.attr('type');
120
                value[j] = (type === "checkbox" || type === "radio") ? $el.prop('checked') : $el.val();
121
            }
122
            self.processDep(self.$element, $elCurr.attr('id'), value, depends);
123
        },
124
        processDep: function ($el, vId, vVal, vDep) {
125
            var self = this, selected, optCount = 0, params = {}, settings, i, ajaxData = {}, vUrl = $el.data('url'),
126
                paramsMain = setParams(vDep, vVal), paramsOther = {}, key, val, vDefault = $el.data('placeholder'),
127
                vLoad = $el.data('loading'), vLoadCss = $el.data('loadingClass'), vLoadMsg = $el.data('loadingText'),
128
                vNullMsg = $el.data('emptyMsg'), vPar = $el.data('params');
129
            ajaxData[self.parentParam] = vVal;
130
            if (!isEmpty(vPar)) {
131
                for (i = 0; i < vPar.length; i++) {
132
                    key = vPar[i];
133
                    val = $('#' + vPar[i]).val();
134
                    params[i] = val;
135
                    paramsOther[key] = val;
136
                }
137
                ajaxData[self.otherParam] = params;
138
            }
139
            ajaxData[self.allParam] = $.extend(true, {}, paramsMain, paramsOther);
140
            settings = {
141
                url: vUrl,
142
                type: 'post',
143
                data: ajaxData,
144
                dataType: 'json',
145
                beforeSend: function () {
146
                    $el.trigger('depdrop.beforeChange', [vId, $("#" + vId).val(), self.initVal]);
147
                    $el.find('option[selected]').removeAttr('selected');
148
                    $el.val('').attr('disabled', 'disabled').html('');
149
                    if (vLoad) {
150
                        $el.removeClass(vLoadCss).addClass(vLoadCss).html('<option id="">' + vLoadMsg + '</option>');
151
                    }
152
                },
153
                success: function (data) {
154
                    selected = isEmpty(data.selected) ? (self.initVal === false ? null : self.initVal) : data.selected;
155
                    if (isEmpty(data)) {
156
                        createOption($el, '', vNullMsg, '');
157
                    }
158
                    else {
159
                        $el.html(self.getSelect(data.output, vDefault, selected));
160
                        if ($el.find('optgroup').length > 0) {
161
                            $el.find('option[value=""]').attr('disabled', 'disabled');
162
                        }
163
                        if (data.output) {
164
                            $el.removeAttr('disabled');
165
                        }
166
                    }
167
                    optCount = $el.find('option').length;
168
                    if ($el.find('option[value=""]').length > 0) {
169
                        optCount -= 1;
170
                    }
171
                    $el.trigger('depdrop.change', [vId, $("#" + vId).val(), optCount, self.initVal]);
172
                    self.parseDisabled();
173
                },
174
                error: function () {
175
                    $el.trigger('depdrop.error', [vId, $("#" + vId).val(), self.initVal]);
176
                },
177
                complete: function () {
178
                    if (vLoad) {
179
                        $el.removeClass(vLoadCss);
180
                    }
181
                    $el.trigger('depdrop.afterChange', [vId, $("#" + vId).val(), self.initVal]);
182
                }
183
            };
184
            $.extend(true, settings, self.ajaxSettings);
185
            $.ajax(settings);
186
        },
187
        getSelect: function (data, placeholder, defVal) {
188
            var self = this, $select = $("<select>"), idParam = self.idParam, nameParam = self.nameParam, options;
189
            if (placeholder !== false) {
190
                createOption($select, "", placeholder, defVal);
191
            }
192
            if (isEmpty(data)) {
193
                data = {};
194
            }
195
            $.each(data, function (i, groups) {
196
                if (groups[idParam]) {
197
                    options = groups[self.optionsParam] || {};
198
                    createOption($select, groups[idParam], groups[nameParam], defVal, options);
199
                }
200
                else {
201
                    var $group = $('<optgroup>', {label: i});
202
                    $.each(groups, function (j, option) {
203
                        options = option[self.optionsParam] || {};
204
                        createOption($group, option[idParam], option[nameParam], defVal, options);
205
                    });
206
                    $group.appendTo($select);
207
                }
208
            });
209
            return $select.html();
210
        }
211
    };
212
213
    $.fn.depdrop = function (option) {
214
        var args = Array.apply(null, arguments), retvals = [];
215
        args.shift();
216
        this.each(function () {
217
            var self = $(this), data = self.data('depdrop'), options = typeof option === 'object' && option,
218
                lang = options.language || self.data('language') || 'en', loc = {}, opts = {};
219
220
            if (!data) {
221
                if (lang !== 'en' && !isEmpty($.fn.depdropLocales[lang])) {
222
                    loc = $.fn.depdropLocales[lang];
223
                }
224
                $.extend(true, opts, $.fn.depdrop.defaults, $.fn.depdropLocales.en, loc, options, self.data());
225
                data = new DepDrop(this, opts);
226
                self.data('depdrop', data);
227
            }
228
229
            if (typeof option === 'string') {
230
                retvals.push(data[option].apply(data, args));
231
            }
232
        });
233
        switch (retvals.length) {
234
            case 0:
235
                return this;
236
            case 1:
237
                return retvals[0];
238
            default:
239
                return retvals;
240
        }
241
    };
242
243
    $.fn.depdrop.defaults = {
244
        language: 'en',
245
        url: '',
246
        depends: '',
247
        initDepends: '',
248
        loading: true,
249
        loadingClass: 'kv-loading',
250
        initialize: false,
251
        idParam: 'id',
252
        nameParam: 'name',
253
        optionsParam: 'options',
254
        parentParam: 'depdrop_parents',
255
        otherParam: 'depdrop_params',
256
        allParam: 'depdrop_all_params',
257
        params: {},
258
        isDisabled: false,
259
        ajaxSettings: {}
260
    };
261
262
    $.fn.depdropLocales.en = {
263
        loadingText: 'Loading ...',
264
        placeholder: 'Select ...',
265
        emptyMsg: 'No data found'
266
    };
267
268
    $.fn.depdrop.Constructor = DepDrop;
269
270
    /**
271
     * Convert automatically select with class 'depdrop' into dependent dropdowns.
272
     */
273
    $(function () {
274
        $('select.depdrop').depdrop();
275
    });
276
}));
277
/*!
278
 * Dependent Dropdown Russian Translations
279
 *
280
 * This file must be loaded after 'dependent-dropdown.js'. Patterns in braces '{}', or
281
 * any HTML markup tags in the messages must not be converted or translated.
282
 *
283
 * @see http://github.com/kartik-v/dependent-dropdown
284
 *
285
 * NOTE: this file must be saved in UTF-8 encoding.
286
 */
287
(function ($) {
288
    "use strict";
289
290
    $.fn.depdropLocales['ru'] = {
291
        loadingText: 'загрузка ...',
292
        placeholder: 'Выбрать ...',
293
        emptyMsg: 'Данные не найдены'
294
    };
295
})(window.jQuery);
296
297
/*!
298
 * Dependent Dropdown French Translations
299
 *
300
 * This file must be loaded after 'dependent-dropdown.js'. Patterns in braces '{}', or
301
 * any HTML markup tags in the messages must not be converted or translated.
302
 *
303
 * @see http://github.com/kartik-v/dependent-dropdown
304
 *
305
 * NOTE: this file must be saved in UTF-8 encoding.
306
 */
307
(function ($) {
308
    "use strict";
309
310
    $.fn.depdropLocales['fr'] = {
311
        loadingText: 'Chargement ...',
312
        placeholder: 'Sélectionner ...',
313
        emptyMsg: 'Aucune donnée disponible'
314
    };
315
})(window.jQuery);