diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 1c46efa..8e2d9bb 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -28,6 +28,7 @@ module.exports = (grunt) -> 'bower_components/angular-sanitize/angular-sanitize.js' 'bower_components/marked/lib/marked.js' 'scripts/app.js' + 'scripts/services/route.js' 'scripts/services/fetch.js' 'scripts/services/poems.js' 'scripts/services/reader/_factory.js' diff --git a/source/scripts/controllers.js.coffee b/source/scripts/controllers.js.coffee index b9e4d7f..ee4af28 100644 --- a/source/scripts/controllers.js.coffee +++ b/source/scripts/controllers.js.coffee @@ -1,40 +1,19 @@ angular.module("sideBySide.controllers", []) .controller "AppController", [ - '$location', '$scope', 'poems' - ($location, $scope, poems) -> - urlParams = $location.url() - .split('/') - .filter((item) -> item) - .map (item) -> - item.split(':') - .reduce((prev, current) -> - prev[current[0]] = current[1] - prev - , {}) - - if 'base' of urlParams - urlParams.base = urlParams.base.replace(/\./g, '/') - else - urlParams.base = '' - - appUrl = $location.absUrl() - .substring(0, $location.absUrl().length - $location.url().length) - - poems.load appUrl + '/' + urlParams.base + () -> null ] - .controller "ComparisonController", [ - '$routeParams', '$scope', '$location', '$anchorScroll', 'poems', 'transformer' - ($routeParams, $scope, $location, $anchorScroll, poems, transformer) -> + '$scope', '$anchorScroll', 'route', 'poems', 'transformer' + ($scope, $anchorScroll, route, poems, transformer) -> min = 1 max = 5 scroll = () -> setTimeout () -> - if ('section' of $routeParams) - section = 'section-' + $routeParams['section'] + if ('section' of route.params) + section = 'section-' + route.params['section'] element = document.getElementById(section) - element.scrollIntoView() + element.scrollIntoView() if element , 100 $scope.$watchCollection () -> @@ -64,4 +43,6 @@ angular.module("sideBySide.controllers", []) $scope.pick = not $scope.pick $scope.pick = false + + poems.load route.appUrl + '/' + route.params.base ] diff --git a/source/scripts/services/route.js.coffee b/source/scripts/services/route.js.coffee new file mode 100644 index 0000000..7dad13a --- /dev/null +++ b/source/scripts/services/route.js.coffee @@ -0,0 +1,17 @@ +angular.module("sideBySide").service "route", ['$location', ($location) -> + @params = $location.url() + .split('/') + .filter((item) -> item) + .map (item) -> + item.split(':') + .reduce((params, param) -> + params[param[0]] = param[1] + params + , {}) + + @params.base = '' if 'base' not of @params + @params.base = @params.base.replace(/\./g, '/') + + @appUrl = $location.absUrl() + .substring(0, $location.absUrl().length - $location.url().length) +]