(function () { // Define the Angular.js app var app = angular.module('embedding', ['angular-data.DSCacheFactory']); app.factory('embedDatacontext', ['$q', '$http', 'DSCacheFactory', embedDatacontext]); function embedDatacontext($q, $http, DSCacheFactory) { var embedlyCache = 'embedlyCache'; createEmbedlyCache(); var service = { getEmbed: getEmbed }; function createEmbedlyCache() { DSCacheFactory(embedlyCache, { storageMode: 'localStorage', maxAge: 3600000, deleteOnExpire: 'aggressive' }); } return service; function getEmbed(sourceUrl, maxWidth) { var url = embedlyOEmbedRemoteServiceUrl + '?key=' + embedlyKey + '&format=json&url=' + escape(sourceUrl); if (maxWidth) url = url + '&maxwidth=' + maxWidth; var deferred = $q.defer(); var cache = DSCacheFactory.get(embedlyCache); var cacheItem = cache.get(url); if (cacheItem) { deferred.resolve(cacheItem); return deferred.promise; } var request = $http({ method: "get", url: url }); return (request.then(function (response) { cache.put(url, response.data); return response.data; }, handleError)); } function handleError(response) { // The API response from the server should be returned in a // nomralized format. However, if the request was not handled by the // server (or what not handles properly - ex. server error), then we // may have to normalize it on our end, as best we can. if ( !angular.isObject(response.data) || !response.data.message ) { return ($q.reject("An unknown error occurred.")); } // Otherwise, use expected error message. return ($q.reject(response.data.message)); } } app.directive('embedFlickr', ["$sce", "$timeout", function ($sce, $timeout) { "use strict"; return { restrict: "E", template: '', scope: { height: "@", width: "@" }, link: function ($scope, element, $attrs) { $scope.$on('resetembed', function () { $timeout(function() { $scope.trustedSrc = ' '; }); }); $attrs.$observe('width', function (w) { $scope.width = w; }); $attrs.$observe('height', function (h) { $scope.height = h; }); //handle the use of both ng-href and href $attrs.$observe('href', function (url) { if (url === undefined || url === null) return; if (url.indexOf('photostream') > -1) { var splits = url.split('/'); splits = splits.slice(0, 6); url = splits.join('/'); } //build and trust the URL var untrustedSrc = url + '/in/explore-2014-10-02/player/'; $scope.trustedSrc = $sce.trustAsResourceUrl(untrustedSrc); }); } } }]); app.directive('embedItem', ["$sce", "$timeout", "embedDatacontext", function ($sce, $timeout, embedDatacontext) { "use strict"; return { restrict: "E", template: '
', scope: { height: "@", width: "@", embedType: '=', item: '=', maxwidth: '@' }, link: function ($scope, element, $attrs) { $attrs.$observe('maxwidth', function (w) { $scope.maxwidth = w; }); $scope.$on('resetembed', function () { $timeout(function() { $scope.embedHtml = ' '; }); }); $attrs.$observe('width', function (w) { $scope.width = w; }); $attrs.$observe('height', function (h) { $scope.height = h; }); function setEmbed(embedType, url, maxwidth) { $scope.item.generatingItem = true; embedDatacontext.getEmbed(url, maxwidth).then(function (data) { $scope.embedHtml = $sce.trustAsHtml(data.html); $scope.item.generatingItem = false; if (data.thumbnail_url) $scope.item.embedThumbnail = data.thumbnail_url; }); } //handle the use of both ng-href and href $attrs.$observe('href', function (url) { if (url === undefined) return; $scope.embedUrl = url; if ($scope.embedType === undefined) return; setEmbed($scope.embedType, url, $scope.maxwidth); }); } } }]); app.directive('embedEmbedly', ["$sce", "$timeout", "embedDatacontext", function ($sce, $timeout, embedDatacontext) { "use strict"; return { restrict: "E", template: ' ', scope: { height: "@", width: "@", embedType: '=', item: '=', maxwidth: '@' }, link: function ($scope, element, $attrs) { $attrs.$observe('maxwidth', function (w) { $scope.maxwidth = w; }); $scope.$on('resetembed', function () { $timeout(function () { $scope.embedHtml = ' '; }); }); $attrs.$observe('width', function (w) { $scope.width = w; }); $attrs.$observe('height', function (h) { $scope.height = h; }); function setEmbed(embedType, url, maxwidth) { $scope.item.generatingItem = true; embedDatacontext.getEmbed(url, maxwidth).then(function (data) { $scope.embedHtml = $sce.trustAsHtml(data.html); $scope.item.generatingItem = false; if (data.thumbnail_url) $scope.item.embedThumbnail = data.thumbnail_url; }); } //handle the use of both ng-href and href $attrs.$observe('href', function (url) { if (url === undefined) return; $scope.embedUrl = url; if ($scope.embedType === undefined) return; setEmbed($scope.embedType, url, $scope.maxwidth); }); } } }]); })();