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