e421d1ad5b430a40763dd4f0a6205e72fc402f44
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Resources / Public / JavaScript / Recordlist.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/Recordlist/Recordlist
16 * Usability improvements for the record list
17 */
18 define(['jquery', 'TYPO3/CMS/Backend/Storage', 'TYPO3/CMS/Backend/Icons'], function($, Storage, Icons) {
19 'use strict';
20
21 /**
22 * @type {Object}
23 * @exports TYPO3/CMS/Recordlist/Recordlist
24 */
25 var Recordlist = {
26 identifier: {
27 entity: '.t3js-entity',
28 toggle: '.t3js-toggle-recordlist',
29 icons: {
30 collapse: 'actions-view-list-collapse',
31 expand: 'actions-view-list-expand',
32 editMultiple: '.t3js-record-edit-multiple'
33 }
34 }
35 };
36
37 /**
38 * @param {MouseEvent} e
39 */
40 Recordlist.toggleClick = function(e) {
41 e.preventDefault();
42
43 var $me = $(this),
44 table = $me.data('table'),
45 $target = $($me.data('target')),
46 isExpanded = $target.data('state') === 'expanded',
47 $collapseIcon = $me.find('.collapseIcon'),
48 toggleIcon = isExpanded ? Recordlist.identifier.icons.expand : Recordlist.identifier.icons.collapse;
49
50 Icons.getIcon(toggleIcon, Icons.sizes.small).done(function(toggleIcon) {
51 $collapseIcon.html(toggleIcon);
52 });
53
54 // Store collapse state in UC
55 var storedModuleDataList = {};
56
57 if (Storage.Persistent.isset('moduleData.list')) {
58 storedModuleDataList = Storage.Persistent.get('moduleData.list');
59 }
60
61 var collapseConfig = {};
62 collapseConfig[table] = isExpanded ? 1 : 0;
63
64 $.extend(true, storedModuleDataList, collapseConfig);
65 Storage.Persistent.set('moduleData.list', storedModuleDataList).done(function() {
66 $target.data('state', isExpanded ? 'collapsed' : 'expanded');
67 });
68 };
69
70 /**
71 * Handles editing multiple records.
72 *
73 * @param {MouseEvent} event
74 */
75 Recordlist.onEditMultiple = function(event) {
76 event.preventDefault();
77
78 var $tableContainer, tableName, entityIdentifiers, uri, patterns;
79
80 $tableContainer = $(this).closest('[data-table]');
81 if ($tableContainer.length === 0) {
82 return;
83 }
84
85 uri = $(this).data('uri');
86 tableName = $tableContainer.data('table');
87 entityIdentifiers = $tableContainer
88 .find(Recordlist.identifier.entity + '[data-uid][data-table="' + tableName + '"]')
89 .map(function(index, entity) { return $(entity).data('uid'); })
90 .toArray()
91 .join(',');
92
93 patterns = uri.match(/{[^}]+}/g);
94 $.each(patterns, function(patternIndex, pattern) {
95 var expression = pattern.substr(1, pattern.length - 2);
96 var pipes = expression.split(':');
97 var name = pipes.shift();
98 var value;
99
100 switch (name) {
101 case 'entityIdentifiers':
102 value = entityIdentifiers;
103 break;
104 case 'T3_THIS_LOCATION':
105 value = T3_THIS_LOCATION;
106 break;
107 default:
108 return;
109 }
110
111 $.each(pipes, function(pipeIndex, pipe) {
112 if (pipe === 'editList') {
113 value = editList(tableName, value);
114 }
115 });
116
117 uri = uri.replace(pattern, value);
118 });
119
120 window.location.href = uri;
121 };
122
123 $(document).on('click', Recordlist.identifier.toggle, Recordlist.toggleClick);
124 $(document).on('click', Recordlist.identifier.icons.editMultiple, Recordlist.onEditMultiple);
125
126 return Recordlist;
127 });