1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-17 14:55:05 -05:00
kaiwa/clientapp/models/state.js

70 lines
1.9 KiB
JavaScript
Raw Normal View History

/*global app, $, me*/
"use strict";
var HumanModel = require('human-model');
module.exports = HumanModel.define({
initialize: function () {
var self = this;
$(window).blur(function () {
self.focused = false;
});
$(window).focus(function () {
self.focused = true;
if (me._activeContact) {
me.setActiveContact(me._activeContact);
}
self.markActive();
});
2013-10-14 16:36:19 -04:00
app.desktop.on('sleep', function () {
clearTimeout(this.idleTimer);
console.log('went to sleep');
self.markInactive();
});
2013-10-11 18:40:42 -04:00
this.markActive();
},
session: {
focused: ['bool', true, true],
active: ['bool', true, false],
connected: ['bool', true, false],
hasConnected: ['bool', true, false],
idleTimeout: ['number', true, 600000],
idleSince: 'date',
allowAlerts: ['bool', true, false],
2013-09-27 12:59:06 -04:00
badge: ['string', true, ''],
2013-10-15 19:21:22 -04:00
pageTitle: ['string', true, ''],
hasActiveCall: ['boolean', true, false]
},
derived: {
title: {
deps: ['pageTitle', 'badge'],
fn: function () {
var base = this.pageTitle ? 'Otalk - ' + this.pageTitle : 'Otalk';
if (this.badge) {
return this.badge + ' • ' + base;
}
return base;
}
}
},
markActive: function () {
clearTimeout(this.idleTimer);
var wasInactive = !this.active;
this.active = true;
this.idleSince = new Date(Date.now());
this.idleTimer = setTimeout(this.markInactive.bind(this), this.idleTimeout);
},
markInactive: function () {
if (this.focused) {
return this.markActive();
}
this.active = false;
this.idleSince = new Date(Date.now());
}
});