%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/shaban/laviva/wp-content/themes/levelup/assets/js/app/core/
Upload File :
Create Path :
Current File : //var/www/html/shaban/laviva/wp-content/themes/levelup/assets/js/app/core/event_emitter.class.js

(function () {
    'use strict';

    APP.CORE.Event_Emitter = APP.CORE.Abstract.extend({
        options: {},

        init: function (options) {
            this._super(options);

            this.callbacks = {};
            this.callbacks.base = {};
        },

        on: function (name, action) {
            if (typeof name === 'undefined' || name === '') {
                console.warn('Wrong name');
                return false;
            }

            if (typeof action === 'undefined') {
                console.warn('Wrong action');
                return false;
            }

            var that = this,
                names = [];

            name = name.replace(/[^a-zA-Z0-9 ,\/.]/g, '');
            name = name.replace(/[,\/]+/g, ' ');

            names = name.split(' ');

            names.forEach(function (name) {
                name = that.resolve_name(name);

                if (!( that.callbacks[name.tag] instanceof Object ))
                    that.callbacks[name.tag] = {};

                if (!( that.callbacks[name.tag][name.value] instanceof Array ))
                    that.callbacks[name.tag][name.value] = [];

                that.callbacks[name.tag][name.value].push(action);
            });

            return this;
        },

        off: function (name) {

            if (typeof name === 'undefined' || name === '') {
                console.warn('Wrong name');
                return false;
            }

            var that = this,
                names = [];

            name = name.replace(/[^a-zA-Z0-9 ,\/.]/g, '');
            name = name.replace(/[,\/]+/g, ' ');

            names = name.split(' ');

            names.forEach(function (name) {
                name = that.resolve_name(name);

                if (name.tag !== 'base' && name.value === '') {
                    delete that.callbacks[name.tag];
                }

                else {
                    if (name.tag === 'base') {
                        for (var tag in that.callbacks) {
                            if (that.callbacks[tag] instanceof Object && that.callbacks[tag][name.value] instanceof Array) {
                                delete that.callbacks[tag][name.value];

                                if (Object.keys(that.callbacks[tag]).length === 0)
                                    delete that.callbacks[tag];
                            }
                        }
                    }

                    else if (that.callbacks[name.tag] instanceof Object && that.callbacks[name.tag][name.value] instanceof Array) {
                        delete that.callbacks[name.tag][name.value];

                        if (Object.keys(that.callbacks[name.tag]).length === 0)
                            delete that.callbacks[name.tag];
                    }
                }
            });

            return this;
        },

        trigger: function (name, args) {

            if (typeof name === 'undefined' || name === '') {
                console.warn('Wrong name');
                return false;
            }

            var that = this,
                final_result = undefined,
                result = undefined;

            if (!( args instanceof Array ))
                args = [];

            name = that.resolve_name(name);

            name.value = name.value.replace(/[^a-zA-Z0-9 ,\/.]/g, '');
            name.value = name.value.replace(/[,\/]+/g, ' ');

            if (name.tag === 'base') {
                for (var tag in that.callbacks) {
                    if (that.callbacks[tag] instanceof Object && that.callbacks[tag][name.value] instanceof Array) {
                        that.callbacks[tag][name.value].forEach(function (action) {
                            result = action.apply(that, args);

                            if (typeof final_result === 'undefined')
                                final_result = result;
                        });
                    }
                }
            }

            else if (this.callbacks[name.tag] instanceof Object) {
                if (name.value === '') {
                    console.warn('Wrong name');
                    return this;
                }

                that.callbacks[name.tag][name.value].forEach(function (action) {
                    result = action.apply(that, args);

                    if (typeof final_result === 'undefined')
                        final_result = result;
                });
            }

            return final_result;
        },

        trigga: function (name, args) {
            return this.trigger(name, args);
        },

        clean_name: function (name) {
            name = name.toLowerCase();
            name = name.replace('-', '_');

            return name;
        },

        resolve_name: function (name) {
            var new_name = {},

                parts = name.split('.');

            new_name.original = name;
            new_name.value = parts[0];
            new_name.tag = 'base'; // Base tag

            if (parts.length > 1 && parts[1] !== '')
                new_name.tag = parts[1];

            return new_name;
        }
    });
})();

Zerion Mini Shell 1.0