|
|
@ -23,61 +23,111 @@ define(['jquery', 'underscore', 'text!partials/buddypictureupload.html'], functi |
|
|
|
// buddyPictureUpload
|
|
|
|
// buddyPictureUpload
|
|
|
|
return ["$compile", function($compile) { |
|
|
|
return ["$compile", function($compile) { |
|
|
|
|
|
|
|
|
|
|
|
var controller = ['$scope', 'safeApply', '$timeout', '$q', function($scope, safeApply, $timeout, $q) { |
|
|
|
var controller = ['$scope', 'safeApply', '$timeout', '$q', 'translation', function($scope, safeApply, $timeout, $q, translation) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var previewWidth = 198; |
|
|
|
|
|
|
|
var previewHeight = 198; |
|
|
|
$scope.showUploadPicture = false; |
|
|
|
$scope.showUploadPicture = false; |
|
|
|
$scope.previewUpload = false; |
|
|
|
$scope.previewUpload = false; |
|
|
|
|
|
|
|
$scope.imgData = null; |
|
|
|
|
|
|
|
$scope.error = { |
|
|
|
|
|
|
|
msg: null |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
$scope.text = { |
|
|
|
|
|
|
|
initial: 'Please choose a picture to upload', |
|
|
|
|
|
|
|
again: 'Upload a different picture' |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var setUploadImageDimension = function(data) { |
|
|
|
|
|
|
|
var img = new Image(); |
|
|
|
|
|
|
|
img.onload = function() { |
|
|
|
|
|
|
|
if(this.width < previewWidth && this.height < previewHeight) { |
|
|
|
|
|
|
|
$scope.prevImage.style.height = null; |
|
|
|
|
|
|
|
$scope.prevImage.style.width = null; |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (this.width < this.height) { |
|
|
|
|
|
|
|
$scope.prevImage.style.width = previewWidth + 'px'; |
|
|
|
|
|
|
|
$scope.prevImage.style.height = null; |
|
|
|
|
|
|
|
console.log('changed width'); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
$scope.prevImage.style.height = previewHeight + 'px'; |
|
|
|
|
|
|
|
$scope.prevImage.style.width = null; |
|
|
|
|
|
|
|
console.log('changed height'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
img.src = data; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$scope.reset = function() { |
|
|
|
|
|
|
|
$scope.showUploadPicture = false; |
|
|
|
|
|
|
|
$scope.previewUpload = false; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
$scope.handleUpload = function(event) { |
|
|
|
$scope.handleUpload = function(event) { |
|
|
|
var file = event.target.files[0]; |
|
|
|
var file = event.target.files[0]; |
|
|
|
console.log('file', file); |
|
|
|
if(!file) { |
|
|
|
|
|
|
|
return; |
|
|
|
if(!file.type.match(/image/)) { |
|
|
|
|
|
|
|
error({event: {target: {error: {name: 'NotImage'}}}}); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
console.log('file', file); |
|
|
|
var reader = new FileReader(); |
|
|
|
|
|
|
|
reader.readAsArrayBuffer(file); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var progress = function(event) { |
|
|
|
var progress = function(event) { |
|
|
|
console.log('file progress', event); |
|
|
|
console.log('file progress', event); |
|
|
|
}; |
|
|
|
}; |
|
|
|
var load = function(event) { |
|
|
|
var load = function(event) { |
|
|
|
console.log('file load', event); |
|
|
|
console.log('file load', event); |
|
|
|
var fileBuffer = event.target.result; |
|
|
|
$scope.$apply(function(scope) { |
|
|
|
// var context = $scope.uploadPrev.getContext('2d');
|
|
|
|
scope.imgData = event.target.result; |
|
|
|
// context.putImageData(fileBuffer, 0, 0);
|
|
|
|
setUploadImageDimension(scope.imgData); |
|
|
|
// $scope.previewUpload = true;
|
|
|
|
$scope.previewUpload = true; |
|
|
|
|
|
|
|
}); |
|
|
|
}; |
|
|
|
}; |
|
|
|
var error = function(event) { |
|
|
|
var error = function(event) { |
|
|
|
console.log('file error', event); |
|
|
|
console.log('file error', event); |
|
|
|
if(event.target.error.name == 'NotReadableError') { |
|
|
|
if(event.target.error.name == 'NotReadableError') { |
|
|
|
// file couldn't be read
|
|
|
|
$scope.$apply(function(scope) { |
|
|
|
|
|
|
|
scope.error.msg = "The file couldn't be read"; |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
if(event.target.error.name == 'NotImage') { |
|
|
|
if(event.target.error.name == 'NotImage') { |
|
|
|
// file is not an image
|
|
|
|
$scope.$apply(function(scope) { |
|
|
|
|
|
|
|
scope.error.msg = "The file is not an image."; |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
reader.onprogress = progress; |
|
|
|
if(!file.type.match(/image/)) { |
|
|
|
reader.onload = load; |
|
|
|
error({target: {error: {name: 'NotImage'}}}); |
|
|
|
reader.onerror = error; |
|
|
|
} else { |
|
|
|
|
|
|
|
var reader = new FileReader(); |
|
|
|
|
|
|
|
reader.readAsDataURL(file); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reader.onprogress = progress; |
|
|
|
|
|
|
|
reader.onload = load; |
|
|
|
|
|
|
|
reader.onerror = error; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$scope.usePicture = function() { |
|
|
|
|
|
|
|
$scope.writeToCanvas($scope.canvasPic, $scope.prevImage); |
|
|
|
|
|
|
|
$scope.user.buddyPicture = $scope.canvasPic.toDataURL("image/jpeg"); |
|
|
|
|
|
|
|
$scope.reset(); |
|
|
|
|
|
|
|
safeApply($scope); |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
}]; |
|
|
|
}]; |
|
|
|
|
|
|
|
|
|
|
|
var link = function($scope, $element) { |
|
|
|
var link = function($scope, $element) { |
|
|
|
|
|
|
|
$scope.prevImage = $(".showUploadPicture .preview").get(0); |
|
|
|
$element.find("#uploadFile").on('change', $scope.handleUpload); |
|
|
|
$element.find("#uploadFile").on('change', $scope.handleUpload); |
|
|
|
$scope.uploadPrev = $element.find("canvas.uploadPrev").get(0); |
|
|
|
$scope.uploadPrev = $element.find("canvas.uploadPrev").get(0); |
|
|
|
$($scope.uploadPrev).attr($scope.captureSize); |
|
|
|
$($scope.uploadPrev).attr($scope.captureSize); |
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
restrict: 'E', |
|
|
|
restrict: 'E', |
|
|
|
transclude: true, |
|
|
|
transclude: true, |
|
|
|
replace: true, |
|
|
|
replace: false, |
|
|
|
template: template, |
|
|
|
template: template, |
|
|
|
controller: controller, |
|
|
|
controller: controller, |
|
|
|
link: link |
|
|
|
link: link |
|
|
|