Cscott has uploaded a new change for review.
https://gerrit.wikimedia.org/r/192878
Change subject: Move `OO.ui.infuse` to `OO.ui.Element.static.infuse`.
......................................................................
Move `OO.ui.infuse` to `OO.ui.Element.static.infuse`.
Change-Id: I1999cbb2bdd1dd2557633882bf9fedc33788f6ce
---
M build/modules.json
M src/Element.js
D src/Infuse.js
M src/core.js
4 files changed, 100 insertions(+), 76 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/78/192878/1
diff --git a/build/modules.json b/build/modules.json
index 5a69c3e..6ae9a4b 100644
--- a/build/modules.json
+++ b/build/modules.json
@@ -15,7 +15,6 @@
"src/WindowManager.js",
"src/Error.js",
"src/HtmlSnippet.js",
- "src/Infuse.js",
"src/Process.js",
"src/ToolFactory.js",
"src/ToolGroupFactory.js",
diff --git a/src/Element.js b/src/Element.js
index 4e469ee..dd05f35 100644
--- a/src/Element.js
+++ b/src/Element.js
@@ -88,6 +88,96 @@
/* Static Methods */
/**
+ * Reconstitute a JavaScript object corresponding to a widget created
+ * by the PHP implementation.
+ *
+ * @param {string|HTMLElement|jQuery} idOrNode
+ * A DOM id (if a string) or node for the widget to infuse.
+ * @return {OO.ui.Element}
+ * The `OO.ui.Element` corresponding to this (infusable) document node.
+ * For `Tag` objects emitted on the HTML side (used occasionally for content)
+ * the value returned is a newly-created Element wrapping around the existing
+ * DOM node.
+ */
+OO.ui.Element.static.infuse = function ( idOrNode ) {
+ var obj = OO.ui.Element.static.unsafeInfuse( idOrNode, false );
+ // Verify that the type matches up.
+ if ( !( obj instanceof this['class'] ) ) {
+ throw new Error( 'Infusion type mismatch!' );
+ }
+ return obj;
+}
+
+/**
+ * Implementation helper for `infuse`.
+ * @private
+ */
+OO.ui.Element.static.unsafeInfuse = function ( idOrNode, dontReplace ) {
+ // look for a cached result of a previous infusion.
+ var id, $elem, data, cls, obj;
+ if ( typeof idOrNode === 'string' ) {
+ id = idOrNode;
+ $elem = $( document.getElementById( id ) );
+ } else {
+ $elem = $( idOrNode );
+ id = $elem.attr( 'id' );
+ }
+ data = $elem.data( 'ooui-infuse' );
+ if ( data ) {
+ // cached!
+ if ( data === true ) {
+ throw new Error( 'Circular dependency! ' + id );
+ }
+ return data;
+ }
+ if ( !$elem.length ) {
+ throw new Error( 'Widget not found: ' + id );
+ }
+ data = $elem.attr( 'data-ooui' );
+ if ( !data ) {
+ throw new Error( 'No infusion data found: ' + id );
+ }
+ try {
+ data = $.parseJSON( data );
+ } catch ( _ ) {
+ data = null;
+ }
+ if ( !( data && data._ ) ) {
+ throw new Error( 'No valid infusion data found: ' + id );
+ }
+ if ( data._ === 'Tag' ) {
+ // 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 );
+ }
+ $elem.data( 'ooui-infuse', true ); // prevent loops
+ data.id = id; // implicit
+ data = OO.copy( data, null, function deserialize( value ) {
+ if ( OO.isPlainObject( value ) ) {
+ if ( value.tag ) {
+ return OO.ui.Element.static.unsafeInfuse(
value.tag, true );
+ }
+ if ( value.html ) {
+ return new OO.ui.HtmlSnippet( value.html );
+ }
+ }
+ } );
+ // jscs:disable requireCapitalizedConstructors
+ obj = new cls( data ); // rebuild widget
+ // now replace old DOM with this new DOM.
+ if ( !dontReplace ) {
+ $elem.replaceWith( obj.$element );
+ }
+ obj.$element.data( 'ooui-infuse', obj );
+ // set the 'data-ooui' attribute so we can identify infused widgets
+ obj.$element.attr( 'data-ooui', '' );
+ return obj;
+};
+
+/**
* Get a jQuery function within a specific document.
*
* @static
diff --git a/src/Infuse.js b/src/Infuse.js
deleted file mode 100644
index 4faacba..0000000
--- a/src/Infuse.js
+++ /dev/null
@@ -1,75 +0,0 @@
-/**
- * Reconstitute a JavaScript object corresponding to a widget created
- * by the PHP implementation.
- *
- * @member OO.ui
- * @param {string|HTMLElement|jQuery} idOrNode
- * A DOM id (if a string) or node for the widget to infuse.
- * @return {OO.ui.Element}
- * The `OO.ui.Element` corresponding to this (infusable) document node.
- * For `Tag` objects emitted on the HTML side (used occasionally for content)
- * the value returned is a newly-created Element wrapping around the existing
- * DOM node.
- */
-OO.ui.infuse = function ( idOrNode, dontReplace ) {
- // look for a cached result of a previous infusion.
- var id, $elem, data, cls, obj;
- if ( typeof idOrNode === 'string' ) {
- id = idOrNode;
- $elem = $( document.getElementById( id ) );
- } else {
- $elem = $( idOrNode );
- id = $elem.attr( 'id' );
- }
- data = $elem.data( 'ooui-infuse' );
- if ( data ) {
- // cached!
- if ( data === true ) {
- throw new Error( 'Circular dependency! ' + id );
- }
- return data;
- }
- if ( !$elem.length ) {
- throw new Error( 'Widget not found: ' + id );
- }
- data = $elem.attr( 'data-ooui' );
- if ( !data ) {
- throw new Error( 'No infusion data found: ' + id );
- }
- try {
- data = $.parseJSON( data );
- } catch ( _ ) {
- data = null;
- }
- if ( !( data && data._ ) ) {
- throw new Error( 'No valid infusion data found: ' + id );
- }
- if ( data._ === 'Tag' ) {
- // 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 );
- }
- $elem.data( 'ooui-infuse', true ); // prevent loops
- data.id = id; // implicit
- data = OO.copy( data, null, function deserialize( value ) {
- if ( OO.isPlainObject( value ) ) {
- if ( value.tag ) {
- return OO.ui.infuse( value.tag, 'rebuilding' );
- }
- if ( value.html ) {
- return new OO.ui.HtmlSnippet( value.html );
- }
- }
- } );
- // jscs:disable requireCapitalizedConstructors
- obj = new cls( data ); // rebuild widget
- // now replace old DOM with this new DOM.
- if ( !dontReplace ) { $elem.replaceWith( obj.$element ); }
- obj.$element.data( 'ooui-infuse', obj );
- // set the 'data-ooui' attribute so we can identify infused widgets
- obj.$element.attr( 'data-ooui', '' );
- return obj;
-};
diff --git a/src/core.js b/src/core.js
index 947a23d..fa5cc56 100644
--- a/src/core.js
+++ b/src/core.js
@@ -103,6 +103,16 @@
return false;
};
+/**
+ * Reconstitute a JavaScript object corresponding to a widget created by
+ * the PHP implementation.
+ *
+ * This is an alias for `OO.ui.Element.static.infuse()`.
+ */
+OO.ui.infuse = function( idOrNode ) {
+ return OO.ui.Element.static.infuse( idOrNode );
+};
+
( function () {
/**
* Message store for the default implementation of OO.ui.msg
--
To view, visit https://gerrit.wikimedia.org/r/192878
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I1999cbb2bdd1dd2557633882bf9fedc33788f6ce
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits