]> code.citadel.org Git - citadel.git/blob - webcit/tiny_mce/plugins/advlink/jscripts/functions.js
* Upgraded TinyMCE to version 2.0RC3. This fixes a conflict with
[citadel.git] / webcit / tiny_mce / plugins / advlink / jscripts / functions.js
1 /* Functions for the advlink plugin popup */
2
3 var templates = {
4         "window.open" : "window.open('${url}','${target}','${options}')"
5 };
6
7 function preinit() {
8         // Initialize
9         tinyMCE.setWindowArg('mce_windowresize', false);
10
11         // Import external list url javascript
12         var url = tinyMCE.getParam("external_link_list_url");
13         if (url != null) {
14                 // Fix relative
15                 if (url.charAt(0) != '/')
16                         url = tinyMCE.documentBasePath + "/" + url;
17
18                 document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + url + '"></sc'+'ript>');
19         }
20 }
21
22 function changeClass() {
23         var formObj = document.forms[0];
24         formObj.classes.value = getSelectValue(formObj, 'classlist');
25 }
26
27 function init() {
28         tinyMCEPopup.resizeToInnerSize();
29
30         var formObj = document.forms[0];
31         var inst = tinyMCE.getInstanceById(tinyMCE.getWindowArg('editor_id'));
32         var elm = inst.getFocusElement();
33         var action = "insert";
34
35         // Resize some elements
36         if (isVisible('hrefbrowser'))
37                 document.getElementById('href').style.width = '260px';
38
39         if (isVisible('popupurlbrowser'))
40                 document.getElementById('popupurl').style.width = '180px';
41
42         elm = tinyMCE.getParentElement(elm, "a");
43         if (elm != null && elm.nodeName == "A")
44                 action = "update";
45
46         formObj.insert.value = tinyMCE.getLang('lang_' + action, 'Insert', true); 
47
48         setPopupControlsDisabled(true);
49
50         if (action == "update") {
51                 var href = tinyMCE.getAttrib(elm, 'href');
52
53                 // Fix for drag-drop/copy paste bug in Mozilla
54                 mceRealHref = tinyMCE.getAttrib(elm, 'mce_real_href');
55                 if (mceRealHref != "")
56                         href = mceRealHref;
57
58                 href = convertURL(href, elm, true);
59
60                 var onclick = tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onclick'));
61                 if (onclick == null || onclick == "")
62                         onclick = tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'mce_onclick'));
63
64                 // Setup form data
65                 setFormValue('href', href);
66                 setFormValue('title', tinyMCE.getAttrib(elm, 'title'));
67                 setFormValue('id', tinyMCE.getAttrib(elm, 'id'));
68                 setFormValue('style', elm.style.cssText.toLowerCase());
69                 setFormValue('rel', tinyMCE.getAttrib(elm, 'rel'));
70                 setFormValue('rev', tinyMCE.getAttrib(elm, 'rev'));
71                 setFormValue('charset', tinyMCE.getAttrib(elm, 'charset'));
72                 setFormValue('hreflang', tinyMCE.getAttrib(elm, 'hreflang'));
73                 setFormValue('dir', tinyMCE.getAttrib(elm, 'dir'));
74                 setFormValue('lang', tinyMCE.getAttrib(elm, 'lang'));
75                 setFormValue('tabindex', tinyMCE.getAttrib(elm, 'tabindex', typeof(elm.tabindex) != "undefined" ? elm.tabindex : ""));
76                 setFormValue('accesskey', tinyMCE.getAttrib(elm, 'accesskey', typeof(elm.accesskey) != "undefined" ? elm.accesskey : ""));
77                 setFormValue('type', tinyMCE.getAttrib(elm, 'type'));
78                 setFormValue('onfocus', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onfocus')));
79                 setFormValue('onblur', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onblur')));
80                 setFormValue('onclick', onclick);
81                 setFormValue('ondblclick', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'ondblclick')));
82                 setFormValue('onmousedown', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onmousedown')));
83                 setFormValue('onmouseup', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onmouseup')));
84                 setFormValue('onmouseover', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onmouseover')));
85                 setFormValue('onmousemove', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onmousemove')));
86                 setFormValue('onmouseout', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onmouseout')));
87                 setFormValue('onkeypress', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onkeypress')));
88                 setFormValue('onkeydown', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onkeydown')));
89                 setFormValue('onkeyup', tinyMCE.cleanupEventStr(tinyMCE.getAttrib(elm, 'onkeyup')));
90                 setFormValue('target', tinyMCE.getAttrib(elm, 'target'));
91                 setFormValue('classes', tinyMCE.getAttrib(elm, 'class'));
92
93                 // Parse onclick data
94                 if (onclick != null && onclick.indexOf('window.open') != -1)
95                         parseWindowOpen(onclick);
96                 else
97                         parseFunction(onclick);
98
99                 // Select by the values
100                 selectByValue(formObj, 'dir', tinyMCE.getAttrib(elm, 'dir'));
101                 selectByValue(formObj, 'rel', tinyMCE.getAttrib(elm, 'rel'));
102                 selectByValue(formObj, 'rev', tinyMCE.getAttrib(elm, 'rev'));
103                 selectByValue(formObj, 'linklisthref', href);
104
105                 if (href.charAt(0) == '#')
106                         selectByValue(formObj, 'anchorlist', href);
107
108                 selectByValue(formObj, 'classlist', tinyMCE.getAttrib(elm, 'class'), true);
109                 selectByValue(formObj, 'targetlist', tinyMCE.getAttrib(elm, 'target'), true);
110         }
111
112         addClassesToList('classlist', 'advlink_styles');
113
114         window.focus();
115 }
116
117 function setFormValue(name, value) {
118         document.forms[0].elements[name].value = value;
119 }
120
121 function convertURL(url, node, on_save) {
122         return eval("tinyMCEPopup.windowOpener." + tinyMCE.settings['urlconverter_callback'] + "(url, node, on_save);");
123 }
124
125 function parseWindowOpen(onclick) {
126         var formObj = document.forms[0];
127
128         // Preprocess center code
129         if (onclick.indexOf('return false;') != -1) {
130                 formObj.popupreturn.checked = true;
131                 onclick = onclick.replace('return false;', '');
132         }
133
134         var onClickData = parseLink(onclick);
135
136         if (onClickData != null) {
137                 formObj.ispopup.checked = true;
138                 setPopupControlsDisabled(false);
139
140                 var onClickWindowOptions = parseOptions(onClickData['options']);
141
142                 formObj.popupname.value = onClickData['target'];
143                 formObj.popupurl.value = onClickData['url'];
144                 formObj.popupwidth.value = getOption(onClickWindowOptions, 'width');
145                 formObj.popupheight.value = getOption(onClickWindowOptions, 'height');
146
147                 formObj.popupleft.value = getOption(onClickWindowOptions, 'left');
148                 formObj.popuptop.value = getOption(onClickWindowOptions, 'top');
149
150                 if (formObj.popupleft.value.indexOf('screen') != -1)
151                         formObj.popupleft.value = "c";
152
153                 if (formObj.popuptop.value.indexOf('screen') != -1)
154                         formObj.popuptop.value = "c";
155
156                 formObj.popuplocation.checked = getOption(onClickWindowOptions, 'location') == "yes";
157                 formObj.popupscrollbars.checked = getOption(onClickWindowOptions, 'scrollbars') == "yes";
158                 formObj.popupmenubar.checked = getOption(onClickWindowOptions, 'menubar') == "yes";
159                 formObj.popupresizable.checked = getOption(onClickWindowOptions, 'resizable') == "yes";
160                 formObj.popuptoolbar.checked = getOption(onClickWindowOptions, 'toolbar') == "yes";
161                 formObj.popupstatus.checked = getOption(onClickWindowOptions, 'status') == "yes";
162                 formObj.popupdependent.checked = getOption(onClickWindowOptions, 'dependent') == "yes";
163         }
164 }
165
166 function parseFunction(onclick) {
167         var formObj = document.forms[0];
168         var onClickData = parseLink(onclick);
169
170         // TODO: Add stuff here
171 }
172
173 function getOption(opts, name) {
174         return typeof(opts[name]) == "undefined" ? "" : opts[name];
175 }
176
177 function setPopupControlsDisabled(state) {
178         var formObj = document.forms[0];
179
180         formObj.popupname.disabled = state;
181         formObj.popupurl.disabled = state;
182         formObj.popupwidth.disabled = state;
183         formObj.popupheight.disabled = state;
184         formObj.popupleft.disabled = state;
185         formObj.popuptop.disabled = state;
186         formObj.popuplocation.disabled = state;
187         formObj.popupscrollbars.disabled = state;
188         formObj.popupmenubar.disabled = state;
189         formObj.popupresizable.disabled = state;
190         formObj.popuptoolbar.disabled = state;
191         formObj.popupstatus.disabled = state;
192         formObj.popupreturn.disabled = state;
193         formObj.popupdependent.disabled = state;
194
195         setBrowserDisabled('popupurlbrowser', state);
196 }
197
198 function parseLink(link) {
199         link = link.replace(new RegExp('&#39;', 'g'), "'");
200
201         var fnName = link.replace(new RegExp("\\s*([A-Za-z0-9\.]*)\\s*\\(.*", "gi"), "$1");
202
203         // Is function name a template function
204         var template = templates[fnName];
205         if (template) {
206                 // Build regexp
207                 var variableNames = template.match(new RegExp("'?\\$\\{[A-Za-z0-9\.]*\\}'?", "gi"));
208                 var regExp = "\\s*[A-Za-z0-9\.]*\\s*\\(";
209                 var replaceStr = "";
210                 for (var i=0; i<variableNames.length; i++) {
211                         // Is string value
212                         if (variableNames[i].indexOf("'${") != -1)
213                                 regExp += "'(.*)'";
214                         else // Number value
215                                 regExp += "([0-9]*)";
216
217                         replaceStr += "$" + (i+1);
218
219                         // Cleanup variable name
220                         variableNames[i] = variableNames[i].replace(new RegExp("[^A-Za-z0-9]", "gi"), "");
221
222                         if (i != variableNames.length-1) {
223                                 regExp += "\\s*,\\s*";
224                                 replaceStr += "<delim>";
225                         } else
226                                 regExp += ".*";
227                 }
228
229                 regExp += "\\);?";
230
231                 // Build variable array
232                 var variables = new Array();
233                 variables["_function"] = fnName;
234                 var variableValues = link.replace(new RegExp(regExp, "gi"), replaceStr).split('<delim>');
235                 for (var i=0; i<variableNames.length; i++)
236                         variables[variableNames[i]] = variableValues[i];
237
238                 return variables;
239         }
240
241         return null;
242 }
243
244 function parseOptions(opts) {
245         if (opts == null || opts == "")
246                 return new Array();
247
248         // Cleanup the options
249         opts = opts.toLowerCase();
250         opts = opts.replace(/;/g, ",");
251         opts = opts.replace(/[^0-9a-z=,]/g, "");
252
253         var optionChunks = opts.split(',');
254         var options = new Array();
255
256         for (var i=0; i<optionChunks.length; i++) {
257                 var parts = optionChunks[i].split('=');
258
259                 if (parts.length == 2)
260                         options[parts[0]] = parts[1];
261         }
262
263         return options;
264 }
265
266 function buildOnClick() {
267         var formObj = document.forms[0];
268
269         if (!formObj.ispopup.checked) {
270                 formObj.onclick.value = "";
271                 return;
272         }
273
274         var onclick = "window.open('";
275
276         onclick += formObj.popupurl.value + "','";
277         onclick += formObj.popupname.value + "','";
278
279         if (formObj.popuplocation.checked)
280                 onclick += "location=yes,";
281
282         if (formObj.popupscrollbars.checked)
283                 onclick += "scrollbars=yes,";
284
285         if (formObj.popupmenubar.checked)
286                 onclick += "menubar=yes,";
287
288         if (formObj.popupresizable.checked)
289                 onclick += "resizable=yes,";
290
291         if (formObj.popuptoolbar.checked)
292                 onclick += "toolbar=yes,";
293
294         if (formObj.popupstatus.checked)
295                 onclick += "status=yes,";
296
297         if (formObj.popupdependent.checked)
298                 onclick += "dependent=yes,";
299
300         if (formObj.popupwidth.value != "")
301                 onclick += "width=" + formObj.popupwidth.value + ",";
302
303         if (formObj.popupheight.value != "")
304                 onclick += "height=" + formObj.popupheight.value + ",";
305
306         if (formObj.popupleft.value != "") {
307                 if (formObj.popupleft.value != "c")
308                         onclick += "left=" + formObj.popupleft.value + ",";
309                 else
310                         onclick += "left='+(screen.availWidth/2-" + (formObj.popupwidth.value/2) + ")+',";
311         }
312
313         if (formObj.popuptop.value != "") {
314                 if (formObj.popuptop.value != "c")
315                         onclick += "top=" + formObj.popuptop.value + ",";
316                 else
317                         onclick += "top='+(screen.availHeight/2-" + (formObj.popupheight.value/2) + ")+',";
318         }
319
320         if (onclick.charAt(onclick.length-1) == ',')
321                 onclick = onclick.substring(0, onclick.length-1);
322
323         onclick += "');";
324
325         if (formObj.popupreturn.checked)
326                 onclick += "return false;";
327
328         // tinyMCE.debug(onclick);
329
330         formObj.onclick.value = onclick;
331 }
332
333 function setAttrib(elm, attrib, value) {
334         var formObj = document.forms[0];
335         var valueElm = formObj.elements[attrib.toLowerCase()];
336
337         if (typeof(value) == "undefined" || value == null) {
338                 value = "";
339
340                 if (valueElm)
341                         value = valueElm.value;
342         }
343
344         if (value != "") {
345                 elm.setAttribute(attrib.toLowerCase(), value);
346
347                 if (attrib == "style")
348                         attrib = "style.cssText";
349
350                 if (attrib == "href")
351                         elm.setAttribute("mce_real_href", value);
352
353                 if (attrib.substring(0, 2) == 'on')
354                         value = 'return true;' + value;
355
356                 if (attrib == "class")
357                         attrib = "className";
358
359                 eval('elm.' + attrib + "=value;");
360         } else
361                 elm.removeAttribute(attrib);
362 }
363
364 function renderAnchorList(id, target) {
365         var inst = tinyMCE.getInstanceById(tinyMCE.getWindowArg('editor_id'));
366         var nodes = inst.getBody().getElementsByTagName("a");
367
368         var html = "";
369
370         html += '<tr><td class="column1"><label for="' + id + '">{$lang_advlink_anchor_names}</label></td><td>';
371         html += '<select id="' + id + '" name="' + id + '" class="mceAnchorList" onfocus="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target + '.value=';
372         html += 'this.options[this.selectedIndex].value;">';
373         html += '<option value="">---</option>';
374
375         for (var i=0; i<nodes.length; i++) {
376                 if ((name = tinyMCE.getAttrib(nodes[i], "name")) != "")
377                         html += '<option value="#' + name + '">' + name + '</option>';
378         }
379
380         html += '</select>';
381
382         document.write(html);
383 }
384
385 function insertAction() {
386         var inst = tinyMCE.getInstanceById(tinyMCE.getWindowArg('editor_id'));
387         var elm = inst.getFocusElement();
388
389         elm = tinyMCE.getParentElement(elm, "a");
390
391         tinyMCEPopup.execCommand("mceBeginUndoLevel");
392
393         // Create new anchor elements
394         if (elm == null) {
395                 if (tinyMCE.isSafari)
396                         tinyMCEPopup.execCommand("mceInsertContent", false, '<a href="#mce_temp_url#">' + inst.getSelectedHTML() + '</a>');
397                 else
398                         tinyMCEPopup.execCommand("createlink", false, "#mce_temp_url#");
399
400                 var elementArray = tinyMCE.getElementsByAttributeValue(inst.getBody(), "a", "href", "#mce_temp_url#");
401                 for (var i=0; i<elementArray.length; i++) {
402                         var elm = elementArray[i];
403
404                         // Move cursor behind the new anchor
405                         if (tinyMCE.isGecko) {
406                                 var sp = inst.getDoc().createTextNode(" ");
407
408                                 if (elm.nextSibling)
409                                         elm.parentNode.insertBefore(sp, elm.nextSibling);
410                                 else
411                                         elm.parentNode.appendChild(sp);
412
413                                 // Set range after link
414                                 var rng = inst.getDoc().createRange();
415                                 rng.setStartAfter(elm);
416                                 rng.setEndAfter(elm);
417
418                                 // Update selection
419                                 var sel = inst.getSel();
420                                 sel.removeAllRanges();
421                                 sel.addRange(rng);
422                         }
423
424                         setAllAttribs(elm);
425                 }
426         } else
427                 setAllAttribs(elm);
428
429         tinyMCEPopup.execCommand("mceEndUndoLevel");
430         tinyMCEPopup.close();
431 }
432
433 function setAllAttribs(elm) {
434         var formObj = document.forms[0];
435         var href = formObj.href.value;
436         var target = getSelectValue(formObj, 'targetlist');
437
438         href = convertURL(href, elm);
439
440         setAttrib(elm, 'href', href);
441         setAttrib(elm, 'title');
442         setAttrib(elm, 'target', target == '_self' ? '' : target);
443         setAttrib(elm, 'id');
444         setAttrib(elm, 'style');
445         setAttrib(elm, 'class', getSelectValue(formObj, 'classlist'));
446         setAttrib(elm, 'rel');
447         setAttrib(elm, 'rev');
448         setAttrib(elm, 'charset');
449         setAttrib(elm, 'hreflang');
450         setAttrib(elm, 'dir');
451         setAttrib(elm, 'lang');
452         setAttrib(elm, 'tabindex');
453         setAttrib(elm, 'accesskey');
454         setAttrib(elm, 'type');
455         setAttrib(elm, 'onfocus');
456         setAttrib(elm, 'onblur');
457         setAttrib(elm, 'onclick');
458         setAttrib(elm, 'ondblclick');
459         setAttrib(elm, 'onmousedown');
460         setAttrib(elm, 'onmouseup');
461         setAttrib(elm, 'onmouseover');
462         setAttrib(elm, 'onmousemove');
463         setAttrib(elm, 'onmouseout');
464         setAttrib(elm, 'onkeypress');
465         setAttrib(elm, 'onkeydown');
466         setAttrib(elm, 'onkeyup');
467
468         // Refresh in old MSIE
469         if (tinyMCE.isMSIE5)
470                 elm.outerHTML = elm.outerHTML;
471 }
472
473 function getSelectValue(form_obj, field_name) {
474         var elm = form_obj.elements[field_name];
475
476         if (elm == null || elm.options == null)
477                 return "";
478
479         return elm.options[elm.selectedIndex].value;
480 }
481
482 function renderLinkList(elm_id, target_form_element, onchange_func) {
483         if (typeof(tinyMCELinkList) == "undefined" || tinyMCELinkList.length == 0)
484                 return;
485
486         var html = "";
487
488         html += '<tr><td class="column1"><label for="' + elm_id + '">{$lang_link_list}</label></td>';
489         html += '<td colspan="2"><select id="' + elm_id + '" name="' + elm_id + '"';
490         html += ' class="mceLinkList" onfocus="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target_form_element + '.value=';
491         html += 'this.options[this.selectedIndex].value;';
492
493         if (typeof(onchange_func) != "undefined")
494                 html += onchange_func + '(\'' + target_form_element + '\',this.options[this.selectedIndex].text,this.options[this.selectedIndex].value);';
495
496         html += '"><option value="">---</option>';
497
498         for (var i=0; i<tinyMCELinkList.length; i++)
499                 html += '<option value="' + tinyMCELinkList[i][1] + '">' + tinyMCELinkList[i][0] + '</option>';
500
501         html += '</select></td></tr>';
502
503         document.write(html);
504
505         // tinyMCE.debug('-- image list start --', html, '-- image list end --');
506 }
507
508 function renderTargetList(elm_id, target_form_element) {
509         var targets = tinyMCE.getParam('theme_advanced_link_targets', '').split(';');
510         var html = '';
511
512         html += '<select id="' + elm_id + '" name="' + elm_id + '" onfocus="tinyMCE.addSelectAccessibility(event, this, window);" onchange="this.form.' + target_form_element + '.value=';
513         html += 'this.options[this.selectedIndex].value;">';
514
515         html += '<option value="_self">' + tinyMCE.getLang('lang_advlink_target_same') + '</option>';
516         html += '<option value="_blank">' + tinyMCE.getLang('lang_advlink_target_blank') + ' (_blank)</option>';
517         html += '<option value="_parent">' + tinyMCE.getLang('lang_advlink_target_parent') + ' (_parent)</option>';
518         html += '<option value="_top">' + tinyMCE.getLang('lang_advlink_target_top') + ' (_top)</option>';
519
520         for (var i=0; i<targets.length; i++) {
521                 var key, value;
522
523                 if (targets[i] == "")
524                         continue;
525
526                 key = targets[i].split('=')[0];
527                 value = targets[i].split('=')[1];
528
529                 html += '<option value="' + key + '">' + value + ' (' + key + ')</option>';
530         }
531
532         html += '</select>';
533
534         document.write(html);
535 }
536
537 // While loading
538 preinit();