kaiwa/clientapp/pages/main.js

68 lines
2.3 KiB
JavaScript
Raw Normal View History

2013-09-18 19:24:40 -04:00
/*global app, me, XMPP, client, Resample*/
2013-08-29 23:38:28 -04:00
"use strict";
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: {
2013-09-18 19:24:40 -04:00
shouldAskForAlertsPermission: '.enableAlerts'
},
srcBindings: {
avatar: '#avatarChanger img'
2013-09-12 14:18:44 -04:00
},
events: {
2013-09-18 19:24:40 -04:00
'click .enableAlerts': 'enableAlerts',
'dragover': 'handleAvatarChangeDragOver',
'drop': 'handleAvatarChange'
2013-09-12 14:18:44 -04:00
},
2013-08-29 23:38:28 -04:00
initialize: function (spec) {
2013-09-12 14:18:44 -04:00
me.shouldAskForAlertsPermission = app.notifier.shouldAskPermission();
2013-08-29 23:38:28 -04:00
this.renderAndBind();
2013-09-12 14:18:44 -04:00
},
enableAlerts: function () {
app.notifier.askPermission(function () {
var shouldAsk = app.notifier.shouldAskPermission();
if (!shouldAsk) {
app.notifier.show({
title: 'Ok, sweet!',
description: "You'll now be notified of stuff that happens."
});
}
});
2013-09-18 19:24:40 -04:00
},
handleAvatarChangeDragOver: function (e) {
e.preventDefault();
return false;
},
handleAvatarChange: function (e) {
e.preventDefault();
var file = e.dataTransfer.files[0];
if (file.type.match('image.*')) {
console.log('Got an image file!', file.type);
var fileTracker = new FileReader();
fileTracker.onload = function () {
var resampler = new Resample(this.result, 80, 80, function (data) {
var b64Data = data.split(',')[1];
var id = XMPP.crypto.createHash('sha1').update(atob(b64Data)).digest('hex');
console.log(id);
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);
}
2013-08-29 23:38:28 -04:00
}
});