searchreplace.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. editor.trigger('clearLastSearchResult');
  118. var findtxt = $G("findtxt1").value.replace(/^\s|\s$/g, ""), obj,
  119. replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
  120. if (!findtxt) {
  121. return false;
  122. }
  123. if (findtxt == replacetxt || (!getMatchCase("matchCase1") && findtxt.toLowerCase() == replacetxt.toLowerCase())) {
  124. return false;
  125. }
  126. obj = {
  127. searchStr:findtxt,
  128. dir:1,
  129. casesensitive:getMatchCase("matchCase1"),
  130. replaceStr:replacetxt
  131. };
  132. frCommond(obj);
  133. };
  134. //全部替换
  135. $G("repalceAllBtn").onclick = function () {
  136. var findtxt = $G("findtxt1").value.replace(/^\s|\s$/g, ""), obj,
  137. replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
  138. if (!findtxt) {
  139. return false;
  140. }
  141. if (findtxt == replacetxt || (!getMatchCase("matchCase1") && findtxt.toLowerCase() == replacetxt.toLowerCase())) {
  142. return false;
  143. }
  144. obj = {
  145. searchStr:findtxt,
  146. casesensitive:getMatchCase("matchCase1"),
  147. replaceStr:replacetxt,
  148. all:true
  149. };
  150. var num = frCommond(obj);
  151. if (num) {
  152. $G('replace-msg').innerHTML = lang.countMsg.replace("{#count}", num);
  153. }
  154. };
  155. //执行
  156. var frCommond = function (obj) {
  157. return editor.execCommand("searchreplace", obj);
  158. };
  159. switchTab("searchtab");
  160. dialog.onclose = function(){
  161. editor.trigger('clearLastSearchResult')
  162. };