Jump to content

MediaWiki:Gadget-templating.js

From National Library of Greece
Revision as of 06:43, 14 March 2025 by Admin (talk | contribs)

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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
console.log('test');

WbTemplates = {

    "person": {
        "prefilledClaims": [{
            "property": {id: "P1", label: "instance of", datatype: "wikibase-item"},
            "value": {
                "mainVal": {id: "Q78", datatype: "wikibase-item"}
            }

        }],
        "userDefinedClaims": [{
        	"property": {id: "P162", label: "has authorized access point for person [a/datatype] (RDA:P50411)"}, 
        	datatype: "string"
        }],
    },
    "corporate": {
        "prefilledClaims": [{
            "property": "P1",
            "value": {
                "mainVal": "Q72"
            }

        }]
    },
};


function statementExists(property, value) {

    items.toArray().some(item => !!$(item).data().propertyId && $(item).data().statementgroupview.options.value.getKey() === property &&
        $(item).data().statementgroupview.options.value.getItemContainer().toArray().some(claimItem =>
            claimItem.getClaim().getMainSnak().getValue().getSerialization() === value));

}


function getPropertyEntity(property) {

    return {
        "id": `${property.id}`,
        "title": `Property:${property.id}`,
        "datatype": property.datatype,
        "label": property.label,
    };


}


function prefillStatement(claim, onlyProperty) {

    statementListView = $(".wikibase-statementgrouplistview").first().data().wikibaseStatementgrouplistview;
    statementListView.enterNewItem();

    items = statementListView.listview.items();
    item = items.last().data();
    sv = item.statementgroupview.statementlistview._listview.items().first().data().statementview;
    snak = sv.$mainSnak.data().snakview;


    es = getPropertyEntity(claim.property);

    selector = snak._getPropertySelector();
    selector.element.val(es.label);
    selector._trigger('change');
    selector._select(es);
    
    if(onlyProperty) return;

    snak._variation._valueView.element.one(
        snak._variation._valueView.widgetEventPrefix + 'afterstartediting',
        function() {
        	debugger;
            valSelector = snak._variation._valueView._expert.$input.data().entityselector;
            valval = getPropertyEntity(claim.value.mainVal);
            
            valSelector.element.val(valval.label);
            valSelector._trigger("change");
            valSelector._select(valval);
            
            if(!!claim.qualifiers) {
            	
            	qlistview = sv._qualifiers;
	            qlistview.enterNewItem();
	            
	            var qsnaklistview = qlistview.value()[qlistview.value().length - 1];
	            qsnaklistview.enterNewItem();
	            qslv = $(sv._qualifiers.items().first()).data().snaklistview;
	            qsnak = qslv._listview.items().first().data().snakview;
	
	            qsel = qsnak._getPropertySelector();
	            qsel.element.val(es.label);
	            qsel._trigger('change');
	            qsel._select(es);
	
	            qsnak._variation._valueView.element.one(
	                qsnak._variation._valueView.widgetEventPrefix + 'afterstartediting',
	                function() {
	                    qvalSelector = qsnak._variation._valueView._expert.$input.data().entityselector;
	                    qvalval = getPropertyEntity(claim.value.mainVal);
	
	                    qvalSelector.element.val(qvalval.label);
	                    qvalSelector._trigger("change");
	
	                    valSelector._trigger("change");
	                    qvalSelector._select(qvalval);
	                }
	            );
	
	
	            qsnak._variation._valueView.element.one(
	                qsnak._variation._valueView.widgetEventPrefix + 'change',
	                function() {
	                    toolbar = item.statementgroupview.statementlistview._listview.items().last().data().edittoolbar;
	                    toolbar._controller.stopEditing(false);
	            	});
            	
            }

   
        }
    );

    snak._variation._valueView.element.one(
        snak._variation._valueView.widgetEventPrefix + 'change',
        function() {
            toolbar = item.statementgroupview.statementlistview._listview.items().last().data().edittoolbar;
            toolbar._controller.stopEditing(false);
        });


}


function applyTemplate(templateName) {
	WbTemplates[templateName].userDefinedClaims.forEach(claim => {
        prefillStatement(claim, true);
    });
    
    WbTemplates[templateName].prefilledClaims.forEach(claim => {
        prefillStatement(claim, false);
    });


}


$(".wikibase-entitytermsview").append('<select id="wbtemplate"></select><button id="applyTemplateBtn">Apply template</button>');
Object.entries(WbTemplates).forEach(entry => {
    $('#wbtemplate').append($('<option>', {
        value: entry[0],
        text: entry[0]
    }));

});




$("#applyTemplateBtn").on("click", function() {
    applyTemplate($("#wbtemplate").find(":selected").val());
});