[BUGFIX] Continue searching after empty result
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / LiveSearch.js
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Module: TYPO3/CMS/Backend/LiveSearch
16 * Global search to deal with everything in the backend that is search-related
17 * @exports TYPO3/CMS/Backend/LiveSearch
18 */
19 define(['jquery', 'jquery/autocomplete', 'TYPO3/CMS/Backend/jquery.clearable'], function ($) {
20 'use strict';
21
22 var containerSelector = '#typo3-cms-backend-backend-toolbaritems-livesearchtoolbaritem';
23 var searchFieldSelector = '.t3js-topbar-navigation-search-field';
24 var formSelector = '.t3js-topbar-navigation-search';
25 var url = TYPO3.settings.ajaxUrls['livesearch'];
26 var category = '';
27
28 $(function() {
29 $(searchFieldSelector).autocomplete({
30 // ajax options
31 serviceUrl: url,
32 paramName: 'q',
33 dataType: 'json',
34 minChars: 2,
35 groupBy: 'typeLabel',
36 containerClass: 'dropdown-list',
37 appendTo: containerSelector + ' .dropdown-menu',
38 forceFixPosition: false,
39 preserveInput: true,
40 showNoSuggestionNotice: true,
41 triggerSelectOnValidInput: false,
42 preventBadQueries: false,
43 noSuggestionNotice: '<div class="dropdown-info">' + TYPO3.LLL.liveSearch.listEmptyText + '</div>'
44 + '<div class="search-list-help-content"><strong>' + TYPO3.LLL.liveSearch.helpTitle + '</strong>'
45 + '<p>' + TYPO3.LLL.liveSearch.helpDescription + '<br>' + TYPO3.LLL.liveSearch.helpDescriptionPages + '</p>'
46 + '</div>',
47 // put the AJAX results in the right format
48 transformResult: function(response) {
49 return {
50 suggestions: $.map(response, function(dataItem) {
51 return { value: dataItem.title, data: dataItem };
52 })
53 };
54 },
55 // Format group is currently modified inside autocomplete to be allowed to be configurable
56 formatGroup: function(suggestion, value, i) {
57 var currentCategory = suggestion.data['typeLabel'];
58 if (category === currentCategory) {
59 return '';
60 }
61 category = currentCategory;
62 var html = '';
63 // add a divider if it's not the first group
64 if (i > 0) {
65 html = '<div class="divider"></div>';
66 }
67 return html + '<div class="dropdown-header">' + category + '</div>';
68 },
69 // Rendering of each item
70 formatResult: function(suggestion, value) {
71 return '<a class="dropdown-list-link" href="#" data-pageid="' + suggestion.data.pageId + '" data-target="' + suggestion.data.editLink + '">' +
72 suggestion.data.iconHTML + ' ' + suggestion.data.title +
73 '</a>';
74 },
75 onSearchComplete: function() {
76 $(containerSelector).addClass('open');
77 },
78 beforeRender: function(container) {
79 // Unset height, width and z-index again, should be fixed by the plugin at a later point
80 container.attr('style', '').append('<div class="divider"></div><div>' +
81 '<a href="#" class="btn btn-primary pull-right t3js-live-search-show-all">' +
82 TYPO3.LLL.liveSearch.showAllResults +
83 '</a>' +
84 '</div>');
85 $(containerSelector).addClass('open');
86 },
87 onHide: function() {
88 $(containerSelector).removeClass('open');
89 }
90 });
91
92 // set up the events
93 $(containerSelector).on('click', '.t3js-live-search-show-all', function(evt) {
94 evt.preventDefault();
95 TYPO3.ModuleMenu.App.showModule('web_list', 'id=0&search_levels=4&search_field=' + encodeURIComponent($(searchFieldSelector).val()));
96 });
97 $(containerSelector).on('click', '.dropdown-list-link', function(evt) {
98 evt.preventDefault();
99 jump($(this).data('target'), 'web_list', 'web', $(this).data('pageid'));
100 });
101
102 $(searchFieldSelector).clearable({
103 onClear: function() {
104 $(containerSelector).removeClass('open');
105 }
106 });
107
108 // Prevent submitting the search form
109 $(formSelector).submit(function(evt) {
110 evt.preventDefault();
111 });
112 });
113
114 });