Mooeypoo has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/204197

Change subject: [wip] Find first focusable element in booklet and add focusable 
utility
......................................................................

[wip] Find first focusable element in booklet and add focusable utility

* Add a OO.ui.isFocusableElement utility to validate whether an
  element is focusable. (Adapted from the :focusable selector in
  jQuery UI v1.11.4).
* Create a method in booklet layout to find the first focusable
  input and focus it.

Change-Id: I746389d66b8751a64b89fc8c05c46753efd77a9e
---
M src/core.js
M src/layouts/BookletLayout.js
2 files changed, 61 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/97/204197/1

diff --git a/src/core.js b/src/core.js
index cf23297..8785a62 100644
--- a/src/core.js
+++ b/src/core.js
@@ -31,6 +31,39 @@
 };
 
 /**
+ * Check if an element is focusable.
+ * Adapted from :focusable in jQueryUI v1.11.4 - 2015-04-14
+ *
+ * @param {jQuery} element Element to test
+ * @return {Boolean} [description]
+ */
+OO.ui.isFocusableElement = function ( $element ) {
+       var node = $element[0],
+               nodeName = node.nodeName.toLowerCase(),
+               // Check if the element have tabindex set
+               isInElementGroup = 
/^(input|select|textarea|button|object)$/.test( nodeName ),
+               // Check if the element is a link with href or if it has 
tabindex
+               isOtherElement = (
+                       ( nodeName === 'a' && node.href ) ||
+                       !isNaN( $element.attr( 'tabindex' ) )
+               ),
+               // Check if the element is visible
+               isVisible = (
+                       // This is quicker than calling $element.is( ':visible' 
)
+                       $.expr.filters.visible( node ) &&
+                       // Check that all parents are visible
+                       !$element.parents().addBack().filter( function () {
+                               return $.css( this, 'visibility' ) === 'hidden';
+                       } ).length
+               );
+
+       return (
+               ( isInElementGroup ? !node.disabled : isOtherElement ) &&
+               isVisible
+       );
+};
+
+/**
  * Get the user's language and any fallback languages.
  *
  * These language codes are used to localize user interface elements in the 
user's language.
diff --git a/src/layouts/BookletLayout.js b/src/layouts/BookletLayout.js
index f90c77c..10054f6 100644
--- a/src/layouts/BookletLayout.js
+++ b/src/layouts/BookletLayout.js
@@ -205,6 +205,34 @@
 };
 
 /**
+ * Find the first focusable input in the booklet layout and focus
+ * on it.
+ */
+OO.ui.BookletLayout.prototype.focusFirstFocusable = function () {
+       var i, len,
+               found = false,
+               items = this.stackLayout.getItems();
+
+       for ( i = 0, len = items.length; i < len; i++ ) {
+               if ( found ) {
+                       break;
+               }
+               // Find all potentially focusable elements in the item
+               // and check if they are focusable
+               items[i].$element
+                       .find( 'input, select, textarea, button, object' )
+                       /* jshint loopfunc:true */
+                       .each( function () {
+                               if ( OO.ui.isFocusableElement( $( this ) ) ) {
+                                       $( this ).focus();
+                                       found = true;
+                                       return;
+                               }
+                       } );
+       }
+};
+
+/**
  * Handle outline widget select events.
  *
  * @private

-- 
To view, visit https://gerrit.wikimedia.org/r/204197
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I746389d66b8751a64b89fc8c05c46753efd77a9e
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <mor...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to