Completed
Push — master ( 108b89...0a9573 )
by Craig
06:27
created

src/lib/Zikula/Bundle/CoreBundle/Resources/public/js/bootstrap-zikula.js   B

Complexity

Conditions 1
Paths 16

Size

Total Lines 101

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 16
nop 1
dl 0
loc 101
rs 8.2857
c 1
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A $(ꞌ[data-toggle="confirmation"]ꞌ).click.bs.modal.data-api 0 20 2
A bootstrap-zikula.js ➔ getValueOfElement 0 10 4
A $(document).ready 0 50 1

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
// Copyright Zikula Foundation, licensed MIT.
2
3
( function($) {
4
5
    // use bootstrap noConflict. See http://getbootstrap.com/javascript/#js-noconflict
6
    var bootstrapButton = $.fn.button.noConflict()
7
    $.fn.bootstrapBtn = bootstrapButton
8
9
    /**
10
     * Confirmation modal
11
     * 
12
     * Usage: <a data-toggle="confirmation" data-title="..." data-text="..." href="...">...</a>
13
     */
14
    $(document).on('click.bs.modal.data-api', '[data-toggle="confirmation"]', function (e) {
15
        
16
        e.preventDefault();
17
        
18
        var $this = $(this);
19
        var title = $this.data('title') || '';
20
        var text  = $this.data('text') || '';
21
        
22
        if ($("#confimationModal").length === 0) {
23
            var Modal = '<div class="modal fade" id="confimationModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title">'+title+'</h4></div><div class="modal-body">' + text + '</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">' + Zikula.__('No') + '</button><button id="confirmationOkButton" type="button" class="btn btn-primary" data-dismiss="modal">' + Zikula.__('Yes') + '</button></div></div></div></div>';
0 ignored issues
show
Bug introduced by
The variable Zikula seems to be never declared. If this is a global, consider adding a /** global: Zikula */ 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...
24
            $(document.body).append(Modal);
25
            $(document).on('click', '#confirmationOkButton', 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...
26
                window.location = $this.attr('href');
27
            });
28
        }
29
        
30
        $('#confimationModal').modal({}, this).one('hide', function () {
31
            $this.is(':visible') && $this.focus();
32
        });
33
    });
34
    
35
    /**
36
     * Return a value of input.
37
     *
38
     * @param element e The input element
0 ignored issues
show
Documentation introduced by
The parameter element does not exist. Did you maybe forget to remove this comment?
Loading history...
39
     *
40
     * @return string Value
41
     */
42
    function getValueOfElement(e) {
43
        if ($(e).is(':checkbox')) {
44
            return $(e).is(':checked') ? '1' : '0';
45
        } else if ($(e).is(':radio')) {
46
            var name = $(e).attr('name');
47
            return $('input[name="' + name + '"]:checked').val();
48
        } else {
49
            return $(e).val();
50
        }
51
    }
52
    
53
    $(document).ready(function() {
54
        
55
        
56
        // remove class hide because bootstrap is using important, that is not
57
        // working with jQuery.show();
58
        // $('.hide').hide().removeClass('hide');
59
        
60
        /**
61
        * Input switch container. 
62
        * 
63
        * This code shows/hide an container dependent on a value of an input 
64
        * element.
65
        * 
66
        * Example: 
67
        * <input type="text" name="abc" value="a">
68
        * <div data-switch="abc" data-switch-value="a">...</div>
69
        * 
70
        * This example shows the div container if the input value is equal "a"
71
        */
72
        $('[data-switch]').each(function() {
73
            var containerElement = $(this);
74
            var containerValues = containerElement.data('switch-value') || '0';
75
            containerValues = containerValues.toString();
76
            containerValues = containerValues.split(',');
77
            var inputName = containerElement.data('switch');
78
            var inputElement = $('[name="'+inputName+'"]');
79
80
            var inputValue = getValueOfElement(inputElement);
81
            if ($.inArray(inputValue, containerValues) === -1) {
82
                containerElement.hide();
83
            }
84
85
            inputElement.change(function() {
86
                inputValue = getValueOfElement(inputElement); 
87
                if ($.inArray(inputValue, containerValues) === -1) {
88
                    containerElement.slideUp();
89
                } else {
90
                    containerElement.slideDown();
91
                }
92
            });
93
        });
94
95
        $('.tooltips').each(function() {
96
            var placement = 'top';
97
            if ($(this).hasClass('tooltips-bottom')) {
98
                placement = 'bottom';
99
            }
100
            $(this).tooltip({placement: placement, animation: false});
101
        });
102
    });
103
})(jQuery);
104