link.html 4.1 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"><label for="target"><var
  43. id="lang_input_target"></var></label> <input id="target" type="checkbox" />
  44. </td>
  45. </tr>
  46. <tr>
  47. <td colspan="2" id="msg"></td>
  48. </tr>
  49. </table>
  50. <script type="text/javascript">
  51. var range = editor.selection.getRange(),
  52. link = range.collapsed ? editor.queryCommandValue( "link" ) : editor.selection.getStart(),
  53. url,
  54. text = $G('text'),
  55. rangeLink = domUtils.findParentByTagName(range.getCommonAncestor(),'a',true),
  56. orgText;
  57. link = domUtils.findParentByTagName( link, "a", true );
  58. if(link){
  59. url = utils.html(link.getAttribute( '_href' ) || link.getAttribute( 'href', 2 ));
  60. if(rangeLink === link && !link.getElementsByTagName('img').length){
  61. text.removeAttribute('disabled');
  62. orgText = text.value = link[browser.ie ? 'innerText':'textContent'];
  63. }else{
  64. text.setAttribute('disabled','true');
  65. text.value = lang.validLink;
  66. }
  67. }else{
  68. if(range.collapsed){
  69. text.removeAttribute('disabled');
  70. text.value = '';
  71. }else{
  72. text.setAttribute('disabled','true');
  73. text.value = lang.validLink;
  74. }
  75. }
  76. $G("title").value = url ? link.title : "";
  77. $G("href").value = url ? url: '';
  78. $G("target").checked = url && link.target == "_blank" ? true : false;
  79. $focus($G("href"));
  80. function handleDialogOk(){
  81. var href =$G('href').value.replace(/^\s+|\s+$/g, '');
  82. if(href){
  83. if(!hrefStartWith(href,["http","/","ftp://",'#'])) {
  84. href = "http://" + href;
  85. }
  86. var obj = {
  87. 'href' : href,
  88. 'target' : $G("target").checked ? "_blank" : '_self',
  89. 'title' : $G("title").value.replace(/^\s+|\s+$/g, ''),
  90. '_href':href
  91. };
  92. //修改链接内容的情况太特殊了,所以先做到这里了
  93. //todo:情况多的时候,做到command里
  94. if(orgText && text.value != orgText){
  95. link[browser.ie ? 'innerText' : 'textContent'] = obj.textValue = text.value;
  96. range.selectNode(link).select()
  97. }
  98. if(range.collapsed){
  99. obj.textValue = text.value;
  100. }
  101. editor.execCommand('link',utils.clearEmptyAttrs(obj) );
  102. dialog.close();
  103. }
  104. }
  105. dialog.onok = handleDialogOk;
  106. $G('href').onkeydown = $G('title').onkeydown = function(evt){
  107. evt = evt || window.event;
  108. if (evt.keyCode == 13) {
  109. handleDialogOk();
  110. return false;
  111. }
  112. };
  113. $G('href').onblur = function(){
  114. if(!hrefStartWith(this.value,["http","/","ftp://",'#'])){
  115. $G("msg").innerHTML = "<span style='color: red'>"+lang.httpPrompt+"</span>";
  116. }else{
  117. $G("msg").innerHTML = "";
  118. }
  119. };
  120. function hrefStartWith(href,arr){
  121. href = href.replace(/^\s+|\s+$/g, '');
  122. for(var i=0,ai;ai=arr[i++];){
  123. if(href.indexOf(ai)==0){
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. </script>
  130. </body>
  131. </html>