985827445066df4404a186399ded38b048ace351
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / FormEngineSuggest.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/FormEngineSuggest
16 * Functionality to load suggest functionality
17 */
18 define(['jquery', 'jquery/autocomplete'], function ($) {
19 var initialize = function($searchField) {
20 var $containerElement = $searchField.closest('.t3-form-suggest-container');
21 var table = $searchField.data('table'),
22 field = $searchField.data('field'),
23 uid = $searchField.data('uid'),
24 pid = $searchField.data('pid'),
25 newRecordRow = $searchField.data('recorddata'),
26 minimumCharacters = $searchField.data('minchars'),
27 url = TYPO3.settings.ajaxUrls['record_suggest'],
28 params = {
29 'table': table,
30 'field': field,
31 'uid': uid,
32 'pid': pid,
33 'newRecordRow': newRecordRow
34 };
35
36 $searchField.autocomplete({
37 // ajax options
38 serviceUrl: url,
39 params: params,
40 type: 'POST',
41 paramName: 'value',
42 dataType: 'json',
43 minChars: minimumCharacters,
44 groupBy: 'typeLabel',
45 containerClass: 'autocomplete-results',
46 appendTo: $containerElement,
47 forceFixPosition: false,
48 preserveInput: true,
49 showNoSuggestionNotice: true,
50 noSuggestionNotice: '<div class="autocomplete-info">No results</div>',
51 minLength: minimumCharacters,
52 // put the AJAX results in the right format
53 transformResult: function(response) {
54 return {
55 suggestions: $.map(response, function(dataItem) {
56 return { value: dataItem.text, data: dataItem };
57 })
58 };
59 },
60 // Rendering of each item
61 formatResult: function(suggestion, value) {
62 return $('<div>').append(
63 $('<a class="autocomplete-suggestion-link" href="#">' +
64 suggestion.data.sprite + suggestion.data.text +
65 '</a></div>').attr({
66 'data-label': suggestion.data.label,
67 'data-table': suggestion.data.table,
68 'data-uid': suggestion.data.uid
69 })).html();
70 },
71 onSearchComplete: function() {
72 $containerElement.addClass('open');
73 },
74 beforeRender: function(container) {
75 // Unset height, width and z-index again, should be fixed by the plugin at a later point
76 container.attr('style', '');
77 $containerElement.addClass('open');
78 },
79 onHide: function() {
80 $containerElement.removeClass('open');
81 }
82 });
83
84 // set up the events
85 $containerElement.on('click', '.autocomplete-suggestion-link', function(evt) {
86 evt.preventDefault();
87 var insertData = '';
88 if ($searchField.data('fieldtype') == 'select') {
89 insertData = $(this).data('uid');
90 } else {
91 insertData = $(this).data('table') + '_' + $(this).data('uid');
92 }
93
94 var formEl = $searchField.data('fieldname');
95 var labelEl = $('<div>').html($(this).data('label'));
96 var label = labelEl.text();
97 var title = labelEl.find('span').attr('title') || label;
98 setFormValueFromBrowseWin(formEl, insertData, label, title);
99 TBE_EDITOR.fieldChanged(table, uid, field, formEl);
100 });
101 };
102
103 /**
104 * Return a function that gets DOM elements that are checked if suggest is already initialized
105 * @exports TYPO3/CMS/Backend/FormEngineSuggest
106 */
107 return function(selectorElements) {
108 $(selectorElements).each(function(key, el) {
109 if (!$(el).data('t3-suggest-initialized')) {
110 initialize($(el));
111 $(el).data('t3-suggest-initialized', true);
112 }
113 });
114 };
115 });