[BIGUPDATE] interface API propagation

This commit is contained in:
xdrm-brackets 2018-03-27 18:21:02 +02:00
parent 665c33be11
commit 6a76d9cd6f
8 changed files with 143 additions and 75 deletions

View File

@ -14,9 +14,6 @@ export default class ChannelController{
{ id: -2, link: null, label: 'add', sub: null, icon: 'add', room: [], add: 1 } { id: -2, link: null, label: 'add', sub: null, icon: 'add', room: [], add: 1 }
]; ];
/* (3) Initialize channel data buffer */
this.buffer = {};
} }
@ -37,23 +34,16 @@ export default class ChannelController{
/* (1) Get channel data */ /* (1) Get channel data */
var channel = this.get(channel_id); var channel = this.get(channel_id);
/* (2) Try to load channel data */ /* (2) Navigate vue-router */
if( !this.load(channel.id) )
return false;
/* (3) Navigate vue-router */
gs.get.router.push(`/channel/${channel.link}`); gs.get.router.push(`/channel/${channel.link}`);
/* (4) Load rooms */ /* (3) Load rooms */
gs.get.room.dump(channel.room); gs.get.room.fetch();
/* (5) Default open first 'text' room */ /* (4) Update active element */
gs.get.room.nav('text', gs.get.room.text.list[0].id);
/* (6) Update active element */
this.current = channel.id; this.current = channel.id;
/* (7) Log channel */ /* (5) Log channel */
console.log(`[channel.current] ${channel.link} (${channel.label})`); console.log(`[channel.current] ${channel.link} (${channel.label})`);
return true; return true;
@ -81,12 +71,14 @@ export default class ChannelController{
this.list.splice(1); this.list.splice(1);
/* (4) Apply new channels */ /* (4) Apply new channels */
this.buffer = {};
for(let c of channels) for(let c of channels)
this.list.push(c); ( this.list.push(c) === 2 ) && ( this.buffer = c );
/* (5) Restore LAST */ /* (5) Restore LAST */
this.list.push(last_item); this.list.push(last_item);
return true; return true;
} }
@ -160,26 +152,74 @@ export default class ChannelController{
} }
/* (5) Fetch channel data
*
* @channel_id<int> Channel ID /* (6) Fetch channel data
*
* @return fetched<bool> Whether channel data have been fetched
* *
---------------------------------------------------------*/ ---------------------------------------------------------*/
load(channel_id=null){ fetch(){
console.log(`channel.load(${channel_id})`);
/* (1) Exit if invalid @channel_id */
if( isNaN(channel_id) ) 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; return false;
/* (2) Call API to get data */ /* (2) Manage invalid @name */
setTimeout(() => { if( typeof name !== 'string' || /^[a-z0-9\._-]$/i.test(name) )
return false;
this.buffer = require('../mockup/api-channel-init.json'); /* (3) Try to create room in API */
api.call('POST /channel', { link: link, name: name }, function(rs){
}, 500); /* (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; return true;

View File

@ -25,14 +25,14 @@ export default class ContentController{
get rbuf(){ get rbuf(){
/* (1) Ignore: if no rooms empty */ /* (1) Ignore: if no rooms empty */
if( this.cbuf.room == null || this.cbuf.room.length === 0 ) if( gs.get.room.text.list == null || gs.get.room.text.list.length === 0 )
return {}; return {};
/* (2) Search for current room */ /* (2) Search for current room */
for( let r of this.cbuf.room ){ for( let r of gs.get.room.text.list ){
// Return if room found // // Return if room found //
if( r.rid === this.rid ) if( r.id === this.rid )
return r; return r;
} }

View File

@ -32,10 +32,11 @@ export default class PopupController{
/* (2) Create a new Channel */ /* (2) Create a new Channel */
this.register('channel.create', { this.register('channel.create', {
data: { data: {
name: '' name: '',
link: ''
}, },
reset(){ this.data.name = ''; }, reset(){ this.data.link = '', this.data.name = ''; },
submit(){ gs.get.channel.create(this.data.name) && this.parent.hide(); } submit(){ gs.get.channel.create(this.data.name, this.data.link) && this.parent.hide(); }
}); });
} }

View File

@ -61,7 +61,7 @@ export default class RoomController{
---------------------------------------------------------*/ ---------------------------------------------------------*/
dump(rooms){ dump(rooms){
console.log(`room.dump([${rooms instanceof Array?rooms.length:0}])`); console.log(`room.dump([${rooms instanceof Array?rooms.length:-1}])`);
/* (1) Check @rooms type */ /* (1) Check @rooms type */
if( !(rooms instanceof Array) ) if( !(rooms instanceof Array) )
@ -73,6 +73,9 @@ export default class RoomController{
this[type].current = null; this[type].current = null;
} }
// redirection
let redirected = false;
/* (3) Browse each room */ /* (3) Browse each room */
for(let r of rooms){ for(let r of rooms){
@ -81,15 +84,37 @@ export default class RoomController{
continue; continue;
// {2} Ignore: if missing field // // {2} Ignore: if missing field //
if( isNaN(r.rid) || typeof r.type !== 'string' || typeof r.name !== 'string' ) if( isNaN(r.rid) || typeof r.type !== 'string' )
continue; continue;
// {3} Ignore: not available type // // {3} Ignore if cannot find name in buffer //
var name = null;
gs.get.content.cbuf.room.map( (v) => { ( v.rid === r.rid ) && ( name = v.name ); });
if( name === null )
continue;
// {4} Default: missing messages //
if( !( r.messages instanceof Array) )
r.messages = [];
// {5} Default: missing members //
if( !( r.members instanceof Array) )
r.members = [];
// {6} Ignore: not available type //
if( this[r.type] == null ) if( this[r.type] == null )
continue; continue;
// {7} store data
this[r.type].list.push({ id: r.rid, name: name, type: r.type, messages: r.messages });
this[r.type].list.push({ id: r.rid, name: r.name, type: r.type }); // {8} redirect if first element
if( !redirected && r.type == 'text' ){
redirected = true;
this.nav('text', r.rid);
}
} }
return true; return true;
@ -147,7 +172,29 @@ export default class RoomController{
} }
/* (5) Create a new room
/* (5) Fetch channel data
*
---------------------------------------------------------*/
fetch(){
api.call(`GET /channel/${gs.get.channel.current}`, {}, function(rs){
/* (1) Check errors */
if( rs.error !== 0 || rs.channel == null )
return;
/* (2) Dump data */
console.log(rs.channel.room);
this.dump(rs.channel.room);
}.bind(this), auth.token);
}
/* (6) Create a new room
* *
* @type<int> room type * @type<int> room type
* @name<String> room name * @name<String> room name

View File

@ -4,10 +4,10 @@
"uid": 1, "uid": 1,
"users": [ "users": [
{ "uid": 1, "online": true, "name": "First user YaY!" }, { "uid": 1, "name": "First user YaY!" },
{ "uid": 3, "online": false, "name": "Another channel's user" }, { "uid": 3, "name": "Another channel's user" },
{ "uid": 4, "online": true, "name": "And another one" }, { "uid": 4, "name": "And another one" },
{ "uid": 5, "online": true, "name": "And another" } { "uid": 5, "name": "And another" }
], ],
"room": [ "room": [

View File

@ -55,27 +55,27 @@
=== CHANNEL <> USER === === CHANNEL <> USER ===
>>> POST @token:/channel/:cid/subscribe >>> POST @token:/channel/subscribe/:cid
{} {}
>>> DELETE @token:/channel/:cid/subscribe >>> DELETE @token:/channel/subscribe/:cid
{} {}
=== ROOM === === ROOM ===
>>> POST @token:/channel/:cid/room/:type/ >>> POST @token:/channel/room/:cid
{ {
name: <String> name: <String>
} }
>>> GET @token:/channel/:cid/room/:type/ --> all channel's rooms of this type >>> GET @token:/channel/room/:cid --> all channel's rooms of this type
>>> GET @token:/channel/:cid/room/:type/:rid --> room information of this type >>> GET @token:/channel/room/:cid/:rid --> room information of this type
>>> PUT @token:/channel/:cid/room/:type/:rid >>> PUT @token:/channel/room/:cid/:rid
{ {
name: <String> name: <String>
} }
>>> DELETE @token:/channel/:cid/room/:type/:rid --> remove room >>> DELETE @token:/channel/room/:cid/:rid --> remove room

View File

@ -10,29 +10,6 @@ import ChannelController from '../../lib/channel-controller'
window.initial_link = gs.get.router.history.current.params.link; window.initial_link = gs.get.router.history.current.params.link;
console.log(`[channel.URL] ${initial_link}`); console.log(`[channel.URL] ${initial_link}`);
/* (2) Fetch channel data */
setTimeout(() => {
/* (3) Fetch data */
gs.get.channel.dump( require('../../mockup/api-channels.json') );
/* (4) Find if @link matches */
var redirect_id = null;
for( let c of gs.get.channel.list ){
if( c.link === window.initial_link ){
redirect_id = c.id;
break;
}
}
/* (4) Emulate navigatation from URL */
console.log(`[restore.channel] ${redirect_id}`);
gs.get.channel.nav(redirect_id);
}, 500);
/* (2) Main components /* (2) Main components
@ -48,3 +25,4 @@ gs.set('room', new RoomController());
/* (4) Initialize channels & channel menu */ /* (4) Initialize channels & channel menu */
gs.set('channel', new ChannelController()); gs.set('channel', new ChannelController());
gs.get.channel.fetch(); // fetch at load time

View File

@ -42,6 +42,8 @@
<span class='body form'> <span class='body form'>
<label for='channel_name'>Channel Name</label> <label for='channel_name'>Channel Name</label>
<input type='text' name='channel_name' v-model='gs.popup.get(`channel.create`).data.name'> <input type='text' name='channel_name' v-model='gs.popup.get(`channel.create`).data.name'>
<label for='channel_link'>Channel Link</label>
<input type='text' name='channel_link' v-model='gs.popup.get(`channel.create`).data.link'>
</span> </span>
<span class='footer form'> <span class='footer form'>