kaiwa/clientapp/pages/main.js

97 lines
2.9 KiB
JavaScript
Raw Normal View History

2013-09-27 01:52:54 -04:00
/*global app, me, client, Resample*/
2013-08-29 23:38:28 -04:00
"use strict";
var crypto = require('crypto');
2013-08-29 23:38:28 -04:00
var BasePage = require('./base');
var templates = require('../templates');
module.exports = BasePage.extend({
template: templates.pages.main,
2013-09-12 14:18:44 -04:00
classBindings: {
2015-01-25 14:21:57 -05:00
shouldAskForAlertsPermission: '.enableAlerts',
soundEnabledClass: '.soundNotifs'
2013-09-18 19:24:40 -04:00
},
srcBindings: {
avatar: '#avatarChanger img'
2013-09-12 14:18:44 -04:00
},
textBindings: {
status: '.status'
},
2013-09-12 14:18:44 -04:00
events: {
2013-09-18 19:24:40 -04:00
'click .enableAlerts': 'enableAlerts',
'click .installFirefox': 'installFirefox',
2015-01-25 14:21:57 -05:00
'click .soundNotifs': 'handleSoundNotifs',
2013-09-18 19:24:40 -04:00
'dragover': 'handleAvatarChangeDragOver',
2013-09-19 00:11:53 -04:00
'drop': 'handleAvatarChange',
'change #uploader': 'handleAvatarChange'
2013-09-12 14:18:44 -04:00
},
2013-08-29 23:38:28 -04:00
initialize: function (spec) {
2013-10-16 13:48:40 -04:00
this.render();
},
render: function () {
2013-08-29 23:38:28 -04:00
this.renderAndBind();
2013-10-16 13:48:40 -04:00
return this;
2013-09-12 14:18:44 -04:00
},
enableAlerts: function () {
2013-10-11 18:40:42 -04:00
if (app.notifications.permissionNeeded()) {
app.notifications.requestPermission(function (perm) {
if (perm === 'granted') {
app.notifications.create('Ok, sweet!', {
body: "You'll now be notified of stuff that happens."
});
}
});
}
2013-09-18 19:24:40 -04:00
},
installFirefox: function () {
2013-10-14 16:36:19 -04:00
if (!app.desktop.installed) {
app.desktop.install();
} else {
app.desktop.uninstall();
}
},
2013-09-18 19:24:40 -04:00
handleAvatarChangeDragOver: function (e) {
e.preventDefault();
return false;
},
handleAvatarChange: function (e) {
2013-09-19 00:11:53 -04:00
var file;
2013-09-18 19:24:40 -04:00
e.preventDefault();
2013-09-19 00:11:53 -04:00
if (e.dataTransfer) {
file = e.dataTransfer.files[0];
} else if (e.target.files) {
file = e.target.files[0];
} else {
return;
}
2013-09-18 19:24:40 -04:00
if (file.type.match('image.*')) {
var fileTracker = new FileReader();
fileTracker.onload = function () {
var resampler = new Resample(this.result, 80, 80, function (data) {
var b64Data = data.split(',')[1];
var id = crypto.createHash('sha1').update(atob(b64Data)).digest('hex');
2013-09-18 19:24:40 -04:00
app.storage.avatars.add({id: id, uri: data});
client.publishAvatar(id, b64Data, function (err, res) {
if (err) return;
client.useAvatars([{
id: id,
width: 80,
height: 80,
type: 'image/png',
bytes: b64Data.length
}]);
});
});
};
fileTracker.readAsDataURL(file);
}
2015-01-25 14:21:57 -05:00
},
handleSoundNotifs: function (e) {
this.model.setSoundNotification(!this.model.soundEnabled);
},
2013-08-29 23:38:28 -04:00
});