zorin-e
8/1/2017 - 5:38 AM

Adds events to items of menu, handlers in methods of the class

Adds events to items of menu, handlers in methods of the class

new MenuHandler(document.getElementsByClassName('context-menu'));
function MenuHandler(el) {
    /**
     *
     * private methods
     *
     */

    function save(e, info, param) {
        var ajax, data;

        data = {
            GMName: e.target.previousSibling.value || e.target.parentElement.previousSibling.value,
            GmID: JSON.parse(info).GmID
        };

        ajax = new Ajax({
            method: 'post',
            url: '/admin/clubs.php',
            data: param.save + '=true&'+ param.name +'=' + (data.GMName || data.code) + '&GmID=' + data.GmID
        }).send();

        ajax
            .then(JSON.parse)
            .then(function(data) {
                toastr.success('Success');
                new ContextMenu().hide();
            })
            .catch(function(error) {
                toastr.error(error);
            });        
    }    


    /**
     *
     * public methods
     *
     */
    
    this.changeGmName = function(e, info) {
        save(e, info, {
            save: 'save_gmname',
            name: 'GMName'
        });
    }

    this.changeIsonisCode = function(e, info) {
        save(e, info, {
            save: 'save_code',
            name: 'Code'
        });        
    }

    this.changeInput = function(e, info) {
        var target = e.target,
            info = JSON.parse(info),
            name = target.getAttribute('data-name');

        target.onkeyup = function() {
            if(target.value !== '' && target.value != info[name]) {
                target.nextSibling.classList.remove('context-menu__button--unactive');
            }
            else {
                target.nextSibling.classList.add('context-menu__button--unactive');
            }
        }
    }

    /**
     *
     * events
     *
     */
    
    var self = this;

    if(el.length) {
        el[0].onclick = function(e) {
            e.preventDefault();
            var action = e.target.getAttribute('data-action'),
                info = el[0].getAttribute('data-info') || null;

            if(action) {
                self[action](e, info);
            }
        }        
    }
}