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