VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /var/www/html/libs/absol-form/demo/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/html/libs/absol-form/demo/sclang.js
//in processing

hr.sclang = hr.sclang || {};
/*********************   EXTENAL OPERATOR    *********************************/
absol.sclang.SCOperatorExecutor.addBinaryOperator('+', function (x, y, next) {
    var res;
    if ((x instanceof Date) && x.hasTime) {
        if (typeof y === "number") {
            res = new Date(x.getTime() + y * 3600000 * 24);
            res.hasTime = true;
            console.log('res = ', res);
            return res;
        }
        else if (typeof y === 'string') {
            return absol.datetime.formatDateTime(x, 'dd/MM/yyyy HH:mm') + y;
        }
        else return next();
    }
    else if (y instanceof Date && y.hasTime) {
        if (typeof x === "string") {
            return x + absol.datetime.formatDateTime(y, 'dd/MM/yyyy HH:mm');
        }
        else return next();
    }
    else
        return next();
});
/*
setTimeout(function () {
    if (window.ABSOL_DEBUG) {
        var x = new Date();
        x.hasTime = true;
        var y = hr.sclang.executeTemplate('Tuần sau là {{x + 7}}', { x: x });
        console.log(y, y.hasTime)
    }
}, 1000)*/


absol.sclang.SCOperatorExecutor.addBinaryOperator('-', function (x, y, next) {
    var res;
    if ((x instanceof Date) && x.hasTime && (typeof y === "number")) {
        res = new Date(x.getTime() + y * 3600000 * 24);
        res.hasTime = true;
        return res;
    }
    else
        return next();
});


/*********************  UI LIB     *********************************/


hr.sclang.ui = hr.sclang.ui || {};
hr.sclang.ui.makeModal = function () {
    var modal = absol._('modal').addTo(document.body);
    modal.contentElt = absol._('div').addTo(modal);
    return modal;
};


/*********************************************************/

hr.excelFunctions = {};


hr.sclang.NAMESPACE_TYPE_PACKAGE = 0;
hr.sclang.NODE_TYPE_BLL_FUNCTION = 129;

if (hr.systemFunctions === undefined) {
    hr.systemFunctions = {};
}
hr.sclang.global = absol.sclang.SCDynamicLibScope;

hr.sclang.global.declareConst('console', console);
hr.sclang.global.declareConst('sys', hr.systemFunctions, 'sys');
hr.sclang.global.declareConst('ui', hr.sclang.ui, 'ui');
hr.sclang.global.declareConst('print', console.log.bind(console));


hr.sclang.notificationCenter = new absol.EventEmitter();
hr.sclang.initModule = function () {
    hr.sclang.globalPackages = {};

    hr.sclang.packagesSync = null;
    hr.sclang.packageById = {};
    hr.sclang.packageScopeById = {};
    hr.sclang.nodeById = {};
    hr.sclang.astById = {};
};

hr.sclang.initModule();

hr.sclang.errorLogs = [];

hr.sclang.loadPackages = function () {
    hr.sclang.packagesSync = Promise.all([data_module.loadSCLangNamespaceList(), data_module.loadSCLangNodeContentList()])
        .then(function (res) {
            var globalPackages = hr.sclang.globalPackages;
            var namespaceList = res[0];
            var packageById = namespaceList.reduce(function (ac, cr) {
                ac[cr.id] = {};
                return ac;
            }, hr.sclang.packageById);
            namespaceList.forEach(function (ns) {
                hr.sclang.packageScopeById[ns.id] = new absol.sclang.SCScope(hr.sclang.global);
                if (packageById[ns.parentid]) {
                    packageById[ns.parentid][ns.name] = packageById[ns.id];
                }
            });
            namespaceList.forEach(function (ns) {
                if (ns.parentid + '' == '-1') {
                    hr.sclang.global.declareConst(ns.name, packageById[ns.id], true);
                    Object.defineProperty(globalPackages, ns.name, {
                        configurable: true,
                        enumerable: true, writable: false,
                        value: packageById[ns.id]
                    });
                }
            });


            var nodeList = res[1];
            var functionById = {};
            nodeList.forEach(function (nodeData) {
                var fName = nodeData.content && nodeData.content.ast && nodeData.content.ast.id && nodeData.content.ast.id.name;
                // var tempName = "function_" + nodeData.id;
                var functionName = nodeData.name;
                if (!fName) return;
                var moduleScope;
                var packageScope = hr.sclang.packageScopeById[nodeData.namespaceid];
                try {
                    nodeData.content.ast.id.name = nodeData.content.ast.id.name || functionName;
                    hr.sclang.astById[nodeData.id] = nodeData.content.ast;
                    moduleScope = new absol.sclang.SCScope(packageScope);
                    var moduleInstance = new absol.sclang.SCProgramInstance(nodeData.content.ast, moduleScope);
                    moduleInstance.exec();
                    var func = moduleScope.get(nodeData.content.ast.id.name);
                    func.ast = nodeData.content.ast;
                    Object.defineProperty(hr.sclang.nodeById, nodeData.id, {
                        writable: false,
                        enumerable: true,
                        configurable: true,
                        value: func
                    });
                    packageScope.declareConst(nodeData.name, func, true);
                    if (packageById[nodeData.namespaceid])
                        packageById[nodeData.namespaceid][nodeData.name] = func;
                } catch (err) {
                    hr.sclang.errorLogs.push([nodeData, err]);
                }

            });
            console.info("Load SCLang module completed");
            return hr.sclang.global;
        });
    return hr.sclang.packagesSync;
};

hr.sclang.loadFunctionSelection = function () {
    return Promise.all([data_module.loadSCLangNamespaceList(), data_module.loadSCLangNodeInfoList()])
        .then(function (res) {
            var namespaceList = res[0];
            var nodeList = res[1];
            var packageById = namespaceList.reduce(function (ac, cr) {
                ac[cr.id] = { value: -10 - parseInt(cr.id, 10) * 10, text: cr.name, rawData: cr };
                return ac;
            }, {});

            var packageSelection = [];
            namespaceList.forEach(function (ns) {
                if (packageById[ns.parentid]) {
                    packageById[ns.parentid].items = packageById[ns.parentid].items || [];
                    packageById[ns.parentid].items.push(packageById[ns.id]);
                }
                if (ns.parentid + '' == '-1') {
                    packageSelection.push(packageById[ns.id]);

                }
            });
            nodeList.forEach(function (nodeData) {
                if (packageById[nodeData.namespaceid]) {
                    packageById[nodeData.namespaceid].items = packageById[nodeData.namespaceid].items || [];
                    packageById[nodeData.namespaceid].items.push({
                        text: nodeData.name,
                        value: parseInt(nodeData.id, 10),
                        // rawData: nodeData,
                        isLeaf: true
                    });
                }
            });
            return packageSelection;
        });
};

hr.sclang.loadTypeDescriptors = function () {
    var globalScope = hr.sclang.global;
    globalScope.findRef('Math').type = 'Math';
    var descriptors = {};

    function argsOf(func) {
        var res = [];
        var txt, matched;
        if (func.ast) {
            res = func.ast.params.map(function (prm, i) {
                return {
                    name: (prm.id && prm.id.name) || ('arg' + i),
                    type: absol.sclang.generateSCCode(prm.typeAnnotation).replace('linktype', '').trim()
                }
            });
        }
        else {
            txt = func + '';
            matched = txt.match(/^\s*function[^(]*\(([^)]*)\)/);
            if (matched) {
                res = matched[1].split(',').map(function(x){return x.trim()}).filter(function (x){return !!x;}).map(function (name) {
                    name = name + '';
                    var optional = false;
                    if (name.indexOf('=') >= 0) {
                        name = name.split('=').shift().trim();
                        optional = true;
                    }
                    var desc = {
                        type: 'any',
                        name: name
                    }
                    if (optional) desc.optional = true;
                    return desc;
                });
            }
        }
        return res;
    }

    function visit(target, path) {
        var res = {
            typeId: path.join('.')
        };
        if (target === undefined) {
            delete res.typeId;
            res.type = 'null';
        }
        else if (target === null) {
            delete res.typeId;
            res.type = 'null';
        }
        else if (typeof target === "object") {
            res.type = 'object';
            res.properties = Object.keys(target).map(function (key) {
                var p = visit(target[key], path.concat([key]));
                p.name = key;
                return p;
            })
        }
        else if (typeof target === "function") {
            res.type = 'function';
            res.args = argsOf(target);
        }


        return res;

    }

    Object.keys(hr.sclang.global.data).forEach(function (key) {
        if (!absol.sclang.SCTypeManager.detectTypeDescriptorOf(key)) {
            descriptors[key] = Object.assign(visit(hr.sclang.global.get(key), [key]));
        }
    });
    // if (window.ABSOL_DEBUG) {
    //     absol.FileSaver.saveTextAs('module.exports = ' + absol.generateJSVariable(descriptors), '');
    // }
    absol.sclang.SCTypeManager.addTypeDescriptors(descriptors);


};

hr.sclang.ready = function () {
    if (!hr.sclang.packagesSync) {
        this.loadPackages();
        hr.sclang.packagesSync.then(function () {
            hr.sclang.loadTypeDescriptors();
        });
    }
    return hr.sclang.packagesSync;
};

hr.sclang.getNodeById = function (id) {
    return hr.sclang.ready().then(function () {
        return hr.sclang.nodeById[id];
    });
};

hr.sclang.getNodeInfoById = function (id) {
    return hr.sclang.ready().then(function () {
        var ast = hr.sclang.astById[id];
        var func = hr.sclang.nodeById[id];
        if (ast) return { ast: ast, func: func };
        return null;
    }).then(function (res) {
        if (!res) return null;
        if (res.ast.type === 'FunctionDeclaration') {
            res.type = 'function';
            res.params = res.ast.params.map(function (param) {
                var prd = { name: param.id.name };
                var type;
                if (param.typeAnnotation && param.typeAnnotation.typeAnnotation && param.typeAnnotation.typeAnnotation.id) {
                    prd.type = param.typeAnnotation.typeAnnotation.id.name;
                    if (prd.type === 'number') prd.type = -3;
                    else if (prd.type === 'string') prd.type = -1;
                    else if (prd.type === 'Date') prd.type = -4;
                    else if (prd.type === 'boolean') prd.type = -6;
                    else if (!isNaN(parseInt(prd.type, 10))) prd.type = parseInt(prd.type, 10);
                }
                return prd;
            });
        }
        return res;
    });
};

hr.sclang.saveNewPackage = function (name, description) {
    return new Promise(function (resolve, reject) {
        FormClass.api_call({
            url: "sclang_save_namespace.php",
            params: [
                { name: "parentid", value: -1 },
                { name: "name", value: name },
                { name: "description", value: EncodingClass.string.fromVariable(description) },
                { name: "type", value: hr.sclang.NAMESPACE_TYPE_PACKAGE }
            ],
            func: function (success, message) {
                console.log(message)
                if (success) {
                    if (message.substr(0, 2) == "ok") {
                        resolve({ id: parseInt(message.substring(2)) })
                    }
                    else {
                        reject({ message: message });
                    }
                }
                else {
                    reject({ message: message });
                }
            }
        });
    });
}

hr.sclang.deletePackage = function (id) {
    return new Promise(function (resolve, reject) {
        FormClass.api_call({
            url: "sclang_delete_namespace.php",
            params: [
                { name: "id", value: id }
            ],
            func: function (success, message) {
                console.log(message)
                if (success) {
                    if (message.substr(0, 2) == "ok") {
                        resolve({ id: parseInt(message.substring(2)) })
                    }
                    else {
                        reject({ message: message });
                    }
                }
                else {
                    reject({ message: message });
                }
            }
        });
    });
};

hr.sclang.deletePackageList = function (idList) {
    return new Promise(function (resolve, reject) {
        FormClass.api_call({
            url: "sclang_delete_namespace.php",
            params: [
                { name: "idlist", value: EncodingClass.string.fromVariable(idList) }
            ],
            func: function (success, message) {
                console.log(message)
                if (success) {
                    if (message.substr(0, 2) == "ok") {
                        resolve({ id: parseInt(message.substring(2)) })
                    }
                    else {
                        reject({ message: message });
                    }
                }
                else {
                    reject({ message: message });
                }
            }
        });
    });
}

hr.sclang.saveNewSubpackage = function (parentId, name, description) {
    return new Promise(function (resolve, reject) {
        FormClass.api_call({
            url: "sclang_save_namespace.php",
            params: [
                { name: "parentid", value: parentId },
                { name: "name", value: name },
                { name: "description", value: EncodingClass.string.fromVariable(description) },
                { name: "type", value: hr.sclang.NAMESPACE_TYPE_PACKAGE }
            ],
            func: function (success, message) {
                console.log(message)
                if (success) {
                    if (message.substr(0, 2) == "ok") {
                        resolve({ id: parseInt(message.substring(2)) })
                    }
                    else {
                        reject({ message: message });
                    }
                }
                else {
                    reject({ message: message });
                }
            }
        });
    });
};


hr.sclang.saveNewNode = function (namespaceid, type, name, description) {
    return new Promise(function (resolve, reject) {
        FormClass.api_call({
            url: "sclang_save_node.php",
            params: [
                { name: "namespaceid", value: namespaceid },
                { name: "name", value: name },
                { name: "description", value: EncodingClass.string.fromVariable(description) },
                { name: "type", value: type }
            ],
            func: function (success, message) {
                console.log(message)
                if (success) {
                    if (message.substr(0, 2) == "ok") {
                        resolve({ id: parseInt(message.substring(2)) })
                    }
                    else {
                        reject({ message: message });
                    }
                }
                else {
                    reject({ message: message });
                }
            }
        });
    });
};


hr.sclang.deleteNode = function (id) {
    return new Promise(function (resolve, reject) {
        FormClass.api_call({
            url: "sclang_delete_node.php",
            params: [
                { name: "id", value: id }
            ],
            func: function (success, message) {
                console.log(message)
                if (success) {
                    if (message.substr(0, 2) == "ok") {
                        resolve({ id: parseInt(message.substring(2)) })
                    }
                    else {
                        reject({ message: message });
                    }
                }
                else {
                    reject({ message: message });
                }
            }
        });
    });
};


hr.sclang.deleteNodeList = function (idList) {
    return new Promise(function (resolve, reject) {
        FormClass.api_call({
            url: "sclang_delete_node.php",
            params: [
                { name: "idlist", value: EncodingClass.string.fromVariable(idList) }
            ],
            func: function (success, message) {
                console.log(message)
                if (success) {
                    if (message.substr(0, 2) == "ok") {
                        resolve({ id: parseInt(message.substring(2)) })
                    }
                    else {
                        reject({ message: message });
                    }
                }
                else {
                    reject({ message: message });
                }
            }
        });
    });
};

hr.sclang.updateNode = function (id, modifiedData) {
    return new Promise(function (resolve, reject) {
        modifiedData.id = parseInt(id + '');
        if (modifiedData.content) modifiedData.content = EncodingClass.string.fromVariable(modifiedData.content);
        FormClass.api_call({
            url: "sclang_save_node.php",
            params: [
                { name: "action", value: 'update' },
                { name: "modifieddata", value: EncodingClass.string.fromVariable(modifiedData) }
            ],
            func: function (success, message) {
                if (success) {
                    if (message.substr(0, 2) == "ok") {
                        resolve({ id: parseInt(message.substring(2)) })
                    }
                    else {
                        reject({ message: message });
                    }
                }
                else {
                    reject({ message: message });
                }
            }
        });
    });
};


hr.sclang.executeExpression = function (expression, variables, options) {
    options = options || {};
    variables = variables || {};
    var res = absol.sclang.SCParser.parse(expression, 'exp');
    if (res.error) throw res.error;
    var namespaceScope = new absol.sclang.SCScope(hr.sclang.global);
    var scope = new absol.sclang.SCScope(namespaceScope);
    if (options.namespaces) {
        options.namespaces.forEach(function (ns) {
            var pkg = hr.sclang.packageById[ns];
            if (typeof ns === "object") {
                pkg = ns;
            }
            else if (typeof ns === "string") {
                pkg = (ns + '').split('.').reduce(function (ac, cr) {
                    if (!ac) return ac;
                    if (typeof ac !== 'object') return ac;
                    if (cr in ac) return ac[cr];
                }, hr.sclang.globalPackages);
            }

            if (pkg && (typeof pkg === "object")) {
                Object.keys(pkg).forEach(function (key) {
                    namespaceScope.declareConst(key, pkg[key]);
                });
            }
        });
    }
    Object.keys(variables).forEach(function (key) {
        scope.declareConst(key, variables[key]);
    });
    instance = new absol.sclang.SCProgramInstance(res.ast, scope);
    return instance.accept(instance.ast, 'const');
}


hr.sclang.executeTemplate = function (text, variables, options) {
    options = options || {};
    variables = variables || {};

    var templatePartRegex = /(\{\{(([^\}]|(\}[^\}]))*)\}\})|(([^\{]|(\{[^\{]))+)/g;
    var parts = text.match(templatePartRegex);
    var partAstList = parts.map(part => {
        if (part.startsWith('{{') && part.endsWith('}}')) {
            var result = absol.sclang.SCParser.parse(part.substring(2, part.length - 2), 'exp');
            if (!result.error) {
                return result.ast;
            }
            else {
                throw result.error;
            }
        }
        else {
            return {
                type: 'StringLiteral',
                value: part
            }
        }
    });

    var ast = partAstList.reduce(function (ac, cr) {
        return {
            type: 'BinaryExpression',
            left: ac,
            right: cr,
            operator: {
                type: "BinaryOperator",
                content: '+'
            }
        }
    });


    var namespaceScope = new absol.sclang.SCScope(hr.sclang.global);
    var scope = new absol.sclang.SCScope(namespaceScope);
    if (options.namespaces) {
        options.namespaces.forEach(function (ns) {
            var pkg = hr.sclang.packageById[ns];
            if (typeof ns === "object") {
                pkg = ns;
            }
            else if (typeof ns === "string") {
                pkg = (ns + '').split('.').reduce(function (ac, cr) {
                    if (!ac) return ac;
                    if (typeof ac !== 'object') return ac;
                    if (cr in ac) return ac[cr];
                }, hr.sclang.globalPackages);
            }

            if (pkg && (typeof pkg === "object")) {
                Object.keys(pkg).forEach(function (key) {
                    namespaceScope.declareConst(key, pkg[key]);
                });
            }
        });
    }
    Object.keys(variables).forEach(function (key) {
        scope.declareConst(key, variables[key]);
    });
    instance = new absol.sclang.SCProgramInstance(ast, scope);
    return instance.accept(instance.ast, 'const');
}


hr.sclang.init = function (host) {
    var launcher = new absol.form.SCLauncher({
        // name: name,
        notificationCenter: hr.sclang.notificationCenter
    });
    host.scLaucher = launcher;
    host.holder.addChild(launcher.getView());
};

/**** LISTENING EVENTS ********/

hr.sclang.notificationCenter.on('node_content_change', function () {
    //todo: optimize load
    console.log("Reaload all function, todo: optimize load");
    hr.sclang.initModule();
    hr.sclang.ready();
});

//test only
//todo: remove it in realease version
setTimeout(function waitData() {
    if (window.data_module) {
        hr.sclang.ready();
    }
    else setTimeout(waitData, 1000);
}, 1000);

VaKeR 2022