2f3e467b46e29ca577eb1d3582b105e00b5af05d
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / htmlarea / plugins / StatusBar / status-bar.js
1 /***************************************************************
2 * Copyright notice
3 *
4 * (c) 2009 Stanislas Rolland <typo3(arobas)sjbr.ca>
5 * All rights reserved
6 *
7 * This script is part of the TYPO3 project. The TYPO3 project is
8 * free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * The GNU General Public License can be found at
14 * http://www.gnu.org/copyleft/gpl.html.
15 * A copy is found in the textfile GPL.txt and important notices to the license
16 * from the author is found in LICENSE.txt distributed with these scripts.
17 *
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /*
28 * StatusBar Plugin for TYPO3 htmlArea RTE
29 *
30 * TYPO3 SVN ID: $Id$
31 */
32 StatusBar = HTMLArea.Plugin.extend({
33
34 constructor : function(editor, pluginName) {
35 this.base(editor, pluginName);
36 },
37
38 /*
39 * This function gets called by the class constructor
40 */
41 configurePlugin : function (editor) {
42
43 /*
44 * Registering plugin "About" information
45 */
46 var pluginInformation = {
47 version : "0.1",
48 developer : "Stanislas Rolland",
49 developerUrl : "http://www.sjbr.ca/",
50 copyrightOwner : "Stanislas Rolland",
51 sponsor : "SJBR",
52 sponsorUrl : "http://www.sjbr.ca/",
53 license : "GPL"
54 };
55 this.registerPluginInformation(pluginInformation);
56
57 /*
58 * Making function refernce to status bar handler
59 */
60 this.statusBarHandlerFunctRef = this.makeFunctionReference("statusBarHandler");
61
62 return true;
63 },
64
65 /*
66 * Create the status bar
67 */
68 onGenerate : function () {
69 var statusBar = document.createElement("div");
70 this.statusBar = statusBar;
71 statusBar.className = "statusBar";
72 var statusBarTree = document.createElement("span");
73 this.statusBarTree = statusBarTree;
74 statusBarTree.className = "statusBarTree";
75 statusBar.appendChild(statusBarTree);
76 statusBarTree.appendChild(document.createTextNode(HTMLArea.I18N.msg["Path"] + ": "));
77 this.editor._htmlArea.appendChild(this.statusBar);
78 this.setSelection(null);
79 this.noUpdate = false;
80 },
81
82 /*
83 * Adapt status bar to current editor mode
84 *
85 * @param string mode: the mode to which the editor got switched to
86 */
87 onMode : function (mode) {
88 switch (mode) {
89 case "wysiwyg":
90 this.statusBar.innerHTML = "";
91 this.statusBar.appendChild(this.statusBarTree);
92 break;
93 case "textmode":
94 default:
95 var statusBarTextMode = document.createElement("span");
96 statusBarTextMode.className = "statusBarTextMode";
97 statusBarTextMode.appendChild(document.createTextNode(HTMLArea.I18N.msg["TEXT_MODE"]));
98 this.statusBar.innerHTML = "";
99 this.statusBar.appendChild(statusBarTextMode);
100 break;
101 }
102 },
103
104 /*
105 * Replace the contents of the staus bar with a text string
106 *
107 * @param string text: the text string to be inserted in the status bar
108 */
109 setText : function(text) {
110 this.statusBarTree.innerHTML = text;
111 },
112
113 /*
114 * Clear the status bar
115 */
116 clear : function() {
117 // Unhook events handlers
118 if (this.statusBarTree) {
119 if (this.statusBarTree.hasChildNodes()) {
120 for (var element = this.statusBarTree.firstChild; element; element = element.nextSibling) {
121 if (element.nodeName.toLowerCase() == "a") {
122 HTMLArea._removeEvents(element, ["click", "contextmenu", "mousedown"], this.statusBarHandlerFunctRef);
123 element.ancestor = null;
124 element.editor = null;
125 }
126 }
127 }
128 this.statusBarTree.innerHTML = "";
129 }
130 this.setSelection(null);
131 },
132
133 /*
134 * Cleanup the status bar when the editor closes
135 */
136 onClose : function() {
137 this.clear();
138 this.statusBarHandlerFunctRef = null;
139 this.statusBar = null;
140 this.statusBarTree = null;
141 },
142
143 /*
144 * Get the status bar selection
145 */
146 getSelection : function() {
147 return this.selected;
148 },
149
150 /*
151 * Set the status bar selection
152 *
153 * @param object element: set the status bar selection to the given element
154 */
155 setSelection : function(element) {
156 this.selected = element ? element : null;
157 },
158
159 /*
160 * Update the status bar
161 */
162 onUpdateToolbar : function() {
163 if (this.getEditorMode() == "wysiwyg" && !this.noUpdate) {
164 var text,
165 language,
166 languageObject = this.editor.getPluginInstance("Language"),
167 classes = new Array(),
168 classText,
169 ancestors = this.editor.getAllAncestors();
170 this.clear();
171 this.statusBarTree.appendChild(document.createTextNode(HTMLArea.I18N.msg["Path"] + ": "));
172 for (var i = ancestors.length; --i >= 0;) {
173 var ancestor = ancestors[i];
174 if (!ancestor) {
175 continue;
176 }
177 var element = document.createElement("a");
178 element.href = "#";
179 element.ancestor = ancestor;
180 element.editor = this.editor;
181 HTMLArea._addEvent(element, (HTMLArea.is_ie ? "click" : "mousedown"), this.statusBarHandlerFunctRef);
182 if (!HTMLArea.is_opera) {
183 HTMLArea._addEvent(element, "contextmenu", this.statusBarHandlerFunctRef);
184 }
185 element.title = ancestor.style.cssText;
186 text = ancestor.nodeName.toLowerCase();
187 if (ancestor.id) {
188 text += "#" + ancestor.id;
189 }
190 if (languageObject && languageObject.getLanguageAttribute) {
191 language = languageObject.getLanguageAttribute(ancestor);
192 if (language != "none") {
193 text += "[" + language + "]";
194 }
195 }
196 if (ancestor.className) {
197 classText = "";
198 classes = ancestor.className.trim().split(" ");
199 for (var j = 0, n = classes.length; j < n; ++j) {
200 if (!HTMLArea.reservedClassNames.test(classes[j])) {
201 classText += "." + classes[j];
202 }
203 }
204 text += classText;
205 }
206 element.appendChild(document.createTextNode(text));
207 this.statusBarTree.appendChild(element);
208 if (i) {
209 this.statusBarTree.appendChild(document.createTextNode(String.fromCharCode(0xbb)));
210 }
211 }
212 }
213 this.noUpdate = false;
214 },
215
216 /*
217 * Handle statusbar element events
218 */
219 statusBarHandler : function (ev) {
220 if (!ev) {
221 var ev = window.event;
222 }
223 var target = (ev.target) ? ev.target : ev.srcElement;
224 var editor = target.editor;
225 target.blur();
226 if (HTMLArea.is_gecko) {
227 editor.selectNodeContents(target.ancestor);
228 } else {
229 var nodeName = target.ancestor.nodeName.toLowerCase();
230 if (nodeName == "table" || nodeName == "img") {
231 var range = editor._doc.body.createControlRange();
232 range.addElement(target.ancestor);
233 range.select();
234 } else {
235 editor.selectNode(target.ancestor);
236 }
237 }
238 this.setSelection(target.ancestor);
239 this.noUpdate = true;
240 editor.updateToolbar();
241 switch (ev.type) {
242 case "mousedown" :
243 if (HTMLArea.is_ie) {
244 return true;
245 }
246 case "click" :
247 HTMLArea._stopEvent(ev);
248 return false;
249 case "contextmenu" :
250 return editor.getPluginInstance("ContextMenu") ? editor.getPluginInstance("ContextMenu").popupMenu(ev, target.ancestor) : false;
251 }
252 }
253 });