[BUGFIX] Avoid redundant SQL queries in ReferenceIndex
[Packages/TYPO3.CMS.git] / typo3 / sysext / rte_ckeditor / Resources / Private / TypeScript / RteLinkBrowser.ts
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 import $ = require('jquery');
19 import LinkBrowser = require('TYPO3/CMS/Recordlist/LinkBrowser');
20 import Modal = require('TYPO3/CMS/Backend/Modal');
21
22 class RteLinkBrowser {
23 private plugin: any = null;
24
25 private CKEditor: CKEDITOR.editor = null;
26
27 private siteUrl = '';
28
29 /**
30 * @param {string} editorId Id of CKEditor
31 */
32 public initialize(editorId: string): void {
33 let callerWindow: Window;
34 if (typeof top.TYPO3.Backend !== 'undefined' && typeof top.TYPO3.Backend.ContentContainer.get() !== 'undefined') {
35 callerWindow = top.TYPO3.Backend.ContentContainer.get();
36 } else {
37 callerWindow = window.parent;
38 }
39
40 $.each((callerWindow as any).CKEDITOR.instances, (name: string, editor: CKEDITOR.editor) => {
41 if (editor.id === editorId) {
42 this.CKEditor = editor;
43 }
44 });
45
46 // siteUrl etc are added as data attributes to the body tag
47 $.extend(RteLinkBrowser, $('body').data());
48
49 $('.t3js-removeCurrentLink').on('click', (event: Event) => {
50 event.preventDefault();
51 this.CKEditor.execCommand('unlink');
52 Modal.dismiss();
53 });
54 }
55
56 /**
57 * Store the final link
58 *
59 * @param {stringify} link The select element or anything else which identifies
60 * the link (e.g. "page:<pageUid>" or "file:<uid>")
61 */
62 public finalizeFunction(link: string): void {
63 const linkElement: CKEDITOR.dom.element = this.CKEditor.document.createElement('a');
64 const attributes = LinkBrowser.getLinkAttributeValues();
65 const params: string = attributes.params ? attributes.params : '';
66
67 if (attributes.target) {
68 linkElement.setAttribute('target', attributes.target);
69 }
70 if (attributes.class) {
71 linkElement.setAttribute('class', attributes.class);
72 }
73 if (attributes.title) {
74 linkElement.setAttribute('title', attributes.title);
75 }
76 delete attributes.title;
77 delete attributes.class;
78 delete attributes.target;
79 delete attributes.params;
80
81 $.each(attributes, (attrName: string, attrValue: string) => {
82 linkElement.setAttribute(attrName, attrValue);
83 });
84
85 linkElement.setAttribute('href', link + params);
86
87 const selection: CKEDITOR.dom.selection = this.CKEditor.getSelection();
88 if (selection && selection.getSelectedText() === '') {
89 selection.selectElement(selection.getStartElement());
90 }
91 if (selection && selection.getSelectedText()) {
92 linkElement.setText(selection.getSelectedText());
93 } else {
94 linkElement.setText(linkElement.getAttribute('href'));
95 }
96 this.CKEditor.insertElement(linkElement);
97
98 Modal.dismiss();
99 }
100 }
101
102 export = new RteLinkBrowser();