Passed
Branch master (53d0e8)
by Chris
08:37
created

vendor/uix/assets/js/uix-core.js   F

Complexity

Total Complexity 126
Complexity/F 3

Size

Lines of Code 597
Function Count 42

Duplication

Duplicated Lines 597
Ratio 100 %

Importance

Changes 0
Metric Value
wmc 126
eloc 377
mnd 84
bc 84
fnc 42
dl 597
loc 597
rs 2
bpm 2
cpm 3
noi 41
c 0
b 0
f 0

How to fix   Duplicated Code    Complexity   

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:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like vendor/uix/assets/js/uix-core.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
 This is a prototype of conduit. It still needs a full refactoring.
3
 This is simply to get the concepts into working order. So ye, I need to work on this still.
4
 */
5 View Code Duplication
var conduitApp = {},
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
6
    coduitTemplates = {},
7
    conduitRegisterApps,
8
    conduitModal,
9
    conduitModalSave,
0 ignored issues
show
Unused Code introduced by
The variable conduitModalSave seems to be never used. Consider removing it.
Loading history...
10
    conduitGenID,
11
    conduitSaveObject,
12
    conduitGetData;
13
14
!( jQuery( function($){
15
16
17
    var currentAjaxProcess = null;
18
19
    conduitException = function( message ){
0 ignored issues
show
Bug introduced by
The variable conduitException seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.conduitException.
Loading history...
20
        this.message = message;
21
        this.name = "ConduitException";
22
    }
23
24
    $.fn.conduitTrigger = function( obj ){
25
        var defaults = {
26
            method	:	'GET',
27
            url		:	ajaxurl
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
28
        };
29
30
        $.extend(true, defaults, obj);
31
32
        $.ajax( defaults ).success( function(){
33
            //console.log( arguments );
34
        } );
35
36
37
        return this;
38
    }
39
40
    $.fn.getObject = function( forex ){
41
        var element = $(this);
42
43
        var fields   = element.find('[name]'),
44
            obj         = {},
45
            arraynames   = {};
46
        for( var v = 0; v < fields.length; v++){
47
            var field     = $( fields[v] ),
48
                name    = field.prop('name').replace(/\]/gi,'').split('['),
49
                value     = field.val(),
50
                lineconf  = {};
51
52
            if( forex ){
53
                if( name.indexOf('_id') >= 0 || name.indexOf('_node_point') >= 0 ){
54
                    continue;
55
                }
56
            }
57
58
            if( field.is(':radio') || field.is(':checkbox') ){
59
                if( !field.is(':checked') ){
60
                    continue;
61
                }
62
            }
63
            if( field.prop('required') && ! field.val().length ){
64
                field.focus();
65
                throw new conduitException('requiredfield');
0 ignored issues
show
Coding Style Best Practice introduced by
By convention, constructors like conduitException should be capitalized.
Loading history...
66
            }
67
            for(var i = name.length-1; i >= 0; i--){
68
                var nestname = name[i];
69
                if( typeof nestname === 'undefined' ){
70
                    nestname = '';
71
                }
72
                if(nestname.length === 0){
73
                    lineconf = [];
74
                    if( typeof arraynames[name[i-1]] === 'undefined'){
75
                        arraynames[name[i-1]] = 0;
76
                    }else{
77
                        arraynames[name[i-1]] += 1;
78
                    }
79
                    nestname = arraynames[name[i-1]];
80
                }
81
                if(i === name.length-1){
82
                    if( value ){
83
                        if( value === 'true' ){
84
                            value = true;
85
                        }else if( value === 'false' ){
86
                            value = false;
87
                        }else if( !isNaN( parseFloat( value ) ) && parseFloat( value ).toString() === value ){
88
                            value = parseFloat( value );
89
                        }else if( typeof value === 'string' && ( value.substr(0,1) === '{' || value.substr(0,1) === '[' ) ){
90
                            try {
91
                                value = JSON.parse( value );
92
93
                            } catch (e) {
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
94
95
                            }
96
                        }
97
                    }
98
                    lineconf[nestname] = value;
99
                }else{
100
                    var newobj = lineconf;
101
                    lineconf = {};
102
                    lineconf[nestname] = newobj;
103
                }
104
            }
105
            $.extend(true, obj, lineconf);
106
        };
107
108
        return obj;
109
    }
110
    conduitGeneralBaldrick = function(){
0 ignored issues
show
Bug introduced by
The variable conduitGeneralBaldrick seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.conduitGeneralBaldrick.
Loading history...
111
        // initialise general triggers
112
        $('.wp-trigger').conduitTrigger({
113
            method      : 'POST',
114
            before		: function( el ){
115
                var trigger = $( el ),
116
                    app = trigger.closest('[data-app]');
117
                if( app.length ){
118
                    trigger.data('data', JSON.stringify( conduitBuildData( app.data('app') ) ) );
119
                }else{
120
                    if( trigger.data('data') ){
121
                        trigger.data('data', JSON.stringify( conduitBuildData( trigger.data('data') ) ) );
122
                    }
123
                }
124
            }
125
        });
126
    }
127
    conduitSaveObject = function( app ){
128
        var obj;
129
        if( true === app ){
130
            obj = conduitPrepObject();
131
        }else{
132
            obj = conduitPrepObject( app );
133
        }
134
        var data = {
135
            action		:	uix.slug + "_save_config",
0 ignored issues
show
Bug introduced by
The variable uix seems to be never declared. If this is a global, consider adding a /** global: uix */ 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...
136
            uix_setup	:	$('#uix_setup').val(),
137
            page_slug	:	uix.page_slug,
138
            config		:	JSON.stringify( obj ),
139
            autosave	:	true
140
        };
141
        if( uix.save_params ){
142
            data.params = uix.save_params;
143
        }
144
145
        $( window ).trigger('uix.saving');
146
        if( currentAjaxProcess ){
147
            currentAjaxProcess.abort();
148
        }
149
        currentAjaxProcess = $.post( ajaxurl, data, function(response) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
Unused Code introduced by
The parameter response is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
150
            $( window ).trigger('uix.saved');
151
            currentAjaxProcess = null;
152
        });
153
    }
154
    conduitPrepObject = function(){
0 ignored issues
show
Bug introduced by
The variable conduitPrepObject seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.conduitPrepObject.
Loading history...
155
        var obj = {};
156
        for( var app in conduitApp ){
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
157
            if( conduitApp[ app ].app ){
158
                if( conduitApp[ app ].app.is(':visible') ){
159
                    // capture current changes
160
                    obj[ app ] = conduitBuildData( app );
161
                }else{
162
                    // changes should have been captured already
163
                    obj[ app ] = conduitApp[ app ].data;
164
                }
165
            }
166
            if( obj[ app ]._tab ){
167
                delete obj[ app ]._tab;
168
            }
169
        }
170
        return obj;
171
    }
172
173
    conduitModalFooter = function( opts ){
0 ignored issues
show
Bug introduced by
The variable conduitModalFooter seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.conduitModalFooter.
Loading history...
174
        var buttons = opts.buttons ? opts.buttons.split(' ') : ["save"],
175
            points 		= opts.modal.split('.'),
176
            app 		= opts.app ? opts.app : opts.trigger.closest('[data-app]').data('app'),
177
            data 		= { "__node_path" : points.join('.') },
178
            template_str = '',
179
            template;
180
181
        data.__app = app;
182
        if( opts.trigger.data('before') ){
183
            data.__before	= opts.trigger.data('before');
184
        }
185
        if( opts.trigger.data('callback') ){
186
            data.__callback	= opts.trigger.data('callback');
187
        }
188
189
        for( var i = 0; i < buttons.length; i ++){
190
            template_str += $( '#__partial_' + buttons[ i ] ).length ? $( '#__partial_' + buttons[ i ] ).html() : '';
191
        }
192
193
        template = Handlebars.compile( template_str, { data : true } );
0 ignored issues
show
Bug introduced by
The variable Handlebars seems to be never declared. If this is a global, consider adding a /** global: Handlebars */ 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...
194
195
        return template( data );
196
    }
197
198
    conduitModal = function( opts, modal ){
199
        var points 		= opts.modal.split('.'),
200
            app 		= opts.app ? opts.app : opts.trigger.closest('[data-app]').data('app'),
201
            hasDefault	= opts.default ? opts.default : null,
202
            template 	= Handlebars.compile( "<div data-app=\"" + opts.template + "\">{{> " + opts.template + "}}</div>", { data : true } );
0 ignored issues
show
Bug introduced by
The variable Handlebars seems to be never declared. If this is a global, consider adding a /** global: Handlebars */ 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...
203
        data 		= {};
0 ignored issues
show
Bug introduced by
The variable data seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.data.
Loading history...
204
205
        // fetch latest data object
206
        conduitBuildData( app );
207
208
        if( conduitApp[ app ] ){
209
            var tmp = conduitApp[ app ].data;
210
            if( hasDefault !== null ){
211
                data = hasDefault;
212
            }else{
213
                for( var i = 0; i < points.length; i++ ){
214
                    if( tmp[ points[ i ] ] ){
215
                        tmp = tmp[ points[ i ] ];
216
                    }else{
217
                        tmp = {};
218
                    }
219
                }
220
                data = tmp;
221
            }
222
        }
223
224
        data.__node_path = opts.points;
225
        data.__app 		 = app;
226
227
        conduitApp[ opts.template ] = {
228
            app		:	modal.content,
229
            data	:	data
230
        };
231
        coduitTemplates[ opts.template ] = template;
232
233
        return template( data );
234
235
    }
236
237
    conduitGetData = function( tr ){
238
239
        var id = tr.trigger.data('app'),
240
            data = {};
241
242
        if( conduitApp[ id ] && conduitApp[ id ].data ){
243
            return conduitApp[ id ].data;
244
        }
245
        return data;
246
    }
247
248
    conduitBuildData = function( app ){
0 ignored issues
show
Bug introduced by
The variable conduitBuildData seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.conduitBuildData.
Loading history...
249
        if( conduitApp[ app ] && conduitApp[ app ].app ){
250
            try{
251
                conduitApp[ app ].data = conduitApp[ app ].app.getObject();
252
            }catch (e){
253
                return false;
254
            }
255
256
        }
257
        if( conduitApp[ app ].data._tab ){
258
            delete conduitApp[ app ].data._tab;
259
        }
260
        return conduitApp[ app ].data;
261
    }
262
263
264
    conduitSyncData = function( app ){
0 ignored issues
show
Bug introduced by
The variable conduitSyncData seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.conduitSyncData.
Loading history...
265
        conduitBuildData( app );
266
        //conduitBuildUI( app );
267
    }
268
269
    conduitRegisterApps = function(){
270
271
        var apps = $('[data-app]').not('._bound_app');
272
        if( ! apps.length ){return;}
273
274
        apps.each( function(){
275
276
            var appWrapper = $( this ),
277
                app = appWrapper.data('app');
278
279
            conduitApp[ app ] = {
280
                app : appWrapper,
281
                data : ( uix.config[ app ] ? uix.config[ app ] : {} )
0 ignored issues
show
Bug introduced by
The variable uix seems to be never declared. If this is a global, consider adding a /** global: uix */ 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...
282
            };
283
284
            appWrapper.addClass('_bound_app');
285
            conduitBuildUI( app );
286
        })
287
288
        if( uix.tabs ){
0 ignored issues
show
Bug introduced by
The variable uix seems to be never declared. If this is a global, consider adding a /** global: uix */ 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...
289
            for( var tab in uix.tabs ){
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
290
                if( uix.tabs[ tab ].default ){
291
                    $('[data-tab="' + tab + '"]').trigger('click');
292
                    break;
293
                }
294
            }
295
        }
296
    }
297
298
    conduitBuildUI = function( app ){
0 ignored issues
show
Bug introduced by
The variable conduitBuildUI seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.conduitBuildUI.
Loading history...
299
        if( conduitApp[ app ] ){
300
            var data = conduitApp[ app ].data;
301
            data._tab = {};
302
            for( var sub_app in conduitApp ){
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
303
                if( sub_app === app ){ continue; }
304
                data._tab[ sub_app ] = conduitApp[ sub_app ].data;
305
            }
306
            conduitApp[ app ].app.html( coduitTemplates[ app ]( data ) );
307
        }
308
309
        // sortables
310
        if( $('.uix-sortable').length ){
311
            $('.uix-sortable').each( function(){
312
                var sort = $(this),
313
                    options = {
314
                        forcePlaceholderSize : true,
315
                        placeholder: "uix-sortable-placeholder"
316
                    };
317
318
                options = $.extend({}, options, sort.data() );
319
                $(this).sortable( options );
320
            });
321
        }
322
323
        $(window).trigger('uix.init');
324
        $(window).trigger('modal.init');
325
    }
326
327
    conduitSetNode = function( node, app, data ){
0 ignored issues
show
Bug introduced by
The variable conduitSetNode seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.conduitSetNode.
Loading history...
328
        var nodes = node.split('.');
329
330
        var node_string = '{ "' + nodes.join( '": { "') + '" : ' + JSON.stringify( data );
331
        for( var cls = 0; cls < nodes.length; cls++){
332
            node_string += '}';
333
        }
334
        var new_nodes = JSON.parse( node_string );
335
        $.extend( true, conduitApp[ app ].data, new_nodes );
336
        conduitBuildUI( app );
337
    }
338
    conduitGenID = function(){
339
        var d = new Date().getTime();
340
        var id = 'ndxxxxxxxx'.replace(/[xy]/g, function(c) {
341
            var r = (d + Math.random()*16)%16 | 0;
342
            d = Math.floor(d/16);
343
            return (c=='x' ? r : (r&0x3|0x8)).toString(16);
344
        });
345
        return id;
346
    }
347
    conduitAddNode = function( node, app, data ){
0 ignored issues
show
Bug introduced by
The variable conduitAddNode seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.conduitAddNode.
Loading history...
348
349
        var id = conduitGenID(),
350
            newnode = { "_id" : id },
0 ignored issues
show
Unused Code introduced by
The variable newnode seems to be never used. Consider removing it.
Loading history...
351
            nodes = node.data ? node.data('addNode').split('.') : node.split('.'),
352
            node_default = data ? data : node.data('nodeDefault'),
353
            node_point_record = nodes.join('.') + '.' + id,
354
            node_defaults = JSON.parse( '{ "_id" : "' + id + '", "_node_point" : "' + node_point_record + '" }' )
355
        node_point_wrappers = $('[data-node-point="' + nodes.join('.') + '"]');
0 ignored issues
show
Bug introduced by
The variable node_point_wrappers seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.node_point_wrappers.
Loading history...
356
357
        if( node_default && typeof node_default === 'object' ){
358
            $.extend( true, node_defaults, node_default );
359
        }
360
        var node_string = '{ "' + nodes.join( '": { "') + '" : { "' + id + '" : ' + JSON.stringify( node_defaults );
361
        for( var cls = 0; cls <= nodes.length; cls++){
362
            node_string += '}';
363
        }
364
        var new_nodes = JSON.parse( node_string );
365
366
        conduitBuildData( app );
367
368
        $.extend( true, conduitApp[ app ].data, new_nodes );
369
370
        if( node_point_wrappers.length && node_point_wrappers.data('template') ){
371
            node_point_wrappers.each( function(){
372
                var wrapper = $(this),
373
                    template = wrapper.data('template');
374
                if( template && coduitTemplates[ '__partial_' + template ] ){
375
                    wrapper.append( coduitTemplates[ '__partial_' + template ]( node_defaults ) );
376
                }
377
            });
378
379
        }else{
380
            // rebuild all
381
            conduitBuildUI( app );
382
        }
383
    };
384
385
386
    // bind slugs
387
    $(document).on('keyup change', '[data-format="slug"]', function(e){
388
389
        var input = $(this);
390
391
        if( input.data('master') && input.prop('required') && this.value.length <= 0 && e.type === "change" ){
392
            this.value = $(input.data('master')).val().replace(/[^a-z0-9]/gi, '_').toLowerCase();
393
            if( this.value.length ){
394
                input.trigger('change');
395
            }
396
            return;
397
        }
398
399
        this.value = this.value.replace(/[^a-z0-9]/gi, '_').toLowerCase();
400
    });
401
402
    // bind label update
403
    $(document).on('keyup change', '[data-sync]', function(){
404
        var input = $(this),
405
            syncs = $(input.data('sync'));
406
407
        syncs.each(function(){
408
            var sync = $(this);
409
410
            if( sync.is('input') ){
411
                sync.val( input.val() ).trigger('change');
412
            }else{
413
                sync.text(input.val());
414
            }
415
        });
416
    });
417
418
    // add node
419
    $(document).on('click', '[data-add-node]', function(e){
420
        var click = $( this ),
421
            app = click.closest('[data-app]').data('app');
422
        if( app && typeof conduitApp[ app ] === 'object' ){
423
            e.preventDefault();
424
            conduitAddNode( click, app );
425
        }
426
    });
427
    // row remover global neeto
428
    $(document).on('click', '[data-remove-element]', function(e){
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
429
        var click = $(this),
430
            app = click.closest('[data-app]').data('app'),
431
            elements = $(click.data('removeElement'));
432
        if( click.data('confirm') ){
433
            if( !confirm(click.data('confirm')) ){
0 ignored issues
show
Debugging Code Best Practice introduced by
The confirm UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
434
                return;
435
            }
436
        }
437
        elements.remove();
438
        conduitSyncData( app );
439
    });
440
441
    $(document).on('click', '[data-save-object]', function(e){
442
        e.preventDefault();
443
        var clicked = $( this ),
444
            app = $( this ).data('saveObject'),
445
            spinner = $('.uix-save-spinner'),
446
            confirm = $('.save-confirm'),
447
            sub_nav = $('.uix-sub-nav'),
0 ignored issues
show
Unused Code introduced by
The variable sub_nav seems to be never used. Consider removing it.
Loading history...
448
            obj;
449
450
        //$('.uix-notice .notice-dismiss').trigger('click');
451
452
        if( true === app ){
453
            obj = conduitPrepObject();
454
        }else{
455
            obj = conduitPrepObject( app );
0 ignored issues
show
Bug introduced by
The call to conduitPrepObject seems to have too many arguments starting with app.
Loading history...
456
        }
457
458
        clicked.addClass('saving');
459
        confirm.hide();
460
        spinner.css({ visibility: "visible", opacity:1, display : "inline-block"});
461
        var data = {
462
            action		:	uix.slug + "_save_config",
0 ignored issues
show
Bug introduced by
The variable uix seems to be never declared. If this is a global, consider adding a /** global: uix */ 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...
463
            uix_setup	:	$('#uix_setup').val(),
464
            page_slug	:	uix.page_slug,
465
            config		:	JSON.stringify( obj ),
466
        };
467
        if( uix.save_params ){
468
            data.params = uix.save_params;
469
        }
470
        $.post( ajaxurl, data, function(response) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ 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...
Unused Code introduced by
The parameter response is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
471
472
            spinner.css({ visibility: '', opacity:0, display: 'none'});
473
            confirm.fadeIn();
474
            setTimeout( function(){ confirm.fadeOut( 800 );}, 2000 );
475
            clicked.removeClass('saving');
476
            //var notice = $( coduitTemplates.__notice( response ) );
477
            //notice.hide().insertAfter( sub_nav ).slideDown( 200 );
478
479
            $( window ).trigger('uix.saved');
480
        });
481
482
    });
483
484
    // initialize live sync rebuild
485
    $(document).on('change', '[data-live-sync]', function(e){
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
486
        var app = $(this).closest('[data-app]').data('app');
487
        conduitSyncData( app );
488
    });
489
    $(document).on('click', 'button[data-live-sync]', function(e){
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
490
        var app = $(this).closest('[data-app]').data('app');
491
        conduitSyncData( app );
492
    });
493
    $(document).on('click', '.uix-notice .notice-dismiss', function(){
494
        var parent =  $( this ).closest( '.uix-notice' );
495
        parent.slideUp(200, function(){
496
            parent.remove();
497
        });
498
    });
499
500
    $(document).on( 'click', '[data-tab]', function( e ){
501
502
        e.preventDefault();
503
504
        var clicked = $( this ),
505
            tab = clicked.data('tab'),
506
            active = $('.current[data-tab]').data('tab')
507
508
        if( active ){
509
            conduitBuildData( active );
510
            if( active === tab ){
511
                return;
512
            }
513
            $('[data-app="' + active + '"]').empty().hide();
514
        }
515
516
        $('[data-tab]').removeClass('current');
517
        $('[data-app="' + tab + '"]').show();
518
        clicked.addClass('current');
519
        conduitBuildUI( tab );
520
    } );
521
522
    $('script[data-template]').each( function(){
523
524
        var element	= $(this),
525
            app		= element.data('template');
526
527
        coduitTemplates[ app ] = Handlebars.compile( element.html(), { data : true } );
0 ignored issues
show
Bug introduced by
The variable Handlebars seems to be never declared. If this is a global, consider adding a /** global: Handlebars */ 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...
528
    });
529
    // init partials
530
    $('script[data-handlebars-partial]').each( function(){
531
        var partial = $( this );
532
        Handlebars.registerPartial( partial.data('handlebarsPartial'), partial.html() );
0 ignored issues
show
Bug introduced by
The variable Handlebars seems to be never declared. If this is a global, consider adding a /** global: Handlebars */ 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...
533
        coduitTemplates[ '__partial_' + partial.data('handlebarsPartial') ] = Handlebars.compile( partial.html(), { data : true } );
534
    });
535
    // modal capture
536
    $(document).on( 'click', '[data-modal-node]', function( e ) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
537
538
        var clicked = $( this ),
539
            nodes = clicked.data('modal-node').split('.'),
540
            app = clicked.data('app') ? clicked.data('app') : nodes.shift(),
541
            type = clicked.data('type') ? clicked.data('type') : 'save',
542
            data;
543
544
        if( clicked.data('before') ){
545
            if( typeof clicked.data('before') === 'function' ){
546
                clicked.data('before')( clicked );
547
            }else if( typeof window[ clicked.data('before') ] === 'function' ){
548
                window[ clicked.data('before') ]( clicked );
549
            }
550
        }
551
552
553
        if( type !== 'delete' ){
554
            try{
555
                data = clicked.closest('.uix-modal-wrap').getObject();
556
            }catch (e){
557
                return;
558
            }
559
        }
560
561
        if( type === 'add' ){
562
            conduitAddNode( nodes.join('.'), app, data );
0 ignored issues
show
Bug introduced by
The variable data does not seem to be initialized in case type !== "delete" on line 553 is false. Are you sure the function conduitAddNode handles undefined variables?
Loading history...
563
        }else if( type === 'delete' ){
564
565
            var selector = nodes.shift();
566
            if( nodes.length ){
567
                selector += '[' + nodes.join('][') + ']';
568
            }
569
            $( '[name^="' + selector + '"]' ).remove();
570
            conduitBuildData( app );
571
            conduitBuildUI( app );
572
        }else{
573
            conduitSetNode( nodes.join('.'), app, data );
574
        }
575
        $( window ).trigger('close.modal');
576
        if( clicked.data('callback') ){
577
            if( typeof clicked.data('callback') === 'function' ){
578
                clicked.data('callback')( data, clicked );
579
            }else if( typeof window[ clicked.data('callback') ] === 'function' ){
580
                window[ clicked.data('callback') ]( data, clicked );
581
            }
582
        }
583
    })
584
    $(window).on('close.modal', function( e ){
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
585
        //console.log( e );
586
        var active = $('.current[data-tab]').data('tab')
587
        if( active ){
588
            conduitBuildUI( active );
589
        }
590
    });
591
592
    // register apps
593
    conduitRegisterApps();
594
595
    window.onbeforeunload = function(){
596
        if( currentAjaxProcess ){
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if currentAjaxProcess 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...
597
            return false;
598
        }
599
    };
600
601
}) );