link.html 4.5 KB

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