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

Change subject: itemview/propertyview: Split off initialization from creation
......................................................................


itemview/propertyview: Split off initialization from creation

The change splits off the initialization from the creation routine in itemview 
and propertyview.
_create should be used to only create DOM structure for TemplatedWidget can 
initialize template
short-cuts. Initialization of sub-widgets is to be done in _init. > 88699

Change-Id: Ie3b09bb780c28198ccf534239ae48323395a39ab
---
M lib/resources/jquery.wikibase/jquery.wikibase.entityview.js
M lib/resources/jquery.wikibase/jquery.wikibase.itemview.js
M lib/resources/jquery.wikibase/jquery.wikibase.propertyview.js
M repo/resources/jquery.ui/jquery.ui.TemplatedWidget.js
4 files changed, 77 insertions(+), 28 deletions(-)

Approvals:
  Adrian Lang: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.entityview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.entityview.js
index db89d4c..8106979 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.entityview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.entityview.js
@@ -110,10 +110,18 @@
         * Main initialization actually calling parent's _create().
         * @see jQuery.ui.TemplatedWidget._create
         * @protected
+        */
+       _createEntityview: function() {
+               PARENT.prototype._create.call( this );
+       },
+
+       /**
+        * @inheritdoc
+        * @protected
         *
         * @throws {Error} if a required options is missing.
         */
-       _initEntityview: function() {
+       _init: function() {
                if(
                        !this.options.value
                        || !this.options.languages
@@ -130,7 +138,7 @@
 
                this._initEntityTerms();
 
-               PARENT.prototype._create.call( this );
+               PARENT.prototype._init.call( this );
 
                this._attachEventHandlers();
        },
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.itemview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.itemview.js
index 3a8999c..001b92b 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.itemview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.itemview.js
@@ -34,20 +34,34 @@
         * @protected
         */
        _create: function() {
+               this._createEntityview();
+
+               this.$statements = $( '.wikibase-statementgrouplistview', 
this.element ).first();
+               if( this.$statements.length === 0 ) {
+                       this.$statements = $( '<div/>' ).appendTo( this.element 
);
+               }
+
+               this.$siteLinks = $( '.wikibase-sitelinkgrouplistview', 
this.element );
+
+               if( !this.$siteLinks.length ) {
+                       this.$siteLinks = $( '<div/>' ).appendTo( this.element 
);
+               }
+       },
+
+       /**
+        * @inheritdoc
+        * @protected
+        */
+       _init: function() {
                this._initStatements();
                this._initSiteLinks();
-               this._initEntityview();
+               PARENT.prototype._init.call( this );
        },
 
        /**
         * @protected
         */
        _initStatements: function() {
-               this.$statements = $( '.wikibase-statementgrouplistview', 
this.element ).first();
-               if( this.$statements.length === 0 ) {
-                       this.$statements = $( '<div/>' ).appendTo( this.element 
);
-               }
-
                this.$statements
                .statementgrouplistview( {
                        value: this.options.value.getStatements(),
@@ -71,16 +85,9 @@
         */
        _initSiteLinks: function() {
                var self = this,
-                       value = [];
-
-               this.$siteLinks = $( '.wikibase-sitelinkgrouplistview', 
this.element );
-
-               if( this.$siteLinks.length ) {
-                       value = scrapeSiteLinks( this.$siteLinks, 
this.options.value.getSiteLinks() );
-               } else {
-                       this.$siteLinks = $( '<div/>' ).appendTo( this.element 
);
-                       value = orderSiteLinksByGroup( 
this.options.value.getSiteLinks() );
-               }
+                       value = $( '.wikibase-sitelinkgrouplistview', 
this.element ).length
+                               ? scrapeSiteLinks( this.$siteLinks, 
this.options.value.getSiteLinks() )
+                               : orderSiteLinksByGroup( 
this.options.value.getSiteLinks() );
 
                this.$siteLinks.sitelinkgrouplistview( {
                        value: value,
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.propertyview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.propertyview.js
index a00207b..0992ed7 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.propertyview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.propertyview.js
@@ -39,15 +39,29 @@
         * @protected
         */
        _create: function() {
-               this._initDataType();
+               this._createEntityview();
+
+               this.$statements = $( '.wikibase-statementgrouplistview', 
this.element ).first();
+               if( this.$statements.length === 0 ) {
+                       this.$statements = $( '<div/>' ).appendTo( this.element 
);
+               }
+
+               this._createDataType();
+       },
+
+       /**
+        * @inheritdoc
+        * @protected
+        */
+       _init: function() {
                this._initStatements();
-               this._initEntityview();
+               PARENT.prototype._init.call( this );
        },
 
        /**
         * @protected
         */
-       _initDataType: function() {
+       _createDataType: function() {
                // TODO: Implement propertyview template to have static HTML 
rendered by the back-end match
                // the HTML rendered here without having to invoke templating 
mechanism here.
 
@@ -68,11 +82,6 @@
         * @protected
         */
        _initStatements: function() {
-               this.$statements = $( '.wikibase-statementgrouplistview', 
this.element ).first();
-               if( this.$statements.length === 0 ) {
-                       this.$statements = $( '<div/>' ).appendTo( this.element 
);
-               }
-
                this.$statements
                .statementgrouplistview( {
                        value: this.options.value.getStatements(),
diff --git a/repo/resources/jquery.ui/jquery.ui.TemplatedWidget.js 
b/repo/resources/jquery.ui/jquery.ui.TemplatedWidget.js
index 0d19c56..584a24e 100644
--- a/repo/resources/jquery.ui/jquery.ui.TemplatedWidget.js
+++ b/repo/resources/jquery.ui/jquery.ui.TemplatedWidget.js
@@ -8,8 +8,8 @@
        var PARENT =  $.Widget;
 
        /**
-        * Base prototype for all widgets which use the mw.wbTemplate 
templating system to create a basic
-        * DOM structure for internal usage.
+        * Base prototype for all widgets which use the mw.wbTemplate 
templating system to create a
+        * basic DOM structure for internal usage.
         *
         * @constructor
         * @abstract
@@ -52,6 +52,11 @@
         *        - {jQuery.Event}
         *        - {boolean} Whether widget has been dis- oder enabled.
         *
+        * @event init
+        *        Triggered after the widget is fully initialized. 
(`jQuery.Widget` native "create"
+        *        event is triggered after the template DOM is ready and 
template short-cuts are
+        *        assigned.)
+        *        - {jQuery.Event}
         */
        $.widget( 'ui.TemplatedWidget', PARENT, {
                /**
@@ -71,6 +76,12 @@
                } ),
 
                /**
+                * Creates the DOM structure according to the template and 
assigns the template short-cuts.
+                * Consequently, when overriding `_create` in inheriting 
widgets, calling the parent's
+                * `_create` should be the first action in the overridden 
`_create`, as that ensures the
+                * basic template DOM is created and template short-cuts can be 
used. The function should
+                * be overridden only to perform DOM manipulation/creation 
while initializing should be
+                * performed in `_init`.
                 * @see jQuery.Widget._create
                 */
                _create: function() {
@@ -84,6 +95,20 @@
                        PARENT.prototype._create.apply( this );
                },
 
+               /**
+                * Initializes any additional widget logic (i.e. child widgets, 
event handlers). DOM
+                * creation/manipulation is supposed to be performed in 
`_create` which is run before
+                * `_init`. With the `TemplatedWidget`'s base `_init` 
implementation triggering the "init"
+                * event, inheriting widgets should call parent's `_init` as 
last action for other
+                * components listening to the "init" event can be sure the 
widget in fully initialized.
+                * @see jQuery.Widget._init
+                * @protected
+                */
+               _init: function() {
+                       PARENT.prototype._init.call( this );
+                       this._trigger( 'init' );
+               },
+
                _applyTemplate: function() {
                        var templateParams = [],
                                self = this;

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie3b09bb780c28198ccf534239ae48323395a39ab
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Adrian Lang <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to