angular.module("oxymoron.config.states", []) .config(['$locationProvider', '$stateProvider', '$urlRouterProvider', '$urlMatcherFactoryProvider', function ($locationProvider, $stateProvider, $urlRouterProvider, $urlMatcherFactoryProvider) { /* * Enable HTML5 History API */ $locationProvider.html5Mode(true).hashPrefix('!'); /* * $stateProvider Rails */ $urlRouterProvider.rule(function($injector, $location) { var path = $location.path(); var hasTrailingSlash = path[path.length-1] === '/'; //for later access in tempalteUrl callback $stateProvider.oxymoron_location = $location; if(hasTrailingSlash) { var newPath = path.substr(0, path.length - 1); return newPath; } }); var resolve = function (action, $stateParams) { return function (actionNames, callback) { try { var actionNames = angular.isArray(actionNames) ? actionNames : [actionNames]; if (actionNames.indexOf(action)!=-1) { callback($stateParams); } } catch (e) { console.error(e); } } } $stateProvider.rails = function () { $stateProvider <% @states.each do |key, value| %> .state('<%= key %>_path', { url: <%= value[:url] %>, <% if value[:params] %> params: <%= value[:params].to_json %>, <% end %> templateUrl: function(params) { params['ng-view']=''; <% unless value[:cache] %> params['v']=(new Date()).getTime(); <% end %> return Routes['<%= key %>_path'](params); }, controller: '<%= value[:controller]%>', resolve: { action: ['$stateParams', function ($stateParams) { return resolve('<%= value[:action]%>', $stateParams) }] } }) <% end %> return $stateProvider; } }]) .config(['$provide', function($provide) { $provide.decorator('$state', ['$delegate', function($delegate) { var state = $delegate; state.baseGo = state.go; var go = function(to, params, options) { options = options || {}; if (state.defaultParams) { var defaultParams = angular.copy(state.defaultParams); params = angular.extend(defaultParams, params); } options.inherit = false; state.baseGo(to, params, options); }; state.go = go; return $delegate; }]); } ])