contacts/js/components/avatar/avatar_directive.js

37 lines
900 B
JavaScript
Raw Normal View History

2016-04-20 21:22:58 +00:00
angular.module('contactsApp')
2016-04-28 08:44:48 +00:00
.directive('avatar', function(ContactService) {
2016-04-20 21:22:58 +00:00
return {
scope: {
contact: '=data'
},
controller: 'avatarCtrl',
controllerAs: 'ctrl',
bindToController: {
contact: '=data'
},
2016-04-20 21:22:58 +00:00
link: function(scope, element) {
var input = element.find('input');
input.bind('change', function() {
var file = input.get(0).files[0];
if (file.size > 1024*1024) { // 1 MB
OC.Notification.showTemporary(t('contacts', 'The selected image is too big (max 1MB)'));
} else {
var reader = new FileReader();
2016-04-20 21:22:58 +00:00
reader.addEventListener('load', function () {
scope.$apply(function() {
scope.contact.photo(reader.result);
ContactService.update(scope.contact);
});
}, false);
2016-04-20 21:22:58 +00:00
if (file) {
reader.readAsDataURL(file);
}
2016-04-20 21:22:58 +00:00
}
});
},
2016-04-28 08:44:48 +00:00
templateUrl: OC.linkTo('contacts', 'templates/avatar.html')
2016-04-20 21:22:58 +00:00
};
});