2018-03-24 15:49:00 +00:00
|
|
|
export default class PopupController{
|
2018-03-22 18:13:52 +00:00
|
|
|
|
|
|
|
/* (1) Construct default attributes
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
constructor(){
|
|
|
|
|
2018-03-22 19:03:29 +00:00
|
|
|
/* (1) Global
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Background Visibility */
|
|
|
|
this.filter = false;
|
2018-03-22 18:13:52 +00:00
|
|
|
|
2018-03-22 19:03:29 +00:00
|
|
|
/* (2) Popup set */
|
|
|
|
this.list = {};
|
|
|
|
|
|
|
|
/* (3) Active popup */
|
|
|
|
this.active = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Popups
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Create a new Room */
|
|
|
|
this.register('room.create', {
|
2018-03-28 23:26:18 +00:00
|
|
|
|
|
|
|
type: new FieldValidator('room.type', 'text'),
|
|
|
|
name: new FieldValidator('basic-name', ''),
|
|
|
|
reset(){
|
|
|
|
this.type.mutable = 'text';
|
|
|
|
this.name.mutable = '';
|
2018-03-22 19:03:29 +00:00
|
|
|
},
|
2018-03-28 23:26:18 +00:00
|
|
|
submit(){
|
|
|
|
|
|
|
|
// validators
|
|
|
|
if( !this.name.is_valid() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( gs.get.room.create(this.type.mutable, this.name.mutable) )
|
|
|
|
return this.parent.hide();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-22 19:03:29 +00:00
|
|
|
});
|
2018-03-28 23:26:18 +00:00
|
|
|
|
2018-03-27 14:41:36 +00:00
|
|
|
/* (2) Create a new Channel */
|
|
|
|
this.register('channel.create', {
|
2018-03-28 23:26:18 +00:00
|
|
|
name: new FieldValidator('basic-name', ''),
|
|
|
|
link: new FieldValidator('url-name', ''),
|
|
|
|
reset(){
|
|
|
|
this.link.mutable = '',
|
|
|
|
this.name.mutable = '';
|
2018-03-27 14:41:36 +00:00
|
|
|
},
|
2018-03-28 23:26:18 +00:00
|
|
|
submit(){
|
|
|
|
|
|
|
|
// validators
|
|
|
|
if( !this.name.is_valid() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( !this.link.is_valid() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( gs.get.channel.create(this.name.mutable, this.link.mutable) )
|
|
|
|
return this.parent.hide();
|
|
|
|
|
|
|
|
}
|
2018-03-27 14:41:36 +00:00
|
|
|
});
|
2018-03-28 23:26:18 +00:00
|
|
|
|
|
|
|
/* (3) Change username */
|
|
|
|
this.register('username.change', {
|
|
|
|
username: new FieldValidator('basic-name', ''),
|
|
|
|
reset(){ this.username.mutable = ''; },
|
|
|
|
submit(){
|
|
|
|
|
|
|
|
// validators
|
|
|
|
if( !this.username.is_valid() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( gs.get.content.change_username(this.username.mutable) )
|
|
|
|
this.parent.hide();
|
|
|
|
|
|
|
|
}
|
2018-03-28 17:54:16 +00:00
|
|
|
});
|
2018-03-28 23:26:18 +00:00
|
|
|
|
2018-03-28 18:06:26 +00:00
|
|
|
/* (4) Invite to channel */
|
|
|
|
this.register('channel.invite', {
|
2018-03-28 23:26:18 +00:00
|
|
|
username: new FieldValidator('basic-name', ''),
|
|
|
|
reset(){ this.username.mutable = ''; },
|
|
|
|
submit(){
|
|
|
|
|
|
|
|
// validators
|
|
|
|
if( !this.username.is_valid() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( gs.get.channel.invite(this.username.mutable) )
|
|
|
|
return this.parent.hide();
|
|
|
|
|
|
|
|
}
|
2018-03-28 18:06:26 +00:00
|
|
|
});
|
2018-03-28 23:26:18 +00:00
|
|
|
|
2018-03-28 19:51:04 +00:00
|
|
|
/* (5) Remove channel */
|
|
|
|
this.register('channel.remove', {
|
|
|
|
reset(){ },
|
|
|
|
submit(){ gs.get.channel.remove() && this.parent.hide(); }
|
|
|
|
});
|
2018-03-28 23:26:18 +00:00
|
|
|
|
2018-03-29 10:31:23 +00:00
|
|
|
/* (6) Remove channel */
|
|
|
|
this.register('room.remove', {
|
|
|
|
data: {
|
|
|
|
id: '',
|
|
|
|
type: '',
|
|
|
|
name: '',
|
|
|
|
messages: []
|
|
|
|
},
|
|
|
|
reset(){ this.data = { id: '', type: '', name: '', messages: [] }; },
|
|
|
|
submit(){ gs.get.room.remove(this.data.type, this.data.id) && this.parent.hide(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
/* (7) Leave channel */
|
2018-03-28 19:51:04 +00:00
|
|
|
this.register('channel.leave', {
|
|
|
|
reset(){ },
|
|
|
|
submit(){ gs.get.channel.remove() && this.parent.hide(); }
|
|
|
|
});
|
2018-03-28 23:26:18 +00:00
|
|
|
|
2018-03-29 10:31:23 +00:00
|
|
|
/* (8) Change password */
|
2018-03-28 20:54:49 +00:00
|
|
|
this.register('password.change', {
|
2018-03-28 23:26:18 +00:00
|
|
|
password: new FieldValidator('password', ''),
|
|
|
|
confirm: new FieldValidator('password', ''),
|
|
|
|
matches: true,
|
|
|
|
reset(){ this.password.mutable = ''; this.confirm.mutable = ''; },
|
|
|
|
submit(){
|
|
|
|
|
|
|
|
this.matches = this.password.mutable === this.confirm.mutable;
|
|
|
|
|
|
|
|
// check passwords matches
|
|
|
|
if( !this.matches )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// field validator
|
|
|
|
if( !this.password.is_valid || !this.confirm.is_valid() )
|
|
|
|
return false;
|
|
|
|
|
2018-03-28 23:37:15 +00:00
|
|
|
if( gs.get.content.change_password(this.password.mutable) )
|
2018-03-28 23:26:18 +00:00
|
|
|
return this.parent.hide();
|
|
|
|
|
|
|
|
}
|
2018-03-28 20:54:49 +00:00
|
|
|
});
|
2018-03-28 19:51:04 +00:00
|
|
|
|
2018-03-22 19:03:29 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Regiters a new popup
|
|
|
|
*
|
|
|
|
* @name<String> Popup name
|
|
|
|
* @data<Object> Popup structure
|
|
|
|
*
|
|
|
|
* [data.structure]
|
|
|
|
* {
|
|
|
|
* reset: <Function>,
|
|
|
|
* submit: <Function>,
|
|
|
|
* active: <bool>, // will be generated automatically
|
|
|
|
* data: <Object>
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
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;
|
2018-03-22 18:13:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-22 19:03:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (3) Unregiters an existing popup
|
|
|
|
*
|
|
|
|
* @name<String> 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<String> 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<String> 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];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-22 18:13:52 +00:00
|
|
|
}
|