Jonas Kress (WMDE) has uploaded a new change for review.

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

Change subject: Make SelectorBox injectable and only use single instance
......................................................................

Make SelectorBox injectable and only use single instance

Change-Id: I8b5b2d32ecf850ef0b735817a2fb74ec51d75353
---
M wikibase/queryService/ui/visualEditor/SelectorBox.js
M wikibase/queryService/ui/visualEditor/VisualEditor.js
2 files changed, 37 insertions(+), 42 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikidata/query/gui 
refs/changes/54/285154/1

diff --git a/wikibase/queryService/ui/visualEditor/SelectorBox.js 
b/wikibase/queryService/ui/visualEditor/SelectorBox.js
index 2fb93d2..66bf9ab 100644
--- a/wikibase/queryService/ui/visualEditor/SelectorBox.js
+++ b/wikibase/queryService/ui/visualEditor/SelectorBox.js
@@ -14,19 +14,15 @@
         *
         * @author Jonas Kress
         * @constructor
-        * @param {jQuery} $element
         * @param {wikibase.queryService.api.Wikibase} api
         */
-       function SELF( $element, api ) {
-               this._$element = $element;
+       function SELF( api ) {
 
                if ( api ) {
                        this._api = api;
                } else {
                        this._api = new wikibase.queryService.api.Wikibase();
                }
-
-               this._create();
        }
 
        /**
@@ -36,37 +32,26 @@
        SELF.prototype._api = null;
 
        /**
-        * @property {Function}
-        * @private
-        */
-       SELF.prototype._$element = null;
-
-       /**
-        * @property {Function}
-        * @private
-        */
-       SELF.prototype._changeListener = null;
-
-       /**
-        * Set the change listener
+        * Add selector box to element
         *
+        * @param {jQuery} $element
         * @param {Function} listener a function called when value selected
         */
-       SELF.prototype.setChangeListener = function( listener ) {
-               this._changeListener = listener;
+       SELF.prototype.add = function( $element, listener ) {
+               this._create( $element, listener );
        };
 
        /**
         * @private
         */
-       SELF.prototype._create = function() {
+       SELF.prototype._create = function( $element, listener ) {
                var self = this;
 
-               var $select = this._getSelectBox();
+               var $select = this._getSelectBox( $element );
                var $close = this._getCloseButton();
                var $content = $( '<div>' ).append( $select, $close );
 
-               this._$element.clickover( {
+               $element.clickover( {
                        placement: 'bottom',
                        'global_close': false,
                        'html': true,
@@ -74,16 +59,16 @@
                                return $content;
                        }
                } ).click( function( e ) {
-                       $.proxy( self._renderSelect2( $select ), self );
+                       $.proxy( self._renderSelect2( $select, $element ), self 
);
                        return false;
                } );
 
                $select.change( function( e ) {
                        $select.remove();
-                       self._$element.clickover( 'hide' );
+                       $element.clickover( 'hide' );
 
-                       if ( self._changeListener ) {
-                               self._changeListener( $select.val() );
+                       if ( listener ) {
+                               listener( $select.val() );
                        }
                } );
        };
@@ -91,9 +76,9 @@
        /**
         * @private
         */
-       SELF.prototype._getSelectBox = function() {
-               var id = this._$element.data( 'id' );
-               var label = this._$element.text();
+       SELF.prototype._getSelectBox = function( $element ) {
+               var id = $element.data( 'id' );
+               var label = $element.text();
 
                var $select = $( '<select>' ).append( $( '<option>' ).attr( 
'value', id ).text( label ) )
                                .append( $( '<option>' ).attr( 'value', id 
).text( label ) );
@@ -113,16 +98,16 @@
        /**
         * @private
         */
-       SELF.prototype._renderSelect2 = function( $select ) {
+       SELF.prototype._renderSelect2 = function( $select, $element ) {
                var self = this;
-               var type = this._$element.data( 'type' );
+               var type = $element.data( 'type' );
 
                var formatter = function( item ) {
                        if ( !item.data ) {
                                return item.text;
                        }
-                       return $( '<span>' + item.text + ' (' + item.data.id + 
')' + '</span><br/><small>'
-                                       + item.data.description + '</small>' );
+                       return $( '<span>' + item.text + ' (' + item.data.id + 
')' + '</span><br/><small>' +
+                                       item.data.description + '</small>' );
                };
 
                var transport = function( params, success, failure ) {
diff --git a/wikibase/queryService/ui/visualEditor/VisualEditor.js 
b/wikibase/queryService/ui/visualEditor/VisualEditor.js
index 7b554fe..4771721 100644
--- a/wikibase/queryService/ui/visualEditor/VisualEditor.js
+++ b/wikibase/queryService/ui/visualEditor/VisualEditor.js
@@ -24,12 +24,18 @@
         * @author Jonas Kress
         * @constructor
         * @param {wikibase.queryService.api.Wikibase} api
+        * @param {wikibase.queryService.ui.visualEditor.SelectorBox} 
selectorBox
         */
-       function SELF( api ) {
+       function SELF( api, selectorBox ) {
                this._api = api;
 
                if ( !this._api ) {
                        this._api = new wikibase.queryService.api.Wikibase();
+               }
+
+               this._selectorBox = selectorBox;
+               if ( !this._selectorBox ) {
+                       this._selectorBox = new 
wikibase.queryService.ui.visualEditor.SelectorBox( this._api );
                }
        }
 
@@ -38,6 +44,12 @@
         * @private
         */
        SELF.prototype._api = null;
+
+       /**
+        * @property {wikibase.queryService.ui.visualEditor.SelectorBox}
+        * @private
+        */
+       SELF.prototype._selectorBox = null;
 
        /**
         * @property {Function}
@@ -143,7 +155,7 @@
        SELF.prototype._i18n = function( key ) {
 
                if ( !$.i18n ) {
-                       return this._labels[ key ];
+                       return this._labels[key];
                }
 
                return $.i18n( I18N_PREFIX + key );
@@ -154,8 +166,9 @@
         */
        SELF.prototype._getHtml = function() {
                var self = this;
-               var $html = $( '<div>' ), $find = $( '<div>' ).text( 
this._i18n( 'find' ) + ' ' ),
-                       $show = $( '<div>' ).text( this._i18n( 'show' ) + ' ' 
), $spacer = $( '<div>' ).addClass( 'spacer' );
+               var $html = $( '<div>' ), $find = $( '<div>' ).text( 
this._i18n( 'find' ) + ' ' ), $show = $(
+                               '<div>' ).text( this._i18n( 'show' ) + ' ' ), 
$spacer = $( '<div>' ).addClass(
+                               'spacer' );
 
                $html.append( $find, $spacer, $show );
 
@@ -436,10 +449,7 @@
        SELF.prototype._valuleChanger = function( $element ) {
                var deferred = $.Deferred();
 
-               // TODO Use only one instance and make that instance injectable
-               var $selector = new 
wikibase.queryService.ui.visualEditor.SelectorBox( $element, this._api );
-
-               $selector.setChangeListener( function( id ) {
+               this._selectorBox.add( $element, function( id ) {
                        deferred.resolve( id );
                } );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8b5b2d32ecf850ef0b735817a2fb74ec51d75353
Gerrit-PatchSet: 1
Gerrit-Project: wikidata/query/gui
Gerrit-Branch: master
Gerrit-Owner: Jonas Kress (WMDE) <[email protected]>

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

Reply via email to