From 6c4a600270d4e610f734db3217157fc2214e9494 Mon Sep 17 00:00:00 2001 From: Vit Brunner Date: Tue, 11 Nov 2014 09:09:01 +0100 Subject: [PATCH] Almost configurable base --- Gruntfile.coffee | 25 ++++++++++++++++-- package.json | 1 + .../comparison.html.jade | 0 source/index.html.jade | 10 ++++--- source/scripts/app.js.coffee | 7 ++--- source/scripts/controllers.js.coffee | 26 +++++++++++-------- source/scripts/services/fetch.js.coffee | 7 ++--- source/scripts/services/poems.js.coffee | 4 +-- source/tests/the_raven/config.json | 14 +++++----- 9 files changed, 60 insertions(+), 34 deletions(-) rename source/{partials => includes}/comparison.html.jade (100%) diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 8d25f66..1c46efa 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -1,4 +1,23 @@ _ = require('lodash'); +modRewrite = require('connect-modrewrite'); +middleware = (connect, options) -> + suffixes = ("\\." + suffix for suffix in [ + 'html' + 'js' + 'css' + 'md' + 'json' + 'eot' + 'svg' + 'ttf' + 'woff' + 'otf' + ]).join('|') + + [ + modRewrite(["!" + suffixes + "$ /index.html [L]"]) + connect.static(options.base[0]) + ] module.exports = (grunt) -> vars = { @@ -93,12 +112,14 @@ module.exports = (grunt) -> main: { options: { base: 'build' + middleware: middleware } } min: { options: { port: 8001 base: 'build-min' + middleware: middleware } } } @@ -137,7 +158,7 @@ module.exports = (grunt) -> main: { expand: true cwd: 'source' - src: '**/*.jade' + src: [ '*.jade', 'partials/**/*.jade' ] dest: 'build' ext: '.html' options: { @@ -148,7 +169,7 @@ module.exports = (grunt) -> min: { expand: true cwd: 'source' - src: '**/*.jade' + src: [ '*.jade', 'partials/**/*.jade' ] dest: 'build-min' ext: '.html' options: { diff --git a/package.json b/package.json index 5d8fe48..a364562 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "description": "Visual comparison of different translations of itemized texts; e.g. poems, bibles, etc.", "devDependencies": { "bower": "1.3.x", + "connect-modrewrite": "0.7.x", "grunt": "0.4.x", "grunt-cli": "0.1.x", "grunt-contrib-coffee": "0.12.x", diff --git a/source/partials/comparison.html.jade b/source/includes/comparison.html.jade similarity index 100% rename from source/partials/comparison.html.jade rename to source/includes/comparison.html.jade diff --git a/source/index.html.jade b/source/index.html.jade index bdeb549..829db9e 100644 --- a/source/index.html.jade +++ b/source/index.html.jade @@ -1,6 +1,7 @@ doctype html html(ng-app="sideBySide") head + base(href='/') title Side By Side viewer meta(charset="utf-8") link(rel="stylesheet", type="text/css", href="//fonts.googleapis.com/css?family=Noticia+Text:400,700,400italic,700italic") @@ -21,10 +22,11 @@ html(ng-app="sideBySide") .pure-menu.pure-menu-open.pure-menu-horizontal.menu-main ul li - a(href="#{{base}}") Home + a(href="") Home li - a(href="#{{base}}/help") Help + a(href="help") Help li - a(href="#{{base}}/about") About + a(href="about") About - div(ng-view class="cols-{{columns}}") + div(ng-controller="ComparisonController" class="cols-{{columns}}") + include includes/comparison.html.jade diff --git a/source/scripts/app.js.coffee b/source/scripts/app.js.coffee index 980eac8..1bf966d 100644 --- a/source/scripts/app.js.coffee +++ b/source/scripts/app.js.coffee @@ -1,7 +1,4 @@ angular.module('sideBySide', ['sideBySide.controllers', 'ngRoute', 'ngSanitize']) - .config [ '$routeProvider', ($routeProvider) -> - $routeProvider.when '/:base?/:section?', { - templateUrl: 'partials/comparison.html' - controller: 'ComparisonController' - } + .config [ '$locationProvider', ($locationProvider) -> + $locationProvider.html5Mode true ] diff --git a/source/scripts/controllers.js.coffee b/source/scripts/controllers.js.coffee index e103ef5..b9e4d7f 100644 --- a/source/scripts/controllers.js.coffee +++ b/source/scripts/controllers.js.coffee @@ -2,21 +2,25 @@ angular.module("sideBySide.controllers", []) .controller "AppController", [ '$location', '$scope', 'poems' ($location, $scope, poems) -> - $scope.$on "$routeChangeSuccess", ($currentRoute, $previousRoute) -> - $scope.base = if $previousRoute.params.base \ - then '/' + $previousRoute.params.base - else '' + 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) - .replace(/[^\/]*?#$/, '') - base = appUrl + $location.url() - .slice(1) - .replace(/(.*)\/.*/, '$1') - .replace(/\./g, '/') - - poems.load base + poems.load appUrl + '/' + urlParams.base ] .controller "ComparisonController", [ diff --git a/source/scripts/services/fetch.js.coffee b/source/scripts/services/fetch.js.coffee index 4ddb3dd..885b051 100644 --- a/source/scripts/services/fetch.js.coffee +++ b/source/scripts/services/fetch.js.coffee @@ -1,12 +1,13 @@ angular.module("sideBySide").factory "fetch", ['$http', '$q', 'readerFactory', ($http, $q, readerFactory) -> # Fetch # - # @param file [String] Path to config file + # @param base [String] Path to config file directory # @return [Object] Promises and heading - (file = "/config.json") -> + (base) -> + file = base + '/config.json' $http.get(file).then (config) -> promises = config.data.files.map (poem) -> - $http.get(poem).then (response) -> + $http.get(base + '/' + poem).then (response) -> readerFactory(poem) response.data { diff --git a/source/scripts/services/poems.js.coffee b/source/scripts/services/poems.js.coffee index 5c88f8c..a67a031 100644 --- a/source/scripts/services/poems.js.coffee +++ b/source/scripts/services/poems.js.coffee @@ -32,8 +32,8 @@ angular.module("sideBySide").service "poems", ['fetch', (fetch) -> # Load poems # # @param base [String] - @load = (base = '') => - fetch(base + "/config.json").then (config) => + @load = (base) => + fetch(base).then (config) => config.fetchPoems.then (poems) => @all = (activize(poem, config.active) for poem in poems) @heading = config.heading diff --git a/source/tests/the_raven/config.json b/source/tests/the_raven/config.json index 4363b36..c974384 100644 --- a/source/tests/the_raven/config.json +++ b/source/tests/the_raven/config.json @@ -1,12 +1,12 @@ { "files": - [ "tests/the_raven/1845-poe.md" - , "tests/the_raven/1881-vrchlicky.md" - , "tests/the_raven/1885-muzik.md" - , "tests/the_raven/1918-dostal-lutinov.md" - , "tests/the_raven/1900-zenon-przesmycki.md" - , "tests/the_raven/1910-barbara-beaupre.md" - , "tests/the_raven/1970-baranczak.md" + [ "1845-poe.md" + , "1881-vrchlicky.md" + , "1885-muzik.md" + , "1918-dostal-lutinov.md" + , "1900-zenon-przesmycki.md" + , "1910-barbara-beaupre.md" + , "1970-baranczak.md" ], "active": { "Language": [ "Czech" ]