Difference between revisions of "MediaWiki:Gadget-QuickPresets.js"
Jump to navigation
Jump to search
Line 2: | Line 2: | ||
mw.notify("Launching User:Podehaye/quickpresets.js") | mw.notify("Launching User:Podehaye/quickpresets.js") | ||
importStylesheet( 'MediaWiki:quickpresets.css' ); | importStylesheet( 'MediaWiki:quickpresets.css' ); | ||
+ | mw.notify("Launching User:Podehaye/quickpresets.js") | ||
+ | |||
if( typeof(quick_props)=="undefined" ) { | if( typeof(quick_props)=="undefined" ) { | ||
importScript( 'MediaWiki:quickpresets_defaultconf.js' ); | importScript( 'MediaWiki:quickpresets_defaultconf.js' ); |
Revision as of 21:29, 2 January 2020
// taken from https://www.wikidata.org/wiki/User:MichaelSchoenitzer/quickpresets.js
mw.notify("Launching User:Podehaye/quickpresets.js")
importStylesheet( 'MediaWiki:quickpresets.css' );
mw.notify("Launching User:Podehaye/quickpresets.js")
if( typeof(quick_props)=="undefined" ) {
importScript( 'MediaWiki:quickpresets_defaultconf.js' );
}
mw.notify("Launching User:Podehaye/quickpresets.js, part 2")
/**
* Load the entityselector-plugin, try until it works
* See: https://stackoverflow.com/questions/47985370/
*/
function quickpresets_loadentityselector(){
try {
$( ".quickpresetsiteminput input" ).entityselector( {
url: 'http://wiki.personaldata.io/w/api.php',
language: mw.config.get('wgUserLanguage')
} );
}
catch(err) {
setTimeout(quickpresets_loadentityselector, 100);
}
}
/**
* Add the interface wich alows to quickly add statements
*/
function quickpresets_addinterface(qid, prop) {
// compose and intert fieldset.
var $fieldset = $("<fieldset id=\"quickpresets\"><legend>Quick Presets (" + prop.name + ")</legend></fieldset>")
.insertAfter($('.wikibase-statementgrouplistview').first());
prop.defaults.forEach(function(dflt) {
if(typeof dflt.type == "undefined") {
dflt.type = "item";
}
var $line = $("<div><p class='addstatement'>Add "+dflt.name+":<span class='links'></span>" +
"<span class='quickpresets" + dflt.type + "input'> <input/> " +
"<a href=\'#\'>✔</a></span></p></div>"
).appendTo($fieldset);
if($('#P'+dflt.pid).find(".wikibase-snakview-value a").length !== 0) {
$line.addClass('disabled');
}
if(dflt.values.length === 0) {
$line.addClass('compact');
}
$line.find('.quickpresets' + dflt.type + 'input a')
.on('click', function(e) {
e.preventDefault();
if(dflt.type == "item") {
var selection = $(this).prev('input').data('entityselector').selectedEntity();
quickpresets_additemstatement(qid, dflt.pid, dflt.name, selection.id.substring(1), selection.label);
}
else if(dflt.type == "string") {
quickpresets_addstringstatement(qid, dflt.pid, dflt.name, $(this).prev('input')[0].value);
}
});
// Add links for all preset-values…
dflt.values.forEach(function(val) {
$("<span> <a href='#'></a> ∙</span>")
.appendTo($line.find('span.links'))
.find('a')
.text(val.name)
.on('click', function(e) {
e.preventDefault();
if(dflt.type == "item") {
quickpresets_additemstatement(qid, dflt.pid, dflt.name, val.qid, val.name);
}
else if(dflt.type == "string") {
quickpresets_addstringstatement(qid, dflt.pid, dflt.name, val.text);
}
});
});
});
setTimeout(quickpresets_loadentityselector, 100);
}
/**
* Add a formated claim and in case of success show succtext
*/
function quickpresets_addclaim(claim, succtext) {
var api = new mw.Api();
var token = mw.user.tokens.values.csrfToken;
api.post( {
action: 'wbsetclaim',
summary: "Added with [[User:Podehaye/quickpresets.js|Quickpresets]]",
claim: JSON.stringify(claim),
token: token
}).then(
function(aw){
if(aw.success == 1)
$('.wikibase-statementgrouplistview').first().after( succtext );
});
}
/**
* Add a statement with datatype 'item'
* qid: the qid of the item wich should get the statment
* pid: the property to add
* pname: the name of the property for showing a success-note
* toqid: the qid of the item that is used as value
* toname: the name of the value-item property for showing a success-note
*/
function quickpresets_additemstatement(qid, pid, pname, toqid, toname) {
var claim = {
id: (new wb.utilities.ClaimGuidGenerator("q" + qid)).newGuid(),
type: "claim",
mainsnak: {
snaktype: "value",
property: "P" + pid,
datavalue: {
value: {
"entity-type": "item",
"numeric-id": toqid
},
type: "wikibase-entityid"
},
datatype: "wikibase-item"
}
};
quickpresets_addclaim(claim, "<p>Added "+pname+" "+toname+"</p>");
// Check if we now support it with another element of the config
if(pid == 3) { // MODIFIED LINE
$('#quickpresets').remove();
for( var t = 0; t<quick_props.length; t++) {
if(quick_props[t].qid == toqid) {
quickpresets_addinterface(qid, quick_props[t]);
}
}
}
}
/**
* Add a statement with datatype 'string'
* qid: the qid of the item wich should get the statment
* pid: the property to add
* pname: the name of the property for showing a success-note
* str: the value of the statement
*/
function quickpresets_addstringstatement(qid, pid, pname, str) {
var claim = {
id: (new wb.utilities.ClaimGuidGenerator("q" + qid)).newGuid(),
type: "claim",
mainsnak: {
snaktype: "value",
property: "P" + pid,
datatype: "string",
datavalue: {
value: str,
type: "string"
}
}
};
quickpresets_addclaim(claim, "<p>Added "+pname+" \""+str+"\"</p>");
}
/**
* check if an item is of a type, if so add options
*/
function quickpresets_checktype(qid, prop) {
var typeqid=prop.qid;
// Search in HTML-DOM
var typefromdom=$.map($('#P3').find(".wikibase-snakview-value a"), function(value, index) {
if(typeof value.attributes.href === "undefined")
{
mw.notify("exit 1");
return [];
}
else
{
mw.notify("exit 2");
return [value.attributes.href.value.substring(6)];
}
});
if ( typefromdom.length === 0) {
if(typeqid === null)
{
mw.notify("exit 3")
quickpresets_addinterface(qid, prop);
}
}
else {
mw.notify("exit 4")
if ( typeqid === 0 ) {
mw.notify("exit 5")
quickpresets_addinterface(qid, prop);
}
else if ( typefromdom.includes("Q"+typeqid) ) {
mw.notify("exit 6")
quickpresets_addinterface(qid, prop);
}
else {
mw.notify("exit 7")
// Now check via sparql (much slower but includes subtypes)
const endpointUrl = 'https://query.personaldata.io/proxy/wdqs/bigdata/namespace/wdq/sparql',
sparqlQuery = 'Ask { pdio:Q' + qid + ' pdiot:P3/pdiot:P4* pdio:Q' + typeqid +
'. hint:Prior hint:gearing "forwards" }',
fullUrl = endpointUrl + '?query=' + encodeURIComponent( sparqlQuery ),
headers = { 'Accept': 'application/sparql-results+json' };
fetch( fullUrl, { headers } ).then( body => body.json() ).then( json => {
if ( json.boolean )
quickpresets_addinterface(qid, prop);
} );
}
}
}
/**
* wait for config to be loaded, then execute func
*/
function quickpresets_waitForConfig(func){
if(typeof quick_props !== "undefined"){
func();
}
else{
setTimeout(quickpresets_waitForConfig, 100, func);
}
}
$( function($) {
/**
* Check if we're viewing an item
*/
var qid = mw.config.get( 'wbEntityId' );
if ( !qid ) {
mw.notify("Launching User:Podehaye/quickpresets.js part 3")
return;
}
else {
qid = qid.substring(1);
mw.notify("Launching User:Podehaye/quickpresets.js part 4")
}
/**
* check if item is a of right kind
*/
quickpresets_waitForConfig(function() {
for( var t = 0; t<quick_props.length; t++) {
mw.notify("Launching User:Podehaye/quickpresets.js part 5")
quickpresets_checktype(qid, quick_props[t]);
}
});
} );