focusmanager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview Defines the {@link CKEDITOR.focusManager} class, which is used
  7. * to handle the focus on editor instances..
  8. */
  9. /**
  10. * Creates a focusManager class instance.
  11. * @class Manages the focus activity in an editor instance. This class is to be
  12. * used mainly by UI elements coders when adding interface elements that need
  13. * to set the focus state of the editor.
  14. * @param {CKEDITOR.editor} editor The editor instance.
  15. * @example
  16. * var focusManager = <b>new CKEDITOR.focusManager( editor )</b>;
  17. * focusManager.focus();
  18. */
  19. CKEDITOR.focusManager = function( editor )
  20. {
  21. if ( editor.focusManager )
  22. return editor.focusManager;
  23. /**
  24. * Indicates that the editor instance has focus.
  25. * @type Boolean
  26. * @example
  27. * alert( CKEDITOR.instances.editor1.focusManager.hasFocus ); // e.g "true"
  28. */
  29. this.hasFocus = false;
  30. /**
  31. * Object used to hold private stuff.
  32. * @private
  33. */
  34. this._ =
  35. {
  36. editor : editor
  37. };
  38. return this;
  39. };
  40. CKEDITOR.focusManager.prototype =
  41. {
  42. /**
  43. * Used to indicate that the editor instance has the focus.<br />
  44. * <br />
  45. * Note that this function will not explicitelly set the focus in the
  46. * editor (for example, making the caret blinking on it). Use
  47. * {@link CKEDITOR.editor#focus} for it instead.
  48. * @example
  49. * var editor = CKEDITOR.instances.editor1;
  50. * <b>editor.focusManager.focus()</b>;
  51. */
  52. focus : function()
  53. {
  54. if ( this._.timer )
  55. clearTimeout( this._.timer );
  56. if ( !this.hasFocus )
  57. {
  58. // If another editor has the current focus, we first "blur" it. In
  59. // this way the events happen in a more logical sequence, like:
  60. // "focus 1" > "blur 1" > "focus 2"
  61. // ... instead of:
  62. // "focus 1" > "focus 2" > "blur 1"
  63. if ( CKEDITOR.currentInstance )
  64. CKEDITOR.currentInstance.focusManager.forceBlur();
  65. var editor = this._.editor;
  66. editor.container.getChild( 1 ).addClass( 'cke_focus' );
  67. this.hasFocus = true;
  68. editor.fire( 'focus' );
  69. }
  70. },
  71. /**
  72. * Used to indicate that the editor instance has lost the focus.<br />
  73. * <br />
  74. * Note that this functions acts asynchronously with a delay of 100ms to
  75. * avoid subsequent blur/focus effects. If you want the "blur" to happen
  76. * immediately, use the {@link #forceBlur} function instead.
  77. * @example
  78. * var editor = CKEDITOR.instances.editor1;
  79. * <b>editor.focusManager.blur()</b>;
  80. */
  81. blur : function()
  82. {
  83. var focusManager = this;
  84. if ( focusManager._.timer )
  85. clearTimeout( focusManager._.timer );
  86. focusManager._.timer = setTimeout(
  87. function()
  88. {
  89. delete focusManager._.timer;
  90. focusManager.forceBlur();
  91. }
  92. , 100 );
  93. },
  94. /**
  95. * Used to indicate that the editor instance has lost the focus. Unlike
  96. * {@link #blur}, this function is synchronous, marking the instance as
  97. * "blured" immediately.
  98. * @example
  99. * var editor = CKEDITOR.instances.editor1;
  100. * <b>editor.focusManager.forceBlur()</b>;
  101. */
  102. forceBlur : function()
  103. {
  104. if ( this.hasFocus )
  105. {
  106. var editor = this._.editor;
  107. editor.container.getChild( 1 ).removeClass( 'cke_focus' );
  108. this.hasFocus = false;
  109. editor.fire( 'blur' );
  110. }
  111. }
  112. };
  113. /**
  114. * Fired when the editor instance receives the input focus.
  115. * @name CKEDITOR.editor#focus
  116. * @event
  117. * @param {CKEDITOR.editor} editor The editor instance.
  118. * @example
  119. * editor.on( 'focus', function( e )
  120. * {
  121. * alert( 'The editor named ' + e.editor.name + ' is now focused' );
  122. * });
  123. */
  124. /**
  125. * Fired when the editor instance loses the input focus.
  126. * @name CKEDITOR.editor#blur
  127. * @event
  128. * @param {CKEDITOR.editor} editor The editor instance.
  129. * @example
  130. * editor.on( 'blur', function( e )
  131. * {
  132. * alert( 'The editor named ' + e.editor.name + ' lost the focus' );
  133. * });
  134. */