Merge branch '5.2' into 5.3
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Ui / File / Delete.js
CommitLineData
eb8bbb53
JR
1/**
2 * Delete files which are uploaded via AJAX.
3 *
4 * @author Joshua Ruesweg
ad3b38ec 5 * @copyright 2001-2019 WoltLab GmbH
eb8bbb53
JR
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/Ui/File/Delete
8 * @since 5.2
9 */
10define(['Ajax', 'Core', 'Dom/ChangeListener', 'Language', 'Dom/Util', 'Dom/Traverse', 'Dictionary'], function(Ajax, Core, DomChangeListener, Language, DomUtil, DomTraverse, Dictionary) {
11 "use strict";
12
13 /**
14 * @constructor
15 */
16 function Delete(buttonContainerId, targetId, isSingleImagePreview, uploadHandler) {
17 this._isSingleImagePreview = isSingleImagePreview;
18 this._uploadHandler = uploadHandler;
19
20 this._buttonContainer = elById(buttonContainerId);
21 if (this._buttonContainer === null) {
22 throw new Error("Element id '" + buttonContainerId + "' is unknown.");
23 }
24
25 this._target = elById(targetId);
26 if (targetId === null) {
27 throw new Error("Element id '" + targetId + "' is unknown.");
28 }
29 this._containers = new Dictionary();
30
31 this._internalId = elData(this._target, 'internal-id');
32
33 if (!this._internalId) {
34 throw new Error("InternalId is unknown.");
35 }
36
37 this.rebuild();
38 }
39
40 Delete.prototype = {
41 /**
42 * Creates the upload button.
43 */
44 _createButtons: function() {
45 var element, elements = elBySelAll('li.uploadedFile', this._target), elementData, triggerChange = false, uniqueFileId;
46 for (var i = 0, length = elements.length; i < length; i++) {
47 element = elements[i];
48 uniqueFileId = elData(element, 'unique-file-id');
49 if (this._containers.has(uniqueFileId)) {
50 continue;
51 }
52
53 elementData = {
54 uniqueFileId: uniqueFileId,
55 element: element
56 };
57
58 this._containers.set(uniqueFileId, elementData);
59 this._initDeleteButton(element, elementData);
60
61 triggerChange = true;
62 }
63
64 if (triggerChange) {
65 DomChangeListener.trigger();
66 }
67 },
68
69 /**
70 * Init the delete button for a specific element.
71 *
72 * @param {HTMLElement} element
73 * @param {string} elementData
74 */
75 _initDeleteButton: function(element, elementData) {
76 var buttonGroup = elBySel('.buttonGroup', element);
77
78 if (buttonGroup === null) {
79 throw new Error("Button group in '" + targetId + "' is unknown.");
80 }
81
82 var li = elCreate('li');
83 var span = elCreate('span');
84 span.classList = "button jsDeleteButton small";
85 span.textContent = Language.get('wcf.global.button.delete');
86 li.appendChild(span);
87 buttonGroup.appendChild(li);
88
89 li.addEventListener(WCF_CLICK_EVENT, this._delete.bind(this, elementData.uniqueFileId));
90 },
91
92 /**
93 * Delete a specific file with the given uniqueFileId.
94 *
95 * @param {string} uniqueFileId
96 */
97 _delete: function(uniqueFileId) {
98 Ajax.api(this, {
99 uniqueFileId: uniqueFileId,
100 internalId: this._internalId
101 });
102 },
103
104 /**
105 * Rebuilds the delete buttons for unknown files.
106 */
107 rebuild: function() {
108 if (this._isSingleImagePreview) {
109 var img = elBySel('img', this._target);
110
111 if (img !== null) {
112 var uniqueFileId = elData(img, 'unique-file-id');
113
114 if (!this._containers.has(uniqueFileId)) {
115 var elementData = {
116 uniqueFileId: uniqueFileId,
117 element: img
118 };
119
120 this._containers.set(uniqueFileId, elementData);
121
122 this._deleteButton = elCreate('p');
123 this._deleteButton.className = 'button deleteButton';
124
125 var span = elCreate('span');
126 span.textContent = Language.get('wcf.global.button.delete');
127 this._deleteButton.appendChild(span);
128
129 this._buttonContainer.appendChild(this._deleteButton);
130
131 this._deleteButton.addEventListener(WCF_CLICK_EVENT, this._delete.bind(this, elementData.uniqueFileId));
132 }
133 }
134 }
135 else {
136 this._createButtons();
137 }
138 },
139
140 _ajaxSuccess: function(data) {
141 elRemove(this._containers.get(data.uniqueFileId).element);
142
143 if (this._isSingleImagePreview) {
144 elRemove(this._deleteButton);
145 this._deleteButton = null;
146 }
147
148 this._uploadHandler.checkMaxFiles();
55ff4bf9 149 Core.triggerEvent(this._target, 'change');
eb8bbb53
JR
150 },
151
152 _ajaxSetup: function () {
153 return {
154 url: 'index.php?ajax-file-delete/&t=' + SECURITY_TOKEN
155 };
156 }
157 };
158
159 return Delete;
160});