0f59806c8344642ff5d4e0b998f4dfaabb7c58f8
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / jsfunc.tceforms_suggest.js
1 /***************************************************************
2 * AJAX selectors for TCEforms
3 *
4 * Copyright notice
5 *
6 * (c) 2007-2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25
26 /**
27 * Class for JS handling of suggest fields in TCEforms.
28 *
29 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
30 * @author Benni Mack <benni@typo3.org>
31 */
32 if (!TCEForms) {
33 var TCEForms = {};
34 }
35
36 TCEForms.Suggest = Class.create({
37 objectId: '',
38 suggestField: '',
39 suggestResultList: '',
40 minimumCharacters: 2,
41 defaultValue: '',
42
43 /**
44 * Wrapper for script.aculo.us' Autocompleter functionality: Assigns a new autocompletion object to
45 * the input field of a given Suggest selector.
46 *
47 * @param string objectId The ID of the object to assign the completer to
48 * @param string table The table of the record which is currently edited
49 * @param string field The field which is currently edited
50 * @param integer uid The uid of the record which is currently edited
51 * @param integer pid The pid of the record which is currently edited
52 * @param integer minimumCharacters the minimum characaters that is need to trigger the initial search
53 */
54 initialize: function(objectId, table, field, uid, pid, minimumCharacters) {
55 var PATH_typo3 = top.TS.PATH_typo3 || window.opener.top.TS.PATH_typo3;
56 this.objectId = objectId;
57 this.suggestField = objectId + 'Suggest';
58 this.suggestResultList = objectId + 'SuggestChoices';
59
60 new Ajax.Autocompleter(this.suggestField, this.suggestResultList, PATH_typo3 + 'ajax.php', {
61 paramName: 'value',
62 minChars: (minimumCharacters ? minimumCharacters : this.minimumCharacters),
63 updateElement: this.addElementToList.bind(this),
64 parameters: 'ajaxID=t3lib_TCEforms_suggest::searchRecord&table=' + table + '&field=' + field + '&uid=' + uid + '&pid=' + pid,
65 indicator: objectId + 'SuggestIndicator'
66 }
67 );
68
69 $(this.suggestField).observe('focus', this.checkDefaultValue.bind(this));
70 $(this.suggestField).observe('keydown', this.checkDefaultValue.bind(this));
71 },
72
73 /**
74 * check for default value of the input field and set it to empty once somebody wants to type something in
75 */
76 checkDefaultValue: function() {
77 if ($(this.suggestField).value == this.defaultValue) {
78 $(this.suggestField).value = '';
79 }
80 },
81
82 /**
83 * Adds an element to the list of items in a group selector.
84 *
85 * @param object item The item to add to the list (usually an li element)
86 * @return void
87 */
88 addElementToList: function(item) {
89 if (item.className.indexOf('noresults') == -1) {
90 var arr = item.id.split('-');
91 var ins_table = arr[0];
92 var ins_uid = arr[1];
93 var rec_table = arr[2];
94 var rec_uid = arr[3];
95 var rec_field = arr[4];
96
97 var formEl = 'data[' + rec_table + '][' + rec_uid + '][' + rec_field + ']';
98 var suggestLabelNode = Element.select(this.escapeObjectId(item.id), '.suggest-label')[0];
99 var label = (suggestLabelNode.textContent ? suggestLabelNode.textContent : suggestLabelNode.innerText)
100 setFormValueFromBrowseWin(formEl, ins_table + '_' + ins_uid, label);
101 TBE_EDITOR.fieldChanged(rec_table, rec_uid, rec_field, formEl);
102
103 $(this.suggestField).value = this.defaultValue;
104 }
105 },
106
107 /**
108 * Escapes object identifiers of e.g. Flexform CSS IDs
109 *
110 * @param string objectId
111 * @return string
112 */
113 escapeObjectId: function(objectId) {
114 var escapedObjectId;
115 escapedObjectId = objectId.replace(/:/g, '\\:');
116 escapedObjectId = objectId.replace(/\./g, '\\.');
117 escapedObjectId = objectId.replace(/\[/g, '\\[');
118 escapedObjectId = objectId.replace(/\]/g, '\\]');
119 return escapedObjectId;
120 }
121 });