Henning Snater has uploaded a new change for review. https://gerrit.wikimedia.org/r/106257
Change subject: Always display "In other languages" section ...................................................................... Always display "In other languages" section (bug 49079) Now, the "In other languages" section is displayed always. If the user has no languages configured using the Babel extension, ULS is queried for frequent languages filling the section. In order to improve modularity along with implementing that new functionality, MediaWiki's hook mechanism is used instead of using the document ready handler. Change-Id: I1978e78cc170c1adb81655eda3d41246fa33f4d4 --- M repo/resources/Resources.php A repo/resources/wikibase.ui.entityInit.js M repo/resources/wikibase.ui.entityViewInit.js A repo/resources/wikibase.ui.termBoxInit.js 4 files changed, 222 insertions(+), 63 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/57/106257/1 diff --git a/repo/resources/Resources.php b/repo/resources/Resources.php index 4dfd934..847fc74 100644 --- a/repo/resources/Resources.php +++ b/repo/resources/Resources.php @@ -23,17 +23,16 @@ 'wikibase.ui.entityViewInit.js' // should probably be adjusted for more modularity ), 'dependencies' => array( + 'mediawiki.api', 'mediawiki.user', 'wikibase.ui.PropertyEditTool', 'jquery.wikibase.entityview', 'jquery.wikibase.toolbarcontroller', 'jquery.wikibase.wbtooltip', - 'wikibase.datamodel', - 'jquery.json', 'jquery.cookie', - 'wikibase.serialization.entities', - 'wikibase.serialization.fetchedcontent', - 'jquery.wikibase.claimgrouplabelscroll' + 'jquery.wikibase.claimgrouplabelscroll', + 'wikibase.ui.entityInit', + 'wikibase.ui.termBoxInit', ), 'messages' => array( 'wikibase-statements', @@ -43,6 +42,39 @@ 'wikibase-entity-property', 'wikibase-restrictionedit-tooltip-message', 'wikibase-blockeduser-tooltip-message', + ) + ), + + 'wikibase.ui.entityInit' => $moduleTemplate + array( + 'scripts' => array( + 'wikibase.ui.entityInit.js', + ), + 'dependencies' => array( + 'jquery.json', + 'wikibase', + 'wikibase.datamodel', + 'wikibase.serialization', + 'wikibase.serialization.entities', + 'wikibase.serialization.fetchedcontent', + 'wikibase.store.FetchedContent', + ), + ), + + 'wikibase.ui.termBoxInit' => $moduleTemplate + array( + 'scripts' => array( + 'wikibase.ui.termBoxInit.js', + ), + 'dependencies' => array( + 'jquery.wikibase.toolbar', + 'jquery.wikibase.toolbareditgroup', + 'mediawiki.Title', + 'wikibase', + 'wikibase.templates', + 'wikibase.ui.entityInit', + 'wikibase.ui.PropertyEditTool', + ), + 'messages' => array( + 'wikibase-terms', ) ), @@ -111,6 +143,8 @@ ); if ( defined( 'ULS_VERSION' ) ) { + $modules['wikibase.ui.termBoxInit']['dependencies'][] = 'ext.uls.displaysettings'; + $modules['wikibase.ui.termBoxInit']['dependencies'][] = 'jquery.uls.data'; $modules['wikibase.special.itemDisambiguation']['dependencies'][] = 'jquery.uls.data'; $modules['wikibase.special.entitiesWithout']['dependencies'][] = 'jquery.uls.data'; } diff --git a/repo/resources/wikibase.ui.entityInit.js b/repo/resources/wikibase.ui.entityInit.js new file mode 100644 index 0000000..3556462 --- /dev/null +++ b/repo/resources/wikibase.ui.entityInit.js @@ -0,0 +1,39 @@ +/** + * Entity initialization. + * Unserializes the entities passed to JavaScript via mw.config variables. + * @since 0.5 + * @licence GNU GPL v2+ + * + * @author: H. Snater < mediaw...@snater.com > + */ +( function( $, mw, wb ) { + 'use strict'; + + mw.hook( 'wikipage.content' ).add( function() { + + if( mw.config.get( 'wbEntity' ) !== null ) { + + var entityJSON = $.evalJSON( mw.config.get( 'wbEntity' ) ), + usedEntitiesJSON = $.evalJSON( mw.config.get( 'wbUsedEntities' ) ), + unserializerFactory = new wb.serialization.SerializerFactory(), + entityUnserializer = unserializerFactory.newUnserializerFor( wb.Entity ); + + // Unserializer for fetched content whose content is a wb.Entity: + var fetchedEntityUnserializer = unserializerFactory.newUnserializerFor( + wb.store.FetchedContent, { + contentUnserializer: entityUnserializer + } + ); + + wb.entity = entityUnserializer.unserialize( entityJSON ); + entityJSON = null; + + $.each( usedEntitiesJSON, function( id, fetchedEntityJSON ) { + wb.fetchedEntities[id] = fetchedEntityUnserializer.unserialize( fetchedEntityJSON ); + } ); + + } + + } ); + +} )( jQuery, mediaWiki, wikibase ); \ No newline at end of file diff --git a/repo/resources/wikibase.ui.entityViewInit.js b/repo/resources/wikibase.ui.entityViewInit.js index 1375d30..21f1646 100644 --- a/repo/resources/wikibase.ui.entityViewInit.js +++ b/repo/resources/wikibase.ui.entityViewInit.js @@ -16,7 +16,8 @@ 'use strict'; /* jshint nonew: false */ - $( document ).ready( function() { + mw.hook( 'wikipage.content' ).add( function() { + // TODO: Remove global DOM adjustments // remove HTML edit links with links to special pages // for site-links we don't want to remove the table cell representing the edit section $( 'td.wb-editsection' ).empty(); @@ -25,6 +26,12 @@ // remove all infos about empty values which are displayed in non-JS $( '.wb-value-empty' ).empty().removeClass( 'wb-value-empty' ); + + // Since the DOM is altered for the property edit tools to property initialize, the + // following hook informs about these operations having finished. + // TODO: This hook is not supposed to be permanent. Remove it as soon as no more global DOM + // adjustments are necessary. + mw.hook( 'wikibase.domready' ).fire(); // add an edit tool for the main label. This will be integrated into the heading nicely: if ( $( '.wb-firstHeading' ).length ) { // Special pages do not have a custom wb heading @@ -57,45 +64,7 @@ } } ); - var termsValueTools = []; - - $( 'tr.wb-terms-label, tr.wb-terms-description' ).each( function() { - var $termsRow = $( this ), - editTool = wb.ui.PropertyEditTool[ - $termsRow.hasClass( 'wb-terms-label' ) - ? 'EditableLabel' - : 'EditableDescription' - ], - $toolbar = mw.template( 'wikibase-toolbar', '', '' ).toolbar(), - toolbar = $toolbar.data( 'toolbar' ), - $editGroup = mw.template( 'wikibase-toolbareditgroup', '', '' ).toolbareditgroup(); - - toolbar.addElement( $editGroup ); - toolbar.$editGroup = $editGroup; // TODO: EditableLabel should not assume that this is set - - termsValueTools.push( editTool.newFromDom( $termsRow, {}, toolbar ) ); - } ); - if( mw.config.get( 'wbEntity' ) !== null ) { - var entityJSON = $.evalJSON( mw.config.get( 'wbEntity' ) ), - usedEntitiesJSON = $.evalJSON( mw.config.get( 'wbUsedEntities' ) ), - unserializerFactory = new wb.serialization.SerializerFactory(), - entityUnserializer = unserializerFactory.newUnserializerFor( wb.Entity ); - - // unserializer for fetched content whose content is a wb.Entity: - var fetchedEntityUnserializer = unserializerFactory.newUnserializerFor( - wb.store.FetchedContent, { - contentUnserializer: entityUnserializer - } - ); - - wb.entity = entityUnserializer.unserialize( entityJSON ); - entityJSON = null; - - $.each( usedEntitiesJSON, function( id, fetchedEntityJSON ) { - wb.fetchedEntities[ id ] = fetchedEntityUnserializer.unserialize( fetchedEntityJSON ); - } ); - // if there are no aliases yet, the DOM structure for creating new ones is created manually since it is not // needed for running the page without JS $( '.wb-aliases-empty' ) @@ -214,18 +183,6 @@ } $( wb ).on( 'startItemPageEditMode', function( event, origin, options ) { - // disable language terms table's editable value or mark it as the active one if it is - // the one being edited by the user and therefore the origin of the event - $.each( termsValueTools, function( i, termValueTool ) { - if ( !( origin instanceof wb.ui.PropertyEditTool.EditableValue ) - || origin.getSubject() !== termValueTool.getSubject() - ) { - termValueTool.disable(); - } else if ( origin && origin.getSubject() === termValueTool.getSubject() ) { - $( 'table.wb-terms' ).addClass( 'wb-edit' ); - } - } ); - // Display anonymous user edit warning: if ( mw.user && mw.user.isAnon() && $.find( '.mw-notification-content' ).length === 0 @@ -326,13 +283,6 @@ $messageAnchor.data( 'wbtooltip' ).degrade( true ); } ); } - } ); - - $( wb ).on( 'stopItemPageEditMode', function( event ) { - $( 'table.wb-terms' ).removeClass( 'wb-edit' ); - $.each( termsValueTools, function( i, termValueTool ) { - termValueTool.enable(); - } ); } ); // remove loading spinner after JavaScript has kicked in diff --git a/repo/resources/wikibase.ui.termBoxInit.js b/repo/resources/wikibase.ui.termBoxInit.js new file mode 100644 index 0000000..727fad2 --- /dev/null +++ b/repo/resources/wikibase.ui.termBoxInit.js @@ -0,0 +1,136 @@ +/** + * Term box initialisation. + * The term box displays label and description in languages other than the user language. + * @since 0.5 + * @licence GNU GPL v2+ + * + * @author: H. Snater < mediaw...@snater.com > + */ +( function( $, mw, wb ) { + 'use strict'; + + mw.hook( 'wikibase.domready' ).add( function() { + var termsValueTools = [], + ulsIsDefined = mw.uls !== undefined && $.uls.data !== undefined; + + if( $( 'tr.wb-terms-label, tr.wb-terms-description' ).length === 0 && ulsIsDefined ) { + // No term box present; Ask ULS to provide languages and generate plain HTML: + var languageCodes = mw.uls.getFrequentLanguageList(), + title = new mw.Title( + mw.config.get( 'wgTitle' ), + mw.config.get( 'wgNamespaceNumber' ) + ); + + if( languageCodes.length === 0 ) { + return; + } + + var $sectionHeading = addTermBoxSection(); + $sectionHeading.after( renderTermBox( title, wb.entity, languageCodes.slice( 1, 4 ) ) ); + } + + $( 'tr.wb-terms-label, tr.wb-terms-description' ).each( function() { + var $termsRow = $( this ), + editTool = wb.ui.PropertyEditTool[ + $termsRow.hasClass( 'wb-terms-label' ) ? 'EditableLabel' : 'EditableDescription' + ], + $toolbar = mw.template( 'wikibase-toolbar', '', '' ).toolbar(), + toolbar = $toolbar.data( 'toolbar' ), + $editGroup = mw.template( 'wikibase-toolbareditgroup', '', '' ).toolbareditgroup(); + + toolbar.addElement( $editGroup ); + + // TODO: EditableLabel should not assume that this is set + toolbar.$editGroup = $editGroup; + + termsValueTools.push( editTool.newFromDom( $termsRow, {}, toolbar ) ); + } ); + + $( wb ) + .on( 'startItemPageEditMode', function( event, origin, options ) { + // disable language terms table's editable value or mark it as the active one if it is + // the one being edited by the user and therefore the origin of the event + $.each( termsValueTools, function( i, termValueTool ) { + if( + !( origin instanceof wb.ui.PropertyEditTool.EditableValue ) + || origin.getSubject() !== termValueTool.getSubject() + ) { + termValueTool.disable(); + } else if( origin && origin.getSubject() === termValueTool.getSubject() ) { + $( 'table.wb-terms' ).addClass( 'wb-edit' ); + } + } ); + } ) + .on( 'stopItemPageEditMode', function( event ) { + $( 'table.wb-terms' ).removeClass( 'wb-edit' ); + $.each( termsValueTools, function( i, termValueTool ) { + termValueTool.enable(); + } ); + } ); + + } ); + + /** + * @return {jQuery} + */ + function addTermBoxSection() { + var $sectionHeading = mw.template( 'wb-terms-heading', mw.msg( 'wikibase-terms' ) ); + + $( '#toc' ) + .after( $sectionHeading ) + .children( 'ul' ).prepend( + $( '<li/>' ) + .addClass( 'toclevel-1' ) + .append( + $( '<a/>' ) + .attr( 'href', '#wb-terms' ) + .text( mw.msg( 'wikibase-terms' ) ) + ) + ) + .find( 'li' ).each( function( i, li ) { + $( li ) + .removeClass( 'tocsection-' + i ) + .addClass( 'tocsection-' + ( i + 1 ) ); + } ); + + return $sectionHeading; + } + + /** + * @param {mediaWiki.Title} title + * @param {wikibase.Entity} entity + * @param {string[]} languageCodes + * @return {jQuery|undefined} + */ + function renderTermBox( title, entity, languageCodes ) { + if( languageCodes === undefined ) { + return; + } + var labels = entity.getLabels(), + descriptions = entity.getDescriptions(), + rowNumber = 0, + $tbody = $( '<tbody/>' ); + + for( var i = 0; i < languageCodes.length; i++ ) { + var languageCode = languageCodes[i], + alternatingClass = ( rowNumber++ % 2 ) ? 'even' : 'uneven'; + + $tbody.append( mw.template( 'wb-term', + languageCode, + alternatingClass, + $.uls.data.getAutonym( languageCode ), + ( labels[languageCode] !== undefined ) ? labels[languageCode] : '', + ( descriptions[languageCode] !== undefined ) ? descriptions[languageCode] : '', + '', + '', + '', + '', + title.getUrl() + '?setlang=' + languageCode + ) ); + } + + return mw.template( 'wb-terms-table', $tbody ); + } + + +} )( jQuery, mediaWiki, wikibase ); \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/106257 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1978e78cc170c1adb81655eda3d41246fa33f4d4 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits