searchreplace.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. //是否区分大小写
  53. function getMatchCase(id) {
  54. return $G(id).checked ? true : false;
  55. }
  56. //查找
  57. $G("nextFindBtn").onclick = function (txt, dir, mcase) {
  58. var findtxt = $G("findtxt").value, obj;
  59. if (!findtxt) {
  60. return false;
  61. }
  62. obj = {
  63. searchStr: findtxt,
  64. dir: 1,
  65. casesensitive: getMatchCase("matchCase")
  66. };
  67. if (!frCommond(obj)) {
  68. alert(lang.getEnd);
  69. }
  70. };
  71. $G("nextReplaceBtn").onclick = function (txt, dir, mcase) {
  72. var findtxt = $G("findtxt1").value, obj;
  73. if (!findtxt) {
  74. return false;
  75. }
  76. obj = {
  77. searchStr: findtxt,
  78. dir: 1,
  79. casesensitive: getMatchCase("matchCase1")
  80. };
  81. frCommond(obj);
  82. };
  83. $G("preFindBtn").onclick = function (txt, dir, mcase) {
  84. var findtxt = $G("findtxt").value, obj;
  85. if (!findtxt) {
  86. return false;
  87. }
  88. obj = {
  89. searchStr: findtxt,
  90. dir: -1,
  91. casesensitive: getMatchCase("matchCase")
  92. };
  93. if (!frCommond(obj)) {
  94. alert(lang.getStart);
  95. }
  96. };
  97. $G("preReplaceBtn").onclick = function (txt, dir, mcase) {
  98. var findtxt = $G("findtxt1").value, obj;
  99. if (!findtxt) {
  100. return false;
  101. }
  102. obj = {
  103. searchStr: findtxt,
  104. dir: -1,
  105. casesensitive: getMatchCase("matchCase1")
  106. };
  107. frCommond(obj);
  108. };
  109. //替换
  110. $G("repalceBtn").onclick = function () {
  111. var findtxt = $G("findtxt1").value.replace(/^\s|\s$/g, ""), obj,
  112. replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
  113. if (!findtxt) {
  114. return false;
  115. }
  116. if (findtxt == replacetxt || (!getMatchCase("matchCase1") && findtxt.toLowerCase() == replacetxt.toLowerCase())) {
  117. return false;
  118. }
  119. obj = {
  120. searchStr: findtxt,
  121. dir: 1,
  122. casesensitive: getMatchCase("matchCase1"),
  123. replaceStr: replacetxt
  124. };
  125. frCommond(obj);
  126. };
  127. //全部替换
  128. $G("repalceAllBtn").onclick = function () {
  129. var findtxt = $G("findtxt1").value.replace(/^\s|\s$/g, ""), obj,
  130. replacetxt = $G("replacetxt").value.replace(/^\s|\s$/g, "");
  131. if (!findtxt) {
  132. return false;
  133. }
  134. if (findtxt == replacetxt || (!getMatchCase("matchCase1") && findtxt.toLowerCase() == replacetxt.toLowerCase())) {
  135. return false;
  136. }
  137. obj = {
  138. searchStr: findtxt,
  139. casesensitive: getMatchCase("matchCase1"),
  140. replaceStr: replacetxt,
  141. all: true
  142. };
  143. var num = frCommond(obj);
  144. if (num) {
  145. alert(lang.countMsg.replace("{#count}", num));
  146. }
  147. };
  148. //执行
  149. var frCommond = function (obj) {
  150. return editor.execCommand("searchreplace", obj);
  151. };
  152. switchTab("searchtab");