20b23963fbea8d9d17955b1b1c1f157c6e73e59a
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / jsfunc.tceforms_suggest.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 * Class for JS handling of suggest fields in TCEforms.
16 *
17 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
18 * @author Benni Mack <benni@typo3.org>
19 */
20 if (!TCEForms) {
21 var TCEForms = {};
22 }
23
24 TCEForms.Suggest = Class.create({
25 objectId: '',
26 suggestField: '',
27 suggestResultList: '',
28 minimumCharacters: 2,
29 defaultValue: '',
30 fieldType: '',
31
32 /**
33 * Wrapper for script.aculo.us' Autocompleter functionality: Assigns a new autocompletion object to
34 * the input field of a given Suggest selector.
35 *
36 * @param string objectId The ID of the object to assign the completer to
37 * @param string table The table of the record which is currently edited
38 * @param string field The field which is currently edited
39 * @param integer uid The uid of the record which is currently edited
40 * @param integer pid The pid of the record which is currently edited
41 * @param integer minimumCharacters the minimum characters that is need to trigger the initial search
42 * @param string fieldType The TCA type of the field (e.g. group, select, ...)
43 */
44 initialize: function(objectId, table, field, uid, pid, minimumCharacters, fieldType) {
45 var PATH_typo3 = top.TS.PATH_typo3 || window.opener.top.TS.PATH_typo3;
46 this.objectId = objectId;
47 this.suggestField = objectId + 'Suggest';
48 this.suggestResultList = objectId + 'SuggestChoices';
49 this.fieldType = fieldType;
50
51 new Ajax.Autocompleter(this.suggestField, this.suggestResultList, PATH_typo3 + TYPO3.settings.ajaxUrls['t3lib_TCEforms_suggest::searchRecord'], {
52 paramName: 'value',
53 minChars: (minimumCharacters ? minimumCharacters : this.minimumCharacters),
54 updateElement: this.addElementToList.bind(this),
55 parameters: 'table=' + table + '&field=' + field + '&uid=' + uid + '&pid=' + pid,
56 indicator: objectId + 'SuggestIndicator'
57 }
58 );
59
60 $(this.suggestField).observe('focus', this.checkDefaultValue.bind(this));
61 $(this.suggestField).observe('keydown', this.checkDefaultValue.bind(this));
62 },
63
64 /**
65 * check for default value of the input field and set it to empty once somebody wants to type something in
66 */
67 checkDefaultValue: function() {
68 if ($(this.suggestField).value == this.defaultValue) {
69 $(this.suggestField).value = '';
70 }
71 },
72
73 /**
74 * Adds an element to the list of items in a group selector.
75 *
76 * @param object item The item to add to the list (usually an li element)
77 * @return void
78 */
79 addElementToList: function(item) {
80 if (item.className.indexOf('noresults') == -1) {
81 var arr = item.id.split('-');
82 var ins_table = arr[0];
83 var ins_uid = arr[1];
84 var ins_uid_string = (this.fieldType == 'select') ? ins_uid : (ins_table + '_' + ins_uid);
85 var rec_table = arr[2];
86 var rec_uid = arr[3];
87 var rec_field = arr[4];
88
89 var formEl = this.objectId;
90
91 var suggestLabelNode = Element.select(item, '.suggest-label')[0];
92 var label = suggestLabelNode.textContent ? suggestLabelNode.textContent : suggestLabelNode.innerText;
93 var suggestLabelTitleNode = Element.select(suggestLabelNode, '[title]')[0];
94 var title = suggestLabelTitleNode ? suggestLabelTitleNode.readAttribute('title') : '';
95
96 setFormValueFromBrowseWin(formEl, ins_uid_string, label, title);
97 TBE_EDITOR.fieldChanged(rec_table, rec_uid, rec_field, formEl);
98
99 $(this.suggestField).value = this.defaultValue;
100 }
101 }
102 });