(function () { "use strict"; // this will hopefully not be needed forever, but it is to iron out a bug in AngularJs related bad cached urls if the user manually changes state // as well as handling sites that support ui-routing and sites that don't angular.module('laz.videoLibrary') .provider('urlStateManager', ['$provide', function($provide) { var usesUiRouter = true; $provide.decorator("$browser", ["$delegate", function($delegate) { // Disable AngularJs Url Change Detection if not using client-side routing var originalOnUrlChange = $delegate.onUrlChange; var originalUrl = $delegate.url; $delegate.onUrlChange = function(){ if(usesUiRouter){ return originalOnUrlChange.apply($delegate, arguments); } }; $delegate.url = function() { if(usesUiRouter){ return originalUrl.apply($delegate, arguments); } return ""; }; return $delegate; }]); function urlStateManagerFactory($location, $window, $injector, $document){ var ON_POP = 'popstate'; return { push: pushState, replace: replaceState, onPop: onPopState }; function pushState(params, transitionTo){ if(usesUiRouter){ var $state = $injector.get('$state'); $state && $state.go(transitionTo ? transitionTo : '.', params); } else { $window.history.pushState(params, $document[0].title, transitionTo); } } function replaceState(params, transitionTo){ if(usesUiRouter){ var $state = $injector.get('$state'); $state && $state.go(transitionTo ? transitionTo : '.', params, { location: 'replace' }); } else { $window.history.replaceState(params, $document[0].title, transitionTo); } } function onPopState(callback){ if(typeof callback === 'function'){ var popStateCallback = function(e){ if(usesUiRouter){ var $stateParams = $injector.get('$stateParams'); callback($stateParams || {}); } else { callback(e.state || {}); } }; $window.addEventListener(ON_POP, popStateCallback); return function() { $window.removeEventListener(ON_POP, popStateCallback); }; } return function(){}; } } function useUiRouter(shouldUseUiRouter){ usesUiRouter = !!shouldUseUiRouter; } return { useUiRouter: useUiRouter, $get: ['$location', '$window', '$injector', '$document', urlStateManagerFactory ] }; }]); })();