1

I'm working on a tinyMCE plugin, and it has to show a full list of FontAwesome icons. To show them, the code is (reduced) this:

    {
    type: 'listbox',
    name: 'caixa_icone',
    label: 'Icon²',
    'values': [
    {
        text:'address-book',
        value:'address-book',
        icon:' fa fa-address-book'
    },
   {
        text:'address-book-o',
        value:'address-book-o',
        icon:' fa fa-address-book-o'
   },
   {
        text:'anchor',
        value:'anchor',
        icon:' fa fa-anchor'
   },
   {
        text:'archive',
        value:'archive',
        icon:' fa fa-archive'
   },
   {
        text:'area-chart',
        value:'area-chart',
        icon:' fa fa-area-chart'
   },
   // ETC...

All values to show icons will result in something like 5k lines of code. How can I, if possible, store those values to reuse anywhere inside the script?

Example (structural, of course not working):

var variables = "{
    text:'address-book',
    value:'address-book',
    icon:' fa fa-address-book'
},
{
    text:'address-book-o',
    value:'address-book-o',
    icon:' fa fa-address-book-o'
}";
Daniel Lemes
  • 279
  • 4
  • 21
  • Look at the javascript singleton pattern, and save that in a global namespace? http://stackoverflow.com/questions/31222765/javascript-singleton-pattern – Snowmonkey Jan 18 '17 at 18:49

2 Answers2

0
window.yourGlobalVariable = ...;

Here's the thread Define global variable in a JavaScript function

Community
  • 1
  • 1
Vincent
  • 204
  • 1
  • 12
0

Thank you all, I found my answer with a not so little help from here: https://wordpress.stackexchange.com/questions/214652/insert-dynamic-listbox-options-in-tinymce-popup-editor

THIS in the main tinymce function:

var textt = new Array('');
var variables = [
    "address-book", "address-book-o", "gear", "user" // ETC
];
var arrayLength = variables.length;

function get_icon_list() {
    var result = [];
    var res = [];
    for (var i = 0; i < arrayLength; i++) {
        textt.push("{text:'"+ variables[i] +"', value:'"+ variables[i] +"', icon:' fa fa-"+ variables[i] +"'}");        

        res[i] = {};
        res[i]['text'] = variables[i];
        res[i]['value'] = variables[i];
        res[i]['icon'] = ' fa fa-'+variables[i];
        result.push(res[i]);

    }
    return result;
}

Any listbox showing icons will be:

{
    type: 'listbox',
    name: 'caixa_icone',
    label: 'Icon²',
    'values': get_icon_list(),
}

And some CSS:

.mce-menu-has-icons i.mce-ico:before {font: 15px FontAwesome;}
Community
  • 1
  • 1
Daniel Lemes
  • 279
  • 4
  • 21