jenkins-bot has submitted this change and it was merged.

Change subject: Allow infusion of widgets in other namespaces
......................................................................


Allow infusion of widgets in other namespaces

...without hacks.

On the PHP side, custom widgets need to override
Element::getJavaScriptClassName(). The infused data will now contain the
full class name, rather than assuming a "OO.ui" prefix.

Bug: T104989
Change-Id: I7f0e74fcd9a5336ab4e4323cfe30f0ec9db342a9
---
M php/Element.php
M src/Element.js
M tests/phpunit/DummyWidget.php
M tests/phpunit/ElementTest.php
4 files changed, 24 insertions(+), 9 deletions(-)

Approvals:
  Cscott: Looks good to me, but someone else must approve
  Bartosz Dziewoński: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/php/Element.php b/php/Element.php
index 7a8cee1..7f4a3be 100644
--- a/php/Element.php
+++ b/php/Element.php
@@ -247,12 +247,18 @@
                };
                array_walk_recursive( $config, $replaceElements );
                // Set '_' last to ensure that subclasses can't accidentally 
step on it.
-               // Strip all namespaces from the class name
-               $exploded = explode( '\\', get_class( $this ) );
-               $config['_'] = end( $exploded );
+               $config['_'] = $this->getJavaScriptClassName();
                return $config;
        }
 
+       /**
+        * The class name of the JavaScript version of this widget
+        * @return string
+        */
+       protected function getJavaScriptClassName() {
+               return str_replace( 'OOUI\\', 'OO.ui.', get_class( $this ) );
+       }
+
        protected function getGeneratedAttributes() {
                $attributesArray = parent::getGeneratedAttributes();
                // Add `data-ooui` attribute from serialized config array.
diff --git a/src/Element.js b/src/Element.js
index 9f0d555..6567fc8 100644
--- a/src/Element.js
+++ b/src/Element.js
@@ -121,7 +121,7 @@
  */
 OO.ui.Element.static.unsafeInfuse = function ( idOrNode, top ) {
        // look for a cached result of a previous infusion.
-       var id, $elem, data, cls, obj;
+       var id, $elem, data, cls, parts, obj;
        if ( typeof idOrNode === 'string' ) {
                id = idOrNode;
                $elem = $( document.getElementById( id ) );
@@ -156,9 +156,15 @@
                // Special case: this is a raw Tag; wrap existing node, don't 
rebuild.
                return new OO.ui.Element( { $element: $elem } );
        }
-       cls = OO.ui[data._];
-       if ( !cls ) {
-               throw new Error( 'Unknown widget type: ' + id );
+       parts = data._.split( '.' );
+       cls = OO.getProp.apply( OO, [ window ].concat( parts ) );
+       if ( cls === undefined ) {
+               // The PHP output might be old and not including the "OO.ui" 
prefix
+               // TODO: Remove this back-compat after next major release
+               cls = OO.getProp.apply( OO, [ OO.ui ].concat( parts ) );
+               if ( cls === undefined ) {
+                       throw new Error( 'Unknown widget type: id: ' + id + ', 
class: ' + data._ );
+               }
        }
        $elem.data( 'ooui-infused', true ); // prevent loops
        data.id = id; // implicit
diff --git a/tests/phpunit/DummyWidget.php b/tests/phpunit/DummyWidget.php
index 3da9939..ea1f8bc 100644
--- a/tests/phpunit/DummyWidget.php
+++ b/tests/phpunit/DummyWidget.php
@@ -5,4 +5,7 @@
 use OOUI\Widget;
 
 class MockWidget extends Widget {
+       protected function getJavaScriptClassName() {
+               return 'foo.bar.baz.MockWidget';
+       }
 }
diff --git a/tests/phpunit/ElementTest.php b/tests/phpunit/ElementTest.php
index 37746b2..38aa74b 100644
--- a/tests/phpunit/ElementTest.php
+++ b/tests/phpunit/ElementTest.php
@@ -10,11 +10,11 @@
                return array(
                        array(
                                new Widget( array( 'infusable' => true ) ),
-                               '"_":"Widget"'
+                               '"_":"OO.ui.Widget"'
                        ),
                        array(
                                new \FooBarBaz\MockWidget( array( 'infusable' 
=> true ) ),
-                               '"_":"MockWidget"'
+                               '"_":"foo.bar.baz.MockWidget"'
                        ),
                );
        }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I7f0e74fcd9a5336ab4e4323cfe30f0ec9db342a9
Gerrit-PatchSet: 3
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Bartosz Dziewoński <matma....@gmail.com>
Gerrit-Reviewer: Cscott <canan...@wikimedia.org>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to