Skip to content

Commit

Permalink
FocusImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
duckpilot committed Jun 13, 2010
1 parent 458e139 commit c3c9238
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/ojox/dOMTypes.ml
Expand Up @@ -117,6 +117,7 @@ object
method _set_lang : string -> unit
method _set_scrollLeft : int -> unit
method _set_scrollTop : int -> unit
method _set_tabIndex : int -> unit
end

class type anchorElement =
Expand Down
1 change: 1 addition & 0 deletions src/ojox/dOMTypes.mli
Expand Up @@ -119,6 +119,7 @@ object
method _set_lang : string -> unit
method _set_scrollLeft : int -> unit
method _set_scrollTop : int -> unit
method _set_tabIndex : int -> unit
end

class type anchorElement =
Expand Down
7 changes: 7 additions & 0 deletions src/ojox/element.mli
Expand Up @@ -462,6 +462,13 @@ val setScrollLeft : #element -> int -> unit
*)
val setScrollTop : #element -> int -> unit

(**
The index that represents the element's position in the tabbing order.
@see <a href="http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-tabindex">W3C HTML Specification</a>
*)
val setTabIndex : #element -> int -> unit

(**
The element's advisory title.
*)
Expand Down
35 changes: 31 additions & 4 deletions src/ojox/focusImpl.mli
@@ -1,4 +1,31 @@
val getTabIndex : DOMTypes.element -> int
val focus : DOMTypes.element -> unit
val blur : DOMTypes.element -> unit
val setTabIndex : DOMTypes.element -> int -> unit
(*
* This file is part of ojox, a library for web browser programming
* Copyright (C) 2009 Jacob Donham
* Original file (user/src/com/google/gwt/event/user/client/ui/impl/FocusImpl.java
* in the GWT source distribution) is Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*)

open DOMTypes

(**
Implementation interface for creating and manipulating focusable elements
that aren't naturally focusable in all browsers, such as DIVs.
*)
val blur : element -> unit
val createFocusable : unit -> element
val focus : element -> unit
val getTabIndex : element -> int
val setAccessKey : element -> char -> unit
val setTabIndex : element -> int -> unit
35 changes: 35 additions & 0 deletions src/ojox/focusImplAbstract.ml
@@ -0,0 +1,35 @@
(*
* This file is part of ojox, a library for web browser programming
* Copyright (C) 2009 Jacob Donham
* Original file (user/src/com/google/gwt/event/user/client/ui/impl/FocusImpl.java
* in the GWT source distribution) is Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*)

open Ocamljs.Inline

let blur = Element.blur

let createFocusable () =
let e = Document.createDivElement (Document.get ()) in
Element.setTabIndex e 0;
e

let focus = Element.focus

let getTabIndex = Element.getTabIndex

let setAccessKey elem key = <:stmt< $elem$.accessKey = String.fromCharCode($key$); >>

let setTabIndex = Element.setTabIndex
20 changes: 20 additions & 0 deletions src/ojox/focusImplForPanel.ml
@@ -0,0 +1,20 @@
(*
* This file is part of ojox, a library for web browser programming
* Copyright (C) 2009 Jacob Donham
* Original file (user/src/com/google/gwt/event/user/client/ui/impl/FocusImpl.java
* in the GWT source distribution) is Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*)

include FocusImplSafari
36 changes: 36 additions & 0 deletions src/ojox/focusImplForPanel.mli
@@ -0,0 +1,36 @@
(*
* This file is part of ojox, a library for web browser programming
* Copyright (C) 2009 Jacob Donham
* Original file (user/src/com/google/gwt/event/user/client/ui/impl/FocusImpl.java
* in the GWT source distribution) is Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*)

open DOMTypes

(**
Implementation interface for creating and manipulating focusable elements
that aren't naturally focusable in all browsers, such as DIVs.
*)
(**
Returns the focus implementation class for creating and manipulating
focusable elements that aren't naturally focusable in all browsers, such as
DIVs.
*)
val blur : element -> unit
val createFocusable : unit -> element
val focus : element -> unit
val getTabIndex : element -> int
val setAccessKey : element -> char -> unit
val setTabIndex : element -> int -> unit
20 changes: 20 additions & 0 deletions src/ojox/focusImplForWidget.ml
@@ -0,0 +1,20 @@
(*
* This file is part of ojox, a library for web browser programming
* Copyright (C) 2009 Jacob Donham
* Original file (user/src/com/google/gwt/event/user/client/ui/impl/FocusImpl.java
* in the GWT source distribution) is Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*)

include FocusImplAbstract
35 changes: 35 additions & 0 deletions src/ojox/focusImplForWidget.mli
@@ -0,0 +1,35 @@
(*
* This file is part of ojox, a library for web browser programming
* Copyright (C) 2009 Jacob Donham
* Original file (user/src/com/google/gwt/event/user/client/ui/impl/FocusImpl.java
* in the GWT source distribution) is Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*)

open DOMTypes

(**
Implementation interface for creating and manipulating focusable elements
that aren't naturally focusable in all browsers, such as DIVs.
*)
(**
Returns the focus implementation class for manipulating focusable elements
that are naturally focusable in all browsers, such as text boxes.
*)
val blur : element -> unit
val createFocusable : unit -> element
val focus : element -> unit
val getTabIndex : element -> int
val setAccessKey : element -> char -> unit
val setTabIndex : element -> int -> unit
44 changes: 44 additions & 0 deletions src/ojox/focusImplSafari.ml
@@ -0,0 +1,44 @@
(*
* This file is part of ojox, a library for web browser programming
* Copyright (C) 2009 Jacob Donham
* Original file (user/src/com/google/gwt/event/user/client/ui/impl/FocusImplSafari.java
* in the GWT source distribution) is Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*)

(**
Safari-specific implementation of {@link FocusImpl} that creates a completely
transparent hidden element, since Safari will not keyboard focus on an input
element that has zero width and height.
*)

open Ocamljs.Inline

include FocusImplStandard

let blur elem = <:stmt<
// Attempts to blur elements from within an event callback will generally
// be unsuccessful, so we invoke blur() from outside of the callback.
$$wnd.setTimeout(function() {
$elem$.blur();
}, 0);
>>

let focus elem = <:stmt<
// Attempts to focus elements from within an event callback will generally
// be unsuccessful, so we invoke focus() from outside of the callback.
$$wnd.setTimeout(function() {
$elem$.focus();
}, 0);
>>
75 changes: 75 additions & 0 deletions src/ojox/focusImplStandard.ml
@@ -0,0 +1,75 @@
(*
* This file is part of ojox, a library for web browser programming
* Copyright (C) 2009 Jacob Donham
* Original file (user/src/com/google/gwt/event/user/client/ui/impl/FocusImplStandard.java
* in the GWT source distribution) is Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*)

open Ocamljs.Inline

include FocusImplAbstract

(**
Implementation of {@link com.google.gwt.user.client.ui.impl.FocusImpl} that
uses a hidden input element to serve as a 'proxy' for accesskeys, which are
only supported on form elements in most browsers.
*)

let focusHandler = <<
function(evt) {
// This function is called directly as an event handler, so 'this' is
// set up by the browser to be the input on which the event is fired. We
// call focus() in a timeout or the element may be blurred when this event
// ends.
var div = this.parentNode;
if (div.onfocus) {
$$wnd.setTimeout(function() {
div.focus();
}, 0);
}
}
>>

let createFocusable () = <:rstmt<
// Divs are focusable in all browsers, but only IE supports the accessKey
// property on divs. We use the infamous 'hidden input' trick to add an
// accessKey to the focusable div. Note that the input is only used to
// capture focus when the accessKey is pressed. Focus is forwarded to the
// div immediately.
var div = $$doc.createElement('div');
div.tabIndex = 0;

var input = $$doc.createElement('input');
input.type = 'text';
input.tabIndex = -1;
input.style.opacity = 0;
input.style.height = '1px';
input.style.width = '1px';
input.style.zIndex = -1;
input.style.overflow = 'hidden';
input.style.position = 'absolute';

// Note that we're using isolated lambda methods as the event listeners
// to avoid creating a memory leaks. (Lambdas here would create cycles
// involving the div and input). This also allows us to share a single
// set of handlers among every focusable item.
input.addEventListener(
'focus',
$focusHandler$,
false);

div.appendChild(input);
return div;
>>
8 changes: 4 additions & 4 deletions src/ojox/focusWidget.ml
Expand Up @@ -42,7 +42,7 @@ object (self)
method addMouseUpHandler : MouseUpEvent.c handler -> handlerRegistration = fun handler -> self#addDomHandler handler MouseUpEvent.getType
method addMouseWheelHandler : MouseWheelEvent.c handler -> handlerRegistration = fun handler -> self#addDomHandler handler MouseWheelEvent.getType

method getTabIndex = FocusImpl.getTabIndex self#getElement
method getTabIndex = FocusImplForWidget.getTabIndex self#getElement
method isEnabled = not (DOM.getElementPropertyBoolean self#getElement "disabled")

method setAccessKey (key : char) =
Expand All @@ -52,10 +52,10 @@ object (self)

method setFocus focused =
if focused
then FocusImpl.focus self#getElement
else FocusImpl.blur self#getElement
then FocusImplForWidget.focus self#getElement
else FocusImplForWidget.blur self#getElement

method setTabIndex index = FocusImpl.setTabIndex self#getElement index
method setTabIndex index = FocusImplForWidget.setTabIndex self#getElement index

method setElement elem =
super#setElement elem;
Expand Down
5 changes: 5 additions & 0 deletions src/ojox/ojox.mllib
Expand Up @@ -27,6 +27,11 @@ Element
Event
FieldSetElement
FocusEvent
FocusImplAbstract
FocusImplForWidget
FocusImplForPanel
FocusImplSafari
FocusImplStandard
FocusWidget
FormElement
FrameElement
Expand Down

0 comments on commit c3c9238

Please sign in to comment.