Henning Snater has uploaded a new change for review.

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

Change subject: Implemented $.util.getDirectionality
......................................................................

Implemented $.util.getDirectionality

By acting as an interface to ULS, the function allows move ULS specifics out of 
the *view widgets.

Change-Id: I987843e00c06e1c5d5b7b9c7b2f25d9801441017
---
M lib/resources/jquery.wikibase/jquery.wikibase.descriptionview.js
M lib/resources/jquery.wikibase/jquery.wikibase.labelview.js
M lib/resources/jquery.wikibase/resources.php
M repo/resources/Resources.php
A repo/resources/jquery/jquery.util.getDirectionality.js
A repo/resources/jquery/resources.php
A repo/tests/qunit/jquery/jquery.util.getDirectionality.tests.js
A repo/tests/qunit/jquery/resources.php
M repo/tests/qunit/resources.php
9 files changed, 198 insertions(+), 61 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/13/177213/1

diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.descriptionview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.descriptionview.js
index eb91f78..8e00e8c 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.descriptionview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.descriptionview.js
@@ -125,17 +125,13 @@
                        return;
                }
 
-               var dir = ( $.uls && $.uls.data ) ?
-                       $.uls.data.getDir( languageCode ) :
-                       $( 'html' ).prop( 'dir' );
-
                var $input = $( '<input />', {
                        // TODO: Inject correct placeholder via options
                        placeholder: mw.msg(
                                
'wikibase-description-edit-placeholder-language-aware',
                                wb.getLanguageNameByCode( languageCode )
                        ),
-                       dir: dir
+                       dir: $.util.getDirectionality( languageCode )
                } )
                .on( 'eachchange.' + this.widgetName, function( event ) {
                        self._trigger( 'change' );
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.labelview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.labelview.js
index 4bfc1f4..01fd2c3 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.labelview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.labelview.js
@@ -140,17 +140,13 @@
                        return;
                }
 
-               var dir = ( $.uls && $.uls.data ) ?
-                       $.uls.data.getDir( languageCode ) :
-                       $( 'html' ).prop( 'dir' );
-
                var $input = $( '<input />', {
                        // TODO: Inject correct placeholder via options
                        placeholder: mw.msg(
                                
'wikibase-label-edit-placeholder-language-aware',
                                wb.getLanguageNameByCode( languageCode )
                        ),
-                       dir: dir
+                       dir: $.util.getDirectionality( languageCode )
                } )
                .on( 'eachchange.' + this.widgetName, function( event ) {
                        self._trigger( 'change' );
diff --git a/lib/resources/jquery.wikibase/resources.php 
b/lib/resources/jquery.wikibase/resources.php
index be5fd06..985a614 100644
--- a/lib/resources/jquery.wikibase/resources.php
+++ b/lib/resources/jquery.wikibase/resources.php
@@ -127,6 +127,7 @@
                        'dependencies' => array(
                                'jquery.inputautoexpand',
                                'jquery.ui.TemplatedWidget',
+                               'jquery.util.getDirectionality',
                                'jquery.wikibase.edittoolbar',
                                'jquery.wikibase.toolbarcontroller',
                                'wikibase.datamodel.Term',
@@ -259,6 +260,7 @@
                        ),
                        'dependencies' => array(
                                'jquery.ui.TemplatedWidget',
+                               'jquery.util.getDirectionality',
                                'jquery.wikibase.edittoolbar',
                                'jquery.wikibase.toolbarcontroller',
                                'wikibase.datamodel.Term',
diff --git a/repo/resources/Resources.php b/repo/resources/Resources.php
index 19d3ade..a6984c5 100644
--- a/repo/resources/Resources.php
+++ b/repo/resources/Resources.php
@@ -25,30 +25,6 @@
 
        $modules = array(
 
-               'jquery.removeClassByRegex' => $moduleTemplate + array(
-                       'scripts' => array(
-                               'jquery/jquery.removeClassByRegex.js',
-                       ),
-               ),
-
-               'jquery.sticknode' => $moduleTemplate + array(
-                       'scripts' => array(
-                               'jquery/jquery.sticknode.js',
-                       ),
-                       'dependencies' => array(
-                               'jquery.util.EventSingletonManager',
-                       ),
-               ),
-
-               'jquery.util.EventSingletonManager' => $moduleTemplate + array(
-                       'scripts' => array(
-                               'jquery/jquery.util.EventSingletonManager.js',
-                       ),
-                       'dependencies' => array(
-                               'jquery.throttle-debounce',
-                       ),
-               ),
-
                'jquery.ui.tagadata' => $moduleTemplate + array(
                        'scripts' => array(
                                'jquery.ui/jquery.ui.tagadata.js',
@@ -287,6 +263,7 @@
                include( __DIR__ . '/experts/resources.php' ),
                include( __DIR__ . '/formatters/resources.php' ),
                include( __DIR__ . '/parsers/resources.php' ),
+               include( __DIR__ . '/jquery/resources.php' ),
                include( __DIR__ . '/store/resources.php' ),
                include( __DIR__ . '/utilities/resources.php' )
        );
diff --git a/repo/resources/jquery/jquery.util.getDirectionality.js 
b/repo/resources/jquery/jquery.util.getDirectionality.js
new file mode 100644
index 0000000..6a3a746
--- /dev/null
+++ b/repo/resources/jquery/jquery.util.getDirectionality.js
@@ -0,0 +1,21 @@
+( function ( $ ) {
+'use strict';
+
+/**
+ * Returns the directionality of a language by querying the Universal Language 
Selector. If ULS is
+ * not available the HTML element's `dir` attribute is evaluated. If that is 
unset, `auto` is
+ * returned.
+ * @method jQuery.util.getDirectionality
+ * @member jQuery.util
+ * @licence GNU GPL v2+
+ * @author H. Snater < [email protected] >
+ *
+ * @param {string} languageCode
+ * @return {string}
+ */
+$.util.getDirectionality = function( languageCode ) {
+       var dir = $.uls && $.uls.data ? $.uls.data.getDir( languageCode ) : $( 
'html' ).prop( 'dir' );
+       return dir !== '' ? dir : 'auto';
+};
+
+}( jQuery ) );
diff --git a/repo/resources/jquery/resources.php 
b/repo/resources/jquery/resources.php
new file mode 100644
index 0000000..076f6a6
--- /dev/null
+++ b/repo/resources/jquery/resources.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * @licence GNU GPL v2+
+ * @author H. Snater < [email protected] >
+ *
+ * @codeCoverageIgnoreStart
+ */
+return call_user_func( function() {
+       preg_match( '+' . preg_quote( DIRECTORY_SEPARATOR ) . 
'(?:vendor|extensions)'
+               . preg_quote( DIRECTORY_SEPARATOR ) . '.*+', __DIR__, 
$remoteExtPath );
+
+       $moduleTemplate = array(
+               'localBasePath' => __DIR__,
+               'remoteExtPath' => '..' . $remoteExtPath[0],
+       );
+
+       $modules = array(
+
+               'jquery.removeClassByRegex' => $moduleTemplate + array(
+                       'scripts' => array(
+                               'jquery.removeClassByRegex.js',
+                       ),
+               ),
+
+               'jquery.sticknode' => $moduleTemplate + array(
+                       'scripts' => array(
+                               'jquery.sticknode.js',
+                       ),
+                       'dependencies' => array(
+                               'jquery.util.EventSingletonManager',
+                       ),
+               ),
+
+               'jquery.util.EventSingletonManager' => $moduleTemplate + array(
+                       'scripts' => array(
+                               'jquery.util.EventSingletonManager.js',
+                       ),
+                       'dependencies' => array(
+                               'jquery.throttle-debounce',
+                       ),
+               ),
+
+               'jquery.util.getDirectionality' => $moduleTemplate + array(
+                       'scripts' => array(
+                               'jquery.util.getDirectionality.js',
+                       ),
+               ),
+
+       );
+
+       if ( defined( 'ULS_VERSION' ) ) {
+               $modules['jquery.util.getDirectionality']['dependencies'][] = 
'ext.uls.mediawiki';
+       }
+
+       return $modules;
+} );
diff --git a/repo/tests/qunit/jquery/jquery.util.getDirectionality.tests.js 
b/repo/tests/qunit/jquery/jquery.util.getDirectionality.tests.js
new file mode 100644
index 0000000..e4e7c6b
--- /dev/null
+++ b/repo/tests/qunit/jquery/jquery.util.getDirectionality.tests.js
@@ -0,0 +1,59 @@
+/**
+ * @licence GNU GPL v2+
+ * @author H. Snater < [email protected] >
+ */
+( function( $, QUnit ) {
+       'use strict';
+
+var htmlDir = null;
+
+QUnit.module( 'jquery.util.getDirectionality', {
+       setup: function() {
+               htmlDir = $( 'html' ).prop( 'dir' );
+       },
+       teardown: function() {
+               $( 'html' ).prop( 'dir', htmlDir );
+       }
+} );
+
+QUnit.test( 'Basic tests', function( assert ) {
+       var $html = $( 'html' );
+
+       if( $.uls && $.uls.data ) {
+               assert.equal(
+                       $.util.getDirectionality( 'fa' ),
+                       'rtl',
+                       'Retrieved language code from ULS.'
+               );
+
+               // There is no reason to further test ULS behaviour as ULS is 
supposed to always return a
+               // sensible directionality string.
+               return;
+       }
+
+       $html.prop( 'dir', 'ltr' );
+
+       assert.equal(
+               $.util.getDirectionality( 'doesNotExist' ),
+               'ltr',
+               'Falling back to HTML "dir" attribute.'
+       );
+
+       $html.prop( 'dir', 'rtl' );
+
+       assert.equal(
+               $.util.getDirectionality( 'doesNotExist' ),
+               'rtl',
+               'Verified falling back to HTML "dir" attribute after changing 
"dir" attribute.'
+       );
+
+       $html.removeAttr( 'dir' );
+
+       assert.equal(
+               $.util.getDirectionality( 'doesNotExist' ),
+               'auto',
+               'Falling back to HTML "auto" attribute if no "dir" attribute is 
set on the HTML element.'
+       );
+} );
+
+}( jQuery, QUnit ) );
diff --git a/repo/tests/qunit/jquery/resources.php 
b/repo/tests/qunit/jquery/resources.php
new file mode 100644
index 0000000..b627d23
--- /dev/null
+++ b/repo/tests/qunit/jquery/resources.php
@@ -0,0 +1,56 @@
+<?php
+/**
+ * @license GNU GPL v2+
+ * @author H. Snater < [email protected] >
+ *
+ * @codeCoverageIgnoreStart
+ */
+return call_user_func( function() {
+       preg_match( '+' . preg_quote( DIRECTORY_SEPARATOR ) . 
'(?:vendor|extensions)'
+               . preg_quote( DIRECTORY_SEPARATOR ) . '.*+', __DIR__, 
$remoteExtPath );
+       $moduleBase = array(
+               'localBasePath' => __DIR__,
+               'remoteExtPath' => '..' . $remoteExtPath[0],
+       );
+
+       return array(
+
+               'jquery.removeClassByRegex.tests' => $moduleBase + array(
+                       'scripts' => array(
+                               'jquery.removeClassByRegex.tests.js',
+                       ),
+                       'dependencies' => array(
+                               'jquery.removeClassByRegex',
+                       ),
+               ),
+
+               'jquery.sticknode.tests' => $moduleBase + array(
+                       'scripts' => array(
+                               'jquery.sticknode.tests.js',
+                       ),
+                       'dependencies' => array(
+                               'jquery.sticknode',
+                       ),
+               ),
+
+               'jquery.util.EventSingletonManager.tests' => $moduleBase + 
array(
+                       'scripts' => array(
+                               'jquery.util.EventSingletonManager.tests.js',
+                       ),
+                       'dependencies' => array(
+                               'jquery.util.EventSingletonManager',
+                       ),
+               ),
+
+               'jquery.util.getDirectionality.tests' => $moduleBase + array(
+                       'scripts' => array(
+                               'jquery.util.getDirectionality.tests.js',
+                       ),
+                       'dependencies' => array(
+                               'jquery.util.getDirectionality',
+                       ),
+               ),
+
+       );
+
+} );
diff --git a/repo/tests/qunit/resources.php b/repo/tests/qunit/resources.php
index 7db7af2..975f871 100644
--- a/repo/tests/qunit/resources.php
+++ b/repo/tests/qunit/resources.php
@@ -17,33 +17,6 @@
 
        $modules = array(
 
-               'jquery.removeClassByRegex.tests' => $moduleBase + array(
-                       'scripts' => array(
-                               'jquery/jquery.removeClassByRegex.tests.js',
-                       ),
-                       'dependencies' => array(
-                               'jquery.removeClassByRegex',
-                       ),
-               ),
-
-               'jquery.sticknode.tests' => $moduleBase + array(
-                       'scripts' => array(
-                               'jquery/jquery.sticknode.tests.js',
-                       ),
-                       'dependencies' => array(
-                               'jquery.sticknode',
-                       ),
-               ),
-
-               'jquery.util.EventSingletonManager.tests' => $moduleBase + 
array(
-                       'scripts' => array(
-                               
'jquery/jquery.util.EventSingletonManager.tests.js',
-                       ),
-                       'dependencies' => array(
-                               'jquery.util.EventSingletonManager',
-                       ),
-               ),
-
                'jquery.ui.tagadata.tests' => $moduleBase + array(
                        'scripts' => array(
                                'jquery.ui/jquery.ui.tagadata.tests.js',
@@ -135,6 +108,7 @@
        return array_merge(
                $modules,
                include( __DIR__ . '/entityChangers/resources.php' ),
+               include( __DIR__ . '/jquery/resources.php' ),
                include __DIR__ . '/store/resources.php',
                include __DIR__ . '/utilities/resources.php'
        );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I987843e00c06e1c5d5b7b9c7b2f25d9801441017
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to