source: trunk/web/app/plugins/tinymce/jscripts/tiny_mce/plugins/advlink/js/advlink.js @ 1

Last change on this file since 1 was 1, checked in by dj3c1t, 12 years ago

import initial

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