Issues (806)

static/org.openpsa.sales/sales.js (2 issues)

1
$(function() {
2
    if ($( "#org_openpsa_sales_salesproject_deliverable_add" ).length > 0) {
3
        $.widget( "custom.productselect", {
4
            _create: function() {
5
                this.wrapper = $( "<span>" )
6
                    .addClass( "custom-productselect" )
7
                    .insertAfter( this.element );
8
                this.element.hide();
9
                this._createAutocomplete();
10
                this._createShowAllButton();
11
                this.input.attr("placeholder", this.element.data('placeholder'));
12
            },
13
            _createAutocomplete: function() {
14
                var selected = this.element.children( ":selected" ),
15
                    value = selected.val() ? selected.text() : "";
16
                this.input = $( "<input>" )
17
                    .appendTo( this.wrapper )
18
                    .val( value )
19
                    .attr( "title", "" )
20
                    .addClass( "custom-productselect-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
21
                    .autocomplete({
22
                        delay: 0,
23
                        minLength: 0,
24
                        source: $.proxy( this, "_source" ),
25
                        select: function(event, ui) {
26
                            setTimeout(function() {
27
                                $(ui.item.option).closest('form').submit();
28
                            }, 10);
29
                        }
30
                    });
31
                this.input.data('ui-autocomplete')._renderItem = function( ul, item ) {
32
                    return $( "<li>" )
33
                        .append( "<a>" + item.label + "<span class='product-description'>" + item.description + "</span></a>" )
34
                        .appendTo( ul );
35
                };
36
37
                this._on( this.input, {
38
                    autocompleteselect: function( event, ui ) {
39
                        ui.item.option.selected = true;
40
                        this._trigger( "select", event, {
41
                            item: ui.item.option
42
                        });
43
                    },
44
                    autocompletechange: "_removeIfInvalid"
45
                });
46
            },
47
            _createShowAllButton: function() {
48
                var input = this.input,
49
                    wasOpen = false;
50
                $( "<a>" )
51
                    .attr( "tabIndex", -1 )
52
                    .attr( "title", "Show All Items" )
53
                    .appendTo( this.wrapper )
54
                    .button({
55
                        icons: {
56
                            primary: "ui-icon-triangle-1-s"
57
                        },
58
                        text: false
59
                    })
60
                    .removeClass( "ui-corner-all" )
61
                    .addClass( "custom-productselect-toggle ui-corner-right" )
62
                    .mousedown(function() {
63
                        wasOpen = input.autocomplete( "widget" ).is( ":visible" );
64
                    })
65
                    .click(function() {
66
                        input.focus();
67
                        // Close if already visible
68
                        if ( wasOpen ) {
69
                            return;
70
                        }
71
                        // Pass empty string as value to search for, displaying all results
72
                        input.autocomplete( "search", "" );
73
                    });
74
            },
75
            _source: function( request, response ) {
76
                var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
77
                response(this.element.children("option").map(function() {
78
                    var text = $( this ).text(),
79
                        desc = $( this ).data('description');
80
                    if (this.value && (!request.term || matcher.test(text) || matcher.test(desc))) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this.value && !request.t...) || matcher.test(desc) 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...
81
                        return {
82
                            label: text,
83
                            description: desc,
84
                            value: text,
85
                            option: this
86
                        };
87
                    }
88
                }));
89
            },
90
            _removeIfInvalid: function( event, ui ) {
91
                // Selected an item, nothing to do
92
                if ( ui.item ) {
93
                    return;
94
                }
95
                // Search for a match (case-insensitive)
96
                var value = this.input.val(),
97
                valueLowerCase = value.toLowerCase(),
98
                valid = false;
99
                this.element.children("option").each(function() {
100
                    if ( $( this ).text().toLowerCase() === valueLowerCase ) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if $(this).text().toLowerCase() === valueLowerCase 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...
101
                        this.selected = valid = true;
102
                        return false;
103
                    }
104
                });
105
                // Found a match, nothing to do
106
                if ( valid ) {
107
                    return;
108
                }
109
                // Remove invalid value
110
                this.input
111
                    .val( "" )
112
                    .attr( "title", value + " didn't match any item" );
113
                this.element.val( "" );
114
                this.input.autocomplete( "instance" ).term = "";
115
            },
116
            _destroy: function() {
117
                this.wrapper.remove();
118
                this.element.show();
119
            }
120
        });
121
        $( "#org_openpsa_sales_salesproject_deliverable_add" )
122
            .productselect()
123
            .closest('form')
124
                .on('submit', function(e) {
125
                    if (!$("#org_openpsa_sales_salesproject_deliverable_add").val()) {
126
                        e.preventDefault();
127
                        return;
128
                    }
129
                    create_dialog($(this), $(this).find('> label').text());
130
                });
131
    }
132
});
133
134
$(document).ready(function() {
135
    var continuous = $('form.datamanager2 #org_openpsa_sales_continuous');
136
    if (continuous.length > 0) {
137
        continuous.on('change', function() {
138
            $('#org_openpsa_sales_end').closest('.element').toggle(!this.checked);
139
        }).trigger('change');
140
    }
141
});
142