7f81a37fd0d6dd0ba5673712bd5e4a6d92481d91
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Resources / Public / JavaScript / Backend / FormEditor / ModalsComponent.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/Form/Backend/FormEditor/ModalsComponent
16 */
17 define(['jquery',
18 'TYPO3/CMS/Form/Backend/FormEditor/Helper',
19 'TYPO3/CMS/Backend/Modal',
20 'TYPO3/CMS/Backend/Severity',
21 'TYPO3/CMS/Backend/Icons'
22 ], function($, Helper, Modal, Severity, Icons) {
23 'use strict';
24
25 return (function($, Helper, Modal, Severity, Icons) {
26
27 /**
28 * @private
29 *
30 * @var object
31 */
32 var _configuration = null;
33
34 /**
35 * @private
36 *
37 * @var object
38 */
39 var _defaultConfiguration = {
40 domElementClassNames: {
41 buttonDefault: 'btn-default',
42 buttonInfo: 'btn-info',
43 buttonWarning: 'btn-warning'
44 },
45 domElementDataAttributeNames: {
46 elementType: 'element-type'
47 },
48 domElementDataAttributeValues: {
49 rowItem: 'rowItem',
50 rowLink: 'rowLink',
51 rowsContainer: 'rowsContainer',
52 templateInsertElements: 'Modal-InsertElements',
53 templateInsertPages: 'Modal-InsertPages',
54 templateValidationErrors: 'Modal-ValidationErrors'
55 }
56 };
57
58 /**
59 * @private
60 *
61 * @var object
62 */
63 var _formEditorApp = null;
64
65 /* *************************************************************
66 * Private Methodes
67 * ************************************************************/
68
69 /**
70 * @private
71 *
72 * @return void
73 * @throws 1478268638
74 */
75 function _helperSetup() {
76 assert('function' === $.type(Helper.bootstrap),
77 'The view model helper does not implement the method "bootstrap"',
78 1478268638
79 );
80 Helper.bootstrap(getFormEditorApp());
81 };
82
83 /**
84 * @private
85 *
86 * @return object
87 */
88 function getFormEditorApp() {
89 return _formEditorApp;
90 };
91
92 /**
93 * @public
94 *
95 * @param object
96 * @return object
97 */
98 function getHelper(configuration) {
99 if (getUtility().isUndefinedOrNull(configuration)) {
100 return Helper.setConfiguration(_configuration);
101 }
102 return Helper.setConfiguration(configuration);
103 };
104
105 /**
106 * @private
107 *
108 * @return object
109 */
110 function getUtility() {
111 return getFormEditorApp().getUtility();
112 };
113
114 /**
115 * @private
116 *
117 * @param mixed test
118 * @param string message
119 * @param int messageCode
120 * @return void
121 */
122 function assert(test, message, messageCode) {
123 return getFormEditorApp().assert(test, message, messageCode);
124 };
125
126 /**
127 * @private
128 *
129 * @return object
130 */
131 function getRootFormElement() {
132 return getFormEditorApp().getRootFormElement();
133 };
134
135 /**
136 * @private
137 *
138 * @return object
139 */
140 function getPublisherSubscriber() {
141 return getFormEditorApp().getPublisherSubscriber();
142 };
143
144 /**
145 * @private
146 *
147 * @param object
148 * @param string
149 * @return mixed
150 */
151 function getFormElementDefinition(formElement, formElementDefinitionKey) {
152 return getFormEditorApp().getFormElementDefinition(formElement, formElementDefinitionKey);
153 };
154
155 /**
156 * @public
157 *
158 * @param string publisherTopicName
159 * @param object publisherTopicArguments
160 * @return void
161 * @throws 1478889044
162 * @throws 1478889049
163 */
164 function _showRemoveElementModal(publisherTopicName, publisherTopicArguments) {
165 var modalButtons = [];
166
167 assert(
168 getUtility().isNonEmptyString(publisherTopicName),
169 'Invalid parameter "publisherTopicName"',
170 1478889049
171 );
172 assert(
173 'array' === $.type(publisherTopicArguments),
174 'Invalid parameter "formElement"',
175 1478889044
176 );
177
178 modalButtons.push({
179 text: getFormElementDefinition(getRootFormElement(), 'modalRemoveElementCancleButton'),
180 active: true,
181 btnClass: getHelper().getDomElementClassName('buttonDefault'),
182 name: 'cancel',
183 trigger: function () {
184 Modal.currentModal.trigger('modal-dismiss');
185 }
186 });
187
188 modalButtons.push({
189 text: getFormElementDefinition(getRootFormElement(), 'modalRemoveElementConfirmButton'),
190 active: true,
191 btnClass: getHelper().getDomElementClassName('buttonWarning'),
192 name: 'confirm',
193 trigger: function () {
194 getPublisherSubscriber().publish(publisherTopicName, publisherTopicArguments);
195 Modal.currentModal.trigger('modal-dismiss');
196 }
197 });
198
199 Modal.show(
200 getFormElementDefinition(getRootFormElement(), 'modalRemoveElementDialogTitle'),
201 getFormElementDefinition(getRootFormElement(), 'modalRemoveElementDialogMessage'),
202 Severity.warning,
203 modalButtons
204 );
205 };
206
207 /**
208 * @private
209 *
210 * @param object modalContent
211 * @param string publisherTopicName
212 * @return void
213 * @publish mixed
214 * @throws 1478910954
215 */
216 function _insertElementsModalSetup(modalContent, publisherTopicName) {
217 assert(
218 getUtility().isNonEmptyString(publisherTopicName),
219 'Invalid parameter "publisherTopicName"',
220 1478910954
221 );
222
223 $('a', modalContent).on("click", function(e) {
224 getPublisherSubscriber().publish(publisherTopicName, [$(this).data(getHelper().getDomElementDataAttribute('elementType'))]);
225 $('a', modalContent).off();
226 Modal.currentModal.trigger('modal-dismiss');
227 });
228 };
229
230 /**
231 * @private
232 *
233 * @param object modalContent
234 * @param object validationResults
235 * @return void
236 * @publish view/modal/validationErrors/element/clicked
237 * @throws 1479161268
238 */
239 function _validationErrorsModalSetup(modalContent, validationResults) {
240 var formElement, newRowItem, rowItemTemplate;
241
242 assert(
243 'array' === $.type(validationResults),
244 'Invalid parameter "validationResults"',
245 1479161268
246 );
247
248 rowItemTemplate = $(
249 getHelper().getDomElementDataIdentifierSelector('rowItem'),
250 modalContent
251 ).clone();
252
253 $(getHelper().getDomElementDataIdentifierSelector('rowItem'), modalContent).remove();
254
255 for (var i = 0, len = validationResults.length; i < len; ++i) {
256 var hasError = false, validationElement;
257 for (var j = 0, len2 = validationResults[i]['validationResults'].length; j < len2; ++j) {
258 if (
259 validationResults[i]['validationResults'][j]['validationResults']
260 && validationResults[i]['validationResults'][j]['validationResults'].length > 0
261 ) {
262 hasError = true;
263 break;
264 }
265 }
266
267 if (hasError) {
268 formElement = getFormEditorApp()
269 .getFormElementByIdentifierPath(validationResults[i]['formElementIdentifierPath']);
270 newRowItem = rowItemTemplate.clone();
271 $(getHelper().getDomElementDataIdentifierSelector('rowLink'), newRowItem)
272 .attr(
273 getHelper().getDomElementDataAttribute('elementIdentifier'),
274 validationResults[i]['formElementIdentifierPath']
275 )
276 .html(_buildTitleByFormElement(formElement));
277 $(getHelper().getDomElementDataIdentifierSelector('rowsContainer'), modalContent)
278 .append(newRowItem);
279 }
280 }
281
282 $('a', modalContent).on("click", function(e) {
283 getPublisherSubscriber().publish('view/modal/validationErrors/element/clicked', [
284 $(this).attr(getHelper().getDomElementDataAttribute('elementIdentifier'))
285 ]);
286 $('a', modalContent).off();
287 Modal.currentModal.trigger('modal-dismiss');
288 });
289 };
290
291 /**
292 * @private
293 *
294 * @param object
295 * @return object
296 * @throws 1479162557
297 */
298 function _buildTitleByFormElement(formElement) {
299 var label;
300 assert('object' === $.type(formElement), 'Invalid parameter "formElement"', 1479162557);
301
302 return $('<span></span>').text((formElement.get('label')
303 ? formElement.get('label')
304 : formElement.get('identifier')));
305 };
306
307 /* *************************************************************
308 * Public Methodes
309 * ************************************************************/
310
311 /**
312 * @public
313 *
314 * @param object formElement
315 * @return void
316 * @publish view/modal/removeFormElement/perform
317 */
318 function showRemoveFormElementModal(formElement) {
319 _showRemoveElementModal('view/modal/removeFormElement/perform', [formElement]);
320 };
321
322 /**
323 * @public
324 *
325 * @param string collectionElementIdentifier
326 * @param string collectionName
327 * @param object formElement
328 * @return void
329 * @publish view/modal/removeCollectionElement/perform
330 * @throws 1478894420
331 * @throws 1478894421
332 */
333 function showRemoveCollectionElementModal(collectionElementIdentifier, collectionName, formElement) {
334 assert(
335 getUtility().isNonEmptyString(collectionElementIdentifier),
336 'Invalid parameter "collectionElementIdentifier"',
337 1478894420
338 );
339 assert(
340 getUtility().isNonEmptyString(collectionName),
341 'Invalid parameter "collectionName"',
342 1478894421
343 );
344
345 _showRemoveElementModal('view/modal/removeCollectionElement/perform', [collectionElementIdentifier, collectionName, formElement]);
346 };
347
348 /**
349 * @public
350 *
351 * @return void
352 * @publish view/modal/close/perform
353 */
354 function showCloseConfirmationModal() {
355 var modalButtons = [];
356
357 modalButtons.push({
358 text: getFormElementDefinition(getRootFormElement(), 'modalCloseCancleButton'),
359 active: true,
360 btnClass: getHelper().getDomElementClassName('buttonDefault'),
361 name: 'cancel',
362 trigger: function () {
363 Modal.currentModal.trigger('modal-dismiss');
364 }
365 });
366
367 modalButtons.push({
368 text: getFormElementDefinition(getRootFormElement(), 'modalCloseConfirmButton'),
369 active: true,
370 btnClass: getHelper().getDomElementClassName('buttonWarning'),
371 name: 'confirm',
372 trigger: function () {
373 getPublisherSubscriber().publish('view/modal/close/perform', []);
374 Modal.currentModal.trigger('modal-dismiss');
375 }
376 });
377
378 Modal.show(
379 getFormElementDefinition(getRootFormElement(), 'modalCloseDialogTitle'),
380 getFormElementDefinition(getRootFormElement(), 'modalCloseDialogMessage'),
381 Severity.warning,
382 modalButtons
383 );
384 };
385
386 /**
387 * @public
388 *
389 * @param string
390 * @param string
391 * @return void
392 */
393 function showInsertElementsModal(publisherTopicName) {
394 var html, template;
395
396 template = getHelper().getTemplate('templateInsertElements');
397 if (template.length > 0) {
398 html = $(template.html());
399 _insertElementsModalSetup(html, publisherTopicName);
400
401 Modal.show(
402 getFormElementDefinition(getRootFormElement(), 'modalInsertElementsDialogTitle'),
403 $(html),
404 Severity.info
405 );
406 }
407 };
408
409 /**
410 * @public
411 *
412 * @param string
413 * @param string
414 * @return void
415 */
416 function showInsertPagesModal(publisherTopicName) {
417 var html, template;
418
419 template = getHelper().getTemplate('templateInsertPages');
420 if (template.length > 0) {
421 html = $(template.html());
422 _insertElementsModalSetup(html, publisherTopicName);
423
424 Modal.show(
425 getFormElementDefinition(getRootFormElement(), 'modalInsertPagesDialogTitle'),
426 $(html),
427 Severity.info
428 );
429 }
430 };
431
432 /**
433 * @public
434 *
435 * @param object
436 * @return void
437 */
438 function showValidationErrorsModal(validationResults) {
439 var html, template, modalButtons = [];
440
441 modalButtons.push({
442 text: getFormElementDefinition(getRootFormElement(), 'modalValidationErrorsConfirmButton'),
443 active: true,
444 btnClass: getHelper().getDomElementClassName('buttonDefault'),
445 name: 'confirm',
446 trigger: function () {
447 Modal.currentModal.trigger('modal-dismiss');
448 }
449 });
450
451 template = getHelper().getTemplate('templateValidationErrors');
452 if (template.length > 0) {
453 html = $(template.html()).clone();
454 _validationErrorsModalSetup(html, validationResults);
455
456 Modal.show(
457 getFormElementDefinition(getRootFormElement(), 'modalValidationErrorsDialogTitle'),
458 html,
459 Severity.error,
460 modalButtons
461 );
462 }
463 }
464
465 /**
466 * @public
467 *
468 * @param object
469 * @param object
470 * @return this
471 */
472 function bootstrap(formEditorApp, configuration) {
473 _formEditorApp = formEditorApp;
474 _configuration = $.extend(true, _defaultConfiguration, configuration || {});
475 _helperSetup();
476 return this;
477 };
478
479 /**
480 * Publish the public methods.
481 * Implements the "Revealing Module Pattern".
482 */
483 return {
484 bootstrap: bootstrap,
485 showCloseConfirmationModal: showCloseConfirmationModal,
486 showInsertElementsModal: showInsertElementsModal,
487 showInsertPagesModal: showInsertPagesModal,
488 showRemoveCollectionElementModal: showRemoveCollectionElementModal,
489 showRemoveFormElementModal: showRemoveFormElementModal,
490 showValidationErrorsModal: showValidationErrorsModal
491 };
492 })($, Helper, Modal, Severity, Icons);
493 });