OSDN Git Service

Fix the bug of X.NodeAnime.
[pettanr/clientJs.git] / 0.6.x / js / 01_core / 18_FocusUtility.js
1
2
3 var FocusUtility_lastElmFocused;
4 var FocusUtility_docActiveElmSupport = X_UA[ 'IE' ] || document.activeElement !== undefined;
5 var FocusUtility_fixActiveElm;
6
7 // http://www.codingforums.com/javascript-programming/19503-determining-focus-ns6-ie6-context-sensitive-help.html
8 // https://developer.mozilla.org/ja/docs/Web/API/Document/activeElement
9 // IE 4+, Chrome 2+, Safari 4+, Firefox 3+ Opera 9.6+
10
11 function FocusUtility_getFocusedElement(){
12         return FocusUtility_fixActiveElm ||
13                 (
14                         X_Script_gte15 ?
15                                 X_Script_try( X_Object_find, [ document, 'activeElement' ] ) :
16                                 // ieは iframe 内で focus がない場合に activeElement に触ると エラーになる
17                                 // VBS 経由で activeElement に触り安全確認する
18                                 ( window[ 'vbs_testAE' ]() && document.activeElement )
19                 );
20 };
21
22 /*
23  * 通常の focus は xnode.call('focus') を使う.
24  * フレームワーク内部で API の実行のために一時的に focus を当てたい場合に使う.focus の復帰は自動で行われる
25  */
26 function FocusUtility_setTemporarilyFocus( elm ){
27         var elmActive = FocusUtility_getFocusedElement();
28         
29         if( elmActive !== elm ){
30                 X_EventDispatcher_ignoreActualEvent = true; // ignoreAllEvent focus, blur
31                 elm.focus();
32                 X_EventDispatcher_ignoreActualEvent = false;
33                 if( !FocusUtility_lastElmFocused ){
34                         FocusUtility_lastElmFocused = elmActive;
35                         // ie5(IE11のエミュレーション) でしばしば空なんですけど...
36                         elmActive && ExecuteAtEnd_add( FocusUtility_restoreFocus );
37                 };
38         };
39 };
40
41 // こっそり復帰
42 function FocusUtility_restoreFocus(){
43         var elmActive = FocusUtility_getFocusedElement(),
44                 elm = FocusUtility_lastElmFocused;
45
46         if( elmActive !== elm ){
47                 X_EventDispatcher_ignoreActualEvent = true;
48                 elm.focus();
49                 X_EventDispatcher_ignoreActualEvent = false;
50         };
51         FocusUtility_lastElmFocused = null;
52 };
53