b23d425addb1a30ece32b7f1584fda5241c14e78
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Resources / Public / JavaScript / Plugins / TextIndicator.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 * TextIndicator Plugin for TYPO3 htmlArea RTE
16 */
17 define('TYPO3/CMS/Rtehtmlarea/Plugins/TextIndicator',
18 ['TYPO3/CMS/Rtehtmlarea/HTMLArea/Plugin/Plugin',
19 'TYPO3/CMS/Rtehtmlarea/HTMLArea/UserAgent/UserAgent',
20 'TYPO3/CMS/Rtehtmlarea/HTMLArea/Event/Event',
21 'TYPO3/CMS/Rtehtmlarea/HTMLArea/Util/Color',
22 'TYPO3/CMS/Rtehtmlarea/HTMLArea/Util/Util'],
23 function (Plugin, UserAgent, Event, Color, Util) {
24
25 var TextIndicator = function (editor, pluginName) {
26 this.constructor.super.call(this, editor, pluginName);
27 };
28 Util.inherit(TextIndicator, Plugin);
29 Util.apply(TextIndicator.prototype, {
30
31 /**
32 * This function gets called by the class constructor
33 */
34 configurePlugin: function (editor) {
35
36 /**
37 * Registering plugin "About" information
38 */
39 var pluginInformation = {
40 version : '1.2',
41 developer : 'Stanislas Rolland',
42 developerUrl : 'http://www.sjbr.ca/',
43 copyrightOwner : 'Stanislas Rolland',
44 sponsor : 'SJBR',
45 sponsorUrl : 'http://www.sjbr.ca/',
46 license : 'GPL'
47 };
48 this.registerPluginInformation(pluginInformation);
49
50 /**
51 * Registering the indicator
52 */
53 var buttonId = 'TextIndicator';
54 var textConfiguration = {
55 id: buttonId,
56 cls: 'indicator',
57 text: 'A',
58 tooltip: this.localize(buttonId.toLowerCase())
59 };
60 this.registerText(textConfiguration);
61 return true;
62 },
63
64 /**
65 * This handler gets called when the editor is generated
66 */
67 onGenerate: function () {
68 var self = this;
69 // Ensure text indicator is updated AFTER style sheets are loaded
70 var blockStylePlugin = this.getPluginInstance('BlockStyle');
71 if (blockStylePlugin && blockStylePlugin.blockStyles) {
72 // Monitor css parsing being completed
73 Event.one(blockStylePlugin.blockStyles, 'HTMLAreaEventCssParsingComplete', function (event) { Event.stopEvent(event); self.onCssParsingComplete(); return false; });
74 }
75 var textStylePlugin = this.getPluginInstance('TextStyle');
76 if (textStylePlugin && textStylePlugin.textStyles) {
77 // Monitor css parsing being completed
78 Event.one(textStylePlugin.textStyles, 'HTMLAreaEventCssParsingComplete', function (event) { Event.stopEvent(event); self.onCssParsingComplete(); return false; });
79 }
80 },
81
82 /**
83 * This handler gets called when parsing of css classes is completed
84 */
85 onCssParsingComplete: function () {
86 var button = this.getButton('TextIndicator'),
87 selection = this.editor.getSelection(),
88 selectionEmpty = selection.isEmpty(),
89 ancestors = selection.getAllAncestors(),
90 endPointsInSameBlock = selection.endPointsInSameBlock();
91 if (button) {
92 this.onUpdateToolbar(button, this.getEditorMode(), selectionEmpty, ancestors, endPointsInSameBlock);
93 }
94 },
95
96 /**
97 * This function gets called when the toolbar is updated
98 */
99 onUpdateToolbar: function (button, mode, selectionEmpty, ancestors) {
100 var editor = this.editor;
101 if (mode === 'wysiwyg' && editor.isEditable()) {
102 var doc = editor.document;
103 var style = {
104 fontWeight: 'normal',
105 fontStyle: 'normal'
106 };
107 try {
108 // Note: IE always reports FFFFFF as background color
109 style.backgroundColor = Color.colorToRgb(doc.queryCommandValue((UserAgent.isIE || UserAgent.isWebKit) ? 'BackColor' : 'HiliteColor'));
110 style.color = Color.colorToRgb(doc.queryCommandValue('ForeColor'));
111 style.fontFamily = doc.queryCommandValue('FontName');
112 } catch (e) { }
113 // queryCommandValue does not work in Gecko
114 if (UserAgent.isGecko) {
115 var computedStyle = editor.iframe.getIframeWindow().getComputedStyle(editor.getSelection().getParentElement(), null);
116 if (computedStyle) {
117 style.color = computedStyle.getPropertyValue('color');
118 style.backgroundColor = computedStyle.getPropertyValue('background-color');
119 style.fontFamily = computedStyle.getPropertyValue('font-family');
120 }
121 }
122 try {
123 style.fontWeight = doc.queryCommandState('Bold') ? 'bold' : 'normal';
124 } catch(e) {
125 style.fontWeight = 'normal';
126 }
127 try {
128 style.fontStyle = doc.queryCommandState('Italic') ? 'italic' : 'normal';
129 } catch(e) {
130 style.fontStyle = 'normal';
131 }
132 button.getEl().setStyle(style);
133 button.getEl().dom.setAttribute('title', button.tooltip);
134 }
135 }
136 });
137
138 return TextIndicator;
139
140 });