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 			this.hasFocus = true;
 70 			this._.editor.fire( 'focus' );
 71 		}
 72 	},
 73
 74 	/**
 75 	 * Indicates that the editor instance has lost the focus. Note that this
 76 	 * functions acts asynchronously with a delay of 100ms to avoid subsequent
 77 	 * blur/focus effects. If you want the "blur" to happen immediately, use
 78 	 * the {@link #forceBlur} function instead.
 79 	 * @example
 80 	 * var editor = CKEDITOR.instances.editor1;
 81 	 * <b>editor.focusManager.blur()</b>;
 82 	 */
 83 	blur : function()
 84 	{
 85 		var focusManager = this;
 86
 87 		if ( focusManager._.timer )
 88 			clearTimeout( focusManager._.timer );
 89
 90 		focusManager._.timer = setTimeout(
 91 			function()
 92 			{
 93 				delete focusManager._.timer;
 94 				focusManager.forceBlur();
 95 			}
 96 			, 100 );
 97 	},
 98
 99 	/**
100 	 * Indicates that the editor instance has lost the focus. Unlike
101 	 * {@link #blur}, this function is synchronous, marking the instance as
102 	 * "blured" immediately.
103 	 * @example
104 	 * var editor = CKEDITOR.instances.editor1;
105 	 * <b>editor.focusManager.forceBlur()</b>;
106 	 */
107 	forceBlur : function()
108 	{
109 		if ( this.hasFocus )
110 		{
111 			this.hasFocus = false;
112 			this._.editor.fire( 'blur' );
113 		}
114 	}
115 };
116