2015-10-25 06:17:45 +01:00
|
|
|
# Pony.fm - A community for pony fan music.
|
2018-04-21 06:25:36 +02:00
|
|
|
# Copyright (C) 2015 Feld0
|
2015-10-25 06:17:45 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-02-14 17:14:30 +01:00
|
|
|
module.exports = angular.module('ponyfm').directive 'pfmImageUpload', () ->
|
2015-10-25 03:35:37 +01:00
|
|
|
$image = null
|
|
|
|
$uploader = null
|
|
|
|
|
|
|
|
restrict: 'E'
|
|
|
|
templateUrl: '/templates/directives/image-upload.html'
|
|
|
|
scope:
|
|
|
|
setImage: '=setImage'
|
|
|
|
image: '=image'
|
2016-03-06 13:27:53 +01:00
|
|
|
# ID of the user to upload images on behalf of
|
|
|
|
userId: '=userId'
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
compile: (element) ->
|
|
|
|
$image = element.find 'img'
|
|
|
|
$uploader = element.find 'input'
|
|
|
|
|
|
|
|
controller: [
|
|
|
|
'images', '$scope', 'lightbox'
|
|
|
|
(images, $scope, lightbox) ->
|
2016-03-06 13:27:53 +01:00
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
$scope.imageObject = null
|
|
|
|
$scope.imageFile = null
|
|
|
|
$scope.imageUrl = null
|
|
|
|
$scope.isImageLoaded = false
|
|
|
|
$scope.error = null
|
|
|
|
|
|
|
|
$scope.$watch 'image', (val) ->
|
|
|
|
$scope.imageObject = $scope.imageFile = $scope.imageUrl = null
|
|
|
|
$scope.isImageLoaded = false
|
2016-03-06 13:27:53 +01:00
|
|
|
return unless val?
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
$scope.imageUrl = val
|
|
|
|
$image.attr 'src', val
|
|
|
|
$scope.isImageLoaded = true
|
|
|
|
|
|
|
|
$image.load () -> $scope.$apply ->
|
|
|
|
$scope.isImageLoaded = true
|
|
|
|
window.setTimeout (() -> window.alignVertically($image)), 0
|
|
|
|
|
2016-03-06 13:27:53 +01:00
|
|
|
$scope.$watch 'userId', (val)->
|
|
|
|
return unless val?
|
2016-06-14 10:27:56 +02:00
|
|
|
images.refresh(true, $scope.userId).done (images) -> $scope.images = images
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
$scope.previewImage = () ->
|
|
|
|
return if !$scope.isImageLoaded
|
|
|
|
|
|
|
|
if $scope.imageObject
|
|
|
|
lightbox.openImageUrl $scope.imageObject.urls.normal
|
|
|
|
else if $scope.imageFile
|
|
|
|
lightbox.openDataUrl $image.attr 'src'
|
|
|
|
else if $scope.imageUrl
|
|
|
|
lightbox.openImageUrl $scope.imageUrl
|
|
|
|
|
2016-06-19 22:13:47 +02:00
|
|
|
# An explicit return value is needed here to avoid this issue:
|
|
|
|
# https://docs.angularjs.org/error/$parse/isecdom
|
|
|
|
return null
|
|
|
|
|
2015-10-25 03:35:37 +01:00
|
|
|
$scope.uploadImage = () ->
|
|
|
|
$uploader.trigger 'click'
|
2016-02-14 11:53:27 +01:00
|
|
|
return
|
2015-10-25 03:35:37 +01:00
|
|
|
|
|
|
|
$scope.clearImage = () ->
|
|
|
|
$scope.imageObject = $scope.imageFile = $scope.imageUrl = null
|
|
|
|
$scope.isImageLoaded = false
|
|
|
|
$scope.setImage null
|
|
|
|
|
|
|
|
$scope.selectGalleryImage = (image) ->
|
|
|
|
$scope.imageObject = image
|
|
|
|
$scope.imageFile = null
|
|
|
|
$scope.imageUrl = image.urls.small
|
|
|
|
$image.attr 'src', image.urls.small
|
|
|
|
$scope.isImageLoaded = true
|
|
|
|
$scope.setImage image, 'gallery'
|
|
|
|
|
|
|
|
$scope.setImageFile = (input) ->
|
|
|
|
$scope.$apply ->
|
|
|
|
file = input.files[0]
|
|
|
|
$scope.imageObject = null
|
|
|
|
$scope.imageFile = file
|
|
|
|
|
2015-12-27 10:43:43 +01:00
|
|
|
if file.type not in ['image/png', 'image/jpeg']
|
|
|
|
$scope.error = 'Image must be a PNG or JPEG!'
|
2015-10-25 03:35:37 +01:00
|
|
|
$scope.isImageLoaded = false
|
|
|
|
$scope.imageObject = $scope.imageFile = $scope.imageUrl = null
|
|
|
|
return
|
|
|
|
|
|
|
|
$scope.error = null
|
|
|
|
$scope.setImage file, 'file'
|
|
|
|
|
|
|
|
reader = new FileReader()
|
|
|
|
reader.onload = (e) -> $scope.$apply ->
|
|
|
|
$image[0].src = e.target.result
|
|
|
|
$scope.isImageLoaded = true
|
|
|
|
|
|
|
|
reader.readAsDataURL file
|
|
|
|
]
|