discord-client/parcel/lib/popup-controller.js

312 lines
6.0 KiB
JavaScript
Raw Permalink Normal View History

export default class PopupController{
/* (1) Construct default attributes
*
---------------------------------------------------------*/
constructor(){
2018-03-22 19:03:29 +00:00
/* (1) Global
---------------------------------------------------------*/
/* (1) Background Visibility */
this.filter = false;
2018-03-22 19:03:29 +00:00
/* (2) Popup set */
this.list = {};
/* (3) Active popup */
this.active = null;
/* (4) Loading state */
this.loading = false;
2018-03-22 19:03:29 +00:00
/* (2) Popups
---------------------------------------------------------*/
/* (1) Create a new Room */
this.register('room.create', {
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
},
submit(){
// validators
if( !this.name.is_valid() )
return;
// loading
this.parent.loading = gs.get.room.create(this.type.mutable, this.name.mutable);
}
2018-03-22 19:03:29 +00:00
});
/* (2) Create a new Channel */
this.register('channel.create', {
name: new FieldValidator('basic-name', ''),
link: new FieldValidator('url-name', ''),
reset(){
this.link.mutable = '',
this.name.mutable = '';
},
submit(){
// validators
if( !this.name.is_valid() )
return false;
if( !this.link.is_valid() )
return false;
// loading
this.parent.loading = gs.get.channel.create(this.name.mutable, this.link.mutable);
}
});
/* (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;
// loading
this.parent.loading = gs.get.content.change_username(this.username.mutable);
}
});
/* (4) Invite to channel */
this.register('channel.invite', {
username: new FieldValidator('basic-name', ''),
reset(){ this.username.mutable = ''; },
submit(){
// validators
if( !this.username.is_valid() )
return false;
// loading
this.parent.loading = gs.get.channel.invite(this.username.mutable);
}
});
/* (5) Remove channel */
this.register('channel.remove', {
reset(){ },
submit(){ this.parent.loading = gs.get.channel.remove(gs.get.content.cid); }
});
/* (6) Remove channel */
this.register('room.remove', {
data: {
id: '',
type: '',
name: '',
messages: []
},
reset(){ this.data = { id: '', type: '', name: '', messages: [] }; },
submit(){ this.parent.loading = gs.get.room.remove(this.data.type, this.data.id); }
});
/* (7) Leave channel */
this.register('channel.leave', {
reset(){ },
submit(){ this.parent.loading = gs.get.channel.leave(); }
});
/* (8) Change password */
this.register('password.change', {
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;
this.loading = gs.get.content.change_password(this.password.mutable);
}
});
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 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.loading = false;
2018-03-22 19:03:29 +00:00
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.loading = false;
2018-03-22 19:03:29 +00:00
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];
}
}