export default class PopupController{ /* (1) Construct default attributes * ---------------------------------------------------------*/ constructor(){ /* (1) Global ---------------------------------------------------------*/ /* (1) Background Visibility */ this.filter = false; /* (2) Popup set */ this.list = {}; /* (3) Active popup */ this.active = null; /* (2) Popups ---------------------------------------------------------*/ /* (1) Create a new Room */ this.register('room.create', { data: { type: 'text', name: '' }, reset(){ this.data.type = 'text'; this.data.name = ''; }, submit(){ gs.get.room.create(this.data.type, this.data.name) && this.parent.hide(); } }); /* (2) Create a new Channel */ this.register('channel.create', { data: { name: '', link: '' }, reset(){ this.data.link = '', this.data.name = ''; }, submit(){ gs.get.channel.create(this.data.name, this.data.link) && this.parent.hide(); } }); /* (3) Change nickname */ this.register('nickname.change', { data: { value: '' }, reset(){ this.data.value = ''; }, submit(){ gs.get.content.change_username(this.data.value) && this.parent.hide(); } }); /* (4) Invite to channel */ this.register('channel.invite', { data: { username: '' }, reset(){ this.data.username = ''; }, submit(){ gs.get.channel.invite(this.data.username) && this.parent.hide(); } }); /* (5) Remove channel */ this.register('channel.remove', { data: {}, reset(){ }, submit(){ gs.get.channel.remove() && this.parent.hide(); } }); /* (6) Leave channel */ this.register('channel.leave', { data: {}, reset(){ }, submit(){ gs.get.channel.remove() && this.parent.hide(); } }); } /* (2) Regiters a new popup * * @name Popup name * @data Popup structure * * [data.structure] * { * reset: , * submit: , * active: , // will be generated automatically * data: * } * ---------------------------------------------------------*/ register(name, popup){ /* (1) Error: invalid @name */ if( typeof name !== 'string' ) return false; /* (2) Error: invalid @data */ if( typeof popup !== 'object' ) return false; /* (3) Error: @name already used */ if( this.list[name] != null ) return false; /* (3) Store popup */ this.list[name] = popup; /* (4) Add @parent ref */ this.list[name].parent = this; /* (5) Hide by default */ this.list[name].active = false; return true; } /* (3) Unregiters an existing popup * * @name Popup name * ---------------------------------------------------------*/ unregister(name){ /* (1) Error: invalid @name */ if( typeof name !== 'string' ) return false; /* (2) Error: @name not used */ if( this.list[name] == null ) return false; /* (3) If popup is @active -> hide */ if( this.active === name ) this.hide(); /* (3) Unregister popup */ delete this.list[name]; } /* (4) Show a popup * * @name Popup name * ---------------------------------------------------------*/ show(name){ /* (1) Error: invalid @name */ if( typeof name !== 'string' ) return false; /* (2) Error: @name not used */ if( this.list[name] == null ) return false; /* (3) Hide @active popup */ this.hide(); /* (4) Active popup */ this.active = name; /* (5) Show background */ this.filter = true; /* (6) Reset popup */ this.list[name].reset(); /* (7) Show popup */ this.list[name].active = true; } /* (5) Hide currently @active popup * ---------------------------------------------------------*/ hide(){ /* (1) Error: if no active popup */ if( this.active == null ) return false; /* (2) Error: @active not exists */ if( this.list[this.active] == null ) return false; /* (3) Hide background */ this.filter = false; /* (4) Hide popup */ this.list[this.active].active = false; /* (5) Reset popup */ this.list[this.active].reset(); /* (6) Reset @active */ this.active = null; } /* (6) Data getter * * @name Popup name * * ---------------------------------------------------------*/ get(name){ /* (1) Error: invalid @name */ if( typeof name !== 'string' ) return { data: {} }; /* (2) Error: @name not used */ if( this.list[name] == null ) return { data: {} }; /* (3) Return data */ return this.list[name]; } }