e108ac3be1656ff8707b81badff7cefb435a2657
[Packages/TYPO3.CMS.git] / typo3 / sysext / rte_ckeditor / Resources / Public / JavaScript / RteLinkBrowser.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 * Module: TYPO3/CMS/RteCkeditor/RteLinkBrowser
16 * LinkBrowser communication with parent window
17 */
18 define(['jquery', 'TYPO3/CMS/Recordlist/LinkBrowser', 'TYPO3/CMS/Backend/Modal'], function ($, LinkBrowser, Modal) {
19 'use strict';
20
21 /**
22 *
23 * @type {{plugin: null, CKEditor: null, siteUrl: string}}
24 * @exports TYPO3/CMS/RteCkeditor/RteLinkBrowser
25 */
26 var RteLinkBrowser = {
27 plugin: null,
28 CKEditor: null,
29 siteUrl: ''
30 };
31
32 /**
33 * @param {String} editorId Id of CKEditor
34 */
35 RteLinkBrowser.initialize = function (editorId) {
36 var callerWindow;
37 if (typeof top.TYPO3.Backend !== 'undefined' && typeof top.TYPO3.Backend.ContentContainer.get() !== 'undefined') {
38 callerWindow = top.TYPO3.Backend.ContentContainer.get();
39 } else {
40 callerWindow = window.parent;
41 }
42
43 $.each(callerWindow.CKEDITOR.instances, function (name, editor) {
44 if (editor.id === editorId) {
45 RteLinkBrowser.CKEditor = editor;
46 }
47 });
48
49 // siteUrl etc are added as data attributes to the body tag
50 $.extend(RteLinkBrowser, $('body').data());
51
52 $('.t3js-removeCurrentLink').on('click', function (event) {
53 event.preventDefault();
54 RteLinkBrowser.CKEditor.execCommand('unlink');
55 Modal.dismiss();
56 });
57 };
58
59 /**
60 * Store the final link
61 *
62 * @param {String} link The select element or anything else which identifies the link (e.g. "page:<pageUid>" or "file:<uid>")
63 */
64 LinkBrowser.finalizeFunction = function (link) {
65
66 var linkElement = RteLinkBrowser.CKEditor.document.createElement('a');
67 var attributes = LinkBrowser.getLinkAttributeValues();
68 var params = attributes.params ? attributes.params : '';
69
70 if (attributes.target) {
71 linkElement.setAttribute('target', attributes.target);
72 }
73 if (attributes.class) {
74 linkElement.setAttribute('class', attributes.class);
75 }
76 if (attributes.title) {
77 linkElement.setAttribute('title', attributes.title);
78 }
79 delete attributes.title;
80 delete attributes.class;
81 delete attributes.target;
82 delete attributes.params;
83
84 $.each(attributes, function (attrName, attrValue) {
85 linkElement.setAttribute(attrName, attrValue);
86 });
87
88 // todo: check if this still needed?
89 // if there is no handler keyword (mailto:, record:, etc) or an external link, we always prepend the siteUrl
90 if (!/^\w+:/.test(link) && !attributes['data-htmlarea-external']) {
91 link = RteLinkBrowser.siteUrl + '?' + link;
92 }
93
94 linkElement.setAttribute('href', link + params);
95
96 var selection = RteLinkBrowser.CKEditor.getSelection();
97 if (selection && selection.getSelectedText()) {
98 linkElement.setText(selection.getSelectedText());
99 } else {
100 linkElement.setText(linkElement.getAttribute('href'));
101 }
102 RteLinkBrowser.CKEditor.insertElement(linkElement);
103
104 Modal.dismiss();
105 };
106
107 return RteLinkBrowser;
108 });