diff --git a/app/coffee/app.coffee b/app/coffee/app.coffee index d9316682..63ea2edf 100644 --- a/app/coffee/app.coffee +++ b/app/coffee/app.coffee @@ -39,10 +39,31 @@ taiga.sessionId = taiga.generateUniqueSessionIdentifier() configure = ($routeProvider, $locationProvider, $httpProvider, $provide, $tgEventsProvider, tgLoaderProvider, $compileProvider, $translateProvider) -> $routeProvider.when("/", - {templateUrl: "home/home-page.html", resolve: {loader: tgLoaderProvider.add()}}) + { + templateUrl: "home/home-page.html", + resolve: { + loader: tgLoaderProvider.add(true), + pageParams: -> { + "title": "PROJECT.WELCOME" + "authRequired": true + } + } + } + ) $routeProvider.when("/projects/", - {templateUrl: "projects/projects-page.html", resolve: {loader: tgLoaderProvider.add(true)}}) + { + templateUrl: "projects/projects-page.html", + resolve: { + loader: tgLoaderProvider.add(true), + pageParams: -> { + "title": "PROJECT.SECTION_PROJECTS" + "authRequired": true + } + }, + controller: "Page" + } + ) $routeProvider.when("/project/:pslug/", {templateUrl: "project/project.html"}) @@ -362,6 +383,7 @@ modules = [ "taigaComponents", "taigaProfile", "taigaHome", + "taigaPage", # template cache "templates", diff --git a/app/modules/home/home-page.jade b/app/modules/home/home-page.jade index 7bfe3e17..9e2a5c4a 100644 --- a/app/modules/home/home-page.jade +++ b/app/modules/home/home-page.jade @@ -1 +1 @@ -div(ng-controller="HomePage", tg-home) +div(tg-home) diff --git a/app/modules/page/page.controller.coffee b/app/modules/page/page.controller.coffee new file mode 100644 index 00000000..5a317157 --- /dev/null +++ b/app/modules/page/page.controller.coffee @@ -0,0 +1,18 @@ +class PageController extends taiga.Controller + @.$inject = [ + "$tgAuth", + "$appTitle", + "$translate", + "$tgLocation", + "$tgNavUrls", + "pageParams" + ] + + constructor: (@auth, @appTitle, @translate, @location, @navUrls, @pageParams) -> + if @pageParams.authRequired && !@auth.isAuthenticated() + @location.path(@navUrls.resolve("login")) + + if @pageParams.title + @translate(@pageParams.title).then (text) => @appTitle.set(text) + +angular.module("taigaPage").controller("Page", PageController) diff --git a/app/modules/page/page.controller.spec.coffee b/app/modules/page/page.controller.spec.coffee new file mode 100644 index 00000000..8163c668 --- /dev/null +++ b/app/modules/page/page.controller.spec.coffee @@ -0,0 +1,123 @@ +describe "PageController", -> + pageCtrl = null + provide = null + controller = null + mocks = {} + + _mockPageParams = () -> + mocks.pageParams = {} + + provide.value "pageParams", mocks.pageParams + + _mockAuth = () -> + mocks.auth = { + isAuthenticated: sinon.stub() + } + + provide.value "$tgAuth", mocks.auth + + _mockAppTitle = () -> + mocks.appTitle = { + set: sinon.spy() + } + + provide.value "$appTitle", mocks.appTitle + + _mockLocation = () -> + mocks.location = { + path: sinon.spy() + } + + provide.value "$tgLocation", mocks.location + + _mockNavUrls = () -> + mocks.navUrls = { + resolve: sinon.stub() + } + + provide.value "$tgNavUrls", mocks.navUrls + + _mockTranslate = () -> + mocks.translate = sinon.stub() + + provide.value "$translate", mocks.translate + + _mocks = () -> + module ($provide) -> + provide = $provide + _mockAppTitle() + _mockPageParams() + _mockAuth() + _mockLocation() + _mockNavUrls() + _mockTranslate() + + return null + + beforeEach -> + module "taigaPage" + + _mocks() + + inject ($controller) -> + controller = $controller + + describe "auth", () -> + it "if auth is required and the user is not logged redirect to login page", () -> + locationPath = "location-path" + + mocks.pageParams.authRequired = true + mocks.auth.isAuthenticated.returns(false) + mocks.navUrls.resolve.withArgs("login").returns(locationPath) + + pageCtrl = controller "Page", + $scope: {} + + expect(mocks.location.path.withArgs(locationPath)).have.been.calledOnce + + it "if auth is not required no redirect to login page", () -> + locationPath = "location-path" + + mocks.pageParams.authRequired = false + mocks.auth.isAuthenticated.returns(false) + mocks.navUrls.resolve.withArgs("login").returns(locationPath) + + pageCtrl = controller "Page", + $scope: {} + + expect(mocks.location.path).have.callCount(0) + + it "if auth is required and the user is logged no redirect", () -> + locationPath = "location-path" + + mocks.pageParams.authRequired = true + mocks.auth.isAuthenticated.returns(true) + mocks.navUrls.resolve.withArgs("login").returns(locationPath) + + pageCtrl = controller "Page", + $scope: {} + + expect(mocks.location.path).have.callCount(0) + + describe "page title", () -> + it "if title is defined set it", () -> + thenStub = sinon.stub() + + mocks.pageParams.title = "TITLE" + mocks.translate.withArgs("TITLE").returns({ + then: thenStub + }) + + pageCtrl = controller "Page", + $scope: {} + + thenStub.callArg(0, "TITLE") + + expect(mocks.appTitle.set.withArgs("TITLE")).have.been.calledOnce + + it "if title is not defined not call appTitle", () -> + pageCtrl = controller "Page", + $scope: {} + + expect(mocks.translate).have.callCount(0) + expect(mocks.appTitle.set.withArgs("TITLE")).have.callCount(0) diff --git a/app/modules/page/page.module.coffee b/app/modules/page/page.module.coffee new file mode 100644 index 00000000..82f679b0 --- /dev/null +++ b/app/modules/page/page.module.coffee @@ -0,0 +1 @@ +module = angular.module("taigaPage", []) diff --git a/app/modules/projects/projects-page.controller.coffee b/app/modules/projects/projects-page.controller.coffee deleted file mode 100644 index 5e09e837..00000000 --- a/app/modules/projects/projects-page.controller.coffee +++ /dev/null @@ -1,26 +0,0 @@ -class ProjectsPageController extends taiga.Controller - @.$inject = [ - "$scope", - "$q", - "$tgResources", - "$rootScope", - "$tgNavUrls", - "$tgAuth", - "$tgLocation", - "$appTitle", - "$projectUrl", - "$tgConfig", - "tgLoader", - "tgProjectsService", - "$translate" - ] - - constructor: (@scope, @q, @rs, @rootscope, @navUrls, @auth, @location, - @appTitle, @projectUrl, @config, tgLoader, @projectsService, @translate) -> - - if !@auth.isAuthenticated() - @location.path(@navUrls.resolve("login")) - - @appTitle.set(@translate.instant("PROJECT.SECTION_PROJECTS")) - -angular.module("taigaProjects").controller("ProjectsPage", ProjectsPageController) diff --git a/app/modules/projects/projects-page.jade b/app/modules/projects/projects-page.jade index 656c15cf..21f045f0 100644 --- a/app/modules/projects/projects-page.jade +++ b/app/modules/projects/projects-page.jade @@ -1 +1 @@ -div(ng-controller="ProjectsPage", tg-projects-listing) +div(tg-projects-listing)