Add media clipboard action to set category
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Media / Clipboard.js
1 /**
2 * Initializes modules required for media clipboard.
3 *
4 * @author Matthias Schmidt
5 * @copyright 2001-2018 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/Media/Clipboard
8 */
9 define([
10 'Ajax',
11 'Dom/ChangeListener',
12 'EventHandler',
13 'Language',
14 'Ui/Dialog',
15 'Ui/Notification',
16 'WoltLabSuite/Core/Controller/Clipboard',
17 'WoltLabSuite/Core/Media/Editor',
18 'WoltLabSuite/Core/Media/List/Upload'
19 ],
20 function(
21 Ajax,
22 DomChangeListener,
23 EventHandler,
24 Language,
25 UiDialog,
26 UiNotification,
27 Clipboard,
28 MediaEditor,
29 MediaListUpload
30 ) {
31 "use strict";
32
33 if (!COMPILER_TARGET_DEFAULT) {
34 var Fake = function() {};
35 Fake.prototype = {
36 init: function() {},
37 _ajaxSetup: function() {},
38 _ajaxSuccess: function() {},
39 _clipboardAction: function() {},
40 _dialogSetup: function() {},
41 _edit: function() {},
42 _setCategory: function() {}
43 };
44 return Fake;
45 }
46
47 var _clipboardObjectIds = [];
48 var _mediaManager;
49
50 /**
51 * @exports WoltLabSuite/Core/Media/Clipboard
52 */
53 return {
54 init: function(pageClassName, hasMarkedItems, mediaManager) {
55 Clipboard.setup({
56 hasMarkedItems: hasMarkedItems,
57 pageClassName: pageClassName
58 });
59
60 _mediaManager = mediaManager;
61
62 EventHandler.add('com.woltlab.wcf.clipboard', 'com.woltlab.wcf.media', this._clipboardAction.bind(this));
63 },
64
65 /**
66 * Returns the data used to setup the AJAX request object.
67 *
68 * @return {object} setup data
69 */
70 _ajaxSetup: function() {
71 return {
72 data: {
73 className: 'wcf\\data\\media\\MediaAction'
74 }
75 }
76 },
77
78 /**
79 * Handles successful AJAX request.
80 *
81 * @param {object} data response data
82 */
83 _ajaxSuccess: function(data) {
84 switch (data.actionName) {
85 case 'getSetCategoryDialog':
86 UiDialog.open(this, data.returnValues.template);
87
88 break;
89
90 case 'setCategory':
91 UiDialog.close(this);
92
93 UiNotification.show();
94
95 Clipboard.reload();
96
97 break;
98 }
99 },
100
101 /**
102 * Returns the data used to setup the dialog.
103 *
104 * @return {object} setup data
105 */
106 _dialogSetup: function() {
107 return {
108 id: 'mediaSetCategoryDialog',
109 options: {
110 onSetup: function(content) {
111 elBySel('button', content).addEventListener(WCF_CLICK_EVENT, function(event) {
112 event.preventDefault();
113
114 this._setCategory(~~elBySel('select[name="categoryID"]', content).value);
115
116 event.currentTarget.disabled = true;
117 }.bind(this));
118 }.bind(this),
119 title: Language.get('wcf.media.setCategory')
120 },
121 source: null
122 }
123 },
124
125 /**
126 * Handles successful clipboard actions.
127 *
128 * @param {object} actionData
129 */
130 _clipboardAction: function(actionData) {
131 var mediaIds = actionData.data.parameters.objectIDs;
132
133 switch (actionData.data.actionName) {
134 case 'com.woltlab.wcf.media.delete':
135 // only consider events if the action has been executed
136 if (actionData.responseData !== null) {
137 _mediaManager.clipboardDeleteMedia(mediaIds);
138 }
139
140 break;
141
142 case 'com.woltlab.wcf.media.insert':
143 _mediaManager.clipboardInsertMedia(mediaIds);
144
145 break;
146
147 case 'com.woltlab.wcf.media.setCategory':
148 _clipboardObjectIds = mediaIds;
149
150 Ajax.api(this, {
151 actionName: 'getSetCategoryDialog'
152 });
153
154 break;
155 }
156 },
157
158 /**
159 * Sets the category of the marked media files.
160 *
161 * @param {int} categoryID selected category id
162 */
163 _setCategory: function(categoryID) {
164 Ajax.api(this, {
165 actionName: 'setCategory',
166 objectIDs: _clipboardObjectIds,
167 parameters: {
168 categoryID: categoryID
169 }
170 });
171 }
172 }
173 });