Page MenuHomePhabricator
Paste P8745

(An Untitled Masterwork)
ActivePublic

Authored by Ladsgroup on Jul 15 2019, 10:35 AM.
/**
* @see [[Wikidata:Tours/Qualifiers]]
*
* @author Bene*
*/
mw.loader.using(['ext.guidedTour', 'ext.guidedTour.launcher']).done( function() {
$.ajax( {
async: false,
url: mw.util.wikiScript() + '?title=MediaWiki:JQuery.overlay.js&action=raw&ctype=text/javascript',
dataType: 'script'
} );
( function( $, mw, gt, wb ) {
mw.util.addCSS(
'.tipsy { z-index: 100000006; } /* tipsy should stay visible */ \
.ui-suggester-list.ui-ooMenu { z-index: 1000000061; } /* suggestions should stay visible */ \
#mw-spinner-guidedtour { position: absolute; height: 100%; background-color: rgba(0, 0, 0, 0.1); z-index: 1000000; } /* the spinner */ \
#bodyContent { z-index: auto; } /* Ugly hack to make the overlay work again */'
);
var defaultStep = {
overlay: false,
closeOnClickOutside: false,
position: 'bottom', // only when attached
actionBtn: '.wikibase-toolbar-button-edit, .wikibase-toolbar-button-save'
};
/**
* Shows a spinner covering the whole page.
*/
function showSpinner() {
$( 'body' ).css( 'overflow', 'hidden' );
mw.loader.using( [ 'jquery.spinner' ], function() {
$.createSpinner( { size: 'large', type: 'block', id: 'guidedtour' } ).prependTo( 'body' );
} );
}
/**
* Removes the data from the current entity and adds the new data.
* When finished reloads the site with data=ok.
*
* @param {string} tourName
* @param {object} newData
*/
function removeData( tourName, newData ) {
mw.loader.using( 'mediawiki.api', function() {
new mw.Api()
.postWithEditToken( {
action: 'wbeditentity',
id: mw.config.get( 'wbEntityId' ),
clear: true,
data: JSON.stringify( newData ),
summary: 'Clearing data for [[Wikidata:Tours#' + tourName + '|' + tourName + ']] tour'
} )
.done( function() {
location.href += '&data=ok';
} );
} );
}
/**
* Parses the given page and returns the sections used for the single steps of the tour.
*
* @param {string} pageName
* @param {string} language
* @return {array}
*/
function getSections( pageName, language ) {
var data = JSON.parse( $.ajax( {
dataType: 'json',
url: '//www.wikidata.org/w/api.php', // this is ugly but necessary if we want to execute the tour on testwikidata
data: {
'action': 'parse',
'format': 'json',
'page': pageName + ( language === 'en' ? '' : '/' + language ),
'prop': 'text|sections',
'disablepp': true,
'disabletoc': true
},
async: false
} ).responseText );
if ( data.error ) {
if ( language !== 'en' ) {
location.href += '&uselang=en';
} else {
alert( data.error );
return false;
}
}
var rawSections = data.parse.text['*'].split( /<h2[^>]*>((?!<\/h2>).)*<\/h2>/gi ),
titles = data.parse.sections,
sections = [];
for ( var i = 0; i < titles.length; i++ ) {
sections.push( {
title: titles[i].line,
description: rawSections[i * 2 + 2]
} );
}
return sections;
}
/**
* Builds the tour's options with steps parsed form the given page.
*
* @param {string} tourName
* @param {string} tourEntityId
* @param {object} options
*
* @return {object}
*/
function buildOptionsFromPage( tourName, tourEntityId, options ) {
var language = mw.config.get( 'wgUserLanguage' ),
sections = getSections( options.pageName, language );
$.extend( options, {
name: tourName,
shouldLog: true
} );
$.each( sections, function( i ) {
var step = options.steps[i],
_onShow = step.onShow;
step = options.steps[i] = $.extend( {}, defaultStep, step, sections[i] );
$.extend( options.steps[i], {
onShow: function() {
if ( typeof _onShow === 'function' ) {
_onShow();
}
if ( step.actionBtn ) {
$( step.actionBtn ).off( 'click.guidedtour' ).one( 'click.guidedtour', function( e ) {
// continue when this button has been clicked
e.stopPropagation();
mw.libs.guiders.next();
} );
}
if ( step.overlay ) {
$( step.overlay ).overlay();
} else {
$.overlay.remove();
}
}
} );
// change the defaults for the end of the tour
if ( i === sections.length - 1 ) {
$.extend( options.steps[i], {
overlay: false,
closeOnClickOutside: true,
buttons: [ { action: 'end' } ]
} );
}
} );
if ( location.href.indexOf( 'uselang=en' ) > -1 ) {
options.steps.unshift( $.extend( {}, defaultStep, {
title: 'Not your language',
description: 'This tour is not available in your language. You may proceed using English or ' +
'<a href="' + mw.util.getUrl( options.pageName ) + '">translate it into your language</a>.'
} ) );
}
return options;
}
/**
* Defines the tour with the given options using the TourBuilder.
*
* @param {object} options
*/
function defineTour( options ) {
var tour = new gt.TourBuilder( {
name: options.name,
shouldLog: true
} );
tour.firstStep( $.extend( {
name: 'step0'
}, options.steps[0] ) ).next( 'step1' );
for ( var i = 1; i < options.steps.length - 1; i++ ) {
tour.step( $.extend( {
name: 'step' + i
}, options.steps[i] ) ).next( 'step' + ( i + 1 ) ).back( 'step' + ( i - 1 ) );
}
tour.step( $.extend( {
name: 'step' + ( options.steps.length - 1 )
}, options.steps[options.steps.length - 1] ) ).back( 'step' + ( options.steps.length - 2 ) );
}
/**
* Checks if the tour name and entity id are correct. Also checks if the data
* has been prepared and clears or adds missing data. If everything is ok,
* starts the tour.
*/
gt.init = function( tourName, tourEntityId, newData, options ) {
// check for the correct page
if ( !wb || location.href.indexOf( 'tour=' + tourName ) < 0 ||
mw.config.get( 'wbEntityId' ) !== tourEntityId || mw.config.get( 'wbIsEditView' ) !== true
) {
return;
}
// check for emptiness
if ( !gt.hasQuery( { 'data' : 'ok' } ) ) {
showSpinner();
removeData( tourName, newData );
return;
}
// launch the tour
defineTour( buildOptionsFromPage( tourName, tourEntityId, options ) );
};
} )( jQuery, mediaWiki, mediaWiki.guidedTour, wikibase );
( function( $, mw, gt, wb ) {
var tourName = 'wbqualifiers',
tourEntityId = 'Q1027',
newData = {
"claims": [ {
"mainsnak": {
"snaktype": "value",
"property": "P610",
"datavalue": {
"value": {
"entity-type": "item",
"numeric-id": 513
},
"type": "wikibase-entityid"
}
},
"type": "statement",
"rank": "normal"
}, {
"mainsnak": {
"snaktype": "value",
"property": "P1082",
"datavalue": {
"value": {
"amount": "+7000000000",
"unit": "1",
"upperBound": "+7000000001",
"lowerBound": "+6999999999"
},
"type": "quantity"
}
},
"type": "statement",
"rank": "normal"
} ]
};
gt.init( tourName, tourEntityId, newData, {
/**
* The page from where the tour texts should be loaded.
*/
pageName: 'Wikidata:Tours/References',
/**
* The steps of the tour.
*/
steps: [ {
// Welcome to Tour 3
}, {
// Statements
attachTo: '.wb-claims .wb-claimlistview:eq(1)',
overlay: '.wb-claims .wb-claimlistview:eq(1)'
}, {
// References (1)
attachTo: '.wb-claims .wb-claimlistview:eq(1)',
overlay: '.wb-claims .wb-claimlistview:eq(1)'
}, {
// References (2)
attachTo: '.wb-claims .wb-claimlistview:eq(1)',
overlay: '.wb-claims .wb-claimlistview:eq(1)'
}, {
// References (3)
attachTo: '.wb-claims .wb-claimlistview:eq(1)',
overlay: '.wb-claims .wb-claimlistview:eq(1)'
}, {
// More on references
}, {
// Creating a reference (1)
actionBtn: '.wb-claims .wb-statement-references:eq(1) .wb-addtoolbar-addbutton',
attachTo: '.wb-claims .wb-statement-references:eq(1) .wb-addtoolbar-addbutton',
overlay: '.wb-claims .wb-claimlistview:eq(1)'
}, {
// Creating a reference (2)
// @todo
overlay: '.wb-referenceview-new'
}, {
// Creating a reference (3)
// @todo
overlay: '.wb-referenceview-new'
}, {
// Editing a reference (1)
// @todo
}, {
// Editing a reference (2)
// @todo
}, {
// Editing a reference (3)
// @todo
}, {
// Editing a reference (4)
// @todo
}, {
// Congratulations!
} ]
} );
} )( jQuery, mediaWiki, mediaWiki.guidedTour, wikibase );
}).fail( function (e) { console.log(e)});

Event Timeline

Ladsgroup created this paste.Jul 15 2019, 10:35 AM