link.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. tinyMCEPopup.requireLangPack();
  2. var LinkDialog = {
  3. preInit : function() {
  4. var url;
  5. if (url = tinyMCEPopup.getParam("external_link_list_url"))
  6. document.write('<script language="javascript" type="text/javascript" src="' + tinyMCEPopup.editor.documentBaseURI.toAbsolute(url) + '"></script>');
  7. },
  8. init : function() {
  9. var f = document.forms[0], ed = tinyMCEPopup.editor;
  10. // Setup browse button
  11. document.getElementById('hrefbrowsercontainer').innerHTML = getBrowserHTML('hrefbrowser', 'href', 'file', 'theme_advanced_link');
  12. if (isVisible('hrefbrowser'))
  13. document.getElementById('href').style.width = '180px';
  14. this.fillClassList('class_list');
  15. this.fillFileList('link_list', 'tinyMCELinkList');
  16. this.fillTargetList('target_list');
  17. if (e = ed.dom.getParent(ed.selection.getNode(), 'A')) {
  18. f.href.value = ed.dom.getAttrib(e, 'href');
  19. f.linktitle.value = ed.dom.getAttrib(e, 'title');
  20. f.insert.value = ed.getLang('update');
  21. selectByValue(f, 'link_list', f.href.value);
  22. selectByValue(f, 'target_list', ed.dom.getAttrib(e, 'target'));
  23. selectByValue(f, 'class_list', ed.dom.getAttrib(e, 'class'));
  24. }
  25. },
  26. update : function() {
  27. var f = document.forms[0], ed = tinyMCEPopup.editor, e, b, href = f.href.value.replace(/ /g, '%20');
  28. tinyMCEPopup.restoreSelection();
  29. e = ed.dom.getParent(ed.selection.getNode(), 'A');
  30. // Remove element if there is no href
  31. if (!f.href.value) {
  32. if (e) {
  33. b = ed.selection.getBookmark();
  34. ed.dom.remove(e, 1);
  35. ed.selection.moveToBookmark(b);
  36. tinyMCEPopup.execCommand("mceEndUndoLevel");
  37. tinyMCEPopup.close();
  38. return;
  39. }
  40. }
  41. // Create new anchor elements
  42. if (e == null) {
  43. ed.getDoc().execCommand("unlink", false, null);
  44. tinyMCEPopup.execCommand("mceInsertLink", false, "#mce_temp_url#", {skip_undo : 1});
  45. tinymce.each(ed.dom.select("a"), function(n) {
  46. if (ed.dom.getAttrib(n, 'href') == '#mce_temp_url#') {
  47. e = n;
  48. ed.dom.setAttribs(e, {
  49. href : href,
  50. title : f.linktitle.value,
  51. target : f.target_list ? getSelectValue(f, "target_list") : null,
  52. 'class' : f.class_list ? getSelectValue(f, "class_list") : null
  53. });
  54. }
  55. });
  56. } else {
  57. ed.dom.setAttribs(e, {
  58. href : href,
  59. title : f.linktitle.value
  60. });
  61. if (f.target_list) {
  62. ed.dom.setAttrib(e, 'target', getSelectValue(f, "target_list"));
  63. }
  64. if (f.class_list) {
  65. ed.dom.setAttrib(e, 'class', getSelectValue(f, "class_list"));
  66. }
  67. }
  68. // Don't move caret if selection was image
  69. if (e.childNodes.length != 1 || e.firstChild.nodeName != 'IMG') {
  70. ed.focus();
  71. ed.selection.select(e);
  72. ed.selection.collapse(0);
  73. tinyMCEPopup.storeSelection();
  74. }
  75. tinyMCEPopup.execCommand("mceEndUndoLevel");
  76. tinyMCEPopup.close();
  77. },
  78. checkPrefix : function(n) {
  79. if (n.value && Validator.isEmail(n) && !/^\s*mailto:/i.test(n.value) && confirm(tinyMCEPopup.getLang('advanced_dlg.link_is_email')))
  80. n.value = 'mailto:' + n.value;
  81. if (/^\s*www\./i.test(n.value) && confirm(tinyMCEPopup.getLang('advanced_dlg.link_is_external')))
  82. n.value = 'http://' + n.value;
  83. },
  84. fillFileList : function(id, l) {
  85. var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
  86. l = window[l];
  87. if (l && l.length > 0) {
  88. lst.options[lst.options.length] = new Option('', '');
  89. tinymce.each(l, function(o) {
  90. lst.options[lst.options.length] = new Option(o[0], o[1]);
  91. });
  92. } else
  93. dom.remove(dom.getParent(id, 'tr'));
  94. },
  95. fillClassList : function(id) {
  96. var dom = tinyMCEPopup.dom, lst = dom.get(id), v, cl;
  97. if (v = tinyMCEPopup.getParam('theme_advanced_styles')) {
  98. cl = [];
  99. tinymce.each(v.split(';'), function(v) {
  100. var p = v.split('=');
  101. cl.push({'title' : p[0], 'class' : p[1]});
  102. });
  103. } else
  104. cl = tinyMCEPopup.editor.dom.getClasses();
  105. if (cl.length > 0) {
  106. lst.options[lst.options.length] = new Option(tinyMCEPopup.getLang('not_set'), '');
  107. tinymce.each(cl, function(o) {
  108. lst.options[lst.options.length] = new Option(o.title || o['class'], o['class']);
  109. });
  110. } else
  111. dom.remove(dom.getParent(id, 'tr'));
  112. },
  113. fillTargetList : function(id) {
  114. var dom = tinyMCEPopup.dom, lst = dom.get(id), v;
  115. lst.options[lst.options.length] = new Option(tinyMCEPopup.getLang('not_set'), '');
  116. lst.options[lst.options.length] = new Option(tinyMCEPopup.getLang('advanced_dlg.link_target_same'), '_self');
  117. lst.options[lst.options.length] = new Option(tinyMCEPopup.getLang('advanced_dlg.link_target_blank'), '_blank');
  118. if (v = tinyMCEPopup.getParam('theme_advanced_link_targets')) {
  119. tinymce.each(v.split(','), function(v) {
  120. v = v.split('=');
  121. lst.options[lst.options.length] = new Option(v[0], v[1]);
  122. });
  123. }
  124. }
  125. };
  126. LinkDialog.preInit();
  127. tinyMCEPopup.onInit.add(LinkDialog.init, LinkDialog);