2 //### Epoz - a cross-browser-wysiwyg-editor for Zope
3 //## Copyright (C) 2004 Maik Jablonski (maik.jablonski@uni-bielefeld.de)
6 // Just to prevent typos when fetching the Epoz-IFrame...
8 var Epoz = "EpozEditor";
10 // Speed-Up-Storage for document.getElementById(Epoz);
17 var form_data; // the document-data
18 var form_name; // the name of the form-element
19 var form_path; // path to buttons, font-selectors, ...
20 var form_toolbox; // path to optional toolbox
21 var form_area_style; // css-definition for wysiwyg-area
22 var form_button_style; // css-definition for buttons
23 var form_css; // css-style for iframe
24 var form_customcss; // customized css-style for iframe
25 var form_charset; // charset for iframe
26 var form_pageurl; // real url for the edited page
29 // Returns the current HTML.
31 function GetHTML(source_mode) {
32 if (source_mode == null)
33 source_mode = document.getElementById('EpozViewMode').checked;
35 return EpozTextArea.value;
39 return EpozElement.contentWindow.document.body.innerHTML;
41 return EpozElement.value;
46 // Just a XMLRPC to a web-service to clean up the html
48 function TidyHTML(html) {
49 window.status = EpozLang["TidyStart"];
51 // Call EpozTidy one step above the given pageurl.
52 // This should avoid some problems with VHM & PageTemplates etc.
53 xmlrpchost = form_pageurl + "/..";
54 result = XMLRPC.call(xmlrpchost, "EpozTidy", html, form_pageurl);
58 errordata = result[2];
61 window.status = EpozLang["TidyError"];
65 window.status = EpozLang["TidyStop"];
73 // -------------------------------------------------------------
74 // Here are the definitions for the control-and-format-functions
76 // Format text with RichText-Controls
78 function FormatText(command, option) {
79 EpozElement.contentWindow.focus();
81 // Mozilla inserts css-styles per default
83 if (browser.isGecko) {
84 EpozElement.contentWindow.document.execCommand('useCSS',false, true);
87 EpozElement.contentWindow.document.execCommand(command, false, option);
91 // Insert arbitrary HTML at current selection
93 function InsertHTML(html) {
95 EpozElement.contentWindow.focus();
97 if (browser.isIE5up) {
98 selection = EpozElement.contentWindow.document.selection;
99 range = selection.createRange();
101 range.pasteHTML(html);
103 // catch error when range is evil for IE
106 selection = EpozElement.contentWindow.window.getSelection();
107 EpozElement.contentWindow.focus();
109 range = selection.getRangeAt(0);
111 range = EpozElement.contentWindow.document.createRange();
114 var fragment = EpozElement.contentWindow.document.createDocumentFragment();
115 var div = EpozElement.contentWindow.document.createElement("div");
116 div.innerHTML = html;
118 while (div.firstChild) {
119 fragment.appendChild(div.firstChild);
122 selection.removeAllRanges();
123 range.deleteContents();
125 var node = range.startContainer;
126 var pos = range.startOffset;
128 switch (node.nodeType) {
130 if (fragment.nodeType == 3) {
131 node.insertData(pos, fragment.data);
132 range.setEnd(node, pos + fragment.length);
133 range.setStart(node, pos + fragment.length);
135 node = node.splitText(pos);
136 node.parentNode.insertBefore(fragment, node);
137 range.setEnd(node, pos + fragment.length);
138 range.setStart(node, pos + fragment.length);
143 node = node.childNodes[pos];
144 node.parentNode.insertBefore(fragment, node);
145 range.setEnd(node, pos + fragment.length);
146 range.setStart(node, pos + fragment.length);
149 selection.addRange(range);
154 // Create an anchor - no browser supports this directly
156 function CreateAnchor(name) {
157 name = prompt(EpozLang["EnterAnchorName"], "");
159 anchorhtml = '<a name="' + name + '" title="' + name + '"></a>';
160 InsertHTML(anchorhtml);
165 // Create a Hyperlink - IE has its own implementation
167 function CreateLink(URL) {
168 if (browser.isIE5up == false && ((URL == null) || (URL == ""))) {
169 URL = prompt(EpozLang["EnterLinkURL"], "");
171 if ((URL != null) && (URL != "")) {
172 EpozElement.contentWindow.document.execCommand("CreateLink",false,URL)
174 EpozElement.contentWindow.document.execCommand("Unlink",false, "")
177 EpozElement.contentWindow.document.execCommand("CreateLink",false,URL)
182 // Insert image via a URL
184 function CreateImage(URL) {
185 if ((URL == null) || (URL == "")) {
186 URL = prompt(EpozLang["EnterImageURL"], "");
188 if ((URL != null) && (URL != "")) {
189 EpozElement.contentWindow.focus()
190 EpozElement.contentWindow.document.execCommand('InsertImage', false, URL);
195 // Creates a simple table
197 function CreateTable(rows, cols, border, head) {
198 rows = parseInt(rows);
199 cols = parseInt(cols);
201 if ((rows > 0) && (cols > 0)) {
202 table = ' <table border="' + border + '">\n';
204 for (var i=0; i < rows; i++) {
205 table = table + " <tr>\n";
206 for (var j=0; j < cols; j++) {
207 if(i==0 && head=="1") {
208 table += " <th>#</th>\n";
210 table += " <td>#</td>\n";
215 table += " </table>\n";
218 EpozElement.contentWindow.focus()
222 // Sets selected formats
224 function SelectFormat(selectname)
226 // First one is only a label
227 if (selectname.selectedIndex != 0) {
228 EpozElement.contentWindow.document.execCommand(selectname.id, false, selectname.options[selectname.selectedIndex].value);
229 selectname.selectedIndex = 0;
231 EpozElement.contentWindow.focus();
235 // Sets foreground-color
237 function SetTextColor() {
238 EpozColorCommand='forecolor';
239 window.open(form_path+'epoz_script_color.html','EpozColor','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=220,height=220');
242 // Sets background-color
244 function SetBackColor() {
245 EpozColorCommand='backcolor';
246 window.open(form_path+'epoz_script_color.html','EpozColor','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=220,height=220');
249 // Submit color-command to Rich-Text-Controls
251 function SetColor(color) {
253 if (browser.isGecko) {
254 EpozElement.contentWindow.document.execCommand('useCSS',false, false);
257 EpozElement.contentWindow.document.execCommand(EpozColorCommand, false, color);
258 EpozElement.contentWindow.focus();
261 // Switch between Source- and Wysiwyg-View
263 function SwitchViewMode(source_mode)
265 var html = GetHTML(!source_mode);
268 EpozTextArea.value=TidyHTML(html);
269 document.getElementById("EpozToolbar").style.display="none";
270 EpozTextArea.style.display="inline";
272 html = html.replace('<script ', '<epoz:script style="display: none" ')
273 html = html.replace('</script>', '</epoz:script>')
275 EpozElement.contentWindow.document.body.innerHTML = html;
276 document.getElementById("EpozToolbar").style.display="inline";
277 EpozTextArea.style.display="none";
279 if (browser.isGecko) {
280 EpozElement.contentDocument.designMode = "on";
285 // Keyboard-Handler for Mozilla (supports same shortcuts as IE)
287 function HandleKeyboardEvent(event)
290 var key = String.fromCharCode(event.charCode).toLowerCase();
292 case 'b': FormatText('bold',''); event.preventDefault(); break;
293 case 'i': FormatText('italic',''); event.preventDefault(); break;
294 case 'u': FormatText('underline',''); event.preventDefault(); break;
295 case 'k': CreateLink(); event.preventDefault(); break;