File size: 10,095 Bytes
45e5708 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
/**
* Json key/value autocomplete for jQuery
* Provides a transparent way to have key/value autocomplete
* Copyright (C) 2008 Ziadin Givan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
*
* Examples
*
* $("input#example").autocomplete("autocomplete.php");//using default parameters
*
* $("input#example").autocomplete("autocomplete.php",{minChars:3,timeout:3000,validSelection:false,parameters:{'myparam':'myvalue'},before : function(input,text) {},after : function(input,text) {}});
*
* minChars = Minimum characters the input must have for the ajax request to be made
* timeOut = Number of miliseconds passed after user entered text to make the ajax request
* validSelection = If set to true then will invalidate (set to empty) the value field if the text is not selected (or modified) from the list of items.
* parameters = Custom parameters to be passed
* after, before = a function that will be caled before/after the ajax request
*/
(function ($) {
$.fn.autocomplete = function(settings)
{
return this.each( function()//do it for each matched element
{
var textInput = $(this);
textInput.attr("name", textInput.attr("name") + "_text");
//create a new hidden input that will be used for holding the return value when posting the form, then swap names with the original input
var hiddenInput = $('<input type=hidden name="' + textInput.attr("name") + '"/>');
hiddenInput.val( textInput.val() );
textInput.after(hiddenInput);
var valueInput = $(this).next();
//create the ul that will hold the text and values
valueInput.after('<ul class="autocomplete"></ul>');
var list = valueInput.next();
var oldText = '';
var typingTimeout;
var size = 0;
var selected = -1;
settings = $.extend(//provide default settings
{
minChars : 1,
timeout: 1000,
after : null,
before : null,
validSelection : true,
url : this.dataset.url,
parameters : {'inputName' : valueInput.attr('name'), 'inputId' : textInput.attr('id')}
} , settings);
function getData(text)
{
window.clearInterval(typingTimeout);
if (text != oldText && (settings.minChars != null && text.length >= settings.minChars))
{
clear();
if (settings.before == "function")
{
settings.before(textInput,text);
}
textInput.addClass('autocomplete-loading');
settings.parameters.text = text;
$.getJSON(settings.url, settings.parameters, function(data)
{
var items = '';
if (data)
{
size = 0;
for ( key in data )//get key => value
{
items += '<li value="' + key + '">' + data[key].replace(new RegExp("(" + text + ")","i"),"<strong>$1</strong>") + '</li>';
size++;
}
list.css({/*top: textInput.offset().top + textInput.outerHeight(), left: textInput.offset().left,*/ width: Math.max(100, textInput.outerWidth())}).html(items);
//on mouse hover over elements set selected class and on click set the selected value and close list
list.show().children().
hover(function() { $(this).addClass("selected").siblings().removeClass("selected");}, function() { $(this).removeClass("selected") } ).
click(function () { value = $(this).attr('value'); text = $(this).text();valueInput.val( value ); textInput.val( text ); textInput.trigger("autocomplete.change", [ value, text ]);clear(); });
if (settings.after == "function")
{
settings.after(textInput,text);
}
}
textInput.removeClass('autocomplete-loading');
});
oldText = text;
}
}
function clear()
{
list.hide();
size = 0;
selected = -1;
}
textInput.keydown(function(e)
{
window.clearInterval(typingTimeout);
if(e.which == 27)//escape
{
clear();
} else if (e.which == 46 || e.which == 8)//delete and backspace
{
clear();
//invalidate previous selection
if (settings.validSelection) valueInput.val('');
}
else if(e.which == 13)//enter
{
if ( list.css("display") == "none")//if the list is not visible then make a new request, otherwise hide the list
{
getData(textInput.val());
} else
{
clear();
}
e.preventDefault();
return false;
}
else if(e.which == 40 || e.which == 9 || e.which == 38)//move up, down
{
switch(e.which)
{
case 40: //down
case 9:
selected = (selected >= size - 1) ? 0 : selected + 1; break;
case 38://up
selected = (selected < 0) ? size -1 : selected - 1; break;
default: break;
}
//set selected item and input values
textInput.val( list.children().removeClass('selected').eq(selected).addClass('selected').text() );
valueInput.val( list.children().eq(selected).attr('value') );
} else
{
//invalidate previous selection
if (settings.validSelection) valueInput.val('');
typingTimeout = window.setTimeout(function() { getData(textInput.val()) },settings.timeout);
}
});
});
};
$.autocompleteList = function(el, settings)
{
var autocomplete = $(el).autocomplete(settings);
var list = $('<div class="autocomplete-list card border-top-0"></div>');
var autocomplete_hidden = autocomplete.next();
var name = autocomplete_hidden.attr("name");
autocomplete_hidden.next().after(list);
var autocomplete_list_hidden = $('<input type=hidden name="' + name + '_list" value="' + autocomplete_hidden.val() + '"/>');
list.after(autocomplete_list_hidden);
function addItem(value, text)
{
list.append($('<div class=""><button type="button" class="remove-btn close text-muted" aria-label="Close"><div aria-hidden="true">×</div></button><span>' + text + '</span>\
<input name="list[]" value="' + value + '" type="hidden">\
</div>'));
autocomplete.val("");
};
function setList()
{
values = {};
$('input[name="list[]"]', list).each(function(i, el)
{
values[this.value] = $("span", this.parentNode).text();
});
autocomplete_list_hidden.val( JSON.stringify(values) );
};
function setValue(value) {
if (value == "" || value == undefined) return false;
// value = decodeURIComponent(value);
values = JSON.parse(value);
for (key in values)
{
addItem(key, values[key]);
}
setList();
};
autocomplete.on("autocomplete.change", function(event, value, text) {
var autolist = $(this).data("autocompleteList");
autolist.addItem(value, text);
autolist.setList();
autolist.trigger("autocompletelist.change", [ JSON.stringify(values) ]);
});
list.on("click", ".remove-btn", function (event, value, text)
{
this.parentNode.remove();
setList();
autocomplete.trigger("autocompletelist.change", [ JSON.stringify(values) ]);
event.preventDefault();
return false;
});
autocomplete.setValue = setValue;
autocomplete.addItem = addItem;
autocomplete.setList = setList;
$.data(el, "autocompleteList", autocomplete);
return autocomplete;
}
$.fn.autocompleteList = function(options)
{
return this.each( function()
{
$.autocompleteList(this, options);
});
};
$.tagsInput = function(el, settings)
{
var autocomplete = $(el).autocomplete(settings);
var list = autocomplete.parent();//$('<div class="form-control autocomplete-list" style="min-height: 100px;height: 100px; overflow: auto;"></div>');
var autocomplete_hidden = autocomplete.next();
var name = autocomplete_hidden.attr("name");
autocomplete_hidden.next();//.after(list);
var autocomplete_list_hidden = $('<input type=hidden name="' + name + '_list" value="' + autocomplete_hidden.val() + '"/>');
list.append(autocomplete_list_hidden);
function addItem(value, text)
{
autocomplete.before($('<div class="badge border m-1"><a href="#" class="btn-link"><i class="la la-close"></i></a> <span>' + text + '</span>\
<input name="list[]" value="' + value + '" type="hidden">\
</div>'));
autocomplete.val("");
};
function setList()
{
var values = {};
//console.log($('input[name="list[]"]', list).serialize());
$('input[name="list[]"]', list).each(function(i, el)
{
values[this.value] = $("span", this.parentNode).text();
});
//values = encodeURIComponent(JSON.stringify(values));
values = JSON.stringify(values);//.replace('"', '\"');
autocomplete_list_hidden.val( values );
return values;
};
function setValue(value) {
if (value == "" || value == undefined) return false;
values = JSON.parse(value);
for (key in values)
{
addItem(key, values[key]);
}
setList();
};
autocomplete.on("autocomplete.change", function(event, value, text) {
var autolist = $(this).data("tagsInput");
autolist.addItem(value, text);
var values = autolist.setList();
autolist.trigger("tagsinput.change", [ values ]);
});
list.on("click", ".remove-btn", function (event, value, text)
{
this.parentNode.remove();
var values = autolist.setList();
autocomplete.trigger("tagsinput.change", [ values ]);
event.preventDefault();
return false;
});
autocomplete.setValue = setValue;
autocomplete.addItem = addItem;
autocomplete.setList = setList;
$.data(el, "tagsInput", autocomplete);
return autocomplete;
}
$.fn.tagsInput = function(options)
{
return this.each( function()//do it for each matched element
{
$.tagsInput(this, options);
});
};
})(jQuery);
|