a6579c8d642c77ec767e514ce7a4ee62c6bbf002
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Resources / Public / JavaScript / ElementBrowser.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 * ElementBrowser communication with parent windows
16 */
17 define('TYPO3/CMS/Recordlist/ElementBrowser', ['jquery'], function($) {
18 "use strict";
19
20 var ElementBrowser = {
21 thisScriptUrl: '',
22 mode: '',
23 formFieldName: '',
24 hasActionMultipleCode: false,
25 fieldReference: '',
26 fieldReferenceSlashed: '',
27 rte: {
28 parameters: '',
29 configuration: ''
30 },
31 irre: {
32 objectId: 0,
33 checkUniqueAction: '',
34 addAction: '',
35 insertAction: ''
36 }
37 };
38
39 ElementBrowser.setReferences = function() {
40 if (
41 window.opener && window.opener.content && window.opener.content.document.editform
42 && window.opener.content.document.editform[ElementBrowser.formFieldName]
43 ) {
44 ElementBrowser.targetDoc = window.opener.content.document;
45 ElementBrowser.elRef = ElementBrowser.targetDoc.editform[ElementBrowser.formFieldName];
46 return true;
47 } else {
48 return false;
49 }
50 };
51
52 /**
53 * Dynamically calls a function on a given context object.
54 *
55 * @param {string} functionName e.g. "inline.somefunc"
56 * @param {object} context e.g. window
57 * @returns {*}
58 */
59 ElementBrowser.executeFunctionByName = function(functionName, context /*, args */) {
60 var args = Array.prototype.slice.call(arguments, 2);
61 var namespaces = functionName.split(".");
62 var func = namespaces.pop();
63 for (var i = 0; i < namespaces.length; i++) {
64 context = context[namespaces[i]];
65 }
66 return context[func].apply(context, args);
67 };
68
69 ElementBrowser.insertElement = function(table, uid, type, filename, fp, filetype, imagefile, action, close) {
70 var performAction = true;
71 // Call a check function in the opener window (e.g. for uniqueness handling):
72 if (ElementBrowser.irre.objectId && ElementBrowser.irre.checkUniqueAction) {
73 if (window.opener) {
74 var res = ElementBrowser.executeFunctionByName(ElementBrowser.irre.checkUniqueAction, window.opener, ElementBrowser.irre.objectId, table ,uid, type);
75 if (!res.passed) {
76 if (res.message) {
77 alert(res.message);
78 }
79 performAction = false;
80 }
81 } else {
82 alert("Error - reference to main window is not set properly!");
83 close();
84 }
85 }
86 // Call performing function and finish this action:
87 if (performAction) {
88 // Call helper function to manage data in the opener window:
89 if (ElementBrowser.irre.objectId && ElementBrowser.irre.addAction) {
90 if (window.opener) {
91 ElementBrowser.executeFunctionByName(ElementBrowser.irre.addAction, window.opener, ElementBrowser.irre.objectId, table ,uid, type, ElementBrowser.fieldReferenceSlashed);
92 } else {
93 alert("Error - reference to main window is not set properly!");
94 close();
95 }
96 }
97 if (ElementBrowser.irre.objectId && ElementBrowser.irre.insertAction) {
98 if (window.opener) {
99 ElementBrowser.executeFunctionByName(ElementBrowser.irre.insertAction, window.opener, ElementBrowser.irre.objectId, table ,uid, type);
100 if (close) {
101 ElementBrowser.focusOpenerAndClose();
102 }
103 } else {
104 alert("Error - reference to main window is not set properly!");
105 if (close) {
106 close();
107 }
108 }
109 } else if (ElementBrowser.fieldReference && !ElementBrowser.rte.parameters && !ElementBrowser.rte.configuration) {
110 ElementBrowser.addElement(filename, table + "_" + uid, fp, close);
111 } else {
112 if (
113 window.opener && window.opener.content && window.opener.content.document.editform
114 && window.opener.content.document.editform[ElementBrowser.formFieldName]
115 ) {
116 window.opener.group_change(
117 "add",
118 ElementBrowser.fieldReference,
119 ElementBrowser.rte.parameters,
120 ElementBrowser.rte.configuration,
121 ElementBrowser.targetDoc.editform[ElementBrowser.formFieldName],
122 ElementBrowser.window.opener.content.document
123 );
124 } else {
125 alert("Error - reference to main window is not set properly!");
126 }
127 if (close) {
128 ElementBrowser.focusOpenerAndClose();
129 }
130 }
131 }
132 return false;
133 };
134
135 ElementBrowser.insertMultiple = function(table, uid) {
136 var type = "";
137 if (ElementBrowser.irre.objectId && ElementBrowser.irre.insertAction) {
138 // Call helper function to manage data in the opener window:
139 if (window.opener) {
140 ElementBrowser.executeFunctionByName(ElementBrowser.irre.insertAction + 'Multiple', window.opener, ElementBrowser.irre.objectId, table ,uid, type, ElementBrowser.fieldReference);
141 } else {
142 alert("Error - reference to main window is not set properly!");
143 close();
144 }
145 }
146 return false;
147 };
148
149 ElementBrowser.addElement = function(elName, elValue, altElValue, close) {
150 if (window.opener && window.opener.setFormValueFromBrowseWin) {
151 window.opener.setFormValueFromBrowseWin(ElementBrowser.fieldReference, altElValue ? altElValue : elValue, elName);
152 if (close) {
153 ElementBrowser.focusOpenerAndClose();
154 }
155 } else {
156 alert("Error - reference to main window is not set properly!");
157 close();
158 }
159 };
160
161 ElementBrowser.focusOpenerAndClose = function() {
162 window.opener.focus();
163 close();
164 };
165
166 $(function() {
167 var data = $('body').data();
168
169 ElementBrowser.thisScriptUrl = data.thisScriptUrl;
170 ElementBrowser.mode = data.mode;
171 ElementBrowser.formFieldName = data.formFieldName;
172 ElementBrowser.fieldReference = data.fieldReference;
173 ElementBrowser.fieldReferenceSlashed = data.fieldReferenceSlashed;
174 ElementBrowser.rte.parameters = data.rteParameters;
175 ElementBrowser.rte.configuration = data.rteConfiguration;
176 ElementBrowser.irre.checkUniqueAction = data.irreCheckUniqueAction;
177 ElementBrowser.irre.addAction = data.irreAddAction;
178 ElementBrowser.irre.insertAction = data.irreInsertAction;
179 ElementBrowser.irre.objectId = data.irreObjectId;
180 ElementBrowser.hasActionMultipleCode = ElementBrowser.irre.objectId && ElementBrowser.irre.insertAction;
181 });
182
183 /**
184 * Global jumpTo function
185 *
186 * Used by tree implementation
187 *
188 * @param {string} URL
189 * @param {string} anchor
190 * @returns {boolean}
191 */
192 window.jumpToUrl = function(URL, anchor) {
193 if (URL.charAt(0) === '?') {
194 URL = ElementBrowser.thisScriptUrl + URL.substring(1);
195 }
196 var add_mode = URL.indexOf("mode=") === -1 ? '&mode=' + encodeURIComponent(ElementBrowser.mode) : "";
197 window.location.href = URL + add_mode + (typeof(anchor) === "string" ? anchor : '');
198 return false;
199 };
200
201 return ElementBrowser;
202 });