ltsecurityinc安防产品在线商城代码
This commit is contained in:
+188
@@ -0,0 +1,188 @@
|
||||
define([
|
||||
'jquery',
|
||||
'underscore',
|
||||
'mage/cookies',
|
||||
'jquery-ui-modules/widget'
|
||||
], function ($, _) {
|
||||
'use strict';
|
||||
|
||||
$.widget('lts.searchAutocomplete', {
|
||||
options: {
|
||||
url: '',
|
||||
formSelector: '#search_mini_form',
|
||||
destinationSelector: '#search_autocomplete',
|
||||
minSearchLength: 2,
|
||||
delay: 280
|
||||
},
|
||||
|
||||
_create: function () {
|
||||
this.form = $(this.options.formSelector);
|
||||
this.panel = $(this.options.destinationSelector).addClass('lts-search-panel').appendTo(document.body);
|
||||
this.submit = this.form.find('button[type="submit"]');
|
||||
this.request = null;
|
||||
this.timer = null;
|
||||
this.backdrop = $('<button type="button" class="lts-search-backdrop" aria-label="Close search results"></button>')
|
||||
.appendTo(document.body)
|
||||
.on('click', this.close.bind(this));
|
||||
|
||||
this.element
|
||||
.attr({autocomplete: 'off', 'aria-haspopup': 'listbox', 'aria-expanded': 'false'})
|
||||
.on('input.ltsSearch', this._schedule.bind(this))
|
||||
.on('focus.ltsSearch', function () {
|
||||
if (this.element.val().trim().length >= this.options.minSearchLength) {
|
||||
this._schedule();
|
||||
}
|
||||
}.bind(this))
|
||||
.on('keydown.ltsSearch', function (event) {
|
||||
if (event.key === 'Escape') {
|
||||
this.close();
|
||||
this.element.trigger('blur');
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
$(window).on('resize.ltsSearch scroll.ltsSearch', this._position.bind(this));
|
||||
$(document).on('mousedown.ltsSearch', function (event) {
|
||||
if (!$(event.target).closest('.block-search, .lts-search-panel').length) {
|
||||
this.close();
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
this.submit.prop('disabled', this.element.val().trim().length < this.options.minSearchLength);
|
||||
},
|
||||
|
||||
_schedule: function () {
|
||||
var value = this.element.val().trim();
|
||||
|
||||
window.clearTimeout(this.timer);
|
||||
this.submit.prop('disabled', value.length < this.options.minSearchLength);
|
||||
if (value.length < this.options.minSearchLength) {
|
||||
this.close();
|
||||
return;
|
||||
}
|
||||
|
||||
this.timer = window.setTimeout(function () {
|
||||
this._load(value);
|
||||
}.bind(this), this.options.delay);
|
||||
},
|
||||
|
||||
_load: function (value) {
|
||||
if (this.request) {
|
||||
this.request.abort();
|
||||
}
|
||||
|
||||
this._position();
|
||||
this.panel.html('<div class="lts-search-loading">Searching…</div>');
|
||||
this.open();
|
||||
this.request = $.getJSON(this.options.url, {q: value})
|
||||
.done(function (response) {
|
||||
if (value !== this.element.val().trim()) {
|
||||
return;
|
||||
}
|
||||
this._render(response, value);
|
||||
}.bind(this))
|
||||
.fail(function (xhr, status) {
|
||||
if (status !== 'abort') {
|
||||
this.panel.html('<div class="lts-search-empty">No matching products found.</div>');
|
||||
}
|
||||
}.bind(this))
|
||||
.always(function () {
|
||||
this.request = null;
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
_render: function (response, value) {
|
||||
var suggestions = response.suggestions || [],
|
||||
products = response.products || [],
|
||||
resultUrl = response.search_url || (this.form.attr('action') + '?q=' + encodeURIComponent(value)),
|
||||
suggestionHtml,
|
||||
productHtml;
|
||||
|
||||
suggestionHtml = suggestions.map(function (item) {
|
||||
var url = this.form.attr('action') + '?q=' + encodeURIComponent(item.title);
|
||||
return '<li><a href="' + _.escape(url) + '">' +
|
||||
'<img src="' + _.escape(this._searchIcon()) + '" alt="" aria-hidden="true">' +
|
||||
'<span>' + _.escape(item.title) + '</span></a></li>';
|
||||
}.bind(this)).join('');
|
||||
|
||||
productHtml = products.map(function (product) {
|
||||
return '<li class="lts-search-product">' +
|
||||
'<a class="lts-search-product__image" href="' + _.escape(product.url) + '">' +
|
||||
'<img src="' + _.escape(product.image) + '" alt="' + _.escape(product.name) + '"></a>' +
|
||||
'<div class="lts-search-product__info"><a href="' + _.escape(product.url) + '">' +
|
||||
_.escape(product.name) + '</a><span>' + _.escape(product.sku) + '</span>' +
|
||||
'<b class="' + (product.in_stock ? 'is-in-stock' : 'is-out-of-stock') + '">' +
|
||||
(product.in_stock ? 'In Stock' : 'Out of Stock') + '</b></div>' +
|
||||
'<strong class="lts-search-product__price">' + _.escape(product.price) + '</strong>' +
|
||||
(product.in_stock ? this._cartForm(product) : '') + '</li>';
|
||||
}.bind(this)).join('');
|
||||
|
||||
if (!productHtml) {
|
||||
productHtml = '<li class="lts-search-empty">No matching products found.</li>';
|
||||
}
|
||||
|
||||
this.panel.html(
|
||||
'<div class="lts-search-suggestions"><h2>Search Suggestions</h2><ul>' + suggestionHtml + '</ul></div>' +
|
||||
'<div class="lts-search-products"><header><h2>Products</h2>' +
|
||||
'<span>Showing ' + products.length + ' of ' + (response.total || 0) + '</span>' +
|
||||
'<a href="' + _.escape(resultUrl) + '">See All Results</a></header>' +
|
||||
'<ul>' + productHtml + '</ul></div>'
|
||||
);
|
||||
this.open();
|
||||
},
|
||||
|
||||
_cartForm: function (product) {
|
||||
return '<form class="lts-search-product__cart" action="' + _.escape(product.add_url) + '" method="post">' +
|
||||
'<input type="hidden" name="product" value="' + _.escape(String(product.id)) + '">' +
|
||||
'<input type="hidden" name="form_key" value="' + _.escape($.mage.cookies.get('form_key') || '') + '">' +
|
||||
'<button type="submit">Add to Cart</button></form>';
|
||||
},
|
||||
|
||||
_searchIcon: function () {
|
||||
return this.form.find('.action.search img').attr('src') || '';
|
||||
},
|
||||
|
||||
_position: function () {
|
||||
var header = document.querySelector('.page-header'),
|
||||
bottom;
|
||||
|
||||
if (!this.element.is(':visible')) {
|
||||
this.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
bottom = header ? header.getBoundingClientRect().bottom : this.element[0].getBoundingClientRect().bottom;
|
||||
document.documentElement.style.setProperty('--lts-search-top', Math.max(10, bottom + 10) + 'px');
|
||||
return true;
|
||||
},
|
||||
|
||||
open: function () {
|
||||
if (this._position() === false) {
|
||||
return;
|
||||
}
|
||||
this.panel.addClass('is-open').attr('role', 'listbox').show();
|
||||
this.backdrop.addClass('is-open');
|
||||
this.element.attr('aria-expanded', 'true');
|
||||
$('body').addClass('lts-search-open');
|
||||
},
|
||||
|
||||
close: function () {
|
||||
this.panel.removeClass('is-open').hide();
|
||||
this.backdrop.removeClass('is-open');
|
||||
this.element.attr('aria-expanded', 'false');
|
||||
$('body').removeClass('lts-search-open');
|
||||
},
|
||||
|
||||
_destroy: function () {
|
||||
window.clearTimeout(this.timer);
|
||||
if (this.request) {
|
||||
this.request.abort();
|
||||
}
|
||||
this.backdrop.remove();
|
||||
$(window).off('.ltsSearch');
|
||||
$(document).off('.ltsSearch');
|
||||
this.element.off('.ltsSearch');
|
||||
}
|
||||
});
|
||||
|
||||
return $.lts.searchAutocomplete;
|
||||
});
|
||||
Reference in New Issue
Block a user