Search code examples
javascriptdrop-down-menuwikimedia

Create Wikimedia drop down list which includes all Templates


I'm trying to create a custom select button to my WikiEdit toolbar. However, I cannot find how to populate it dynamically with all the Templates I have on my wiki page.

Here is what I have added to the common.js file so far:

var customizeToolbar = function() {
    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
    'sections': {
    'templates': {
        'type': 'toolbar', // Can also be 'booklet'
        'label': 'Templates'
        // or 'labelMsg': 'section-emoticons-label' for a localized label
    }
}
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
    'section': 'templates',
    'groups': {
        'templates': {
            'label': 'add-template' // or use labelMsg for a localized label, see above
        }
    }
} );

$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
    'section': 'templates',
    'group': 'templates',
    'tools': {
        'choose': {
            label: 'Add Template!', 
            type: 'select',
            action: {
                type: 'module',
                options: {
                    {{Special:Allpages/Template|mode=list}} // this does not work, but I suppose I need something similar
                }
            }
        }
    }
} );

}


Solution

  • I had to assign all templates to different buttons from the dropdown list like this:

    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
        'section': 'main',
        'group': 'templates',
        'tools': { // creates tools in the templates group
            'choose': {
                label: 'Add Template!', // the title of the drop-down
                type: 'select', // creates drop down menu
                list: {
                        Hijacker : {
                            label: 'your-label',
                            action: {
                                type: 'encapsulate',
                                options: {
                                    pre: '{{your-template-name|parameters-to-send-to-template}}',
                                }
                            }
                        } });
    

    The way to add a function to be executed when you click on a button:

     $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
        'section': 'your-section-name',
        'group': 'your-group-name',
        'tools': {
            'image': {
                label: 'your-label',
                type: 'button',
                icon: 'path-to-icon-image/image.png',
                action: {
                    type: 'callback',
                        execute: function()
                                        { 
                                          //your code to execute
                                        }
                }
            }
        }
    });