JSFiddle - React, Tailwind, and code Playground

by pavel_krupala

JavaScript

var MessageComposer = {
	
    // detection if command is allowed to call or not
    isAllowed: function(cmd) {        
        return _.indexOf([
            'inittab', 'exec', 'call', 'requestcfg', 'loadplugin', 'requestbuttons', 'registerbutton',
            'movebutton', 'removebutton', 'requestdynbutton',
            'run'                
        ], cmd) !== -1;
    },
    
    // replaced command RUN
    cmd_run: function(data) {
        _.extend(data, data, {foo: 'bar'});        
        MessageComposer.sendData(data);
    },
    
    // abstract function which returns current TabID
    getCurrentTabID: function(data) {
        return Math.floor((Math.random() * 10) + 1);
    },
    
    // make our sending data safe, all output types are string, append TabID
    preprocessData: function(data) {
        _.extend(data, data, {TabID: MessageComposer.getCurrentTabID(data)});
        for (key in data) data[key] = data[key].toString();
        return data;
    },
    
    // abstract function which actually sends JSON object to DLL/EXE
    sendData: function(data) {
        console.log("MC::sendData", data);
    },
    
    /* execution function
     * - detects if command is allowed
     * - calls cmd_<command> function if present or default sendData
     */
	execute: function(cmd_name, data) {
		var input_data = {};
        _.extend(input_data, { Command: cmd_name }, data);
        if (! MessageComposer.isAllowed(cmd_name) ) {
            console.log("ERROR (MSGCOMP): calling '"+cmd_name+"' is not supported.");
        } else {
            if (MessageComposer['cmd_'+cmd_name]) {
                return MessageComposer['cmd_'+cmd_name].call(MessageComposer, MessageComposer.preprocessData(input_data));
            } else {
		        return MessageComposer['sendData'].call(MessageComposer, MessageComposer.preprocessData(input_data));
            }
        }
    }
};

/*
// overwrite of sendData for custom behaviour
MessageComposer.sendData = function(data) {...