9b4dd2bd41a55b30e308faf9cf659a265a193b17
[TYPO3CMS/Extensions/powermailCond.git] / Resources / Public / Js / PowermailCond.js
1 jQuery(document).ready(function() {
2 base = getBaseUrl();
3 if ($('form.powermail_form').length > 0) { // only if the powermail form is on the page (not for confirmation page)
4 checkConditions(0); // check if something should be changed
5 }
6
7 // save values via ajax to session
8 $('.powermail_input, .powermail_textarea, .powermail_select, .powermail_radio, .powermail_checkbox').change(function() {
9 $this = $(this); // caching
10 var pid = 5; // TODO
11 var url = base + '/index.php';
12 var timestamp = Number(new Date()); // timestamp is needed for a internet explorer workarround (always change a parameter)
13 var value = $this.val(); // current value
14 var uid = $this.closest('.powermail_fieldwrap').attr('id').substr(20); // current field uid (without "uid")
15 var name = $this.attr('name');
16 var params = 'eID=' + 'powermailcond_saveToSession' + '&id=' + pid + '&tx_powermailcond_pi1[uid]=' + uid + '&tx_powermailcond_pi1[value]=' + value + '&ts=' + timestamp;
17
18 /*
19
20 $.ajax({
21 type: 'GET', // type
22 url: url, // send to this url
23 data: params, // add params
24 cache: false, // disable cache (for ie)
25 success: function(data) { // return values
26 if (data != '') { // if there is a response
27 //alert(data); // alert the response
28 $('form.tx_powermail_pi1_form').append('Error in powermail_cond.js in change function:' + data);
29 }
30 checkConditions(uid); // check if something should be changed
31 }
32 });
33 */
34 });
35 });
36
37 /**
38 * Main function to check conditions and do something (if necessary)
39 *
40 * @param integer uid: Field uid (if available)
41 * @return void
42 */
43 function checkConditions(uid) {
44 var url = base + '/index.php';
45 var params = '';
46 if (uid > 0) {
47 params += '&tx_powermailcond_pi1[uid]=' + uid;
48 }
49 $.ajax({
50 type: 'GET', // type
51 url: url, // send to this url
52 data: 'eID=' + 'powermailcond_getFieldStatus' + params + '&tx_powermailcond_pi1[formUid]=' + $('input[name="tx_powermail_pi1[form]"]').val(), // add params
53 cache: false, // disable cache (for ie)
54 beforeSend: function() {
55 document.body.style.cursor = 'progress'; // change cursor to busy
56 },
57 complete: function() {
58 document.body.style.cursor = 'auto'; // normal cursor
59 },
60 success: function(data) { // return values
61 if (data != 'nochange') {
62 $('.powermail_fieldwrap select option').show(); // show all options at the beginning
63 $('.powermail_fieldwrap select option').removeAttr('disabled'); // enable all options at the beginning
64 if (data != '') { // if there is a response
65 if (data.length < 1000) { // stop if wrong result (maybe complete t3 page)
66 doAction(data); // hide all given fields
67 }
68 } else { // if there is no response
69 showAll(); // show all fields and fieldsets at the beginning
70 }
71 }
72 },
73 error: function() {
74 $('form.powermail_form').append('Error in PowermailCond.js in checkCondtions function by opening the given url');
75 }
76 });
77 }
78
79 /**
80 * Do some actions (hide and/or filter)
81 *
82 * @param string list: commaseparated list with uids (1,2,3)
83 * @return void
84 */
85 function doAction(list) {
86 showAll(); // show all fields and fieldsets at the beginning
87
88 var uid = list.split(',');
89 if (uid.length < 1) {
90 return false; // stop process
91 }
92 for (var i=0; i < uid.length; i++) { // one loop for every affected field
93 if (uid[i].indexOf('fieldset:') != '-1') { // fieldset part
94 hideFieldset(uid[i]);
95 } else if (uid[i].indexOf('filter:') != '-1') { // filter part
96 filterSelection(uid[i]);
97 } else { // fields part
98 hideField(uid[i]);
99 }
100 }
101 }
102
103 /**
104 * Hide a field and clear its value
105 *
106 * @param integer uid: uid of the element
107 * @return void
108 */
109 function hideField(uid) {
110 $('.powermail_fieldwrap_' + uid).hide(); // hide current field
111 if ($('.powermail_fieldwrap_' + uid + ' .powermail_field').val() != '') { // only if value is not yet empty
112 clearValue('.powermail_fieldwrap_' + uid + ' .powermail_field'); // clear value of current field
113 clearSession(uid); // clear value of current field
114 }
115 }
116
117 /**
118 * Hide some fields, which are bundled in a fieldset and clear there value
119 *
120 * @param string string: mix of uid and values (fieldset:5:12;13;14)
121 * @return void
122 */
123 function hideFieldset(string) {
124 var params = string.split(':'); // filter / uid / values
125 var values = params[2].split(';'); // value1 / value2 / value3
126 $('powermail_fieldset_' + params[1]).hide(); // hide current fieldset
127 for (var k=0; k < values.length; k++) { // one loop for every field inside the fieldset
128 clearValue('.powermail_fieldwrap_' + values[k] + ' .powermail_field'); // clear value of current field
129 }
130 }
131
132 /**
133 * Hide some fields and clear there value
134 *
135 * @param string string: mix of uid and values (filter:123:Value1;Value2;Value3)
136 * @return void
137 */
138 function filterSelection(string) {
139 var params = string.split(':'); // filter / uid / values
140 var values = params[2].split(';'); // value1 / value2 / value3
141 $('.powermail_fieldwrap_' + params[1] + ' .powermail_field > option').hide().attr('disabled', 'disabled'); // disable all options
142
143 for (var j=0; j < values.length; j++) { // one loop for every option in select field
144 $('.powermail_fieldwrap_' + params[1] + ' .powermail_field > option:contains(' + values[j] + ')').show().removeAttr('disabled'); // show this option
145 }
146
147 var valueSelected = $('.powermail_fieldwrap_' + params[1] + ' .powermail_field > option:selected').val(); // give me the value of the selected option
148 if (params[2].indexOf(valueSelected) == '-1') { // if current selected value is one of the not allowed options
149 $('.powermail_fieldwrap_' + params[1] + ' .powermail_field').get(0).selectedIndex = 0; // remove selection (because the selected option is not allowed)
150 }
151 }
152
153 /**
154 * Show all fields and fieldsets
155 *
156 * @return void
157 */
158 function showAll() {
159 $('.powermail_fieldwrap, .powermail_fieldset').show(); // show all fields and fieldsets
160 }
161
162 /**
163 * Clear value of an inputfield, set selectedIndex to 0 for selection and so on
164 *
165 * @param string selection: selection for jQuery (e.g. input.powermail)
166 * @return void
167 */
168 function clearValue(selection) {
169 if ($(selection).attr('type') == 'radio' || $(selection).attr('type') == 'checkbox') {
170 $(selection).attr('checked', false);
171 } else {
172 $(selection).val('');
173 }
174 }
175
176 /**
177 * Read BaseUrl
178 *
179 * @return string BaseUrl from Tag in DOM
180 */
181 function getBaseUrl() {
182 var base = $('base').attr('href');
183 if (!base || base == undefined) {
184 base = '';
185 }
186 return base;
187 }
188
189 /**
190 * Clear session of a uid
191 * TODO
192 *
193 * @param integer uid: uid of the element
194 * @return void
195 */
196 function clearSession(uid) {
197 var pid = 5;
198 var url = base + '/index.php';
199 var timestamp = Number(new Date()); // timestamp is needed for a internet explorer workarround (always change a parameter)
200 var params = 'eID=' + 'powermailcond_saveToSession' + '&id=' + pid + '&tx_powermailcond_pi1[uid]=' + uid + '&tx_powermailcond_pi1[value]=&ts=' + timestamp;
201
202 $.ajax({
203 type: 'GET', // type
204 url: url, // send to this url
205 data: params, // add params
206 cache: false, // disable cache (for ie)
207 success: function(data) { // return values
208 if (data != '') { // if there is a response
209 //alert(data); // alert the response
210 $('form.tx_powermail_pi1_form').append('Error in powermail_cond.js in clearSession function:' + data);
211 }
212 checkConditions(uid); // check if something should be changed
213 }
214 });
215 };