PageSourceSearch

https://manodelfriki.com/modules/ambjolisearch/views/js/ambjolisearch.js

js manodelfriki.com collected 2026-10-04 13:25:10 UTC 20,127 bytes, 409 lines download raw bytes

1/**
2 *  @module     Advanced search (AmbJoliSearch)
3 *  @file       ambjolisearch.php
4 *  @subject    script principal pour gestion du module (install/config/hook)
5 *  @copyright  Copyright (c) 2013-2022 Ambris Informatique SARL
6 *  @license    Commercial license
7 *  Support by mail: [email protected]
8 **/
9
10var _gaq = _gaq || [];
11
12if (!window.console) {
13    window.console = {
14        log: function() {}
15    };
16}
17
18(function($){
19    var compat_jQ = (function(){ try { return amb_jQ; } catch(err){ return $; } })();
20    (function($) {
21        var joli_settings = {
22            display_manufacturer: true,
23            display_category: true,
24        };
25
26        var customizeRender = function() {
27            var __superRenderItem = $.ui.autocomplete.prototype._renderItem;
28            var __superRenderMenu = $.ui.autocomplete.prototype._renderMenu;
29
30
31            $.widget('ambjolisearch.ambSearchAutocomplete', $.ui.autocomplete, {
32                _renderItem: function(ul, item) {
33                    if (item.data) {
34                        if (this.options.customRender) {
35                            return $("<li></li>")
36                            .data("item.autocomplete", item)
37                            .addClass((item.title ? 'jolisearch-container' : ''))
38                            .addClass(item.data && item.data.type ? item.data.type : '')
39                            .append(item.title ? $("<span>").addClass("jolisearch-title").html(item.title) : '')
40                            .append($("<a></a>").attr('href', (item.data && item.data.link ? item.data.link : ""))[this.options.html ? "html" : "text"](item.label).addClass(item.data ? item.data.type : ""))
41                            .appendTo(ul);
42                        } else {
43                            return __superRenderItem.call(this, ul, item);
44                        }
45                    } else {
46                        return $("<li></li>")
47                                .data("item.autocomplete", item)
48                                .html(item.value)
49                                .appendTo(ul);
50                    }
51                },
52                _renderMenu: function(ul, items) {
53                    $(ul).addClass('ui-jolisearch');
54                    $(ul).addClass(jolisearch['classes']);
55                    $(ul).removeAttr('style');
56                    return __superRenderMenu.call(this, ul, items);
57                }
58            });
59        };
60
61
62        var matchAccents = function(s) {
63                // http://jsfiddle.net/uJ99L/4/
64                var accents = {
65                    a: new Array('à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'Â', 'Ã', 'Ä', 'À', 'Á', 'Å', 'Æ'),
66                    e: new Array('é', 'è', 'ê', 'ë', 'É', 'È', 'Ë', 'Ê'),
67                    i: new Array('ì', 'í', 'î', 'ï', 'Ì', 'Í', 'Î','Ï'),
68                    o: new Array('ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø'),
69                    u: new Array('ù', 'ú', 'û', 'ü', 'Ù', 'Ú', 'Û', 'Ü'),
70                    n: new Array('ñ', 'Ñ'),
71                    c: new Array('ç', 'Ç')
72                };
73                for (var key in accents) {
74                    var reg = "[" + key;
75                    var search_term = "(" + key;
76
77                    for (var letterindex = 0; letterindex < accents[key].length; letterindex++) {
78                        reg += accents[key][letterindex];
79                        search_term += '|' + accents[key][letterindex];
80                    }
81
82                    reg += "]";
83                    search_term += ")";
84                    var regexp = new RegExp(search_term, "g");
85                    $('#term').append("--> " + reg + " - " + search_term + " --> ");
86                    s = s.replace(regexp, reg);
87                    $('#term').append(s + "\n");
88                }
89                return s;
90            },
91
92            builder = {
93                'product': function(item, filter, firstOfItsKind) {
94                    var img = $('<img src="' + item.img + '">')
95                        .addClass("jolisearch-image"),
96                    prod = $('<span>').addClass('jolisearch-name').html(filter(item.pname)),
97                    supplier = item.supname ? $('<span>').addClass('jolisearch-pre').html(filter(item.supname)) : undefined,
98                    manuf = item.mname && joli_settings.display_manufacturer ? $('<span>').addClass('jolisearch-pre').html(filter(item.mname)) : undefined,
99                    cat = item.cname && joli_settings.display_category ? $('<span>').addClass('jolisearch-post').html(filter(item.cname)) : undefined,
100                    features = item.feats ? $('<span>
100').addClass('jolisearch-features').html(filter(item.feats)) : undefined,
101                    prc = item.price ? $('<div>').addClass('jolisearch-post-right').html(item.price) : undefined,
102                    dummy = $('<div>').addClass('jolisearch-item product')
103                        .append(img)
104                        .append(
105                            $('<div>').addClass("jolisearch-description " + item.type)
106                                .append(supplier)
107                                .append(manuf)
108                                .append(prod)
109                                .append(features)
110                                .append(cat)
111                        )
112                        .append(prc);
113
114                    return {
115                        data: item,
116                        value: item.pname,
117                        label: dummy.html(),
118                        title: firstOfItsKind
119                    };
120                },
121
122                'manufacturer': function(item, filter, firstOfItsKind) {
123                    var img = $('<img src="' + item.img + '">')
124                        .addClass("jolisearch-image"),
125                    prod = $('<span>').addClass('jolisearch-name').html(filter(item.man_name)),
126                    results = $('<span>').addClass('jolisearch-results').html(filter(item.man_results))
127                    dummy = $('<div>').addClass('jolisearch-item manufacturer')
128                        .append(img)
129                        .append(
130                            $('<div>').addClass("jolisearch-description " + item.type)
131                                .append(prod)
132                                .append(results)
133                                );
134
135                    return {
136                        data: item,
137                        value: item.man_name,
138                        label: dummy.html(),
139                        title: firstOfItsKind
140                    };
141
142                },
143
144                'category': function(item, filter, firstOfItsKind) {
145                    var img = $('<img src="' + item.img + '">')
146                        .addClass("jolisearch-image"),
147                    prod = $('<span>').addClass('jolisearch-name').html(filter(item.cat_name)),
148                    results = $('<span>').addClass('jolisearch-results').html(filter(item.cat_results))
149                    dummy = $('<div>').addClass('jolisearch-item category')
150                        .append(img)
151                        .append(
152                            $('<div>').addClass("jolisearch-description " + item.type)
153                                .append(prod)
154                                .append(results));
155
156                    return {
157                        data: item,
158                        value: item.cat_name,
159                        label: dummy.html(),
160                        title: firstOfItsKind
161                    };
162                },
163
164                'no-results-found': function(item, filter, firstOfItsKind) {
165                    var message = $('<span>').addClass('jolisearch-post').html(firstOfItsKind),
166                        dummy = $('<div>').addClass('jolisearch-item no-results-found')
167                            .append(
168                                $('<div>').addClass("jolisearch-description " + item.type)
169                                    .append(message));
170                    return {
171                        value: '',
172                        data: item,
173                        label: dummy.html()
174                    };
175                },
176
177                'more-results': function(item, filter, firstOfItsKind) {
178                    var message = $('<span>').addClass('jolisearch-post').html(firstOfItsKind),
179                        dummy = $('<div>').addClass('jolisearch-item no-results-found')
180                            .append(
181                                $('<div>').addClass("jolisearch-additionnal " + item.type)
182                                    .append(message));
183                    return {
184                        value: '',
185                        data: item,
186                        label: dummy.html()
187                    };
188                }
189            },
190
191            filterClosure = function(term) {
192                var matcher = new RegExp("(" + matchAccents($.ui.autocomplete.escapeRegex(term.trim())) + ")", "gi");
193                return function(data) {
194                    if (data)
195                        return data.replace(matcher, '<strong>$1</strong>');
196                    return '';
197                }
198            },
199            filterHtmlClosure = function(term) {
200                var matcher = new RegExp("(" + matchAccents($.ui.autocomplete.escapeRegex(term.trim())) + ")", "gi");
201                return function(data) {
202                    if (data) {
203                        var $data = $(data);
204                        $data.find('.product-name, .product-category, .product-manufacturer, .category-name, .manufacturer-name').each(function(idx, elt) {
205                            $(elt).html($(elt).text().replace(matcher, '<strong>$1</strong>'));
206                        });
207                        return $('<div/>').append($data).html();
208                    }
209                    return '';
210                }
211            },
212            sourceClosure = function(that) {
213                return function(request, response) {
214                    var filter = filterClosure(request.term),
215                        filterHtml = filterHtmlClosure(request.term);
216                    $.ajax({
217                        url: that.attr('data-autocomplete'),
218                        dataType: "json",
219                        data: {
220                            s: request.term,
221                            //ajaxSearch: 1,
222                            ajax: true,
223                            id_lang: that.attr('data-lang'),
224                            maxRows: that.attr('data-autocomplete-max') || 10
225                        },
226                        success: function(data) {
227                            var lastType = undefined;
228                            if (data && data.use_rendered_products) {
229                                response([filterHtml(data.rendered_products)]);
230                            } else {
231                                response($.map(data.products, function(item) {
232                                    item.type = item.type.replace(/\_/gi, '-');
233                                    var firstOfItsKind = (lastType !== item.type);
234                                    lastType = item.type;
235                                    if (item.type == "no-results-found") {
236                                        if (that.attr('data-ga-acc') != 0) {
237                                            _gaq.push(["_setAccount", that.attr('data-ga-acc')]);
238                                            _gaq.push(['_trackPageview', '/notfound?search_query=' + request.term + '&fast_search=fs']);
239                                        }
240                                    }
241
242                                    return builder[item.type](item, filter, (firstOfItsKind ? that.attr('data-' + item.type) : false));
243                                }));
244                            }
245                        },
246
247                        error: function(xhr, textStatus, errorThrown) {
248                            console.log("error: " + errorThrown);
249                        }
250                    });
251                }
252            };
253
254        $('document').ready(function() {
255
256            if (typeof(jolisearch) != 'undefined') {
257                joli_settings.display_manufacturer = jolisearch.display_manufacturer;
258                joli_settings.display_category = jolisearch.display_category;
259
260                input = $('.jolisearch').find('input')
261                if(input.length == 0){
262                    input = $('input[name=s]');
263                }
264                if(input.length == 0){
265                    input = $('input[name=search_query]');
266                }
267
268                input
269                    .attr('data-autocomplete-mode', jolisearch['use_autocomplete'])
270                    .attr('data-autocomplete', jolisearch['amb_joli_search_action'])
271                    .attr('data-lang', jolisearch['id_lang'])
272                    .attr('data-manufacturer', jolisearch['l_manufacturers'])
273                    .attr('data-product', jolisearch['l_products'])
274                    .attr('data-category', jolisearch['l_categories'])
275                    .attr('data-minwordlen', jolisearch['minwordlen'])
276                    .attr('data-no-results-found', jolisearch['l_no_results_found'])
277                    .attr('data-more-results', jolisearch['l_more_results'])
278                    .attr('autocomplete', 'off');
279
280                input
281                    .closest('form').attr('action', jolisearch['amb_joli_search_action']);
282
283                $form = input.closest('form');
284                $controller = input.closest('form').find('input[name=controller]');
285                if ($controller.val() != 'jolisearch') {
286                    // remove previous search bar configuration
287                    $controller.remove();
288                    $form.find('input[name=fc]').remove();
289                    $form.closest('form').find('input[name=module]').remove();
290                }
291
292                if (jolisearch['amb_joli_search_action'].indexOf('fc=') >= 0) {
293                    // friendly url Off...
294                    request = jolisearch['amb_joli_search_action'].split('?');
295                    parameters = $.each(request[1].split('&'), function(i, value) {
296                        param = value.split('=');
297                        $form.append($('<input>').attr('type', 'hidden').attr('name', param[0]).val(param[1]));
298                        $form.attr('method', 'post');
299                    });
300                    $form.attr('action', request[0]);
301                }
302
303                $('#search_widget').attr('data-search-controller-url', '');
304
305                if (jolisearch.theme) {
306                    $('body').addClass('jolisearch-theme-' + jolisearch.theme);
307                }
308/*
309                if (typeof $.fn.psBlockSearchAutocomplete !== 'undefined'){
310                    $('#search_widget').find('input[type=text]').psBlockSearchAutocomplete('destroy');
311                }
312                if (typeof prestashop.psBlockSearchAutocomplete !== 'undefined'){
313                    $('#search_widget').find('input[type=text]').psBlockSearchAutocomplete('destroy');
314                }
315*/
316                if ($.fn.version >= '2') {
317                    var searchbarAutocomplete = $('*:data(prestashop-psBlockSearchAutocomplete)');
318                    if (searchbarAutocomplete.length) {
319                        searchbarAutocomplete.off('.psBlockSearchAutocomplete0');
320                        searchbarAutocomplete.off('.psBlockSearchAutocomplete2');
321                        searchbarAutocomplete.psBlockSearchAutocomplete('destroy');
322                    }
323                    // add support for warehouse theme and its child themes
324                    var scAutocomplete = $('input[name=s]:data(sc)');
325                    if (scAutocomplete.length && typeof $.fn.autoComplete !== 'undefined') {
326                        scAutocomplete.autoComplete('destroy');
327                    }
328                } else {
329                    var searchbarAutocomplete = $('input[name=s]');
330                    searchbarAutocomplete.each(function(i, elt) {
331                        var e = $(elt);
332                        if (e.data('prestashop-psBlockSearchAutocomplete') || e.data('prestashopPsBlockSearchAutocomplete')) {
333                            e.off('.psBlockSearchAutocomplete0');
334                            e.off('.psBlockSearchAutocomplete2');
335                            e.psBlockSearchAutocomplete('destroy');
336                        }
337                        // add support for warehouse theme and its child themes
338                        if (e.data('sc') && typeof $.fn.autoComplete !== 'undefined') {
339                            e.autoComplete('destroy');
340                        }
341                    });
342                }
343
344            }
345
346            var responders = $('*:input[type=text][data-autocomplete], *:input[type=search][data-autocomplete]');
347            customizeRender();
348            responders.each(function() {
349                var that = $(this);
350                var mode = that.data('autocomplete-mode');
351                    if (mode == 2 || (mode == 1 && window.matchMedia("(min-width: 767px)").matches)) {
352
353                    var default_options = {
354                            source: sourceClosure(that),
355                            minLength: that.data('minwordlen'),
356                            max: 10,
357                            width: 500,
358                            delay: 500,
359                            selectFirst: false,
360                            scroll: false,
361                            html: true,
362                            customRender: true,
363                            classes: { 'ui-autocomplete' : ('ui-jolisearch' + ' ' + jolisearch['classes']) },
364                            position: (that.data('position') !== undefined ? that.data('position') : jolisearch['position']),
365                            select: function(e, ui) {
366                                if (ui.item.data)
367                                    document.location.href = ui.item.data.link;
368                                else
369                                    return false;
370                            },
371                            search: function(event, ui) {
372                                $('.ui-autocomplete.ui-jolisearch').css('width', 'auto');
373                                var position = (that.data('position') !== undefined ? that.data('position') : jolisearch['position']);
374                                if (window.matchMedia("(max-width: 576px)").matches)
375                                    that.ambSearchAutocomplete("option", "position", {my: 'center top', at: 'center bottom'});
376                                else
377                                    that.ambSearchAutocomplete("option", "position", position);
378
379                                return true;
380                            },
381                            focus: function(event, ui) {
382                                return false;
383                            },
384                            open: function(event, ui) {
385                                $('body').addClass('jolisearch-on');
386                            },
387                            close: function(event, ui) {
388                                $('body').removeClass('jolisearch-on');
389                            }
390                    };
391
392                    var autocomplete_options = default_options;
393                    if (jolisearch && jolisearch.autocomplete_target) {
394                        autocomplete_options = $.extend(autocomplete_options, {
395                            appendTo: jolisearch.autocomplete_target
396                        })
397                    }
398
399                    that.ambSearchAutocomplete(autocomplete_options);
400
401                    that.on('focus', function() {
402                        that.ambSearchAutocomplete('search', that.val())
403                    });
404
405                }
406            });
407        })
408    })(compat_jQ);
409})(jQuery);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.