1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-13 21:15:04 -05:00
kaiwa/clientapp/pages/main.js

116 lines
3.5 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');
2013-10-16 13:48:40 -04:00
var ContactRequestItem = require('../views/contactRequest');
2013-08-29 23:38:28 -04:00
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',
'click .installFirefox': 'installFirefox',
2013-10-16 13:48:40 -04:00
'click .addContact': 'handleAddContact',
2013-09-18 19:24:40 -04:00
'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-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
this.renderCollection(this.model.contactRequests, ContactRequestItem, this.$('#contactrequests'));
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);
}
},
handleStatusChange: function (e) {
var text = e.target.textContent;
me.status = text;
client.sendPresence({
status: text,
caps: client.disco.caps
});
2013-10-16 13:48:40 -04:00
},
handleAddContact: function (e) {
e.preventDefault();
var contact = this.$('#addcontact').val();
if (contact) {
app.api.sendPresence({to: contact, type: 'subscribe'});
}
this.$('#addcontact').val('');
return false;
2013-08-29 23:38:28 -04:00
}
});