-
Notifications
You must be signed in to change notification settings - Fork 638
/
ElementBrowser.js
278 lines (264 loc) · 8.96 KB
/
ElementBrowser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
/**
* Module: TYPO3/CMS/Recordlist/ElementBrowser
* ElementBrowser communication with parent windows
*/
define([
'jquery',
'TYPO3/CMS/Backend/Modal'
], function($, Modal) {
'use strict';
/**
*
* @type {{thisScriptUrl: string, mode: string, formFieldName: string, hasActionMultipleCode: boolean, fieldReference: string, fieldReferenceSlashed: string, rte: {parameters: string, configuration: string}, irre: {objectId: number, checkUniqueAction: string, addAction: string, insertAction: string}}}
* @exports TYPO3/CMS/Recordlist/ElementBrowser
*/
var ElementBrowser = {
thisScriptUrl: '',
mode: '',
formFieldName: '',
hasActionMultipleCode: false,
fieldReference: '',
fieldReferenceSlashed: '',
rte: {
parameters: '',
configuration: ''
},
irre: {
objectId: 0,
checkUniqueAction: '',
addAction: '',
insertAction: ''
}
};
/**
*
* @returns {Boolean}
*/
ElementBrowser.setReferences = function() {
if (
ElementBrowser.getParent() && ElementBrowser.getParent().content && ElementBrowser.getParent().content.document.editform
&& ElementBrowser.getParent().content.document.editform[ElementBrowser.formFieldName]
) {
ElementBrowser.targetDoc = ElementBrowser.getParent().content.document;
ElementBrowser.elRef = ElementBrowser.targetDoc.editform[ElementBrowser.formFieldName];
return true;
} else {
return false;
}
};
/**
* Dynamically calls a function on a given context object.
*
* @param {String} functionName e.g. "inline.somefunc"
* @param {Object} context e.g. window
* @returns {*}
*/
ElementBrowser.executeFunctionByName = function(functionName, context /*, args */) {
var args = Array.prototype.slice.call(arguments, 2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for (var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(context, args);
};
/**
* Returns the parent document object
*/
ElementBrowser.getParent = function() {
var opener = null;
if (
typeof window.parent !== 'undefined' &&
typeof window.parent.document.list_frame !== 'undefined' &&
window.parent.document.list_frame.parent.document.querySelector('.t3js-modal-iframe') !== null
) {
opener = window.parent.document.list_frame;
} else if (
typeof window.parent !== 'undefined' &&
typeof window.parent.frames.list_frame !== 'undefined' &&
window.parent.frames.list_frame.parent.document.querySelector('.t3js-modal-iframe') !== null
) {
opener = window.parent.frames.list_frame;
} else if (
typeof window.frames !== 'undefined' &&
typeof window.frames.frameElement !== 'undefined' &&
window.frames.frameElement !== null &&
window.frames.frameElement.classList.contains('t3js-modal-iframe')
) {
opener = window.frames.frameElement.contentWindow.parent;
} else if (window.opener) {
opener = window.opener;
}
return opener;
};
/**
*
* @param {String} table
* @param {Number} uid
* @param {String} type
* @param {String} filename
* @param {String} fp
* @param {String} filetype
* @param {String} imagefile
* @param {String} action
* @param {String} close
* @returns {Boolean}
*/
ElementBrowser.insertElement = function(table, uid, type, filename, fp, filetype, imagefile, action, close) {
var performAction = true;
// Call a check function in the opener window (e.g. for uniqueness handling):
if (ElementBrowser.irre.objectId && ElementBrowser.irre.checkUniqueAction) {
if (ElementBrowser.getParent()) {
var res = ElementBrowser.executeFunctionByName(ElementBrowser.irre.checkUniqueAction, ElementBrowser.getParent(), ElementBrowser.irre.objectId, table ,uid, type);
if (!res.passed) {
if (res.message) {
alert(res.message);
}
performAction = false;
}
} else {
alert("Error - reference to main window is not set properly!");
ElementBrowser.focusOpenerAndClose();
}
}
// Call performing function and finish this action:
if (performAction) {
// Call helper function to manage data in the opener window:
if (ElementBrowser.irre.objectId && ElementBrowser.irre.addAction) {
if (ElementBrowser.getParent()) {
ElementBrowser.executeFunctionByName(ElementBrowser.irre.addAction, ElementBrowser.getParent(), ElementBrowser.irre.objectId, table ,uid, type, ElementBrowser.fieldReferenceSlashed);
} else {
alert("Error - reference to main window is not set properly!");
ElementBrowser.focusOpenerAndClose();
}
}
if (ElementBrowser.irre.objectId && ElementBrowser.irre.insertAction) {
if (ElementBrowser.getParent()) {
ElementBrowser.executeFunctionByName(ElementBrowser.irre.insertAction, ElementBrowser.getParent(), ElementBrowser.irre.objectId, table ,uid, type);
if (close) {
ElementBrowser.focusOpenerAndClose();
}
} else {
alert("Error - reference to main window is not set properly!");
if (close) {
ElementBrowser.focusOpenerAndClose();
}
}
} else if (ElementBrowser.fieldReference && !ElementBrowser.rte.parameters && !ElementBrowser.rte.configuration) {
ElementBrowser.addElement(filename, table + "_" + uid, fp, close);
} else {
if (
ElementBrowser.getParent() && ElementBrowser.getParent().content && ElementBrowser.getParent().content.document.editform
&& ElementBrowser.getParent().content.document.editform[ElementBrowser.formFieldName]
) {
ElementBrowser.getParent().group_change(
"add",
ElementBrowser.fieldReference,
ElementBrowser.rte.parameters,
ElementBrowser.rte.configuration,
ElementBrowser.targetDoc.editform[ElementBrowser.formFieldName],
ElementBrowser.getParent().content.document
);
} else {
alert("Error - reference to main window is not set properly!");
}
if (close) {
ElementBrowser.focusOpenerAndClose();
}
}
}
return false;
};
/**
*
* @param {String} table
* @param {Number} uid
* @returns {Boolean}
*/
ElementBrowser.insertMultiple = function(table, uid) {
var type = "";
if (ElementBrowser.irre.objectId && ElementBrowser.irre.insertAction) {
// Call helper function to manage data in the opener window:
if (ElementBrowser.getParent()) {
ElementBrowser.executeFunctionByName(ElementBrowser.irre.insertAction + 'Multiple', ElementBrowser.getParent(), ElementBrowser.irre.objectId, table ,uid, type, ElementBrowser.fieldReference);
} else {
alert("Error - reference to main window is not set properly!");
ElementBrowser.focusOpenerAndClose();
}
}
return false;
};
/**
*
* @param {String} elName
* @param {String} elValue
* @param {String} altElValue
* @param {String} close
*/
ElementBrowser.addElement = function(elName, elValue, altElValue, close) {
if (ElementBrowser.getParent() && ElementBrowser.getParent().setFormValueFromBrowseWin) {
ElementBrowser.getParent().setFormValueFromBrowseWin(ElementBrowser.fieldReference, altElValue ? altElValue : elValue, elName);
if (close) {
ElementBrowser.focusOpenerAndClose();
}
} else {
alert("Error - reference to main window is not set properly!");
ElementBrowser.focusOpenerAndClose();
}
};
/**
*
*/
ElementBrowser.focusOpenerAndClose = function() {
if (ElementBrowser.getParent()) {
ElementBrowser.getParent().focus();
}
Modal.dismiss();
close();
};
$(function() {
var data = $('body').data();
ElementBrowser.thisScriptUrl = data.thisScriptUrl;
ElementBrowser.mode = data.mode;
ElementBrowser.formFieldName = data.formFieldName;
ElementBrowser.fieldReference = data.fieldReference;
ElementBrowser.fieldReferenceSlashed = data.fieldReferenceSlashed;
ElementBrowser.rte.parameters = data.rteParameters;
ElementBrowser.rte.configuration = data.rteConfiguration;
ElementBrowser.irre.checkUniqueAction = data.irreCheckUniqueAction;
ElementBrowser.irre.addAction = data.irreAddAction;
ElementBrowser.irre.insertAction = data.irreInsertAction;
ElementBrowser.irre.objectId = data.irreObjectId;
ElementBrowser.hasActionMultipleCode = ElementBrowser.irre.objectId && ElementBrowser.irre.insertAction;
});
/**
* Global jumpTo function
*
* Used by tree implementation
*
* @param {String} URL
* @param {String} anchor
* @returns {Boolean}
*/
window.jumpToUrl = function(URL, anchor) {
if (URL.charAt(0) === '?') {
URL = ElementBrowser.thisScriptUrl + URL.substring(1);
}
var add_mode = URL.indexOf("mode=") === -1 ? '&mode=' + encodeURIComponent(ElementBrowser.mode) : "";
window.location.href = URL + add_mode + (typeof(anchor) === "string" ? anchor : '');
return false;
};
return ElementBrowser;
});