Jump to content

MediaWiki:Gadget-templating.js: Difference between revisions

From National Library of Greece
No edit summary
No edit summary
Line 149: Line 149:


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


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

Revision as of 00:10, 14 March 2025

console.log('test');

mw.loader.using('ext.maps', function() {
    mw.hook('wikipage.maps').add(function(maps) {
        console.log('Maps loaded:', maps);
    });
});



WbTemplates = {

    "person": {
        "prefilledClaims": [{
            "property": "P9",
            "value": {
                "mainVal": "P1"
            }

        }]
    },
    "corporate": {
        "prefilledClaims": [{
            "property": "P9",
            "value": {
                "mainVal": "P1"
            }

        }]
    },
};


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(propertyId, label) {

    return {
        "id": `${propertyId}`,
        "title": `Property:${propertyId}`,
        "datatype": "wikibase-property"
    };


}


function applyTemplate(templateName) {

    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(WbTemplates[templateName].prefilledClaims[0].property);

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


    snak._variation._valueView.element.one(
        snak._variation._valueView.widgetEventPrefix + 'afterstartediting',
        function() {

            valSelector = snak._variation._valueView._expert.$input.data().entityselector;


            valval = getPropertyEntity(WbTemplates[templateName].prefilledClaims[0].value.mainVal);


            valSelector.element.val(valval.label);
            valSelector._trigger("change");
            valSelector._select(valval);


            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(WbTemplates[templateName].prefilledClaims[0].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);
        });

}


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




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