Jump to content

MediaWiki:Gadget-templating.js

From National Library of Greece
Revision as of 00:02, 14 March 2025 by Admin (talk | contribs) (Created page with "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" } }] } }; function statementExists(property, value) { items.toArray().some(item => !!$(item).data().propertyId && $(item).da...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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');

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"
            }

        }]
    }
};


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>');

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