occur_commands.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. var config = require("../config"),
  32. Occur = require("../occur").Occur;
  33. // These commands can be installed in a normal command handler to start occur:
  34. var occurStartCommand = {
  35. name: "occur",
  36. exec: function(editor, options) {
  37. var alreadyInOccur = !!editor.session.$occur;
  38. var occurSessionActive = new Occur().enter(editor, options);
  39. if (occurSessionActive && !alreadyInOccur)
  40. OccurKeyboardHandler.installIn(editor);
  41. },
  42. readOnly: true
  43. };
  44. var occurCommands = [{
  45. name: "occurexit",
  46. bindKey: 'esc|Ctrl-G',
  47. exec: function(editor) {
  48. var occur = editor.session.$occur;
  49. if (!occur) return;
  50. occur.exit(editor, {});
  51. if (!editor.session.$occur) OccurKeyboardHandler.uninstallFrom(editor);
  52. },
  53. readOnly: true
  54. }, {
  55. name: "occuraccept",
  56. bindKey: 'enter',
  57. exec: function(editor) {
  58. var occur = editor.session.$occur;
  59. if (!occur) return;
  60. occur.exit(editor, {translatePosition: true});
  61. if (!editor.session.$occur) OccurKeyboardHandler.uninstallFrom(editor);
  62. },
  63. readOnly: true
  64. }];
  65. var HashHandler = require("../keyboard/hash_handler").HashHandler;
  66. var oop = require("../lib/oop");
  67. function OccurKeyboardHandler() {}
  68. oop.inherits(OccurKeyboardHandler, HashHandler);
  69. ;(function() {
  70. this.isOccurHandler = true;
  71. this.attach = function(editor) {
  72. HashHandler.call(this, occurCommands, editor.commands.platform);
  73. this.$editor = editor;
  74. }
  75. var handleKeyboard$super = this.handleKeyboard;
  76. this.handleKeyboard = function(data, hashId, key, keyCode) {
  77. var cmd = handleKeyboard$super.call(this, data, hashId, key, keyCode);
  78. return (cmd && cmd.command) ? cmd : undefined;
  79. }
  80. }).call(OccurKeyboardHandler.prototype);
  81. OccurKeyboardHandler.installIn = function(editor) {
  82. var handler = new this();
  83. editor.keyBinding.addKeyboardHandler(handler);
  84. editor.commands.addCommands(occurCommands);
  85. }
  86. OccurKeyboardHandler.uninstallFrom = function(editor) {
  87. editor.commands.removeCommands(occurCommands);
  88. var handler = editor.getKeyboardHandler();
  89. if (handler.isOccurHandler)
  90. editor.keyBinding.removeKeyboardHandler(handler);
  91. }
  92. exports.occurStartCommand = occurStartCommand;
  93. });