[BUGFIX] RTE Image Wizard
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Resources / Public / JavaScript / BrowseFiles.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/BrowseFiles
16 * File selection
17 */
18 define(['jquery', 'TYPO3/CMS/Recordlist/ElementBrowser', 'TYPO3/CMS/Backend/LegacyTree'], function($, ElementBrowser, Tree) {
19 'use strict';
20
21 /**
22 *
23 * @type {{elements: {}}}
24 * @exports TYPO3/CMS/Recordlist/BrowseFiles
25 */
26 var BrowseFiles = {
27 elements: {}
28 };
29
30 /**
31 * when selecting one or multiple files, this action is called
32 *
33 * @type {{insertElement: Function, insertElementMultiple: Function}}
34 */
35 BrowseFiles.File = {
36 insertElement: function(index, close) {
37 var result = false;
38 if (typeof BrowseFiles.elements[index] !== 'undefined') {
39 var element = BrowseFiles.elements[index];
40 result = ElementBrowser.insertElement(element.table, element.uid, element.type, element.fileName, element.filePath, element.fileExt, element.fileIcon, '', close);
41 }
42 return result;
43 },
44 insertElementMultiple: function(list) {
45 var uidList = [];
46 for (var i = 0, n = list.length; i < n; i++) {
47 if (typeof BrowseFiles.elements[list[i]] !== 'undefined') {
48 var element = BrowseFiles.elements[list[i]];
49 uidList.push(element.uid);
50 }
51 }
52 ElementBrowser.insertMultiple('sys_file', uidList);
53 }
54 };
55
56 /**
57 * Selector when using "Import selection" and "Toggle selection"
58 */
59 BrowseFiles.Selector = {
60 // Toggle selection button is pressed
61 toggle: function(e) {
62 e.preventDefault();
63 var items = BrowseFiles.Selector.getItems();
64 if (items.length) {
65 items.each(function(position, item) {
66 item.checked = (item.checked ? null : 'checked');
67 });
68 }
69 },
70 // Import selection button is pressed
71 handle: function(e) {
72 e.preventDefault();
73 var items = BrowseFiles.Selector.getItems();
74 var selectedItems = [];
75 if (items.length) {
76 items.each(function(position, item) {
77 if (item.checked && item.name) {
78 selectedItems.push(item.name);
79 }
80 });
81 if (selectedItems.length > 0) {
82 if (ElementBrowser.hasActionMultipleCode) {
83 BrowseFiles.File.insertElementMultiple(selectedItems);
84 } else {
85 for (var i = 0; i < selectedItems.length; i++) {
86 BrowseFiles.File.insertElement(selectedItems[i]);
87 }
88 }
89 }
90 ElementBrowser.focusOpenerAndClose(true);
91 }
92 },
93 getItems: function() {
94 return $('#typo3-filelist').find('.typo3-bulk-item');
95 }
96 };
97
98 Tree.ajaxID = 'sc_alt_file_navframe_expandtoggle';
99
100 $(function() {
101 $.extend(BrowseFiles.elements, $('body').data('elements'));
102
103 $('[data-close]').on('click', function (e) {
104 e.preventDefault();
105 BrowseFiles.File.insertElement('file_' + $(this).data('fileIndex'), $(this).data('close'));
106 });
107
108 $('#t3js-importSelection').on('click', BrowseFiles.Selector.handle);
109 $('#t3js-toggleSelection').on('click', BrowseFiles.Selector.toggle);
110 });
111
112 return BrowseFiles;
113 });