Aude has uploaded a new change for review.

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


Change subject: (bug 46321) Add init js file for site link widget
......................................................................

(bug 46321) Add init js file for site link widget

- wrap linkItem in a variable, and put in scope of wb. stuff

@todo:

- needs spinner while the dependency js is loading
- what the widget depends on could be optimized

Change-Id: I21ffaf3396da28aecbb92ea405268b98cf609ca1
---
M client/WikibaseClient.hooks.php
M client/resources/Resources.php
A client/resources/wbclient.linkItem.init.js
M client/resources/wbclient.linkItem.js
4 files changed, 140 insertions(+), 104 deletions(-)


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

diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index 13e8fd5..1e042a1 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -426,7 +426,7 @@
                                // (as that only runs after the element 
initially appeared).
                                $out->addModules( 'wikibase.client.nolanglinks' 
);
                                // Add the JavaScript to link pages locally
-                               $out->addModules( 'wbclient.linkItem' );
+                               $out->addModules( 'wbclient.linkItem.init' );
                        }
                }
 
diff --git a/client/resources/Resources.php b/client/resources/Resources.php
index 9e63bec..759f547 100644
--- a/client/resources/Resources.php
+++ b/client/resources/Resources.php
@@ -67,7 +67,18 @@
                                'wikibase-sitelinks-sitename-columnheading',
                                'wikibase-sitelinks-link-columnheading'
                        ),
-               )
+               ),
+               'wbclient.linkItem.init' => $moduleTemplate + array(
+                       'scripts' => array(
+                               'wbclient.linkItem.init.js'
+                       ),
+                       'dependencies' => array(
+//                             'wbclient.linkItem'
+                       ),
+                       'messages' => array(
+                               'wikibase-linkitem-addlinks'
+                       ),
+               ),
        );
 
 } );
diff --git a/client/resources/wbclient.linkItem.init.js 
b/client/resources/wbclient.linkItem.init.js
new file mode 100644
index 0000000..1d63831
--- /dev/null
+++ b/client/resources/wbclient.linkItem.init.js
@@ -0,0 +1,36 @@
+/**
+* JavaScript that allows linking articles with Wikibase items or creating
+* new wikibase items directly in the client wikis
+*
+* @since 0.4
+*
+* Author: Marius Hoch [email protected]
+*/
+( function( mw, $ ) {
+'use strict';
+
+       /**
+        * Displays the link which shows the dialog after checking whether the 
user is logged ins
+        *
+        */
+       $( document ).ready( function() {
+               $( '#wbc-linkToItem' )
+                       .empty()
+                       .append(
+                               $( '<a>' )
+                               .attr( {
+                                       href: '#',
+                                       id: 'wbc-linkToItem-link'
+                               } )
+                               .text( mw.msg( 'wikibase-linkitem-addlinks' ) )
+                               .click( function( event ) {
+                                       var $e = event;
+                                       mw.loader.using( 'wbclient.linkItem', 
function( event ) {
+                                               
wikibase.AddSiteLinkWidget.checkLoggedin( $e );
+                                       });
+                               })
+                       );
+               $( '#p-lang' ).show();
+       } );
+
+} )( mediaWiki, jQuery );
diff --git a/client/resources/wbclient.linkItem.js 
b/client/resources/wbclient.linkItem.js
index 603ecbe..d0f2dc9 100644
--- a/client/resources/wbclient.linkItem.js
+++ b/client/resources/wbclient.linkItem.js
@@ -7,20 +7,23 @@
 * Author: Marius Hoch [email protected]
 */
 ( function( wb, mw, $ ) {
-       var repoApi = new wb.RepoApi(),
-               $dialog, $spinner, $goButton, targetSite, targetArticle;
+
+var repoApi = new wb.RepoApi(), $siteLinkWidget,
+       $dialog, $spinner, $goButton, targetSite, targetArticle;
+
+$siteLinkWidget = {
 
        /**
         * Check whether the user is logged in on both the client and the repo
         * show the dialog if he is, error if not
         *
         */
-       function checkLoggedin( event ) {
+       checkLoggedin: function( event ) {
                event.preventDefault();
 
                if ( mw.user.isAnon() ) {
                        // User isn't logged in
-                       notLoggedin();
+                       $siteLinkWidget.notLoggedin();
                        return;
                }
 
@@ -40,11 +43,11 @@
 
                        if ( data.query.userinfo.anon !== undefined ) {
                                // User isn't logged into the repo
-                               notLoggedin();
+                               $siteLinkWidget.notLoggedin();
                                return;
                        }
 
-                       createDialog();
+                       $siteLinkWidget.createDialog();
                } )
                .fail( function() {
                        $dialogSpinner.remove();
@@ -58,16 +61,16 @@
                                tooltip.destroy();
                        } );
                } );
-       }
+       },
 
        /**
         * Show an error to the user in case he isn't logged in on both the 
client and the repo
         *
         */
-       function notLoggedin() {
+       notLoggedin: function() {
                $( '#wbc-linkToItem-link' ).show();
 
-               var userLogin = linkRepoTitle( 'Special:UserLogin' );
+               var userLogin = $siteLinkWidget.linkRepoTitle( 
'Special:UserLogin' );
                $( '<div>' )
                        .dialog( {
                                title: mw.msg( 
'wikibase-linkitem-not-loggedin-title' ),
@@ -80,12 +83,12 @@
                                        .addClass( 
'wbclient-linkItem-not-loggedin-message' )
                                        .html( mw.message( 
'wikibase-linkitem-not-loggedin', userLogin ).parse() )
                        );
-       }
+       },
 
        /**
         * Create the dialog asking for a page the user wants to link with the 
current one
         */
-       function createDialog() {
+       createDialog: function() {
                $dialog = $( '<div>' )
                        .attr( 'id', 'wbclient-linkItem-dialog' )
                        .dialog( {
@@ -97,25 +100,25 @@
                                        text: mw.message( 
'wikibase-linkitem-linkpage' ).escaped(),
                                        id: 'wbclient-linkItem-goButton',
                                        disabled: 'disabled',
-                                       click: secondStep
+                                       click: $siteLinkWidget.secondStep
                                } ],
-                               close: onDialogClose
+                               close: $siteLinkWidget.onDialogClose
                        } )
                        .append(
                                $( '<p>' )
                                        .text( mw.message( 
'wikibase-linkitem-selectlink' ).escaped() )
                        )
-                       .append( getSiteLinkForm() );
+                       .append( $siteLinkWidget.getSiteLinkForm() );
 
                $goButton = $( '#wbclient-linkItem-goButton' );
-       }
+       },
 
        /**
         * Called by the time the dialog get's closed. Removes the values of 
all persistent variables
         * and makes the link reapper
         *
         */
-       function onDialogClose() {
+       onDialogClose: function() {
                $dialog.remove();
                if ( $spinner && $spinner.length ) {
                        $spinner.remove();
@@ -125,14 +128,14 @@
                targetArticle = null;
                $( '#wbc-linkToItem-link' )
                        .show();
-       }
+       },
 
        /**
         * Gets an object with all sites despite the current one (as we can't 
link pages on the same wiki)
         *
         * @return {object}
         */
-       function getLinkableSites() {
+       getLinkableSites: function() {
                var sites = wb.getSites(),
                        linkableSites = {},
                        site;
@@ -142,12 +145,12 @@
                        }
                }
                return linkableSites;
-       }
+       },
 
        /**
         * Get a form for selecting the site and the page to link in a user 
friendly manner (with autocompletion)
         */
-       function getSiteLinkForm() {
+       getSiteLinkForm: function() {
                return $( '<form>' )
                        .attr( {
                                name: 'wikibase-linkItem-form'
@@ -167,7 +170,7 @@
                                                'class': 
'wbclient-linkItem-Input'
                                        } )
                                        .siteselector( {
-                                               resultSet: getLinkableSites()
+                                               resultSet: 
$siteLinkWidget.getLinkableSites()
                                        } )
                                        .on( 'siteselectoropen 
siteselectorclose siteselectorautocomplete blur', function() {
                                                var apiUrl;
@@ -222,29 +225,29 @@
                                                $goButton.button( 'enable' );
                                        } )
                        );
-       }
+       },
 
        /**
         * Let the user know that we're currently doing something by
         * replacing the go on button with a spinning animation
         */
-       function showSpinner() {
+       showSpinner: function() {
                $spinner = $.createSpinner();
                $goButton
                        .hide()
                        .after( $spinner );
-       }
+       },
 
        /**
         * Remove the spinner created with showSpinner and show the original 
button again
         */
-       function removeSpinner() {
+       removeSpinner: function() {
                if ( !$spinner || !$spinner.length ) {
                        return;
                }
                $spinner.remove();
                $goButton.show();
-       }
+       },
 
        /**
         * Create a table row for a site link
@@ -253,46 +256,46 @@
         * @param {object} entitySitelinks
         * @return {jQuery}
         */
-       function siteLinkRow( site, entitySitelinks ) {
+       siteLinkRow: function( site, entitySitelinks ) {
                return $( '<tr>' )
                        .append(
                                $( '<td>' )
-                                       .addClass( 
'wbclient-linkItem-colum-site' )
+                                       .addClass( 
'wbclient-linkItem-column-site' )
                                        .text( site.getName() )
                                        .css( 'direction', 
site.getLanguage().dir )
                        )
                        .append(
                                $( '<td>' )
-                                       .addClass( 
'wbclient-linkItem-colum-page' )
+                                       .addClass( 
'wbclient-linkItem-column-page' )
                                        .append(
                                                site.getLinkTo( 
entitySitelinks.title )
                                        )
                                        .css( 'direction', 
site.getLanguage().dir )
                        );
-       }
+       },
 
        /**
        * Called after the user gave us a language and a page name. Looks up 
any existing items then or
        * tries to link the currently viewed page with an existing item
        */
-       function secondStep() {
+       secondStep: function() {
                if ( $( '#wbclient-linkItem-Site' ).siteselector( 
'getSelectedSite' ) ) {
                        targetSite = $( '#wbclient-linkItem-Site' 
).siteselector( 'getSelectedSite' ).getGlobalSiteId();
                } else {
                        // This should never happen because the button 
shouldn't be enabled if the site isn't valid
                        // ...keeping this for sanity and paranoia
-                       invalidSiteGiven();
+                       $siteLinkWidget.invalidSiteGiven();
                        return;
                }
                targetArticle = $( '#wbclient-linkItem-page' ).val();
 
                // Show a spinning animation and do an API request
-               showSpinner();
+               $siteLinkWidget.showSpinner();
 
                repoApi.getEntitiesByPage( targetSite, targetArticle, ['info', 
'sitelinks'], mw.config.get( 'wgUserLanguage' ), 'sitelinks', 'ascending' )
-                       .done( onEntityLoad )
-                       .fail( onError );
-       }
+                       .done( $siteLinkWidget.onEntityLoad )
+                       .fail( $siteLinkWidget.onError );
+       },
 
        /**
         * Returns a table with all sitelinks linked to an entity
@@ -300,7 +303,7 @@
         * @param {object} entity
         * @return {jQuery}
         */
-       function siteLinkTable( entity )  {
+       siteLinkTable: function( entity )  {
                var i, $siteLinks;
 
                $siteLinks = $( '<div>' )
@@ -331,7 +334,7 @@
                                $siteLinks
                                        .find( 'table' )
                                        .append(
-                                               siteLinkRow(
+                                               $siteLinkWidget.siteLinkRow(
                                                        wb.getSiteByGlobalId( 
entity.sitelinks[ i ].site ),
                                                        entity.sitelinks[ i ]
                                                )
@@ -339,14 +342,14 @@
                        }
                }
                return $siteLinks;
-       }
+       },
 
        /**
         * Get the entity for the current page in case there is one
         *
         * @param {jQuery.promise}
         */
-       function getEntityForCurrentPage() {
+       getEntityForCurrentPage: function() {
                return repoApi.getEntitiesByPage(
                        mw.config.get( 'wbCurrentSite' ).globalSiteId,
                        mw.config.get( 'wgPageName' ),
@@ -355,7 +358,7 @@
                        'sitelinks',
                        'ascending'
                );
-       }
+       },
 
        /**
         * Handles the data from getEntitiesByPage and either creates a new 
item or in case there already is an
@@ -363,11 +366,11 @@
         *
         * @param {object} data
         */
-       function onEntityLoad( data ) {
+       onEntityLoad: function( data ) {
                var i, entity, itemLink;
 
                if ( !data.entities['-1'] ) {
-                       removeSpinner();
+                       $siteLinkWidget.removeSpinner();
 
                        var siteLinkCount = 0;
                        // Show a table with links to the user and ask for 
confirmation
@@ -377,7 +380,7 @@
                                        break;
                                }
                        }
-                       itemLink = linkRepoTitle( entity.title );
+                       itemLink = $siteLinkWidget.linkRepoTitle( entity.title 
);
 
                        // Count site links and abort in case the entity 
already is linked with a page on this wiki
                        for ( i in entity.sitelinks ) {
@@ -385,7 +388,7 @@
                                        siteLinkCount += 1;
                                        if ( entity.sitelinks[ i ].site === 
mw.config.get( 'wbCurrentSite' ).globalSiteId ) {
                                                // Abort as the entity already 
is linked with a page on this wiki
-                                               onError(
+                                               $siteLinkWidget.onError(
                                                        mw.message( 
'wikibase-linkitem-alreadylinked', itemLink, entity.sitelinks[ i ].title 
).parse()
                                                );
                                                return;
@@ -395,7 +398,7 @@
 
                        if ( siteLinkCount === 1 ) {
                                // The item we want to link with only has a 
single langlink so we don't have to ask for confirmation
-                               linkWithEntity( entity );
+                               $siteLinkWidget.linkWithEntity( entity );
                        } else {
 
                                $dialog
@@ -407,7 +410,7 @@
                                                $( '<br />' )
                                        )
                                        .append(
-                                               siteLinkTable( entity )
+                                               $siteLinkWidget.siteLinkTable( 
entity )
                                        );
 
                                $goButton
@@ -415,15 +418,15 @@
                                        .button( 'option', 'label', mw.msg( 
'wikibase-linkitem-confirmitem-button' ) )
                                        .click( function() {
                                                // The user confirmed that this 
is the right item...
-                                               linkWithEntity( entity );
+                                               $siteLinkWidget.linkWithEntity( 
entity );
                                        } );
                        }
                } else {
                        // There is no item for the page the user wants to link
                        // Maybe there's one for the current page though 
(without other links then)
 
-                       getEntityForCurrentPage()
-                               .fail( onError )
+                       $siteLinkWidget.getEntityForCurrentPage()
+                               .fail( $siteLinkWidget.onError )
                                .done( function( data ) {
                                        if ( data.entities['-1'] ) {
                                                // There's no entity yet, 
create one
@@ -449,8 +452,8 @@
                                                        title: targetArticle
                                                };
                                                repoApi.createEntity( 
entityData )
-                                                       .done( 
successfullyCreated )
-                                                       .fail( onError );
+                                                       .done( 
$siteLinkWidget.successfullyCreated )
+                                                       .fail( 
$siteLinkWidget.onError );
                                        } else {
                                                // There already is an entity 
with the current page linked
                                                // but it's empty cause this 
dialog isn't shown on pages with langlinks
@@ -469,12 +472,12 @@
                                                        targetSite,
                                                        targetArticle
                                                )
-                                               .done( successfullyLinked )
-                                               .fail( onError );
+                                               .done( 
$siteLinkWidget.successfullyLinked )
+                                               .fail( $siteLinkWidget.onError 
);
                                        }
                                } );
                }
-       }
+       },
 
        /**
         * Links the current page with the given entity. If the current page 
yet is linked with an item we have to unlink it first.
@@ -482,10 +485,10 @@
         *
         * @param {object} entity
         */
-       function linkWithEntity( entity ) {
-               showSpinner();
-               getEntityForCurrentPage()
-                       .fail( onError )
+       linkWithEntity: function( entity ) {
+               $siteLinkWidget.showSpinner();
+               $siteLinkWidget.getEntityForCurrentPage()
+                       .fail( $siteLinkWidget.onError )
                        .done( function( data ) {
 
                                /**
@@ -498,8 +501,8 @@
                                                mw.config.get( 'wbCurrentSite' 
).globalSiteId,
                                                mw.config.get( 'wgPageName' )
                                        )
-                                       .done( successfullyLinked )
-                                       .fail( onError );
+                                       .done( 
$siteLinkWidget.successfullyLinked )
+                                       .fail( $siteLinkWidget.onError );
                                }
 
                                if ( data.entities['-1'] ) {
@@ -523,8 +526,8 @@
                                        if ( siteLinkCount === 1 ) {
                                                // The current page has an own 
item with no other links... unlink us
                                                repoApi.removeSitelink( 
selfEntity.id, selfEntity.lastrevid, mw.config.get( 'wbCurrentSite' 
).globalSiteId )
-                                                       .done( doLink )
-                                                       .fail( onError );
+                                                       .done( 
$siteLinkWidget.doLink )
+                                                       .fail( 
$siteLinkWidget.onError );
                                        } else {
                                                // The current page already is 
linked with an item which is linked with other pages... this probably some kind 
of edit conflict.
                                                // Show an error and let the 
user purge the page
@@ -535,35 +538,35 @@
                                                        { gravity: 'nw' }
                                                );
 
-                                               removeSpinner();
+                                               $siteLinkWidget.removeSpinner();
                                                tooltip.show();
 
                                                // Replace the button with one 
asking to close the dialog and reload the current page
                                                $goButton
                                                        .off( 'click' )
                                                        .click( function() {
-                                                               showSpinner();
+                                                               
$siteLinkWidget.showSpinner();
                                                                
window.location.href = mw.config.get( 'wgServer' ) + mw.config.get('wgScript' ) 
+ '?title=' + encodeURIComponent( mw.config.get( 'wgPageName' ) ) + 
'&action=purge';
                                                        } )
                                                        .button( 'option', 
'label', mw.msg( 'wikibase-linkitem-close' ) );
                                        }
                                }
                        } );
-       }
+       },
 
        /**
         * Called after an entity has succesfully been created.
         */
-       function successfullyCreated() {
-               onSuccess( 'create' );
-       }
+       successfullyCreated: function() {
+               $siteLinkWidget.onSuccess( 'create' );
+       },
 
        /**
         * Called after an entity has succesfully been linked.
         */
-       function successfullyLinked() {
-               onSuccess( 'link' );
-       }
+       successfullyLinked: function() {
+               $siteLinkWidget.onSuccess( 'link' );
+       },
 
        /**
         * Called after an entity has succesfully been linked or created. 
Replaces the dialog content with a useful
@@ -571,9 +574,9 @@
         *
         * @param {string} type ( create or link )
         */
-       function onSuccess( type ) {
+       onSuccess: function( type ) {
                var mwApi = new mw.Api(),
-                       itemUri = linkRepoTitle( 'Special:ItemByTitle/' + 
mw.config.get( 'wbCurrentSite' ).globalSiteId + '/' + mw.config.get( 
'wgPageName' ) );
+                       itemUri = $siteLinkWidget.linkRepoTitle( 
'Special:ItemByTitle/' + mw.config.get( 'wbCurrentSite' ).globalSiteId + '/' + 
mw.config.get( 'wgPageName' ) );
 
                $dialog
                        .empty()
@@ -592,13 +595,13 @@
                                $( '<p>' )
                                        .text( mw.msg( 
'wikibase-replicationnote' ) )
                        );
-               removeSpinner();
+               $siteLinkWidget.removeSpinner();
 
                // Replace the button with one asking to close the dialog and 
reload the current page
                $goButton
                        .off( 'click' )
                        .click( function() {
-                               showSpinner();
+                               $siteLinkWidget.showSpinner();
                                window.location.reload( true );
                        } )
                        .button( 'option', 'label', mw.msg( 
'wikibase-linkitem-close' ) );
@@ -608,18 +611,18 @@
                        action: 'purge',
                        titles: mw.config.get( 'wgPageName' )
                } );
-       }
+       },
 
        /**
         * Called in case an error occurs and displays an error message.
-        * 
+        *
         * Can either show a given errorCode (as html) or use data from an
         * API failure (pass two parameters in this case).
         *
         * @param {string} errorCode
         * @param {object} errorInfo
         */
-       function onError( errorCode, errorInfo ) {
+       onError: function( errorCode, errorInfo ) {
                var $elem, tooltip, error;
                if ( $( '#wbclient-linkItem-page' ).length ) {
                        $elem = $( '#wbclient-linkItem-page' );
@@ -635,7 +638,7 @@
 
                tooltip = new wb.ui.Tooltip( $elem, {}, error, { gravity: 'nw' 
} );
 
-               removeSpinner();
+               $siteLinkWidget.removeSpinner();
                tooltip.show();
 
                // Remove the tooltip if the user clicks onto the dialog trying 
to correct the input
@@ -643,13 +646,13 @@
                $dialog.on( 'dialogclose click', function() {
                        tooltip.destroy();
                } );
-       }
+       },
 
        /**
         * Let the user know that the site given is invalid
         *
         */
-       function invalidSiteGiven() {
+       invalidSiteGiven: function() {
                var $linkItemSite = $( '#wbclient-linkItem-Site' ),
                        tooltip = new wb.ui.Tooltip( $linkItemSite, {}, mw.msg( 
'wikibase-linkitem-invalidsite' ) );
 
@@ -658,7 +661,7 @@
                        // Remove the tooltip by the time the user tries to 
correct the input
                        tooltip.destroy();
                } );
-       }
+       },
 
        /**
         * Returns a link to the given title on the repo.
@@ -666,26 +669,12 @@
         * @param {string} title
         * @return {string}
         */
-       function linkRepoTitle( title ) {
+       linkRepoTitle: function( title ) {
                return mw.config.get( 'wbRepoUrl' ) + mw.config.get( 
'wbRepoArticlePath' ).replace( /\$1/g, mw.util.wikiUrlencode( title ) );
        }
 
-       /**
-        * Displays the link which shows the dialog after checking whether the 
user is logged ins
-        *
-        */
-       $( document ).ready( function() {
-               $( '#wbc-linkToItem' )
-                       .empty()
-                       .append(
-                               $( '<a>' )
-                               .attr( {
-                                       href: '#',
-                                       id: 'wbc-linkToItem-link'
-                               } )
-                               .text( mw.msg( 'wikibase-linkitem-addlinks' ) )
-                               .click( checkLoggedin )
-                       );
-               $( '#p-lang' ).show();
-       } );
+};
+
+wb.AddSiteLinkWidget = $siteLinkWidget;
+
 } )( wikibase, mediaWiki, jQuery );

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

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

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

Reply via email to