227 lines
4.7 KiB
JavaScript
227 lines
4.7 KiB
JavaScript
export default class ChannelController{
|
|
|
|
/* (1) Construct default attributes
|
|
*
|
|
---------------------------------------------------------*/
|
|
constructor(){
|
|
|
|
/* (1) Set default active channel */
|
|
this.current = null;
|
|
|
|
/* (2) Initialize channel list */
|
|
this.list = [
|
|
{ id: -1, link: 'me', label: 'My data', sub: '0 online', icon: 'group', room: [] },
|
|
{ id: -2, link: null, label: 'add', sub: null, icon: 'add', room: [], add: 1 }
|
|
];
|
|
|
|
}
|
|
|
|
|
|
/* (2) Channel navigation
|
|
*
|
|
* @channel_id<int> Channel id (NULL uses get())
|
|
*
|
|
* @return status<boolean> Whether the navigation has been successful
|
|
*
|
|
---------------------------------------------------------*/
|
|
nav(channel_id=null){
|
|
|
|
if( channel_id == -2 )
|
|
return gs.get.popup.show('channel.create');
|
|
|
|
console.log(`channel.nav(${channel_id})`);
|
|
|
|
/* (1) Get channel data */
|
|
var channel = this.get(channel_id);
|
|
|
|
/* (2) Navigate vue-router */
|
|
gs.get.router.push(`/channel/${channel.link}`);
|
|
|
|
/* (3) Update active element */
|
|
this.current = channel.id;
|
|
|
|
/* (4) Load rooms */
|
|
gs.get.room.fetch();
|
|
|
|
/* (5) Log channel */
|
|
console.log(`[channel.current] ${channel.link} (${channel.label})`);
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
/* (3) Dump/Update channel data
|
|
*
|
|
* @channels<array> Channels data
|
|
*
|
|
* @return udpated<boolean> Whether channels have been updated
|
|
*
|
|
---------------------------------------------------------*/
|
|
dump(channels){
|
|
console.log(`channel.dump([${channels instanceof Array?channels.length:0}])`);
|
|
|
|
/* (1) Check @channels type */
|
|
if( !(channels instanceof Array) )
|
|
return false;
|
|
|
|
/* (2) Store LAST item */
|
|
let last_item = this.list.pop();
|
|
|
|
/* (3) Clear list (except FIRST) */
|
|
this.list.splice(1);
|
|
|
|
/* (4) Apply new channels */
|
|
for(let c of channels)
|
|
this.list.push(c);
|
|
|
|
/* (5) Restore LAST */
|
|
this.list.push(last_item);
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
/* (4) Get channel data
|
|
*
|
|
* @channel_id<int> Channel ID
|
|
* NULL: current channel || from URL (and set current)
|
|
*
|
|
* @return channel<array> Channel data
|
|
*
|
|
---------------------------------------------------------*/
|
|
get(channel_id=null){
|
|
// console.log(`channel.get(${channel_id})`);
|
|
|
|
if( channel_id === null ){
|
|
|
|
/* (1) Get @current channel
|
|
---------------------------------------------------------*/
|
|
/* (1) If @current is set */
|
|
if( !isNaN(this.current) ){
|
|
|
|
/* (2) Return matching id in list */
|
|
for( let c of this.list ){
|
|
|
|
if( c.id === this.current )
|
|
return c; // exit point
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/* (2) Get from URL
|
|
---------------------------------------------------------*/
|
|
/* (1) If vue-router has @link param */
|
|
if( gs.get.router.history.current.params.link ){
|
|
|
|
/* (2) Extract @link */
|
|
let link = gs.get.router.history.current.params.link;
|
|
|
|
/* (3) Return matching link in list */
|
|
for( let c of this.list ){
|
|
|
|
if( c.link === link ){
|
|
|
|
this.current = c.id; // set @current
|
|
return c; // exit point
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/* (3) Get channel data
|
|
---------------------------------------------------------*/
|
|
/* (1) Return channel matching id */
|
|
for( let c of this.list )
|
|
if( c.id === channel_id )
|
|
return c; // exit point
|
|
|
|
/* (2) Return default: if ID not found */
|
|
this.current = this.list[0].id;
|
|
return this.list[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* (6) Fetch channel data
|
|
*
|
|
---------------------------------------------------------*/
|
|
fetch(){
|
|
|
|
|
|
api.call('GET /channel', {}, function(rs){
|
|
|
|
/* (1) Check errors */
|
|
if( rs.error !== 0 || rs.channels == null )
|
|
return;
|
|
|
|
/* (2) Dump data */
|
|
this.dump(rs.channels);
|
|
|
|
/* (3) Find if @link matches */
|
|
var redirect_id = null;
|
|
for( let c of this.list ){
|
|
|
|
if( c.link === window.initial_link ){
|
|
redirect_id = c.id;
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
/* (4) Emulate navigatation from URL */
|
|
console.log(`[restore.channel] ${redirect_id}`);
|
|
this.nav(redirect_id);
|
|
|
|
}.bind(this), auth.token);
|
|
}
|
|
|
|
|
|
/* (7) Create a new channel
|
|
*
|
|
* @name<String> channel name
|
|
* @link<String> channel URI link
|
|
*
|
|
* @return created<bool> Whether the channel has been created
|
|
*
|
|
---------------------------------------------------------*/
|
|
create(name=null, link=null){
|
|
|
|
/* (1) Manage invalid @link */
|
|
if( typeof link !== 'string' || /^[a-z0-9\._-]$/i.test(link) )
|
|
return false;
|
|
|
|
/* (2) Manage invalid @name */
|
|
if( typeof name !== 'string' || /^[a-z0-9\._-]$/i.test(name) )
|
|
return false;
|
|
|
|
/* (3) Try to create room in API */
|
|
api.call('POST /channel', { link: link, name: name }, function(rs){
|
|
|
|
/* (1) Manage error */
|
|
if( rs.error !== 0 || rs.cid == null )
|
|
return false;
|
|
|
|
/* (2) Reload channel list */
|
|
this.fetch();
|
|
|
|
/* (3) Hide popup */
|
|
gs.get.popup.hide();
|
|
|
|
}, auth.token);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} |