$.widget('wpj.admin_autocomplete', { options: { $menu: null, minLength: 2, xhr: null, limit: 50, type: 'product_id', visible: 0, parameters: null, variations: false, source: null, searchTerm: null, data: null, templateContext: null, resetInputOnClose: false, showFlags: true, searchEvent: null, showResetButton: true, flags: [], template: '{{? it.items.length>2}} {{#def.header}} {{?}} {{~it.items :item :index}} {{#def.menuItem}} {{~}}', subtemplates: { header: '{{? it.me.options.showFlags && it.me.flags}}
{{~it.me.flags :flag}}{{#def.flag_button}}{{~}}
{{?}}', flag_button: ' ', menuItem: '
' + '{{? item.image && item.image.id}}{{=item.image.descr}}{{?}}{{? !item.image}}{{?}}' + '

{{=item.text}}
Kód: {{? item.code}}{{=item.code}} {{?}}| Skladem: {{=item.in_store}} ks{{? item.visible == "N"}} | Skrytý!{{?}}{{? item.visible == "O"}} | Prodej ukončen!{{?}}

' + '{{#def.buttons}}' + '
' + '{{#def.bottom}}', buttons: '', bottom: '' } }, searchIconsWrapper: null, selectPosition: NaN, _create: function() { this._super(); if (this.options.type === 'product_id' || this.options.type === 'productId') { this.flags = [['s', 'pouze skladem'], ['v', 'pouze viditelné']]; } this.options.source = wpj.template.compile(this.options.source); var me = this; if (!this.searchIconsWrapper && this.options.showResetButton) { me.element.wrap('
'); this.searchIconsWrapper = me.element.parent(); const iconLoading = $(''); this.searchIconsWrapper.append(iconLoading); const iconClear = $(''); iconClear.on('click',function() { me.element.val(''); me.searchIconsWrapper.find('.search-clear-icon').addClass('hidden'); }); this.searchIconsWrapper.append(iconClear); } // set placeholder if (!this.element.attr('placeholder')) { this.element.attr('placeholder', 'Vyhledejte psaním...'); } var on = 'input'; if (this.options.minLength == 0) { on = on + ' focus'; } this.options.searchEvent = function(e) { me._doSearch(me.element.val()); }; this.element.on(on, this.options.searchEvent); }, _destroy: function() { this.close(); if (this.options.$menu) { this.options.$menu.remove(); this.options.$menu = null; } var on = 'input'; if (this.options.minLength == 0) { on = on + ' focus'; } this.element.off(on, this.options.searchEvent); this._super(); }, _doSearch: function(term) { var me = this; me.searchTerm = term; if (this.searchIconsWrapper) { if (term.length > 0) { this.searchIconsWrapper.find('.search-clear-icon').removeClass('hidden'); } else { this.searchIconsWrapper.find('.search-clear-icon').addClass('hidden'); this.searchIconsWrapper.find('.search-loading-icon').addClass('hidden'); } } if (term.length >= this.options.minLength) { wpj.domUtils.resetTimer('autocomplete', 300, function() { if (me.searchIconsWrapper){ me.searchIconsWrapper.find('.search-loading-icon').removeClass('hidden'); } me.search(term); }); } else { this.cancelSearch(); this.close(); } }, onKeyPress: function(e) { switch (e.key) { case 'ArrowDown': if (isNaN(this.selectPosition)) { this.selectPosition = -1; } this.selectPosition++; this.focusItem(this.selectPosition); break; case 'ArrowUp': if (!isNaN(this.selectPosition) && this.selectPosition > 0) { this.selectPosition--; } this.focusItem(this.selectPosition); break; case 'Enter': if (!isNaN(this.selectPosition)) { var $item = $('.autocomplete [data-autocomplete-item="' + this.selectPosition + '"]'); this.close(); this.select(e, $item); } else { e.preventDefault(); } break; default: return true; } return false; }, focusItem: function(itemId) { $('[data-autocomplete-item="' + itemId + '"]').focus(); return true; }, search: function(term) { if (this.options.xhr) { this.options.xhr.abort(); } term = encodeURIComponent(term); var me = this, url = this.options.source({ value: term, limit: this.options.limit, visible: this.options.visible, type: this.options.type, parameters: this.options.parameters, me: this }); this.options.xhr = $.getJSON(url, function(data) { me.data = me.prepareData(data); me.open(); me.render(me.data); if (me.searchIconsWrapper){ me.searchIconsWrapper.find('.search-loading-icon').addClass('hidden'); } }); }, cancelSearch: function() { if (!this.options.xhr) { return false; } this.options.xhr.abort(); this.options.xhr = null; }, prepareData: function(data) { return jQuery.extend( { items: data }, this.options.templateContext); }, select: function(e, $item) { var value = $item.data('autocomplete-item'); var elementName = this.element.attr('name'); var clone; var text = $item.text(); this.element.val(text); if (elementName != '') { clone = this.element.clone(); clone.val(value); clone.insertAfter(this.element); clone.hide(); this.element.attr('name', ''); } else { clone = this.element.next('input'); clone.val(value); } return false; }, menu: function() { var me = this; this.options.$menu = $('
').insertBefore(this.element); if (!me.element.closest('body').hasClass('window')) { // Is the autocomplete in a window? If not, show it bellow input. this.options.$menu.addClass('autocomplete-bottom'); } else if ((me.element.offset().top - $(window).scrollTop()) < ($(window).height() / 2)) { // Is the input in the bottom half of window? Show autocomplete bellow input. this.options.$menu.addClass('autocomplete-bottom'); this.options.$menu.css('max-height', ($(window).height() - (me.element.offset().top - $(window).scrollTop()) - 120)); } else { // Is the input in the top half of window? Show autocomplete above. this.options.$menu.addClass('autocomplete-top'); this.options.$menu.css('max-height', me.element.offset().top - $(window).scrollTop() - 20); } this.options.$menu.on('click', '[data-autocomplete-item]', function(e) { if (!$(e.target).is('[data-autocomplete-add]')) { me.close(); return me.select(e, $(e.currentTarget)); } }); }, open: function() { if (this.element.is('.opened')) { return false; } this.selectPosition = NaN; var me = this; if (!this.options.$menu) { this.menu(); } this.element.addClass('opened'); this.options.$menu.addClass('opened'); $('body') .on('click.admin_autocomplete', function(e) { if (me.element.has(e.target).length) { return; } if ($(e.target).is('[data-autocomplete-flag]')) { var flag = $(e.target).data('autocomplete-flag'); var flagExp = new RegExp(flag + ': ', 'g'); var term = me.element.val(); if (flagExp.test(term)) { term = term.replace(flagExp, ''); } else { term = flag + ': ' + term; } me.element.val(term); me._doSearch(term); return; } if ($(e.target).is('[data-autocomplete-add]')) { return; } me.close(); }) .on('keydown.admin_autocomplete', function(e) { /* todo chrome neumi keypres */ if (e.key != 'Escape') { return me.onKeyPress(e); } me.close(); return false; }); }, close: function() { if (!this.element.is('.opened')) { return false; } $('body').off('click.admin_autocomplete') .off('keydown.admin_autocomplete'); if (this.options.resetInputOnClose) { this.element.val(''); } this.element.removeClass('opened'); this.options.$menu.removeClass('opened'); }, render: function(data) { data.me = this; var template = this.getTemplate(), html = template(data); this.options.$menu.html(html); }, getTemplate: function() { return wpj.template.compile(this.options.template, this.options.subtemplates); } }); $.widget('wpj.adminAutoComplete', $.wpj.admin_autocomplete, { options: { source: './launch.php?s=autocomplete.php&type={{=it.type}}&term={{=it.value}}&limit={{=it.limit}}&visible={{=it.visible}}{{? it.parameters}}&{{=it.parameters}}{{?}}' }, render: function(data) { if (this._trigger('render', {}, data)) { if (this._super(data)) { return true; } } return false; }, select: function(e, $item) { if (this._trigger('select', e, { item: $item, data: this.data })) { if ($item.data('autocomplete-item') != 'show-all') { if (this._super(e, $item)) { return true; } } } return false; } }); $.widget('wpj.adminProductMultiselect', $.wpj.adminAutoComplete, { options: { source: './launch.php?s=autocomplete.php&type={{=it.type}}&term={{=it.value}}&limit={{=it.limit}}&visible={{=it.visible}}{{? it.parameters}}&{{=it.parameters}}{{?}}{{? it.me.options.variations}}&variations=1{{?}}', showResetButton: false, itemIdentifierKey: 'id', targetSelect: null, // CSS selector pro