kaiwa/clientapp/pages/main.js

90 lines
2.7 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 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: {
2013-09-18 19:24:40 -04:00
shouldAskForAlertsPermission: '.enableAlerts'
},
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',
'dragover': 'handleAvatarChangeDragOver',
2013-09-19 00:11:53 -04:00
'drop': 'handleAvatarChange',
'change #uploader': 'handleAvatarChange',
'blur .status': 'handleStatusChange'
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) {
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);
}
},
handleStatusChange: function (e) {
var text = e.target.textContent;
me.status = text;
client.sendPresence({
status: text,
caps: client.disco.caps
});
2013-08-29 23:38:28 -04:00
}
});