From 171aaa1f6b210296051353c911c90542362f6943 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 18 Jun 2014 19:02:17 +0200 Subject: [PATCH] Implement navigation url service. --- app/coffee/app.coffee | 3 + app/coffee/modules/auth.coffee | 16 ++- app/coffee/modules/base/navurl.coffee | 78 +++++++++++++++ app/coffee/modules/common.coffee | 0 app/coffee/modules/navigation.coffee | 38 ++++++++ app/coffee/modules/resources.coffee | 123 +++++++++++++----------- app/coffee/utils.coffee | 5 + app/partials/views/modules/sprints.jade | 3 +- 8 files changed, 203 insertions(+), 63 deletions(-) create mode 100644 app/coffee/modules/base/navurl.coffee delete mode 100644 app/coffee/modules/common.coffee create mode 100644 app/coffee/modules/navigation.coffee diff --git a/app/coffee/app.coffee b/app/coffee/app.coffee index 4907cd66..0271385e 100644 --- a/app/coffee/app.coffee +++ b/app/coffee/app.coffee @@ -57,6 +57,9 @@ modules = [ "taigaBase", "taigaResources", "taigaLocales", + "taigaAuth", + + "taigaNavigation", # Specific Modules "taigaBacklog", diff --git a/app/coffee/modules/auth.coffee b/app/coffee/modules/auth.coffee index a4249769..0483af77 100644 --- a/app/coffee/modules/auth.coffee +++ b/app/coffee/modules/auth.coffee @@ -20,22 +20,28 @@ taiga = @.taiga class AuthService extends taiga.Service @.$inject = ["$rootScope", "$tgStorage", "$tgModel", "$tgHttp"] - constructor: (@rootScope, @storage, @model, @http) -> + constructor: (@rootscope, @storage, @model, @http) -> super() getUser: -> + if @rootscope.user + return @rootscope.user + userData = @storage.get("userInfo") if userData - return @model.make_model("users", userData) + user = @model.make_model("users", userData) + @rootscope.user = user + return user + return null setUser: (user) -> - @rootScope.auth = user - @rootScope.$broadcast("i18n:change", user.default_language) + @rootscope.auth = user + @rootscope.$broadcast("i18n:change", user.default_language) @storage.set("userInfo", user.getAttrs()) clear: -> - @rootScope.auth = null + @rootscope.auth = null @storage.remove("userInfo") setToken: (token) -> diff --git a/app/coffee/modules/base/navurl.coffee b/app/coffee/modules/base/navurl.coffee new file mode 100644 index 00000000..d2c9f8c6 --- /dev/null +++ b/app/coffee/modules/base/navurl.coffee @@ -0,0 +1,78 @@ +### +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino Garcia +# Copyright (C) 2014 David Barragán Merino +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# File: modules/base/navurl.coffee +### + + +taiga = @.taiga +trim = @.taiga.trim + +parseNav = (data, scope) -> + options = {} + + [name, params] = _.map(data.split(":"), trim) + params = _.map(params.split(","), trim) + + for item in params + [key, value] = _.map(item.split("="), trim) + options[key] = scope.$eval(value) + + return [name, options] + +formatUrl = (url, ctx={}) -> + replacer = (match) -> + match = trim(match, ":") + return ctx[match] or "undefined" + return url.replace(/(:\w+)/g, replacer) + + +class NavigationUrlsService extends taiga.Service + constructor: -> + @.urls = {} + + update: (urls) -> + @.urls = _.merge({}, @.urls, urls or {}) + + resolve: (name) -> + return @.urls[name] + + +NavigationUrlsDirective = ($navurls, $auth) -> + # Example: + # link(tg-nav="project-backlog:project='sss',") + + link = ($scope, $el, $attrs) -> + [name, options] = parseNav($attrs.tgNav, $scope) + + user = $auth.getUser() + options.user = user.username if user + + url = $navurls.resolve(name) + fullUrl = formatUrl(url, options) + + $el.attr("href", fullUrl) + + return {link: link} + + +module = angular.module("taigaBase") +module.service("$tgNavUrls", NavigationUrlsService) +module.directive("tgNav", ["$tgNavUrls", "$tgAuth", NavigationUrlsDirective]) + + diff --git a/app/coffee/modules/common.coffee b/app/coffee/modules/common.coffee deleted file mode 100644 index e69de29b..00000000 diff --git a/app/coffee/modules/navigation.coffee b/app/coffee/modules/navigation.coffee new file mode 100644 index 00000000..b66c456f --- /dev/null +++ b/app/coffee/modules/navigation.coffee @@ -0,0 +1,38 @@ +### +# Copyright (C) 2014 Andrey Antukh +# Copyright (C) 2014 Jesús Espino Garcia +# Copyright (C) 2014 David Barragán Merino +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +# File: modules/navigation.coffee +### + +# Simple module that uses navurls service and register +# navigation urls for taiga. + +urls = { + "home": "/", + "profile": "/:user", + "project": "/:user/:project", + "project-backlog": "/:user/:project/backlog", + "project-taskboard": "/:user/:project/taskboard/:sprint", +} + +init = ($log, $navurls) -> + $log.debug "Initialize navigation urls" + $navurls.update(urls) + +module = angular.module("taigaNavigation", ["taigaBase"]) +module.run(["$log", "$tgNavUrls", init]) diff --git a/app/coffee/modules/resources.coffee b/app/coffee/modules/resources.coffee index 9a979aa7..532a6691 100644 --- a/app/coffee/modules/resources.coffee +++ b/app/coffee/modules/resources.coffee @@ -21,66 +21,75 @@ taiga = @.taiga + + + + class ResourcesService extends taiga.Service -initUrls = (urls) -> - urls.update({ - "auth": "/api/v1/auth" - "auth-register": "/api/v1/auth/register" - "permissions": "/api/v1/permissions" - "roles": "/api/v1/roles" - "projects": "/api/v1/projects" - "memberships": "/api/v1/memberships" - "milestones": "/api/v1/milestones" - "userstories": "/api/v1/userstories" - "bulk-create-us": "/api/v1/userstories/bulk_create" - "bulk-update-us-order": "/api/v1/userstories/bulk_update_order" - "userstories-restore": "/api/v1/userstories/%s/restore" - "tasks": "/api/v1/tasks" - "bulk-create-tasks": "/api/v1/tasks/bulk_create" - "tasks-restore": "/api/v1/tasks/%s/restore" - "issues": "/api/v1/issues" - "issues-restore": "/api/v1/issues/%s/restore" - "wiki": "/api/v1/wiki" - "wiki-restore": "/api/v1/wiki/%s/restore" - "choices/userstory-statuses": "/api/v1/userstory-statuses" - "choices/userstory-statuses/bulk-update-order": "/api/v1/userstory-statuses/bulk_update_order" - "choices/points": "/api/v1/points" - "choices/points/bulk-update-order": "/api/v1/points/bulk_update_order" - "choices/task-statuses": "/api/v1/task-statuses" - "choices/task-statuses/bulk-update-order": "/api/v1/task-statuses/bulk_update_order" - "choices/issue-statuses": "/api/v1/issue-statuses" - "choices/issue-statuses/bulk-update-order": "/api/v1/issue-statuses/bulk_update_order" - "choices/issue-types": "/api/v1/issue-types" - "choices/issue-types/bulk-update-order": "/api/v1/issue-types/bulk_update_order" - "choices/priorities": "/api/v1/priorities" - "choices/priorities/bulk-update-order": "/api/v1/priorities/bulk_update_order" - "choices/severities": "/api/v1/severities" - "choices/severities/bulk-update-order": "/api/v1/severities/bulk_update_order" - "search": "/api/v1/search" - "sites": "/api/v1/sites" - "project-templates": "/api/v1/project-templates" - "site-members": "/api/v1/site-members" - "site-projects": "/api/v1/site-projects" - "users": "/api/v1/users" - "users-password-recovery": "/api/v1/users/password_recovery" - "users-change-password-from-recovery": "/api/v1/users/change_password_from_recovery" - "users-change-password": "/api/v1/users/change_password" - "resolver": "/api/v1/resolver" - "wiki-attachment": "/media/attachment-files/%s/wikipage/%s" - # History - "history/userstory": "/api/v1/history/userstory" - "history/issue": "/api/v1/history/issue" - "history/task": "/api/v1/history/task" - "history/wiki": "/api/v1/history/wiki" +urls = { + "auth": "/api/v1/auth" + "auth-register": "/api/v1/auth/register" + "permissions": "/api/v1/permissions" + "roles": "/api/v1/roles" + "projects": "/api/v1/projects" + "memberships": "/api/v1/memberships" + "milestones": "/api/v1/milestones" + "userstories": "/api/v1/userstories" + "bulk-create-us": "/api/v1/userstories/bulk_create" + "bulk-update-us-order": "/api/v1/userstories/bulk_update_order" + "userstories-restore": "/api/v1/userstories/%s/restore" + "tasks": "/api/v1/tasks" + "bulk-create-tasks": "/api/v1/tasks/bulk_create" + "tasks-restore": "/api/v1/tasks/%s/restore" + "issues": "/api/v1/issues" + "issues-restore": "/api/v1/issues/%s/restore" + "wiki": "/api/v1/wiki" + "wiki-restore": "/api/v1/wiki/%s/restore" + "choices/userstory-statuses": "/api/v1/userstory-statuses" + "choices/userstory-statuses/bulk-update-order": "/api/v1/userstory-statuses/bulk_update_order" + "choices/points": "/api/v1/points" + "choices/points/bulk-update-order": "/api/v1/points/bulk_update_order" + "choices/task-statuses": "/api/v1/task-statuses" + "choices/task-statuses/bulk-update-order": "/api/v1/task-statuses/bulk_update_order" + "choices/issue-statuses": "/api/v1/issue-statuses" + "choices/issue-statuses/bulk-update-order": "/api/v1/issue-statuses/bulk_update_order" + "choices/issue-types": "/api/v1/issue-types" + "choices/issue-types/bulk-update-order": "/api/v1/issue-types/bulk_update_order" + "choices/priorities": "/api/v1/priorities" + "choices/priorities/bulk-update-order": "/api/v1/priorities/bulk_update_order" + "choices/severities": "/api/v1/severities" + "choices/severities/bulk-update-order": "/api/v1/severities/bulk_update_order" + "search": "/api/v1/search" + "sites": "/api/v1/sites" + "project-templates": "/api/v1/project-templates" + "site-members": "/api/v1/site-members" + "site-projects": "/api/v1/site-projects" + "users": "/api/v1/users" + "users-password-recovery": "/api/v1/users/password_recovery" + "users-change-password-from-recovery": "/api/v1/users/change_password_from_recovery" + "users-change-password": "/api/v1/users/change_password" + "resolver": "/api/v1/resolver" + "wiki-attachment": "/media/attachment-files/%s/wikipage/%s" - # Attachments - "userstories/attachments": "/api/v1/userstories/attachments" - "issues/attachments": "/api/v1/issues/attachments" - "tasks/attachments": "/api/v1/tasks/attachments" - "wiki/attachments": "/api/v1/wiki/attachments" - }) + # History + "history/userstory": "/api/v1/history/userstory" + "history/issue": "/api/v1/history/issue" + "history/task": "/api/v1/history/task" + "history/wiki": "/api/v1/history/wiki" + + # Attachments + "userstories/attachments": "/api/v1/userstories/attachments" + "issues/attachments": "/api/v1/issues/attachments" + "tasks/attachments": "/api/v1/tasks/attachments" + "wiki/attachments": "/api/v1/wiki/attachments" +} + +# Initialize api urls service +initUrls = ($log, $urls) -> + $log.debug "Initialize api urls" + $urls.update(urls) # Initialize resources service populating it with methods # defined in separated files. @@ -95,7 +104,7 @@ module = angular.module("taigaResources", ["taigaBase"]) module.service("$tgResources", ResourcesService) # Module entry point -module.run(["$tgUrls", initUrls]) +module.run(["$log", "$tgUrls", initUrls]) module.run([ "$log", "$tgResources", diff --git a/app/coffee/utils.coffee b/app/coffee/utils.coffee index a4710cdf..6593271e 100644 --- a/app/coffee/utils.coffee +++ b/app/coffee/utils.coffee @@ -40,6 +40,11 @@ mixOf = (base, mixins...) -> Mixed +trim = (data, char) -> + return _.str.trim(data, char) + + taiga = @.taiga taiga.bindOnce = bindOnce taiga.mixOf = mixOf +taiga.trim = trim diff --git a/app/partials/views/modules/sprints.jade b/app/partials/views/modules/sprints.jade index 7367ef35..230349cd 100644 --- a/app/partials/views/modules/sprints.jade +++ b/app/partials/views/modules/sprints.jade @@ -32,7 +32,8 @@ section.sprints span(tg-bo-ref="us.ref") span(tg-bo-html="us.subject") div.column-points.width-1(tg-bo-html="us.total_points") - a.button.button-gray(href="", title="Current Sprint Taskboard") + a.button.button-gray(href="", tg-nav="project-taskboard:project=projectId,sprint=sprint.id", + title="Current Sprint Taskboard") span Sprint Taskboard // If is current sprint