9 changed files with 316 additions and 117 deletions
@ -0,0 +1,10 @@
|
||||
Ext.define('Rambox.profile.Offline', { |
||||
extend: 'Ext.app.Profile' |
||||
,isActive: function() { |
||||
return !localStorage.getItem('id_token'); |
||||
} |
||||
|
||||
,launch: function() { |
||||
console.warn('USER NOT LOGGED IN'); |
||||
} |
||||
}); |
@ -0,0 +1,12 @@
|
||||
Ext.define('Rambox.profile.Online', { |
||||
extend: 'Ext.app.Profile' |
||||
,isActive: function() { |
||||
return localStorage.getItem('id_token'); |
||||
} |
||||
|
||||
,launch: function() { |
||||
console.info('USER LOGGED IN'); |
||||
|
||||
Rambox.ux.Firebase.createEvents(false); |
||||
} |
||||
}); |
@ -0,0 +1,107 @@
|
||||
Ext.define('Rambox.ux.Firebase', { |
||||
singleton: true |
||||
|
||||
// private
|
||||
,eventsDefined: false |
||||
|
||||
,createEvents: function(createTabs) { |
||||
if ( this.eventsDefined || !localStorage.getItem('id_token') ) return; |
||||
|
||||
console.log('Define listeners for Firebase'); |
||||
|
||||
var ref = fireRef.database().ref('test/' + Ext.decode(localStorage.getItem('profile')).user_id).child('services'); |
||||
|
||||
// Attach an asynchronous callback to read the data at our posts reference
|
||||
ref.on("child_changed", function(snapshot, prevChildKey) { |
||||
console.info('Firebase - Child Changed', snapshot.val(), snapshot.key, prevChildKey); |
||||
|
||||
Ext.getStore('Services').suspendEvent('update'); |
||||
var rec = Ext.getStore('Services').findRecord('firebase_key', snapshot.key); |
||||
|
||||
|
||||
Ext.getCmp('tab_'+rec.get('id')).setTitle(snapshot.val().name); |
||||
|
||||
if ( rec.get('position') !== snapshot.val().position ) { |
||||
var pos = parseInt(snapshot.val().position); |
||||
if ( rec.get('align') === 'right' ) pos++; |
||||
Ext.cq1('app-main').move(Ext.getCmp('tab_'+rec.get('id')), pos); |
||||
} |
||||
|
||||
rec.set(snapshot.val()); |
||||
rec.save(); |
||||
Ext.getStore('Services').resumeEvent('update'); |
||||
Ext.getStore('Services').load(); |
||||
}, function (errorObject) { |
||||
|
||||
}); |
||||
|
||||
ref.on("child_added", function(snapshot, prevChildKey) { |
||||
console.info('Firebase - Child Added', snapshot.val(), snapshot.key, prevChildKey); |
||||
|
||||
Ext.getStore('Services').suspendEvents(['add', 'update']); |
||||
var rec = Ext.getStore('Services').findRecord('firebase_key', snapshot.key); |
||||
|
||||
// Update current services
|
||||
if ( rec ) { |
||||
rec.set(snapshot.val()); |
||||
rec.save(); |
||||
} else { // Add new ones if exist
|
||||
var data = snapshot.val(); |
||||
data.firebase_key = snapshot.key; |
||||
rec = Ext.create('Rambox.model.Service', data); |
||||
rec.save(); |
||||
Ext.getStore('Services').add(rec); |
||||
|
||||
var tabData = { |
||||
xtype: 'webview' |
||||
,id: 'tab_'+rec.get('id') |
||||
,title: rec.get('name') |
||||
,icon: rec.get('type') !== 'custom' ? 'resources/icons/'+rec.get('logo') : ( rec.get('logo') === '' ? 'resources/icons/custom.png' : rec.get('logo')) |
||||
,src: rec.get('url') |
||||
,type: rec.get('type') |
||||
,align: rec.get('align') |
||||
,notifications: rec.get('notifications') |
||||
,muted: rec.get('muted') |
||||
,record: rec |
||||
,tabConfig: { |
||||
service: rec |
||||
} |
||||
}; |
||||
|
||||
if ( rec.get('align') === 'left' ) { |
||||
var tbfill = Ext.cq1('app-main').getTabBar().down('tbfill'); |
||||
Ext.cq1('app-main').insert(Ext.cq1('app-main').getTabBar().items.indexOf(tbfill), tabData); |
||||
} else { |
||||
Ext.cq1('app-main').add(tabData); |
||||
} |
||||
} |
||||
|
||||
Ext.getStore('Services').resumeEvents(['add', 'update']); |
||||
Ext.getStore('Services').load(); |
||||
//rec.commit();
|
||||
}, function (errorObject) { |
||||
|
||||
}); |
||||
|
||||
ref.on("child_removed", function(snapshot) { |
||||
console.info('Firebase - Child Removed', snapshot.val(), snapshot.key); |
||||
|
||||
var rec = Ext.getStore('Services').findRecord('firebase_key', snapshot.key); |
||||
|
||||
// Remove record from localStorage
|
||||
if ( rec ) { |
||||
Ext.getStore('Services').suspendEvent('remove'); |
||||
Ext.getStore('Services').remove(rec); |
||||
Ext.cq1('app-main').suspendEvent('remove'); |
||||
Ext.getCmp('tab_'+rec.get('id')).close(); |
||||
Ext.cq1('app-main').resumeEvent('remove'); |
||||
Ext.getStore('Services').resumeEvent('remove'); |
||||
Ext.getStore('Services').load(); |
||||
} |
||||
}, function (errorObject) { |
||||
|
||||
}); |
||||
|
||||
this.eventsDefined = true; |
||||
} |
||||
}); |
Loading…
Reference in new issue