User:Teester/DisplayColourSwatches.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
var entityID = mw.config.get( "wbEntityId" );

if (entityID) {
    $.ajax({
        datatype: "json",
        url: "https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=" + entityID
    })
    .done(function( data ) {
        showColour_addColour(data);
    })
    .fail(function() {
        console.log( 'The ajax request failed.' );
    });
}

function showColour_addColour(data) {
    
    if (data.entities[entityID].claims.P465) {
        showColour_addDiv(entityID, data, "P465");
    }
    
    if (data.entities[entityID].claims.P462) {
        try {
        	P462Value = data.entities[entityID].claims.P462[0].mainsnak.datavalue.value.id;
        } catch (e) {
        	// TypeError: Cannot read property '0' of undefined 
        	return;
        }
        $.ajax({
            datatype: "json",
            url: "https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=" + P462Value
        })
        .done(function( data ) {
            showColour_addDiv(P462Value, data, "P462");
        })
        .fail(function() {
            console.log( 'The ajax request failed.' );
        });
    }
    
    if (data.entities[entityID].claims.P2827) {
        P2827Value = data.entities[entityID].claims.P2827[0].mainsnak.datavalue.value.id;
        $.ajax({
            datatype: "json",
            url: "https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=" + P2827Value
        })
        .done(function( data ) {
            showColour_addDiv(P2827Value, data, "P2827");
        })
        .fail(function() {
            console.log( 'The ajax request failed.' );
        });
    }

    if (data.entities[entityID].claims.P6364) {
        P6364Value = data.entities[entityID].claims.P6364[0].mainsnak.datavalue.value.id;
        $.ajax({
            datatype: "json",
            url: "https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=" + P6364Value
        })
        .done(function( data ) {
            showColour_addDiv(P6364Value, data, "P6364");
        })
        .fail(function() {
            console.log( 'The ajax request failed.' );
        });
    }
    
    if (data.entities[entityID].claims.P11220) {
        P11220Value = data.entities[entityID].claims.P11220[0].mainsnak.datavalue.value.id;
        $.ajax({
            datatype: "json",
            url: "https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=" + P11220Value
        })
        .done(function( data ) {
            showColour_addDiv(P11220Value, data, "P11220");
        })
        .fail(function() {
            console.log( 'The ajax request failed.' );
        });
    }
}

function showColour_addDiv(entity, data, htmlClass) {
    var showColour = data.entities[entity].claims.P465[0].mainsnak.datavalue.value;
    var propertyDiv = document.getElementById(htmlClass);
    $(propertyDiv).find(".wikibase-snakview-body").append("<div class='div" + htmlClass + "'/>");
    var div = $(propertyDiv).find(".div" + htmlClass);
    $(div).css("background-color", "#" + showColour);
    $(div).css("width", "100%");
    $(div).css("height", "50px");
}