angular.module('mediaModalTemplates', []).run(['$templateCache', function($templateCache) { 'use strict'; $templateCache.put('camera.html', "
\n" + "

Upload Media

\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + " \n" + " \n" + " \n" + " Add files...\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + "
\n" + " \n" + "
\n" + " \n" + "
 
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "
{{file.name}}
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "
{{file.name}}
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "\n" + "
\n" + "\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
" ); $templateCache.put('gallery.html', "
\n" + " \n" + " \n" + " \n" + " \n" + " {{resource.title}}\n" + " \n" + "
\n" + "
\n" + "
\n" + "
\n" + "" ); $templateCache.put('galleryWindow.html', "
\n" + "

Add Image

\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + " \n" + "
" ); $templateCache.put('upload.html', "
\n" + "

Upload Media

\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + " \n" + " \n" + " \n" + " Add files...\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "
\n" + " \n" + "
\n" + " \n" + "
\n" + " \n" + "
 
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "
{{file.name}}
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "
{{file.name}}
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "\n" + "
\n" + "\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
" ); $templateCache.put('webService.html', "
\n" + "

{{resource.title}}

\n" + "
\n" + "
\n" + " \n" + " \n" + " \n" + " \n" + "
\n" + "
\n" + "\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "
\n" + " \n" + "
\n" + "
\n" + "
{{image.title}}
\n" + "
\n" + "
\n" + "
\n" + "
\n" + "\n" + "
\n" + " \n" + "
\n" + "\n" + "
" ); }]); angular.module("mediaModal", ["ui.bootstrap", "angularSpinner", "flow", "images-resizer", "mediaModalTemplates", "blueimp.fileupload"]); angular.module("mediaModal") .config([ '$httpProvider', 'fileUploadProvider', '$sceDelegateProvider', function ($httpProvider, fileUploadProvider, $sceDelegateProvider) { $sceDelegateProvider.resourceUrlWhitelist([ 'self', 'http://business.otonomic.com/**' ]); delete $httpProvider.defaults.headers.common['X-Requested-With']; fileUploadProvider.defaults.redirect = window.location.href.replace( /\/[^\/]*$/, '/cors/result.html?%s' ); // Uploader settings: angular.extend(fileUploadProvider.defaults, { autoUpload: true, forceIframeTransport: true, // Enable image resizing, egrunt devxcept for Android and Opera, // which actually support image resizing, but fail to // send Blob objects via XHR requests: disableImageResize: /Android(?!.*Chrome)|Opera/ .test(window.navigator.userAgent), /*maxFileSize: 9990000,*/ acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i }); } ]) .constant("flickrApiKey", "b5245e9b9cbaeecee26ea278bfa20253") .constant("upload_url", "http://business.otonomic.com/api/v1/?cmd=upload") .constant("instagramApiKey", "44171713.10d405b.25e232f920f94ac9907d8c0ea34ce1de") //.constant("facebookApiKey", "CAACEdEose0cBAJuIrBZCXQZBpMRGixqcQun3gJAwUL2cfGZB0iuvLu5lpDJy3txqnhAbdbFneQHpZACHiWQsymYxZCtFbH3GuB87NpQdZB8DvtUGIpmZCF3yip71ZBqqajhgw8uPcuaerccTBYZC1gXtGZAV11gvcrYOodJNnT87aJQ1ZBR9T4AX7NHJZCQfmJIIOd6k6oR55ubMZBy7R6ZBAYylzC") .constant("facebookApiKey", "CAANkofzWtSoBAG0FcIPXZAFFUJjZBdZAiejDxnPXXapvIP1UZA9ZBKiryHlB3GZCjrY1P3rgnKO70AWYQ1bLFp03MxZAuYTaimqsETawQxE9PRhCuLKHZCxDd9wrzJQrqXMsscI80UzPapvPoQKa9WeE7NHIN8H30lb48AhXqZAbVbX7w33nuEhI8ZCPc56VnTS2IZD") .constant("pexelsApiKey", "563492ad6f9170000100000183fb18af4b5f4b5c6a67f7e6433578ba") .factory("availableResources", ["enums", function(enums){ return [{ title: "Camera", icon: "fa fa-camera", viewPath: "camera.html" },{ title: "Upload", icon: "fa fa-cloud-upload", viewPath: "upload.html" },{ title: "Flickr", icon: "fa fa-flickr", viewPath: "webService.html", webServiceType: enums.webServiceTypes.flickr },{ title: "Instagram", icon: "fa fa-instagram", viewPath: "webService.html", webServiceType: enums.webServiceTypes.instagram },{ title: "Background Patterns", icon: "fa fa-star", viewPath: "webService.html", webServiceType: enums.webServiceTypes.backgroundPatterns },{ title: "Facebook Photos", icon: "fa fa-facebook", viewPath: "webService.html", webServiceType: enums.webServiceTypes.facebookPhotos },{ title: "Facebook Users", icon: "fa fa-facebook-official", viewPath: "webService.html", webServiceType: enums.webServiceTypes.facebookUsers },{ title: "Pexels", icon: "fa fa-picture-o", viewPath: "webService.html", webServiceType: enums.webServiceTypes.pexels }]; }]); /** * Created by lobanovana on 23.03.2016. */ angular.module("mediaModal") .controller("cameraController", ['$scope', '$http', 'upload_url', function ($scope, $http, upload_url) { $scope.upload_url = upload_url; $scope.options = { url: upload_url }; $scope.loadingFiles = true; $http.get(upload_url) .then( function (response) { $scope.loadingFiles = false; $scope.filequeue = response.data.files || []; //console.log($scope.queue); }, function () { $scope.loadingFiles = false; } ); $scope.selectImage = function(image){ //console.log(image); image.selected = !image.selected; image.thumbnail = image.url; if ($scope.selectedImages){ if (image.selected){ $scope.selectedImages.push(image); return; } var imageIndex = $scope.selectedImages.indexOf(image); if (imageIndex != -1){ $scope.selectedImages.splice(imageIndex, 1); } } }; $scope.$on('fileuploaddone', function(e, data){ // Your code here $.each(data.result.files, function (index, file) { $scope.selectImage(file); }); }); } ]); angular.module("mediaModal") .controller('FileDestroyController', [ '$scope', '$http', 'upload_url', function ($scope, $http, upload_url) { var file = $scope.file, state; if (file.url) { file.$state = function () { return state; }; file.$destroy = function () { state = 'pending'; return $http({ url: file.deleteUrl, method: file.deleteType }).then( function () { state = 'resolved'; $scope.clear(file); }, function () { state = 'rejected'; } ); }; } else if (!file.$cancel && !file._index) { file.$cancel = function () { $scope.clear(file); }; } } ]); (function(){ angular.module("mediaModal").controller("GalleryController", ["$scope", "availableResources", "$uibModalInstance", function($scope, availableResources, $uibModalInstance) { $scope.availableResources=availableResources; $scope.appState = {}; $scope.selectedImages = []; $scope.tabSelected = function(resource){ $scope.$broadcast("currentResourceChanged", resource.webServiceType) }; $scope.cancel = function(){ $uibModalInstance.dismiss("cancel"); }; $scope.selectImages = function(){ $uibModalInstance.close($scope.selectedImages); } }]); }()); angular.module("mediaModal") .controller("uploadController", ['$scope', '$http', 'upload_url', function ($scope, $http, upload_url) { $scope.upload_url = upload_url; $scope.options = { url: upload_url }; $scope.loadingFiles = true; $http.get(upload_url) .then( function (response) { $scope.loadingFiles = false; $scope.filequeue = response.data.files || []; }, function () { $scope.loadingFiles = false; } ); $scope.selectImage = function(image){ //console.log(image); image.selected = !image.selected; image.thumbnail = image.url; if ($scope.selectedImages){ if (image.selected){ $scope.selectedImages.push(image); return; } var imageIndex = $scope.selectedImages.indexOf(image); if (imageIndex != -1){ $scope.selectedImages.splice(imageIndex, 1); } } }; $scope.$on('fileuploaddone', function(e, data){ // Your code here $.each(data.result.files, function (index, file) { $scope.selectImage(file); }); }); } ]); angular.module("mediaModal") .controller("webServicesController", ["$scope", "webStorage", "enums", function($scope, webStorage, enums){ $scope.currentPage = 0; $scope.itemsOnPage = 25; $scope.search = function(){ $scope.appState.loading = true; webStorage.search($scope.webServiceType, $scope.imageFilter, $scope.currentPage, $scope.itemsOnPage) .then( function(result){ $scope.fullImagesCount = result.count; $scope.images = result.images; } ) .finally( function(){ $scope.appState.loading = false; } ); }; $scope.loadMore = function(){ $scope.appState.loading = true; webStorage.loadMore($scope.webServiceType) .then(function(result){ if (!$scope.images){ $scope.images = []; } $scope.images = $scope.images.concat(result.images); }) .finally(function(){ $scope.appState.loading = false; }); }; $scope.setWebServiceType = function(webServiceType){ $scope.webServiceType = webServiceType; }; $scope.selectImage = function(image){ image.selected = !image.selected; if ($scope.selectedImages){ if (image.selected){ $scope.selectedImages.push(image); return; } var imageIndex = $scope.selectedImages.indexOf(image); if (imageIndex != -1){ $scope.selectedImages.splice(imageIndex, 1); } } }; $scope.showSearcher = function(){ return $scope.webServiceType !== enums.webServiceTypes.backgroundPatterns; }; $scope.$on("currentResourceChanged", function(event, resourceType){ if ($scope.webServiceType === enums.webServiceTypes.backgroundPatterns && resourceType === enums.webServiceTypes.backgroundPatterns){ $scope.search(); } }) }] ); angular.module("mediaModal") .factory("enums", function(){ return { webServiceTypes: { flickr: 1, instagram: 2, backgroundPatterns: 3, facebookUsers: 4, pexels: 5 } } } ); angular.module("mediaModal") .factory("backgroundPatterns", ["$http", "$q", function($http, $q){ var _currentPage = 0; var _count = 20; function search(searchString, page, count){ return $http.jsonp("http://www.colourlovers.com/api/patterns/top?format=json&jsonCallback=JSON_CALLBACK&numResults=" + _count + "&resultOffset=" + _currentPage) .then(function(response){ _currentPage++; return { count: 0, images: response.data.map(function(image){ return { thumbnail: image.imageUrl, full: image.imageUrl, title: image.title }; }) }; }, function(response){ return $q.reject(response.data); } ); } function loadMore(){ return $http.jsonp("http://www.colourlovers.com/api/patterns/top?format=json&jsonCallback=JSON_CALLBACK&numResults=" + _count + "&resultOffset=" + _currentPage) .then(function(response){ _currentPage++; return { count: 0, images: response.data.map(function(image){ return { thumbnail: image.imageUrl, full: image.imageUrl, title: image.title }; }) }; }, function(response){ return $q.reject(response.data); } ); } return { search: search, loadMore: loadMore } }] ); angular.module("mediaModal") .factory("facebookPhotos", ["$http", "facebookApiKey", "$q", function($http, facebookApiKey, $q){ var _nextUrl; function search(searchString, page, count){ return $http.get("https://graph.facebook.com/v2.5/" + searchString + "/photos?access_token=" + facebookApiKey) .then(function(response){ _nextUrl = response.data.paging.next; return { count: 0, images: response.data.data.map(function(user){ return { thumbnail: "http://graph.facebook.com/" + user.id + "/picture?type=normal", full: "http://graph.facebook.com/" + user.id + "/picture?type=large", title: user.name }; }) }; }, function(response){ return $q.reject(response.data); } ); } function loadMore(){ if (!_nextUrl){ return $q.reject("no more images"); } return $http.get(_nextUrl) .then(function(response){ _nextUrl = response.data.paging.next; return { count: 0, images: response.data.data.map(function(user){ return { thumbnail: "http://graph.facebook.com/" + user.id + "/picture?type=normal", full: "http://graph.facebook.com/" + user.id + "/picture?type=large", title: user.name }; }) }; }, function(response){ return $q.reject(response.data); } ); } return { search: search, loadMore: loadMore } }] ); angular.module("mediaModal") .factory("facebookUsers", ["$http", "facebookApiKey", "$q", function($http, facebookApiKey, $q){ var _nextUrl; function search(searchString, page, count){ return $http.get("https://graph.facebook.com/v2.2/search?q=" + searchString + "&type=user&access_token=" + facebookApiKey) .then(function(response){ _nextUrl = response.data.paging.next; return { count: 0, images: response.data.data.map(function(user){ return { thumbnail: "http://graph.facebook.com/" + user.id + "/picture?type=normal", full: "http://graph.facebook.com/" + user.id + "/picture?type=large", title: user.name }; }) }; }, function(response){ return $q.reject(response.data); } ); } function loadMore(){ if (!_nextUrl){ return $q.reject("no more images"); } return $http.get(_nextUrl) .then(function(response){ _nextUrl = response.data.paging.next; return { count: 0, images: response.data.data.map(function(user){ return { thumbnail: "http://graph.facebook.com/" + user.id + "/picture?type=normal", full: "http://graph.facebook.com/" + user.id + "/picture?type=large", title: user.name }; }) }; }, function(response){ return $q.reject(response.data); } ); } return { search: search, loadMore: loadMore } }] ); angular.module("mediaModal") .factory("flickr", ["$http", "flickrApiKey", function($http, flickrApiKey){ var _lastPage = 1; var _count = 50; var _searchString = ""; function search(searchString, page, count){ _lastPage = 1; _count = count; _searchString = searchString; return $http.get("https://api.flickr.com/services/rest/?api_key=" + flickrApiKey + "&format=json&nojsoncallback=1&method=flickr.photos.search&format=json&page=" + page + "&per_page=" + count +"&text=" + searchString) .then(function(response){ return { count: +response.data.photos.total, images: response.data.photos.photo.map(function(photo){ return { thumbnail: "https://farm" + photo.farm + ".staticflickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_t.jpg", full: "https://farm" + photo.farm + ".staticflickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg" }; }) }; }, function(response){ return $q.reject(response.data); } ); } function loadMore(){ _lastPage++; return $http.get("https://api.flickr.com/services/rest/?api_key=" + flickrApiKey + "&format=json&nojsoncallback=1&method=flickr.photos.search&format=json&page=" + _lastPage + "&per_page=" + _count +"&text=" + _searchString) .then(function(response){ return { count: +response.data.photos.total, images: response.data.photos.photo.map(function(photo){ return { thumbnail: "https://farm" + photo.farm + ".staticflickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_t.jpg", full: "https://farm" + photo.farm + ".staticflickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg" }; }) }; }, function(response){ return $q.reject(response.data); } ); } return { search: search, loadMore: loadMore } }] ); angular.module("mediaModal") .factory("gallery", ["$q", "$uibModal", function($q, $uibModal){ function open(animate){ return $uibModal.open({ animation: animate, templateUrl: 'galleryWindow.html', controller: 'GalleryController', size: "lg" }).result; } return { open: open } }] ); angular.module("mediaModal") .factory("instagram", ["$http", "instagramApiKey", "$q", function($http, instagramApiKey, $q){ var _nextUrl; function search(searchString, page, count){ return $http.jsonp("https://api.instagram.com/v1/tags/" + searchString +"/media/recent?access_token=" + instagramApiKey + "&callback=JSON_CALLBACK&count=20") .then(function(response){ _nextUrl = response.data.pagination.next_url; return { count: 0, images: response.data.data.map(function(image){ return { thumbnail: image.images.thumbnail.url, full: image.images.standard_resolution.url, title: image.caption.text.substr(0, 50) }; }) }; }, function(response){ return $q.reject(response.data); } ); } function loadMore(){ if (!_nextUrl){ return $q.reject("no more images"); } return $http.jsonp(_nextUrl + "&callback=JSON_CALLBACK") .then(function(response){ _nextUrl = response.data.pagination.next_url; return { count: 0, images: response.data.data.map(function(image){ return { thumbnail: image.images.thumbnail.url, full: image.images.standard_resolution.url, title: image.caption.text.substr(0, 50) }; }) }; }, function(response){ return $q.reject(response.data); } ); } return { search: search, loadMore: loadMore } }] ); angular.module("mediaModal") .factory("pexels", ["$http", "pexelsApiKey", "$q", function($http, pexelsApiKey, $q){ function search(searchString, page, count){ return $http.get("http://api.pexels.com/v1/search?query=" + searchString + "&per_page=" + count + "&page=" + (page + 1), { headers: { "Authorization": pexelsApiKey } }) .then(function(response){ return { count: response.data.total_results, images: response.data.photos.map(function(photo){ return { thumbnail: photo.src.square, full: photo.src.large, title: photo.photographer }; }) }; }, function(response){ return $q.reject(response.data); } ); } return { search: search } }] ); angular.module("mediaModal") .factory("webStorage", ["enums", "flickr", "instagram", "backgroundPatterns", "facebookUsers", "pexels", "facebookPhotos", function(enums, flickr, instagram, backgroundPatterns, facebookUsers, pexels, facebookPhotos){ function getService(serviceType){ switch (serviceType){ case enums.webServiceTypes.flickr: return flickr; case enums.webServiceTypes.instagram: return instagram; case enums.webServiceTypes.backgroundPatterns: return backgroundPatterns; case enums.webServiceTypes.facebookUsers: return facebookUsers; case enums.webServiceTypes.pexels: return pexels; case enums.webServiceTypes.facebookPhotos: return facebookPhotos; } } function search(storageType, searchString, page, count){ return getService(storageType).search(searchString, page, count); } function loadMore(storageType){ return getService(storageType).loadMore(); } return{ search: search, loadMore: loadMore } }] );