link.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <title></title>
  6. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  7. <script type="text/javascript" src="../internal.js"></script>
  8. <style type="text/css">
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. color: #838383;
  13. }
  14. table {
  15. font-size: 12px;
  16. margin: 10px;
  17. line-height: 30px
  18. }
  19. .txt {
  20. width: 300px;
  21. height: 21px;
  22. line-height: 21px;
  23. border: 1px solid #d7d7d7;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <table>
  29. <tr>
  30. <td><label for="text"> <var id="lang_input_text"></var></label></td>
  31. <td><input class="txt" id="text" type="text" disabled="true"/></td>
  32. </tr>
  33. <tr>
  34. <td><label for="href"> <var id="lang_input_url"></var></label></td>
  35. <td><input class="txt" id="href" type="text"/></td>
  36. </tr>
  37. <tr>
  38. <td><label for="title"> <var id="lang_input_title"></var></label></td>
  39. <td><input class="txt" id="title" type="text"/></td>
  40. </tr>
  41. <tr>
  42. <td colspan="2">
  43. <label for="target"><var id="lang_input_target"></var></label>
  44. <input id="target" type="checkbox"/>
  45. </td>
  46. </tr>
  47. <tr>
  48. <td colspan="2" id="msg"></td>
  49. </tr>
  50. </table>
  51. <script type="text/javascript">
  52. var range = editor.selection.getRange(),
  53. link = range.collapsed ? editor.queryCommandValue("link") : editor.selection.getStart(),
  54. url,
  55. text = $G('text'),
  56. rangeLink = domUtils.findParentByTagName(range.getCommonAncestor(), 'a', true),
  57. orgText;
  58. link = domUtils.findParentByTagName(link, "a", true);
  59. if (link) {
  60. url = utils.html(link.getAttribute('_href') || link.getAttribute('href', 2));
  61. if (rangeLink === link && !link.getElementsByTagName('img').length) {
  62. text.removeAttribute('disabled');
  63. orgText = text.value = link[browser.ie ? 'innerText' : 'textContent'];
  64. } else {
  65. text.setAttribute('disabled', 'true');
  66. text.value = lang.validLink;
  67. }
  68. } else {
  69. if (range.collapsed) {
  70. text.removeAttribute('disabled');
  71. text.value = '';
  72. } else {
  73. text.setAttribute('disabled', 'true');
  74. text.value = lang.validLink;
  75. }
  76. }
  77. $G("title").value = url ? link.title : "";
  78. $G("href").value = url ? url : '';
  79. $G("target").checked = url && link.target == "_blank" ? true : false;
  80. $focus($G("href"));
  81. function handleDialogOk() {
  82. var href = $G('href').value.replace(/^\s+|\s+$/g, '');
  83. if (href) {
  84. if (!hrefStartWith(href, ["http", "/", "ftp://"])) {
  85. href = "http://" + href;
  86. }
  87. var obj = {
  88. 'href': href,
  89. 'target': $G("target").checked ? "_blank" : '_self',
  90. 'title': $G("title").value.replace(/^\s+|\s+$/g, ''),
  91. '_href': href
  92. };
  93. //修改链接内容的情况太特殊了,所以先做到这里了
  94. //todo:情况多的时候,做到command里
  95. if (orgText && text.value != orgText) {
  96. link[browser.ie ? 'innerText' : 'textContent'] = obj.textValue = text.value;
  97. range.selectNode(link).select()
  98. }
  99. if (range.collapsed) {
  100. obj.textValue = text.value;
  101. }
  102. editor.execCommand('link', obj);
  103. dialog.close();
  104. }
  105. }
  106. dialog.onok = handleDialogOk;
  107. $G('href').onkeydown = $G('title').onkeydown = function (evt) {
  108. evt = evt || window.event;
  109. if (evt.keyCode == 13) {
  110. handleDialogOk();
  111. return false;
  112. }
  113. };
  114. $G('href').onblur = function () {
  115. if (!hrefStartWith(this.value, ["http", "/", "ftp://"])) {
  116. $G("msg").innerHTML = "<span style='color: red'>" + lang.httpPrompt + "</span>";
  117. } else {
  118. $G("msg").innerHTML = "";
  119. }
  120. };
  121. function hrefStartWith(href, arr) {
  122. href = href.replace(/^\s+|\s+$/g, '');
  123. for (var i = 0, ai; ai = arr[i++];) {
  124. if (href.indexOf(ai) == 0) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. </script>
  131. </body>
  132. </html>