searchbox.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var dom = require("ace/lib/dom");
  33. var lang = require("ace/lib/lang");
  34. var event = require("ace/lib/event");
  35. var searchboxCss = require("ace/requirejs/text!ace/ext/searchbox.css");
  36. var HashHandler = require("ace/keyboard/hash_handler").HashHandler;
  37. var keyUtil = require("ace/lib/keys");
  38. //dom.importCssString(searchboxCss, "ace_searchbox");
  39. var html = '<div class="ace_search right">\
  40. <button action="hide" class="ace_searchbtn_close"></button>\
  41. <div class="ace_search_form form-horizontal">\
  42. <input class="ace_search_field" placeholder="Search for" spellcheck="false">\
  43. <button action="findNext" type="button" class="btn ace_searchbtn next">\
  44. <i action="findNext" class="icon-chevron-down"></i>\
  45. </button>\
  46. <button action="findPrev" type="button" class="btn ace_searchbtn prev">\
  47. <i action="findPrev" class="icon-chevron-up"></i>\
  48. </button>\
  49. </div>\
  50. <div class="ace_replace_form form-horizontal">\
  51. <input class="ace_search_field" placeholder="Replace with" spellcheck="false">\
  52. <button action="replace" class="btn ace_replacebtn">Replace</button>\
  53. <button action="replaceAll" class="btn ace_replacebtn">All</button>\
  54. </div>\
  55. </div>'.replace(/>\s+/g, ">");
  56. var SearchBox = function(editor, range, showReplaceForm) {
  57. var div = dom.createElement("div");
  58. div.innerHTML = html;
  59. this.element = div.firstChild;
  60. this.$init();
  61. this.setEditor(editor);
  62. };
  63. (function() {
  64. this.setEditor = function(editor) {
  65. editor.searchBox = this;
  66. editor.container.appendChild(this.element);
  67. this.editor = editor;
  68. };
  69. this.$init = function() {
  70. var sb = this.element;
  71. this.searchBox = sb.querySelector(".ace_search_form");
  72. this.replaceBox = sb.querySelector(".ace_replace_form");
  73. this.searchInput = this.searchBox.querySelector(".ace_search_field");
  74. this.replaceInput = this.replaceBox.querySelector(".ace_search_field");
  75. var _this = this;
  76. event.addListener(sb, "mousedown", function(e) {
  77. setTimeout(function(){
  78. _this.activeInput.focus();
  79. }, 0);
  80. event.stopPropagation(e);
  81. });
  82. event.addListener(sb, "click", function(e) {
  83. var t = e.target;
  84. var action = t.getAttribute("action");
  85. if (action && _this[action])
  86. _this[action]();
  87. event.stopPropagation(e);
  88. });
  89. event.addCommandKeyListener(sb, function(e, hashId, keyCode) {
  90. var keyString = keyUtil.keyCodeToString(keyCode);
  91. var command = _this.$searchBarKb.findKeyCommand(hashId, keyString);
  92. if (command && command.exec) {
  93. command.exec(_this);
  94. event.stopEvent(e);
  95. }
  96. });
  97. this.$onChange = lang.delayedCall(function() {
  98. _this.find(false, false);
  99. });
  100. event.addListener(this.searchInput, "input", function() {
  101. _this.$onChange.schedule(20);
  102. });
  103. event.addListener(this.searchInput, "focus", function() {
  104. _this.activeInput = _this.searchInput;
  105. });
  106. event.addListener(this.replaceInput, "focus", function() {
  107. _this.activeInput = _this.replaceInput;
  108. });
  109. };
  110. //keybinging outsite of the searchbox
  111. /*this.$closeSearchBarKb = new HashHandler([{
  112. bindKey: "Esc",
  113. name: "closeSearchBar",
  114. exec: function(editor) {
  115. editor.searchBox.hide();
  116. }
  117. }]);*/
  118. //keybinging outsite of the searchbox
  119. this.$searchBarKb = new HashHandler();
  120. this.$searchBarKb.bindKeys({
  121. "Ctrl-f|Command-f|Ctrl-H|Command-Option-F": function(sb) {
  122. var isReplace = sb.isReplace = !sb.isReplace;
  123. //sb.replaceBox.style.display = isReplace ? "" : "none";
  124. sb[isReplace ? "replaceInput" : "searchInput"].focus();
  125. },
  126. "esc": function(sb) {
  127. setTimeout(function() { sb.hide();});
  128. },
  129. "Return": function(sb) {
  130. if (sb.activeInput == sb.replaceInput)
  131. sb.replace();
  132. sb.findNext();
  133. },
  134. "Shift-Return": function(sb) {
  135. if (sb.activeInput == sb.replaceInput)
  136. sb.replace();
  137. sb.findPrev();
  138. },
  139. "Tab": function(sb) {
  140. (sb.activeInput == sb.replaceInput ? sb.searchInput : sb.replaceInput).focus();
  141. }
  142. });
  143. this.find = function(skipCurrent, backwards) {
  144. this.editor.find(this.searchInput.value, {
  145. skipCurrent: skipCurrent,
  146. backwards: backwards,
  147. wrap: true
  148. });
  149. this.editor.session.highlight(this.editor.$search.$options.re);
  150. };
  151. this.findNext = function() {
  152. this.find(true, false);
  153. };
  154. this.findPrev = function() {
  155. this.find(true, true);
  156. };
  157. this.replace = function() {
  158. this.editor.replace(this.replaceInput.value);
  159. this.findNext();
  160. };
  161. this.replaceAll = function() {
  162. this.editor.replaceAll(this.replaceInput.value);
  163. };
  164. this.hide = function () {
  165. this.element.style.display = "none";
  166. this.editor.keyBinding.removeKeyboardHandler(this.$searchKeybingin);
  167. this.editor.focus();
  168. };
  169. this.show = function(value, isReplace) {
  170. this.element.style.display = "";
  171. //this.replaceBox.style.display = isReplace ? "" : "none";
  172. this.isReplace = isReplace;
  173. if (value)
  174. this.searchInput.value = value;
  175. this.searchInput.focus();
  176. this.searchInput.select();
  177. this.editor.keyBinding.addKeyboardHandler(this.$closeSearchBarKb);
  178. };
  179. }).call(SearchBox.prototype);
  180. exports.SearchBox = SearchBox;
  181. exports.Search = function(editor, isReplace) {
  182. var sb = editor.searchBox || new SearchBox(editor);
  183. sb.show(editor.session.getTextRange(), isReplace);
  184. };
  185. exports.ISearch = function(session, options) {
  186. this.$changeListener = this.$changeListener.bind(this);
  187. this.startRange = session.selection.toOrientedRange();
  188. this.options = options || {};
  189. };
  190. (function(){
  191. this.setSession = function(session) {
  192. if (this.session) {
  193. this.session.removeListener(this.$changeListener);
  194. }
  195. this.session = session;
  196. this.session.addListener(this.$changeListener);
  197. };
  198. this.setSearchString = function() {
  199. };
  200. this.getValue = function() {
  201. if (this.value == null)
  202. this.value = this.session.getValue();
  203. return this.value;
  204. };
  205. this.$changeListener = function() {
  206. this.value = null;
  207. };
  208. this.find = function() {
  209. };
  210. this.$edgeBefore = function() {
  211. this.cursor = this.startRange[this.options.backwards ? "start" : "end"];
  212. };
  213. this.$edgeAfter = function() {
  214. };
  215. this.next = function(dir) {
  216. };
  217. }).call(exports.ISearch.prototype);
  218. });
  219. /* ------------------------------------------------------------------------------------------
  220. * TODO
  221. * --------------------------------------------------------------------------------------- */
  222. /*
  223. - move search form to the left if it masks current word
  224. - includ all options that search has. ex: regex
  225. - searchbox.searchbox is not that pretty. we should have just searchbox
  226. - disable prev button if it makes sence
  227. */