|
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script> |
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/2.2.0/ui-bootstrap-tpls.min.js"></script> |
|
<script> |
|
angular.module('youcreate', ['ui.bootstrap']) |
|
.factory('GallerySvc', ['$q', function($q) { |
|
var Gallery = {}; |
|
|
|
Gallery.pictures = []; |
|
|
|
Gallery.getPictures = function() { |
|
var deferred = $q.defer(); |
|
|
|
google.script.run |
|
.withSuccessHandler(function(response) { |
|
|
|
Gallery.pictures = JSON.parse(response); |
|
|
|
deferred.resolve(Gallery.pictures); |
|
}) |
|
.withFailureHandler(function(err) { |
|
deferred.reject(err); |
|
}) |
|
.getPictures(); |
|
|
|
return deferred.promise; |
|
} |
|
|
|
Gallery.postPicture = function(pic) { |
|
var deferred = $q.defer(); |
|
|
|
|
|
google.script.run |
|
.withSuccessHandler(function(response) { |
|
Gallery.pictures.push(response.value[0]); |
|
|
|
deferred.resolve(response); |
|
}) |
|
.withFailureHandler(function(err) { |
|
deferred.reject(err); |
|
}) |
|
.postPicture(pic); |
|
|
|
return deferred.promise; |
|
} |
|
return Gallery; |
|
}]) |
|
|
|
.factory('FileReader', ['$q', function($q) { |
|
var onLoad = function(reader, deferred, scope) { |
|
return function () { |
|
scope.$apply(function () { |
|
deferred.resolve(reader.result); |
|
}); |
|
}; |
|
}; |
|
var onError = function (reader, deferred, scope) { |
|
return function () { |
|
scope.$apply(function () { |
|
deferred.reject(reader.result); |
|
}); |
|
}; |
|
}; |
|
var onProgress = function(reader, scope) { |
|
return function (event) { |
|
scope.$broadcast("fileProgress", |
|
{ |
|
total: event.total, |
|
loaded: event.loaded |
|
}); |
|
}; |
|
}; |
|
var getReader = function(deferred, scope) { |
|
var reader = new FileReader(); |
|
reader.onload = onLoad(reader, deferred, scope); |
|
reader.onerror = onError(reader, deferred, scope); |
|
reader.onprogress = onProgress(reader, scope); |
|
return reader; |
|
}; |
|
var readAsDataURL = function (file, scope) { |
|
var deferred = $q.defer(); |
|
|
|
var reader = getReader(deferred, scope); |
|
reader.readAsDataURL(file); |
|
|
|
return deferred.promise; |
|
}; |
|
return { |
|
readAsDataUrl: readAsDataURL |
|
}; |
|
}]) |
|
|
|
.directive('allowAdaptations', [function() { |
|
return { |
|
restrict: 'E', |
|
scope: {sort: '='}, |
|
replace: true, |
|
template: '<select ng-model="sort.allowAdaptations"> \ |
|
<option value="">ALLOW ADAPTATIONS?</option>\ |
|
<option value="true">Yes</option>\ |
|
<option value="false">No</option>\ |
|
</select>' |
|
} |
|
}]) |
|
|
|
.directive('allowCommercial', [function() { |
|
return { |
|
restrict: 'E', |
|
scope: {sort: '='}, |
|
replace: true, |
|
template: '<select ng-model="sort.allowCommercial">\ |
|
<option value="">ALLOW COMMERCIAL USE?</options>\ |
|
<option value="true">Yes</option>\ |
|
<option value="false">No</option>\ |
|
</select>' |
|
} |
|
}]) |
|
|
|
.directive('type', [function() { |
|
return { |
|
restrict: 'E', |
|
scope: {sort: '='}, |
|
replace: true, |
|
template: '<select ng-model="sort.type">\ |
|
<option value="">TYPE?</option>\ |
|
<option value="photograph">Photograph</option>\ |
|
<option value="artwork">Artwork</option>\ |
|
<option value="animation">Animation</option>\ |
|
</select>' |
|
} |
|
}]) |
|
|
|
.directive('category', [function() { |
|
return { |
|
restrict: 'E', |
|
scope: {sort: '='}, |
|
replace: true, |
|
template: '<select ng-model="sort.category">\ |
|
<option value="">CATEGORY?</option>\ |
|
<option value="abstract">Abstract</option>\ |
|
<option value="animals">Animals</option>\ |
|
<option value="art">Art</option>\ |
|
<option value="architecture">Architecture</option>\ |
|
<option value="illustrations">Illustrations</option>\ |
|
<option value="nature">Nature</option>\ |
|
<option value="objects">Objects</option>\ |
|
<option value="people">People</option>\ |
|
<option value="technology">Technology</option>\ |
|
<option value="travel">Travel</option>\ |
|
</select>' |
|
} |
|
}]) |
|
|
|
.directive('ngFileSelect', [function(){ |
|
return { |
|
link: function($scope,el){ |
|
|
|
el.bind("change", function(e){ |
|
|
|
$scope.file = (e.srcElement || e.target).files[0]; |
|
$scope.getFile(); |
|
}) |
|
|
|
} |
|
|
|
} |
|
}]) |
|
|
|
//.controller('HeaderCtrl', ['$scope', '$uibModal', 'GallerySvc', 'SortSvc', function($scope, $uibModal, GallerySvc, SortSvc) { |
|
// |
|
// $scope.sort = SortSvc; |
|
// |
|
// $scope.openUpload = function() { |
|
// var modal = $uibModal.open({ |
|
// animation: true, |
|
// ariaLabelledBy: 'modal-title', |
|
// ariaDescribedBy: 'modal-body', |
|
// templateUrl: 'uploadModal.html', |
|
// controller: 'UploadCtrl', |
|
// size: 'lg' |
|
// }); |
|
// |
|
// } |
|
// |
|
//}]) |
|
|
|
.controller('GalleryCtrl', ['$scope', '$uibModal', 'GallerySvc', function($scope, $uibModal, GallerySvc) { |
|
|
|
GallerySvc.getPictures().then(function(response) { |
|
$scope.pictures = response; |
|
}); |
|
|
|
$scope.$watch('pictures', function(update) { |
|
$scope.pictures = update; |
|
}); |
|
|
|
$scope.openShare = function(pic) { |
|
$uibModal.open({ |
|
animation: true, |
|
ariaLabelledBy: 'modal-title', |
|
ariaDescribedBy: 'modal-body', |
|
templateUrl: 'shareModal.html', |
|
controller: 'ShareCtrl', |
|
size: 'lg', |
|
resolve: { |
|
picture: function() { |
|
return pic; |
|
} |
|
} |
|
}); |
|
} |
|
|
|
$scope.openUpload = function() { |
|
var modal = $uibModal.open({ |
|
animation: true, |
|
ariaLabelledBy: 'modal-title', |
|
ariaDescribedBy: 'modal-body', |
|
templateUrl: 'uploadModal.html', |
|
controller: 'UploadCtrl', |
|
size: 'lg' |
|
}); |
|
|
|
} |
|
}]) |
|
|
|
.controller('ShareCtrl', ['$scope', '$uibModalInstance', 'picture', function($scope, $uibModalInstance, picture) { |
|
|
|
$scope.pic = picture; |
|
|
|
$scope.ok = function() { |
|
$uibModalInstance.close(); |
|
} |
|
$scope.cancel = function() { |
|
$uibModalInstance.dismiss('cancel'); |
|
} |
|
}]) |
|
.controller('UploadCtrl', ['$scope', '$uibModalInstance', 'GallerySvc', 'FileReader', function($scope, $uibModalInstance, GallerySvc, FileReader) { |
|
$scope.upload = { |
|
file: { |
|
complete: false |
|
} |
|
}; |
|
|
|
$scope.getFile = function () { |
|
FileReader.readAsDataUrl($scope.file, $scope) |
|
.then(function(result) { |
|
$scope.preview = result; |
|
$scope.upload.file.name = $scope.file.name; |
|
$scope.upload.file.data = result; |
|
$scope.upload.file.complete = true; |
|
}); |
|
}; |
|
|
|
$scope.submit = function() { |
|
if($scope.upload.file.complete) { |
|
GallerySvc.postPicture($scope.upload).then(function(response) { |
|
$uibModalInstance.close(response); |
|
}); |
|
} |
|
} |
|
|
|
$scope.cancel = function() { |
|
$uibModalInstance.dismiss('cancel'); |
|
} |
|
}]) |
|
</script> |