4a42d96e439736e58da9054893e1e30a7b6060c9
[Packages/TYPO3.CMS.git] / typo3 / sysext / rte_ckeditor / Resources / Public / JavaScript / Plugins / typo3link.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 'use strict';
15
16 (function () {
17
18 CKEDITOR.plugins.add('typo3link', {
19 elementBrowser: null,
20 init: function (editor) {
21 var allowedAttributes = ['!href', 'title', 'class', 'target', 'rel'],
22 required = 'a[href]';
23
24 var additionalAttributes = getAdditionalAttributes(editor);
25 if (additionalAttributes.length) {
26 allowedAttributes.push.apply(allowedAttributes, additionalAttributes);
27 }
28
29 // Override link command
30 editor.addCommand('link', {
31 exec: openLinkBrowser,
32 allowedContent: 'a[' + allowedAttributes.join(',') + ']',
33 requiredContent: required
34 });
35
36 // Override doubleclick opening default link dialog
37 editor.on('doubleclick', function (evt) {
38 var element = CKEDITOR.plugins.link.getSelectedLink(editor) || evt.data.element;
39 if (!element.isReadOnly() && element.is('a') && element.getAttribute('href')) {
40 evt.stop();
41 openLinkBrowser(editor, element);
42 }
43 }, null, null, 30);
44
45 }
46 });
47
48 /**
49 * Open link browser
50 *
51 * @param {Object} editor CKEditor object
52 * @param {Object} element Selected link element
53 */
54 function openLinkBrowser(editor, element) {
55 var additionalParameters = '';
56
57 if (!element) {
58 element = CKEDITOR.plugins.link.getSelectedLink(editor);
59 }
60 if (element) {
61 additionalParameters = '&curUrl[url]=' + encodeURIComponent(element.getAttribute('href'));
62 var i = 0,
63 attributeNames = ["target", "class", "title", "rel"];
64 for (i = 0; i < attributeNames.length; ++i) {
65 if (element.getAttribute(attributeNames[i])) {
66 additionalParameters += '&curUrl[' + attributeNames[i] + ']=';
67 additionalParameters += encodeURIComponent(element.getAttribute(attributeNames[i]));
68 }
69 }
70
71 var additionalAttributes = getAdditionalAttributes(editor);
72 for (i = additionalAttributes.length; --i >= 0;) {
73 if (element.hasAttribute(additionalAttributes[i])) {
74 additionalParameters += '&curUrl[' + additionalAttributes[i] + ']=';
75 additionalParameters += encodeURIComponent(element.getAttribute(additionalAttributes[i]));
76 }
77 }
78 }
79
80 openElementBrowser(
81 editor,
82 editor.lang.link.toolbar,
83 TYPO3.settings.Textarea.RTEPopupWindow.height,
84 makeUrlFromModulePath(
85 editor,
86 editor.config.typo3link.routeUrl,
87 additionalParameters
88 ));
89 }
90
91 /**
92 * Make url from url
93 *
94 * @param {Object} editor CKEditor object
95 * @param {String} routeUrl URL
96 * @param {String} parameters Additional parameters
97 *
98 * @return {String} The url
99 */
100 function makeUrlFromModulePath(editor, routeUrl, parameters) {
101
102 return routeUrl
103 + (routeUrl.indexOf('?') === -1 ? '?' : '&')
104 + '&contentsLanguage=' + editor.config.contentsLanguage
105 + '&editorId=' + editor.id
106 + (parameters ? parameters : '');
107 }
108
109 /**
110 * Open a window with container iframe
111 *
112 * @param {Object} editor The CKEditor instance
113 * @param {String} title The window title (will be localized here)
114 * @param {Integer} height The height of the containing iframe
115 * @param {String} url The url to load ino the iframe
116 */
117 function openElementBrowser(editor, title, height, url) {
118 require([
119 'jquery',
120 'TYPO3/CMS/Backend/Modal',
121 'TYPO3/CMS/Backend/Severity'
122 ], function ($, Modal, Severity) {
123
124 var $iframe = $('<iframe />', {
125 src: url,
126 'class': 'content-iframe',
127 style: 'border: 0; width: 100%; height: ' + height * 1 + 'px;'
128 }),
129 $content = $('<div />', {'class': 'rte-ckeditor-window', id: editor.id}).append($iframe);
130
131 var elementBrowser = Modal.show(title, $content, Severity.notice);
132
133 // TODO: add this to less/css (.rte-ckeditor-window .modal-body)
134 // further, make modal wider and maybe resize-able
135 elementBrowser.find('.modal-body').css('padding', 0);
136 });
137 }
138
139 /**
140 * Fetch attributes for the <a> tag which are allowed additionally
141 * @param {Object} editor The CKEditor instance
142 *
143 * @return {Array} registered attributes available for the link
144 */
145 function getAdditionalAttributes(editor) {
146 if (editor.config.typo3link.additionalAttributes && editor.config.typo3link.additionalAttributes.length) {
147 return editor.config.typo3link.additionalAttributes;
148 } else {
149 return [];
150 }
151 }
152
153 })();