searchreplace.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Created with JetBrains PhpStorm.
  3. * User: xuheng
  4. * Date: 12-9-26
  5. * Time: 下午12:29
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. //清空上次查选的痕迹
  9. editor.firstForSR = 0;
  10. editor.currentRangeForSR = null;
  11. //给tab注册切换事件
  12. /**
  13. * tab点击处理事件
  14. * @param tabHeads
  15. * @param tabBodys
  16. * @param obj
  17. */
  18. function clickHandler( tabHeads,tabBodys,obj ) {
  19. //head样式更改
  20. for ( var k = 0, len = tabHeads.length; k < len; k++ ) {
  21. tabHeads[k].className = "";
  22. }
  23. obj.className = "focus";
  24. //body显隐
  25. var tabSrc = obj.getAttribute( "tabSrc" );
  26. for ( var j = 0, length = tabBodys.length; j < length; j++ ) {
  27. var body = tabBodys[j],
  28. id = body.getAttribute( "id" );
  29. if ( id != tabSrc ) {
  30. body.style.zIndex = 1;
  31. } else {
  32. body.style.zIndex = 200;
  33. }
  34. }
  35. }
  36. /**
  37. * TAB切换
  38. * @param tabParentId tab的父节点ID或者对象本身
  39. */
  40. function switchTab( tabParentId ) {
  41. var tabElements = $G( tabParentId ).children,
  42. tabHeads = tabElements[0].children,
  43. tabBodys = tabElements[1].children;
  44. for ( var i = 0, length = tabHeads.length; i < length; i++ ) {
  45. var head = tabHeads[i];
  46. if ( head.className === "focus" )clickHandler(tabHeads,tabBodys, head );
  47. head.onclick = function () {
  48. clickHandler(tabHeads,tabBodys,this);
  49. }
  50. }
  51. }
  52. $G('searchtab').onmousedown = function(){
  53. $G('search-msg').innerHTML = '';
  54. $G('replace-msg').innerHTML = ''
  55. }
  56. //是否区分大小写
  57. function getMatchCase(id) {
  58. return $G(id).checked ? true : false;
  59. }
  60. //查找
  61. $G("nextFindBtn").onclick = function (txt, dir, mcase) {
  62. var findtxt = $G("findtxt").value, obj;
  63. if (!findtxt) {
  64. return false;
  65. }
  66. obj = {
  67. searchStr:findtxt,
  68. dir:1,
  69. casesensitive:getMatchCase("matchCase")
  70. };
  71. if (!frCommond(obj)) {
  72. var bk = editor.selection.getRange().createBookmark();
  73. $G('search-msg').innerHTML = lang.getEnd;
  74. editor.selection.getRange().moveToBookmark(bk).select();
  75. }
  76. };
  77. $G("nextReplaceBtn").onclick = function (txt, dir, mcase) {
  78. var findtxt = $G("findtxt1").value, obj;
  79. if (!findtxt) {
  80. return false;
  81. }
  82. obj = {
  83. searchStr:findtxt,
  84. dir:1,
  85. casesensitive:getMatchCase("matchCase1")
  86. };
  87. frCommond(obj);
  88. };
  89. $G("preFindBtn").onclick = function (txt, dir, mcase) {
  90. var findtxt = $G("findtxt").value, obj;
  91. if (!findtxt) {
  92. return false;
  93. }
  94. obj = {
  95. searchStr:findtxt,
  96. dir:-1,
  97. casesensitive:getMatchCase("matchCase")
  98. };
  99. if (!frCommond(obj)) {
  100. $G('search-msg').innerHTML = lang.getStart;
  101. }
  102. };
  103. $G("preReplaceBtn").onclick = function (txt, dir, mcase) {
  104. var findtxt = $G("findtxt1").value, obj;
  105. if (!findtxt) {
  106. return false;
  107. }
  108. obj = {
  109. searchStr:findtxt,
  110. dir:-1,
  111. casesensitive:getMatchCase("matchCase1")
  112. };
  113. frCommond(obj);
  114. };
  115. //替换
  116. $G("repalceBtn").onclick = function () {
  117. var findtxt = $G("findtxt1").value.replace(/^\s|\s$/g, ""), obj,
  118. replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
  119. if (!findtxt) {
  120. return false;
  121. }
  122. if (findtxt == replacetxt || (!getMatchCase("matchCase1") && findtxt.toLowerCase() == replacetxt.toLowerCase())) {
  123. return false;
  124. }
  125. obj = {
  126. searchStr:findtxt,
  127. dir:1,
  128. casesensitive:getMatchCase("matchCase1"),
  129. replaceStr:replacetxt
  130. };
  131. frCommond(obj);
  132. };
  133. //全部替换
  134. $G("repalceAllBtn").onclick = function () {
  135. var findtxt = $G("findtxt1").value.replace(/^\s|\s$/g, ""), obj,
  136. replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
  137. if (!findtxt) {
  138. return false;
  139. }
  140. if (findtxt == replacetxt || (!getMatchCase("matchCase1") && findtxt.toLowerCase() == replacetxt.toLowerCase())) {
  141. return false;
  142. }
  143. obj = {
  144. searchStr:findtxt,
  145. casesensitive:getMatchCase("matchCase1"),
  146. replaceStr:replacetxt,
  147. all:true
  148. };
  149. var num = frCommond(obj);
  150. if (num) {
  151. $G('replace-msg').innerHTML = lang.countMsg.replace("{#count}", num);
  152. }
  153. };
  154. //执行
  155. var frCommond = function (obj) {
  156. return editor.execCommand("searchreplace", obj);
  157. };
  158. switchTab("searchtab");