source: trunk/prototype/plugins/widgets/combobox.js @ 7720

Revision 7720, 3.5 KB checked in by adriano, 11 years ago (diff)

Ticket #2523 - criado componente generico combobox utilizado na tela de sinalizacao para acompanhamento

Line 
1        /**
2         * Implementação do widget de input-combobox
3         */
4 
5        (function( $ ) {
6                $.widget( "ui.combobox", {
7                        _create: function() {
8                                var self = this,
9                                        select = this.element.hide(),
10                                        selected = select.children( ":selected" ),
11                                        value = selected.val() ? selected.text() : "";
12                                var input = this.input = $( "<input>" )
13                                        .insertAfter( select )
14                                        .val( value )
15                                        .autocomplete({
16                                                delay: 0,
17                                                minLength: 0,
18                                                source: function( request, response ) {
19                                                        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
20                                                        response( select.children( "option" ).map(function() {
21                                                                var text = $( this ).text();
22                                                                if ( this.value && ( !request.term || matcher.test(text) ) )
23                                                                        return {
24                                                                                label: text.replace(
25                                                                                        new RegExp(
26                                                                                                "(?![^&;]+;)(?!<[^<>]*)(" +
27                                                                                                $.ui.autocomplete.escapeRegex(request.term) +
28                                                                                                ")(?![^<>]*>)(?![^&;]+;)", "gi"
29                                                                                        ), "<strong>$1</strong>" ),
30                                                                                value: text,
31                                                                                option: this
32                                                                        };
33                                                        }) );
34                                                },
35                                                select: function( event, ui ) {
36                                                        ui.item.option.selected = true;
37                                                        self._trigger( "selected", event, {
38                                                                item: ui.item.option
39                                                        });
40                                                },
41                                                change: function( event, ui ) {
42                                                        if ( !ui.item ) {
43                                                                var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
44                                                                        valid = false;
45                                                                select.children( "option" ).each(function() {
46                                                                        if ( $( this ).text().match( matcher ) ) {
47                                                                                this.selected = valid = true;
48                                                                                return false;
49                                                                        }
50                                                                });
51                                                                /*
52                                                                if ( !valid ) {
53                                                                        // remove invalid value, as it didn't match anything
54                                                                        $( this ).val( "" );
55                                                                        select.val( "" );
56                                                                        input.data( "autocomplete" ).term = "";
57                                                                        return false;
58                                                                }
59                                                                */
60                                                                if ( !valid ) {
61                                                                        if(select.has('option[value="custom"]').length > 0) {
62                                                                                select.find('option:last').val('custom').html($(this).val()).attr('selected', 'selected');
63                                                                        } else {
64                                                                                select.append(select.find('option:last').clone().val('custom').html($(this).val()));
65                                                                                select.find('option[value="custom"]').attr('selected', 'selected');
66                                                                        }
67                                                                }
68                                                        }
69                                                }
70                                        })
71                                        .addClass( "ui-widget ui-widget-content ui-corner-left" );
72
73                                input.data( "autocomplete" )._renderItem = function( ul, item ) {
74                                        return $( "<li></li>" )
75                                                .data( "item.autocomplete", item )
76                                                .append( "<a>" + item.label + "</a>" )
77                                                .appendTo( ul );
78                                };
79
80                                this.button = $( "<button type='button'>&nbsp;</button>" )
81                                        .attr( "tabIndex", -1 )
82                                        .attr( "title", "Show All Items" )
83                                        .insertAfter( input )
84                                        .button({
85                                                icons: {
86                                                        primary: "ui-icon-triangle-1-s"
87                                                },
88                                                text: false
89                                        })
90                                        .removeClass( "ui-corner-all" )
91                                        .addClass( "ui-corner-right ui-button-icon" )
92                                        .click(function() {
93                                                // close if already visible
94                                                if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
95                                                        input.autocomplete( "close" );
96                                                        return;
97                                                }
98
99                                                // work around a bug (likely same cause as #5265)
100                                                $( this ).blur();
101
102                                                // pass empty string as value to search for, displaying all results
103                                                input.autocomplete( "search", "" );
104                                                input.focus();
105                                        });
106                        },
107
108                        destroy: function() {
109                                this.input.remove();
110                                this.button.remove();
111                                this.element.show();
112                                $.Widget.prototype.destroy.call( this );
113                        }
114                });
115        })( jQuery );
116        /**
117         * #END: Implementação do widget de input-combobox
118         */
Note: See TracBrowser for help on using the repository browser.