2018-03-21 19:25:01 +00:00
|
|
|
/* (1) Initialisation
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Init @channel object */
|
|
|
|
gs.set('channel', {});
|
|
|
|
|
|
|
|
/* (2) Set default active channel */
|
2018-03-21 19:27:34 +00:00
|
|
|
gs.get.channel.current = null;
|
2018-03-21 19:25:01 +00:00
|
|
|
|
|
|
|
/* (3) Initialize list */
|
|
|
|
gs.get.channel.list = [
|
2018-03-21 21:54:24 +00:00
|
|
|
{ id: -1, link: 'me', label: 'My data', sub: '0 online', icon: 'group', local: [] },
|
|
|
|
{ id: -2, link: null, label: 'add', sub: null, icon: 'add', local: [], add: 1 }
|
2018-03-21 19:25:01 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/* (4) Initialize vue-router channel navigation */
|
|
|
|
gs.get.channel.nav = function(){};
|
|
|
|
|
|
|
|
/* (5) Initialize adding fetched channel */
|
|
|
|
gs.get.channel.dump = function(){};
|
|
|
|
|
|
|
|
/* (6) Initialize accessor for channel data */
|
|
|
|
gs.get.channel.get = function(){};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Channel navigation
|
2018-03-21 22:03:17 +00:00
|
|
|
*
|
|
|
|
* @channel_id<int> Channel id (NULL uses get())
|
|
|
|
*
|
|
|
|
* @return status<boolean> Whether the navigation has been successful
|
|
|
|
*
|
2018-03-21 19:25:01 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
gs.get.channel.nav = function(channel_id=null){
|
|
|
|
|
|
|
|
/* (1) Get channel data */
|
|
|
|
var channel = this.get(channel_id);
|
|
|
|
|
|
|
|
/* (3) Abort if same channel */
|
2018-03-21 19:37:03 +00:00
|
|
|
if( gs.get.router.history.current.params.link === channel.link )
|
2018-03-21 19:25:01 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (3) Navigate vue-router */
|
2018-03-21 19:37:03 +00:00
|
|
|
gs.get.router.push(`/channel/${channel.link}`);
|
2018-03-21 19:25:01 +00:00
|
|
|
|
|
|
|
/* (4) Update active element */
|
2018-03-21 19:27:34 +00:00
|
|
|
this.current = channel.id;
|
2018-03-21 19:25:01 +00:00
|
|
|
|
|
|
|
/* (5) Log channel */
|
2018-03-21 19:37:03 +00:00
|
|
|
console.log(`[channel.current] ${channel.link} (${channel.label})`);
|
2018-03-21 19:25:01 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Dump/Update channel data
|
|
|
|
*
|
|
|
|
* @channels<array> Channels data
|
|
|
|
*
|
|
|
|
* @return udpated<boolean> Whether channels have been updated
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
gs.get.channel.dump = function(channels){
|
|
|
|
|
|
|
|
/* (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
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
gs.get.channel.get = function(channel_id=null){
|
|
|
|
|
|
|
|
if( channel_id === null ){
|
|
|
|
|
2018-03-21 19:37:03 +00:00
|
|
|
/* (1) Get @current channel
|
2018-03-21 19:25:01 +00:00
|
|
|
---------------------------------------------------------*/
|
2018-03-21 19:37:03 +00:00
|
|
|
/* (1) If @current is set */
|
2018-03-21 19:27:34 +00:00
|
|
|
if( !isNaN(this.current) ){
|
2018-03-21 19:25:01 +00:00
|
|
|
|
|
|
|
/* (2) Return matching id in list */
|
|
|
|
for( let c of this.list ){
|
|
|
|
|
2018-03-21 19:27:34 +00:00
|
|
|
if( c.id === this.current )
|
2018-03-21 19:25:01 +00:00
|
|
|
return c; // exit point
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Get from URL
|
|
|
|
---------------------------------------------------------*/
|
2018-03-21 19:37:03 +00:00
|
|
|
/* (1) If vue-router has @link param */
|
|
|
|
if( gs.get.router.history.current.params.link ){
|
2018-03-21 19:25:01 +00:00
|
|
|
|
2018-03-21 19:37:03 +00:00
|
|
|
/* (2) Extract @link */
|
|
|
|
let link = gs.get.router.history.current.params.link;
|
2018-03-21 19:25:01 +00:00
|
|
|
|
2018-03-21 19:37:03 +00:00
|
|
|
/* (3) Return matching link in list */
|
2018-03-21 19:25:01 +00:00
|
|
|
for( let c of this.list ){
|
|
|
|
|
2018-03-21 19:37:03 +00:00
|
|
|
if( c.link === link ){
|
2018-03-21 19:25:01 +00:00
|
|
|
|
2018-03-21 19:37:03 +00:00
|
|
|
this.current = c.id; // set @current
|
2018-03-21 19:25:01 +00:00
|
|
|
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 */
|
2018-03-21 19:27:34 +00:00
|
|
|
this.current = this.list[0].id;
|
2018-03-21 19:25:01 +00:00
|
|
|
return this.list[0];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (N) Manage active channel from URL
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Set current active channel item from URL */
|
|
|
|
// gs.get.channel.nav();
|