development/factory/_common/form/asset/js/form.bundle/form-register-callbacks.js   A
last analyzed

Complexity

Total Complexity 29
Complexity/F 1.93

Size

Lines of Code 250
Function Count 15

Duplication

Duplicated Lines 250
Ratio 100 %

Importance

Changes 0
Metric Value
wmc 29
eloc 122
mnd 14
bc 14
fnc 15
dl 250
loc 250
rs 10
bpm 0.9333
cpm 1.9333
noi 11
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
(function ( $ ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
3
    // Callback containers.
4
    $.fn.aAdminPageFrameworkAddRepeatableFieldCallbacks        = [];
5
    $.fn.aAdminPageFrameworkRepeatFieldCallbacks               = [];    // 3.8.8+
6
    $.fn.aAdminPageFrameworkRemoveRepeatableFieldCallbacks     = [];
7
    $.fn.aAdminPageFrameworkSortedFieldsCallbacks              = [];
8
    $.fn.aAdminPageFrameworkStoppedSortingFieldsCallbacks      = [];
9
    $.fn.aAdminPageFrameworkAddedWidgetCallbacks               = [];
10
    $.fn.aAdminPageFrameworkStoppedSortingSectionsCallbacks    = [];    // 3.8.0+
11
12
    /**
13
     * Gets triggered when the + (add) button of a repeatable field is pressed.
14
     */
15
    $( document ).on( 'admin-page-framework_added_repeatable_field', function( oEvent, sFieldType, sID, iCallType, iSectionIndex, iFieldIndex ){
16
        var _oThisNode = jQuery( oEvent.target );
17
        $.each( $.fn.aAdminPageFrameworkAddRepeatableFieldCallbacks, function( iIndex, aCallback ) {
18
            var _hfCallback  = aCallback[ 0 ];
19
            var _aFieldTypes = aCallback[ 1 ]; // '_nested', 'inline_mixed' are built-in
20
21
            // 2 here is reserved for built-in field types.
22
            if ( 2 < _aFieldTypes.length && -1 === $.inArray( sFieldType, _aFieldTypes ) ) {
23
                return true; // continue
24
            }
25
            if ( 'function' !== typeof _hfCallback ) {
26
                return true; // continue
27
            }
28
            // Show console warnings for a deprecated method.
29
            if ( -1 === $.inArray( sFieldType, [ '_nested', 'inline_mixed' ] ) ) {
30
                console.warn( 'Admin Page Framework (' + sFieldType + ' field type): The `added_repeatable_field` callback argument for the `registerAdminPageFrameworkCallbacks` method is deprecated. Use `repeated_field` instead.' );
31
            }
32
            _hfCallback( _oThisNode, sFieldType, sID, iCallType, iSectionIndex, iFieldIndex );
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
33
        });
34
35
36
    });
37
    /**
38
     * Another way to handle repeatable fields.
39
     *
40
     * Unlike the `admin-page-framework_added_repeatable_field` event, this does not call the callback function
41
     * if it does not match the field type. This means `_nested` and `inline_mixed` will not trigger the callback.
42
     *
43
     * @since       3.8.8
44
     * @param       oEvent              The jQuery event object.
45
     * @param       iCallType           0: repeated field, 1: repeated section.
46
     * @param       oModelContainer     The container that has data of model strings to generate incremented IDs and names.
47
     */
48
    $( document ).on( 'admin-page-framework_repeated_field', function( oEvent, iCallType, oModelContainer ){
49
50
        var _oThis     = jQuery( oEvent.target );
51
        var sFieldType = $( oEvent.target ).data( 'type' );
52
        var _aModel    = {};
53
        // var _aModel    = oModelContainer.data();
54
        _aModel[ 'call_type' ]      = iCallType;
55
        _aModel[ 'field_type' ]     = sFieldType;
56
        _aModel[ 'model_element' ]  = oModelContainer;
57
        _aModel[ 'added_element' ]  = _oThis;
58
        switch( iCallType ) {
59
60
            // Repeatable sections (calling a belonging field)
61
            case 1:
62
                _aModel[ 'incremented_from' ] = Number( oModelContainer.attr( 'data-largest_index' ) );
63
                _aModel[ 'index' ]            = _aModel[ 'incremented_from' ] + 1;
64
                _aModel[ 'id' ]               = oModelContainer.attr( 'data-section_id_model' );
65
                _aModel[ 'name' ]             = oModelContainer.attr( 'data-section_name_model' );
66
                _aModel[ 'flat_name' ]        = oModelContainer.attr( 'data-flat_section_name_model' );
67
                _aModel[ 'address' ]          = oModelContainer.attr( 'data-section_address_model' );
68
                break;
69
70
            // Repeatable fields
71
            default:
0 ignored issues
show
Coding Style Comprehensibility introduced by
The default case is not the last statement in this switch statement. For the sake of readability, you might want to move it to the end of the statement.
Loading history...
72
            case 0:
73
            case 2:
74
                _aModel[ 'incremented_from' ] = Number( oModelContainer.attr( 'data-largest_index' ) - 1 );
75
                _aModel[ 'index' ]            = _aModel[ 'incremented_from' ] + 1;
76
                _aModel[ 'id' ]               = oModelContainer.attr( 'data-field_tag_id_model' );
77
                _aModel[ 'name' ]             = oModelContainer.attr( 'data-field_name_model' );
78
                _aModel[ 'flat_name' ]        = oModelContainer.attr( 'data-field_name_flat_model' );
79
                _aModel[ 'address' ]          = oModelContainer.attr( 'data-field_address_model' );
80
                break;
81
82
        }
83
84
        $.each( $.fn.aAdminPageFrameworkRepeatFieldCallbacks, function( iIndex, aCallback ) {
85
            var _hfCallback  = aCallback[ 0 ];
86
            var _aFieldTypes = aCallback[ 1 ]; // '_nested', 'inline_mixed' are built-in
87
            if ( -1 !== $.inArray( sFieldType, [ '_nested', 'inline_mixed' ] ) ) {
88
                return true;    // continue
89
            }
90
            if ( -1 === $.inArray( sFieldType, _aFieldTypes ) ) {
91
                return true;    // continue
92
            }
93
            if ( 'function' !== typeof _hfCallback ) {
94
                return true;    // continue
95
            }
96
            _hfCallback( _oThis, _aModel );
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
97
        } );
98
99
    } );
100
    /**
101
     * Gets triggered when sorting sections stops.
102
     * @since       3.8.0
103
     */
104
    $( document ).on( 'admin-page-framework_stopped_sorting_sections', function( oEvent ){
105
106
        var _oThisNode = jQuery( oEvent.target );
107
        $.each( $.fn.aAdminPageFrameworkStoppedSortingSectionsCallbacks, function( iIndex, aCallback ) {
108
            var _hfCallback  = aCallback[ 0 ];
109
            var _aFieldTypes = aCallback[ 1 ];
0 ignored issues
show
Unused Code introduced by
The variable _aFieldTypes seems to be never used. Consider removing it.
Loading history...
110
            if ( 'function' !== typeof _hfCallback ) {
111
                return true;    // continue
112
            }
113
            _hfCallback( _oThisNode );
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
114
        });
115
116
    });
117
118
    /**
119
     * Supposed to get triggered when a repeatable field remove button is pressed.
120
     * @remark      Currently not used.
121
     */
122
    /* $( document ).on( 'admin-page-framework_removed_field', function( oEvent, sFieldType, sID, iCallType, iSectionIndex, iFieldIndex ){
123
        var _oThisNode = jQuery( oEvent.target );
124
        $.each( $.fn.aAdminPageFrameworkRemoveRepeatableFieldCallbacks, function( iIndex, aCallback ) {
125
            var _hfCallback  = aCallback[ 0 ];
126
            var _aFieldTypes = aCallback[ 1 ];
127
            if ( 2 < _aFieldTypes.length && -1 === $.inArray( sFieldType, _aFieldTypes ) ) {
128
                return true; // continue
129
            }
130
            if ( 'function' !== typeof _hfCallback ) {
131
                return true;    // continue
132
            }
133
            _hfCallback( _oThisNode, sFieldType, sID, iCallType, iSectionIndex, iFieldIndex );
134
        });
135
    });   */
136
137
    /**
138
     * Gets triggered when a sortable field is dropped and the sort event occurred.
139
     */
140
    $.fn.callBackSortedFields = function( sFieldType, sID, iCallType ) {
141
        var oThisNode = this;
142
        $.fn.aAdminPageFrameworkSortedFieldsCallbacks.forEach( function( aCallback ) {
143
            var _hfCallback  = aCallback[ 0 ];
144
            var _aFieldTypes = aCallback[ 1 ]; // '_nested', 'inline_mixed' are bult-in
145
            if ( 2 < _aFieldTypes.length && -1 === $.inArray( sFieldType, _aFieldTypes ) ) {
146
                return true; // continue
147
            }
148
            if ( 'function' === typeof _hfCallback ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if "function" === typeof _hfCallback is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
149
                _hfCallback( oThisNode, sFieldType, sID, iCallType );
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
150
            }
151
        });
152
    };
153
154
    /**
155
     * Gets triggered when sorting fields stopped.
156
     * @since   3.1.6
157
     */
158
    $.fn.callBackStoppedSortingFields = function( sFieldType, sID, iCallType ) {
159
        var oThisNode = this;
160
        $.fn.aAdminPageFrameworkStoppedSortingFieldsCallbacks.forEach( function( aCallback ) {
161
            var _hfCallback  = aCallback[ 0 ];
162
            var _aFieldTypes = aCallback[ 1 ]; // '_nested', 'inline_mixed' are built-in
163
            if ( 2 < _aFieldTypes.length && -1 === $.inArray( sFieldType, _aFieldTypes ) ) {
164
                return true; // continue
165
            }
166
            if ( 'function' === typeof _hfCallback ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if "function" === typeof _hfCallback is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
167
                _hfCallback( oThisNode, sFieldType, sID, iCallType );
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
168
            }
169
        });
170
    };
171
172
    /**
173
     * Gets triggered when a widget of the framework is saved.
174
     * @since    3.2.0
175
     */
176
    $( document ).on( 'admin-page-framework_saved_widget', function( event, oWidget ){
177
        $.each( $.fn.aAdminPageFrameworkAddedWidgetCallbacks, function( iIndex, aCallback ) {
178
            var _hfCallback  = aCallback[ 0 ];
179
            var _aFieldTypes = aCallback[ 1 ];
0 ignored issues
show
Unused Code introduced by
The variable _aFieldTypes seems to be never used. Consider removing it.
Loading history...
180
            if ( 'function' !== typeof _hfCallback ) {
181
                return true;    // continue
182
            }
183
            _hfCallback( oWidget );
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
184
        });
185
    });
186
187
    /**
188
     * Registers callbacks. This will be called in each field type definition class.
189
     *
190
     * @since       unknown
191
     * @since       3.6.0       Changed the name from `registerAPFCallback()`.
192
     */
193
    $.fn.registerAdminPageFrameworkCallbacks = function( oCallbacks, aFieldTypeSlugs ) {
194
195
        // This is the easiest way to have default options.
196
        var oCallbacks = $.extend(
197
            {
198
                // The user specifies the settings with the following options.
199
                added_repeatable_field      : null, // @deprecated 3.8.8
200
                repeated_field              : null, // 3.8.8+
201
                removed_repeatable_field    : null, // @deprecated 3.6.0
202
                sorted_fields               : null,
203
                stopped_sorting_fields      : null,
204
                saved_widget                : null,
205
                stopped_sorting_sections    : null, // 3.8.0+
206
            },
207
            oCallbacks
208
        );
209
        var aFieldTypeSlugs = 'undefined' === typeof aFieldTypeSlugs
210
            ? []
211
            : aFieldTypeSlugs;
212
        aFieldTypeSlugs.push( '_nested', 'inline_mixed' );    // 3.8.0+
213
214
        // Store the callback functions
215
        $.fn.aAdminPageFrameworkAddRepeatableFieldCallbacks.push(
216
            [ oCallbacks.added_repeatable_field, aFieldTypeSlugs ]
217
        );
218
219
        $.fn.aAdminPageFrameworkRepeatFieldCallbacks.push(  // 3.8.8+
220
            [ oCallbacks.repeated_field, aFieldTypeSlugs ]
221
        );
222
        $.fn.aAdminPageFrameworkRemoveRepeatableFieldCallbacks.push(
223
            [ oCallbacks.removed_repeatable_field, aFieldTypeSlugs ]
224
        );
225
        $.fn.aAdminPageFrameworkSortedFieldsCallbacks.push(
226
            [ oCallbacks.sorted_fields, aFieldTypeSlugs ]
227
        );
228
        $.fn.aAdminPageFrameworkStoppedSortingFieldsCallbacks.push(
229
            [ oCallbacks.stopped_sorting_fields, aFieldTypeSlugs ]
230
        );
231
        $.fn.aAdminPageFrameworkAddedWidgetCallbacks.push(
232
            [ oCallbacks.saved_widget, aFieldTypeSlugs ]
233
        );
234
235
        // 3.8.0
236
        $.fn.aAdminPageFrameworkStoppedSortingSectionsCallbacks.push(
237
            [ oCallbacks.stopped_sorting_sections, aFieldTypeSlugs ]
238
        );
239
240
    };
241
    /**
242
     * An alias of the `registerAdminPageFrameworkCalbacks()` method.
243
     * @remark      Kept for backward compatibility. There are some custom field types which call the old method name.
244
     * @deprecated
245
     */
246
    $.fn.registerAPFCallback = function( oCallbacks, aFieldTypeSlugs ) {
247
        $.fn.registerAdminPageFrameworkCallbacks( oCallbacks, aFieldTypeSlugs );
248
    }
249
250
}( jQuery ));