eggification.
[Epoz.git] / Products / Epoz / epoz / epoz_core / epoz_redirect.js.dtml
1 // This script allow to use several Epoz WYSIWYG fields.
2 // Copyright (C) 2005 Benoît PIN <mailto:pin@cri.ensmp.fr>
3
4 var IFramePrefix = "Iframe_";
5 var IFramePrefixLength = IFramePrefix.length;
6 var DocPrefix = "doc_";
7 var DocPrefixLength = DocPrefix.length;
8 var CheckBoxPrefix = "CB_";
9 var CheckBoxPrefixLength = CheckBoxPrefix.length;
10 var ToolBarPrefix = "ToolBar_";
11 var ToolBarPrefixLength = ToolBarPrefix.length;
12
13
14 function redirectEpoz(iframe) {
15 if(EpozElement) {
16 if (EpozElement == iframe)
17 return;
18 changeBorderStyle(EpozElement, "dashed");
19 unwrapEpozVariables();
20 }
21
22 // update Epoz variables
23 wrapEpozVariables(iframe);
24
25 //cosmetic
26 changeBorderStyle(EpozElement, "solid");
27 EpozElement.contentWindow.focus();
28 }
29
30
31 function wrapEpozVariables(iframe) {
32 try {
33 fieldId = iframe.contentWindow.document.id.slice(DocPrefixLength);
34 }
35 catch (e) {
36 // maybe not an epoz iframe
37 return false;
38 }
39
40 iframe.id = Epoz ;
41 EpozElement=iframe;
42 EpozTextArea = document.getElementById(fieldId);
43 toolBar = document.getElementById(ToolBarPrefix + fieldId);
44 toolBar.id = "EpozToolbar";
45 checkBox = document.getElementById(CheckBoxPrefix + fieldId);
46 checkBox.id = "EpozViewMode";
47 return true;
48 }
49
50
51 function unwrapEpozVariables() {
52 if (!EpozElement) // no redirection happens yet.
53 return;
54 fieldId = EpozElement.contentWindow.document.id.slice(DocPrefixLength);
55
56 EpozElement.id = IFramePrefix + fieldId;
57 toolBar = document.getElementById("EpozToolbar");
58 toolBar.id = ToolBarPrefix + fieldId;
59 checkBox = document.getElementById("EpozViewMode");
60 checkBox.id = CheckBoxPrefix + fieldId;
61 }
62
63
64
65 // Initialization functions
66
67 if (!formDatas) {
68 var formDatas = new Array();
69 }
70
71 function InitIframe(name, data, path, toolbox, style, button, css, customcss, charset, pageurl) {
72 form_name = name;
73 form_data = data;
74 form_path = path;
75 form_toolbox = toolbox;
76 form_area_style = style;
77 form_button_style = button;
78 form_css = css;
79 form_customcss = customcss;
80 form_charset = charset;
81 form_pageurl = pageurl;
82
83 var richText;
84
85 if (browser.isIE55 || browser.isIE6up) {
86 // Mac-IE doesn't support RichText-Edit at the moment
87 if (browser.isMac) {
88 CreateTextarea();
89 richText = false;
90 } else {
91 CreateEpoz();
92 richText = true;
93 }
94 }
95 else if (browser.isGecko) {
96 //check to see if midas is enabled
97 try {
98 // Just a few cleanups for Mozilla
99
100 form_data = form_data.replace(/<strong>/ig,'<b>');
101 form_data = form_data.replace(/<strong(\s[^>]*)>/ig,'<b$1>');
102 form_data = form_data.replace(/<\/strong>/ig,'</b>');
103
104 form_data = form_data.replace(/<em>/ig,'<i>');
105 form_data = form_data.replace(/<em(\s[^>]*)>/ig,'<i$1>');
106 form_data = form_data.replace(/<\/em>/ig,'</i>');
107
108 CreateEpoz();
109 richText = true;
110 } catch (e) {
111 CreateTextarea();
112 richText = false;
113 }
114 }
115 else {
116 CreateTextarea();
117 richText = false;
118 }
119
120 if (richText) {
121 // change iframe id
122 formDatas[name] = form_data;
123 var iframe = document.getElementById(Epoz);
124 iframe.id = IFramePrefix + name;
125
126 // change EpozToolbar id
127 var toolBar = document.getElementById("EpozToolbar");
128 toolBar.id = ToolBarPrefix + name;
129
130 // Retrieve the switch HTML mode checkbox and change id and onclick function.
131 nodes = iframe.parentNode.nextSibling.nextSibling.childNodes;
132 var i;
133 for (i=0 ; i<nodes.length ; i++ ) {
134 if (nodes[i].nodeType == 1 && nodes[i].nodeName == 'INPUT')
135 break;
136 }
137 var checkbox = nodes[i];
138 checkbox.id = CheckBoxPrefix + name;
139
140 changeBorderStyle(iframe, "dashed");
141 }
142 }
143
144
145
146 function HandleEpozRedirect() {
147 if (browser.isIE55 || browser.isIE6up) {
148 iframe = event.srcElement;
149 }
150 else if (browser.isGecko) {
151 iframe = this;
152 }
153 redirectEpoz(iframe);
154 }
155
156 function redirectAndSwitchViewMode(checkbox) {
157 if (checkbox.id != "EpozViewMode") { // Otherwise, EpozElement already targets the good field.
158 var iframe = document.getElementById(IFramePrefix + checkbox.id.slice(CheckBoxPrefixLength));
159 redirectEpoz(iframe);
160 }
161 SwitchViewMode(checkbox.checked);
162 }
163
164
165 // cosmetics
166 function changeBorderStyle(ob, borderStyle) {
167 s = ob.style;
168 s.borderBottomStyle = borderStyle;
169 s.borderLeftStyle = borderStyle;
170 s.borderRightStyle = borderStyle;
171 s.borderTopStyle = borderStyle;
172 }
173
174 // « overloads »
175
176 function InitDocument(iframe) {
177 fieldId = iframe.id.slice(IFramePrefixLength);
178 var data = formDatas[fieldId];
179 delete formDatas[fieldId];
180 ta = document.getElementById(fieldId);
181 iframe.contentWindow.document.body.innerHTML = data;
182 ta.value = data;
183 iframe.contentWindow.document.id = DocPrefix + fieldId;
184 addListener(iframe, "click", HandleEpozRedirect);
185 form = retrieveForm(ta);
186 form.onsubmit=SyncEpoz;
187 if (browser.isGecko) {
188 scriptExpr = 'EnableDesignMode("' + iframe.id + '");';
189 window.setTimeout(scriptExpr, 10);
190 }
191 }
192
193
194 function SyncEpoz() {
195 if (document.getElementsByTagName)
196 var iframes = document.getElementsByTagName("IFRAME");
197 else if (document.all)
198 var iframes = document.all.tags("IFRAME");
199
200
201 for (var i=0;i<iframes.length;i++) {
202
203 unwrapEpozVariables();
204 if(!wrapEpozVariables(iframes[i]))
205 continue;
206
207 var html = TidyHTML(GetHTML());
208
209 // strip trailing whitespace
210 html = (html.replace(/^\s*/,'')).replace(/\s*$/,'');
211
212 // remove single br left by Firefox / Mozilla
213 if (html=="<br />" || html=="<br>" || html=="<p></p>") {
214 html = "";
215 }
216 form_name = EpozElement.contentWindow.document.id.slice(DocPrefixLength);
217 document.getElementById(form_name).value = html;
218 }
219
220 }
221
222 function EnableDesignMode(iframeId) {
223 iframe = document.getElementById(iframeId);
224 try {
225 iframe.contentDocument.designMode = "on";
226 iframe.contentWindow.document.addEventListener("keypress", HandleKeyboardEvent, true);
227 } catch (e) {
228 scriptExpr = 'EnableDesignMode("' + iframeId + '");';
229 setTimeout(scriptExpr, 10);
230 }
231 }
232
233
234
235 //utils
236
237 function retrieveForm(baseOb){
238 var pn = baseOb.parentNode;
239
240 while (pn != document) {
241 if (pn.nodeName == 'FORM')
242 break;
243 else {
244 pn = pn.parentNode;
245 }
246 }
247 return pn;
248 }
249
250
251 function addListener(ob, eventName, functionReference){
252 if (browser.isIE55 || browser.isIE6up) {
253 eventName = "on" + eventName;
254 ob.attachEvent(eventName, functionReference);
255 }
256 else if (browser.isGecko) {
257 ob.addEventListener(eventName, functionReference, false);
258 }
259
260 }