JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.0
[ckeditor.git] / _source / core / focusmanager.js
diff --git a/_source/core/focusmanager.js b/_source/core/focusmanager.js
new file mode 100644 (file)
index 0000000..0a3f514
--- /dev/null
@@ -0,0 +1,123 @@
+/*\r
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+/**\r
+ * @fileOverview Defines the {@link CKEDITOR.focusManager} class, which is used\r
+ *             to handle the focus on editor instances..\r
+ */\r
+\r
+/**\r
+ * Manages the focus activity in an editor instance. This class is to be used\r
+ * mainly by UI elements coders when adding interface elements to CKEditor.\r
+ * @constructor\r
+ * @param {CKEDITOR.editor} editor The editor instance.\r
+ * @example\r
+ */\r
+CKEDITOR.focusManager = function( editor )\r
+{\r
+       if ( editor.focusManager )\r
+               return editor.focusManager;\r
+\r
+       /**\r
+        * Indicates that the editor instance has focus.\r
+        * @type Boolean\r
+        * @example\r
+        * alert( CKEDITOR.instances.editor1.focusManager.hasFocus );  // e.g "true"\r
+        */\r
+       this.hasFocus = false;\r
+\r
+       /**\r
+        * Object used to hold private stuff.\r
+        * @private\r
+        */\r
+       this._ =\r
+       {\r
+               editor : editor\r
+       };\r
+\r
+       return this;\r
+};\r
+\r
+CKEDITOR.focusManager.prototype =\r
+{\r
+       /**\r
+        * Indicates that the editor instance has the focus.\r
+        *\r
+        * This function is not used to set the focus in the editor. Use\r
+        * {@link CKEDITOR.editor#focus} for it instead.\r
+        * @example\r
+        * var editor = CKEDITOR.instances.editor1;\r
+        * <b>editor.focusManager.focus()</b>;\r
+        */\r
+       focus : function()\r
+       {\r
+               if ( this._.timer )\r
+                       clearTimeout( this._.timer );\r
+\r
+               if ( !this.hasFocus )\r
+               {\r
+                       // If another editor has the current focus, we first "blur" it. In\r
+                       // this way the events happen in a more logical sequence, like:\r
+                       //              "focus 1" > "blur 1" > "focus 2"\r
+                       // ... instead of:\r
+                       //              "focus 1" > "focus 2" > "blur 1"\r
+                       if ( CKEDITOR.currentInstance )\r
+                               CKEDITOR.currentInstance.focusManager.forceBlur();\r
+\r
+                       var editor = this._.editor;\r
+\r
+                       editor.container.getFirst().addClass( 'cke_focus' );\r
+\r
+                       this.hasFocus = true;\r
+                       editor.fire( 'focus' );\r
+               }\r
+       },\r
+\r
+       /**\r
+        * Indicates that the editor instance has lost the focus. Note that this\r
+        * functions acts asynchronously with a delay of 100ms to avoid subsequent\r
+        * blur/focus effects. If you want the "blur" to happen immediately, use\r
+        * the {@link #forceBlur} function instead.\r
+        * @example\r
+        * var editor = CKEDITOR.instances.editor1;\r
+        * <b>editor.focusManager.blur()</b>;\r
+        */\r
+       blur : function()\r
+       {\r
+               var focusManager = this;\r
+\r
+               if ( focusManager._.timer )\r
+                       clearTimeout( focusManager._.timer );\r
+\r
+               focusManager._.timer = setTimeout(\r
+                       function()\r
+                       {\r
+                               delete focusManager._.timer;\r
+                               focusManager.forceBlur();\r
+                       }\r
+                       , 100 );\r
+       },\r
+\r
+       /**\r
+        * Indicates that the editor instance has lost the focus. Unlike\r
+        * {@link #blur}, this function is synchronous, marking the instance as\r
+        * "blured" immediately.\r
+        * @example\r
+        * var editor = CKEDITOR.instances.editor1;\r
+        * <b>editor.focusManager.forceBlur()</b>;\r
+        */\r
+       forceBlur : function()\r
+       {\r
+               if ( this.hasFocus )\r
+               {\r
+                       var editor = this._.editor;\r
+\r
+                       editor.container.getFirst().removeClass( 'cke_focus' );\r
+\r
+                       this.hasFocus = false;\r
+                       editor.fire( 'blur' );\r
+               }\r
+       }\r
+};\r