comparison src/DML/MainVisBundle/Resources/assets/lib/jquery.ui/combobox.orig.js @ 0:493bcb69166c

added public content
author Daniel Wolff
date Tue, 09 Feb 2016 20:54:02 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:493bcb69166c
1 (function($) {
2 $.widget( "custom.combobox", {
3 _create : function() {
4 this.wrapper = $("<span>").addClass(
5 "custom-combobox")
6 .insertAfter(this.element);
7 this.element.hide();
8 this._createAutocomplete();
9 this._createShowAllButton();
10 },
11
12 _createAutocomplete : function() {
13 var selected = this.element.children(":selected"), value = selected
14 .val() ? selected.text() : "";
15 this.input = $("<input>")
16 .appendTo(this.wrapper)
17 .val(value)
18 .attr("title", "")
19 .addClass(
20 "custom-combobox-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 }).tooltip({
26 tooltipClass : "ui-state-highlight"
27 });
28 this._on(this.input, {
29 autocompleteselect : function(event, ui) {
30 ui.item.option.selected = true;
31 this._trigger("select", event, {
32 item : ui.item.option
33 });
34 },
35 autocompletechange : "_removeIfInvalid"
36 });
37 },
38
39 _createShowAllButton : function() {
40 var input = this.input, wasOpen = false;
41 $("<a>").attr("tabIndex", -1).attr("title",
42 "Show All Items").tooltip().appendTo(
43 this.wrapper).button({
44 icons : {
45 primary : "ui-icon-triangle-1-s"
46 },
47 text : false
48 }).removeClass("ui-corner-all").addClass(
49 "custom-combobox-toggle ui-corner-right")
50 .mousedown(
51 function() {
52 wasOpen = input.autocomplete(
53 "widget")
54 .is(":visible");
55 }).click(function() {
56 input.focus();
57 // Close if already visible
58 if (wasOpen) {
59 return;
60 }
61 // Pass empty string as value to search
62 // for, displaying all results
63 input.autocomplete("search", "");
64 });
65 },
66 _source : function(request, response) {
67 var matcher = new RegExp($.ui.autocomplete
68 .escapeRegex(request.term), "i");
69 response(this.element.children("option").map(
70 function() {
71 var text = $(this).text();
72 if (this.value
73 && (!request.term || matcher
74 .test(text)))
75 return {
76 label : text,
77 value : text,
78 option : this
79 };
80 }));
81 },
82
83 _removeIfInvalid : function(event, ui) {
84 // Selected an item, nothing to do
85 if (ui.item) {
86 return;
87 }
88 // Search for a match (case-insensitive)
89 var value = this.input.val(), valueLowerCase = value
90 .toLowerCase(), valid = false;
91 this.element
92 .children("option")
93 .each(
94 function() {
95 if ($(this).text()
96 .toLowerCase() === valueLowerCase) {
97 this.selected = valid = true;
98 return false;
99 }
100 });
101 // Found a match, nothing to do
102 if (valid) {
103 return;
104 }
105 // Remove invalid value
106 this.input.val("").attr("title",
107 value + " didn't match any item").tooltip(
108 "open");
109 this.element.val("");
110 this._delay(function() {
111 this.input.tooltip("close").attr("title", "");
112 }, 2500);
113 this.input.autocomplete("instance").term = "";
114 },
115 _destroy : function() {
116 this.wrapper.remove();
117 this.element.show();
118 }
119 });
120 })(jQuery);