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