Refactoring services
parent
e3f4a6344e
commit
498d69032f
|
@ -12,18 +12,6 @@ DutyDirective = (navurls, projectsService, $translate) ->
|
||||||
if scope.vm.duty._name == "issues"
|
if scope.vm.duty._name == "issues"
|
||||||
return $translate.instant("COMMON.ISSUE")
|
return $translate.instant("COMMON.ISSUE")
|
||||||
|
|
||||||
scope.vm.getUrl = () ->
|
|
||||||
if scope.vm.duty
|
|
||||||
ctx = {
|
|
||||||
project: projectsService.projectsById.get(String(scope.vm.duty.project)).slug
|
|
||||||
ref: scope.vm.duty.ref
|
|
||||||
}
|
|
||||||
return navurls.resolve("project-#{scope.vm.duty._name}-detail", ctx)
|
|
||||||
|
|
||||||
scope.vm.getProjectName = () ->
|
|
||||||
if scope.vm.duty
|
|
||||||
return projectsService.projectsById.get(String(scope.vm.duty.project)).name
|
|
||||||
|
|
||||||
directive = {
|
directive = {
|
||||||
templateUrl: "home/duties/duty.html"
|
templateUrl: "home/duties/duty.html"
|
||||||
scope: {
|
scope: {
|
||||||
|
|
|
@ -73,5 +73,3 @@ describe "dutyDirective", () ->
|
||||||
elm = createDirective()
|
elm = createDirective()
|
||||||
scope.$apply()
|
scope.$apply()
|
||||||
expect(elm.isolateScope().vm.getDutyType()).to.be.equal("User story translated")
|
expect(elm.isolateScope().vm.getDutyType()).to.be.equal("User story translated")
|
||||||
expect(elm.isolateScope().vm.getUrl()).to.be.equal("http://jstesting.taiga.io")
|
|
||||||
expect(elm.isolateScope().vm.getProjectName()).to.be.equal("testing js project")
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
a(href="{{ ::vm.getUrl() }}", title="{{ ::duty.subject }}")
|
a(href="{{ ::vm.duty.url }}", title="{{ ::duty.subject }}")
|
||||||
img.avatar(
|
img.avatar(
|
||||||
src="{{ ::vm.duty.assigned_to_extra_info.photo }}"
|
src="{{ ::vm.duty.assigned_to_extra_info.photo }}"
|
||||||
title="{{ ::vm.duty.assigned_to_extra_info.full_name_display }}")
|
title="{{ ::vm.duty.assigned_to_extra_info.full_name_display }}")
|
||||||
|
@ -10,4 +10,4 @@ a(href="{{ ::vm.getUrl() }}", title="{{ ::duty.subject }}")
|
||||||
span.duty-title
|
span.duty-title
|
||||||
span.duty-id(tg-bo-ref="duty.ref")
|
span.duty-id(tg-bo-ref="duty.ref")
|
||||||
span.duty-name {{ ::duty.subject }}
|
span.duty-name {{ ::duty.subject }}
|
||||||
div.duty-project {{ ::vm.getProjectName()}}
|
div.duty-project {{ ::vm.duty.projectName}}
|
||||||
|
|
|
@ -1,18 +1,25 @@
|
||||||
HomeDirective = (homeService) ->
|
HomeDirective = ($q, homeService, projectsService) ->
|
||||||
link = (scope, el, attrs, ctrl) ->
|
link = (scope, el, attrs, ctrl) ->
|
||||||
scope.vm = {}
|
scope.vm = {}
|
||||||
taiga.defineImmutableProperty(scope.vm, "workInProgress", () -> homeService.workInProgress)
|
|
||||||
|
|
||||||
scope.$watch "vm.workInProgress", (workInProgress) ->
|
projectsPromise = projectsService.getCurrentUserProjects()
|
||||||
if workInProgress.size > 0
|
workInProgresPromise = homeService.getWorkInProgress()
|
||||||
userStories = workInProgress.get("assignedTo").get("userStories")
|
|
||||||
tasks = workInProgress.get("assignedTo").get("tasks")
|
$q.all([projectsPromise, workInProgresPromise]).then ->
|
||||||
issues = workInProgress.get("assignedTo").get("issues")
|
homeService.attachProjectInfoToWorkInProgress(projectsService.currentUserProjectsById)
|
||||||
|
|
||||||
|
taiga.defineImmutableProperty(scope.vm, "projects", () -> projectsService.currentUserProjects)
|
||||||
|
taiga.defineImmutableProperty(scope.vm, "workInProgress", () -> homeService.workInProgress)
|
||||||
|
|
||||||
|
if scope.vm.workInProgress.size > 0
|
||||||
|
userStories = scope.vm.workInProgress.get("assignedTo").get("userStories")
|
||||||
|
tasks = scope.vm.workInProgress.get("assignedTo").get("tasks")
|
||||||
|
issues = scope.vm.workInProgress.get("assignedTo").get("issues")
|
||||||
scope.vm.assignedTo = userStories.concat(tasks).concat(issues)
|
scope.vm.assignedTo = userStories.concat(tasks).concat(issues)
|
||||||
|
|
||||||
userStories = workInProgress.get("watching").get("userStories")
|
userStories = scope.vm.workInProgress.get("watching").get("userStories")
|
||||||
tasks = workInProgress.get("watching").get("tasks")
|
tasks = scope.vm.workInProgress.get("watching").get("tasks")
|
||||||
issues = workInProgress.get("watching").get("issues")
|
issues = scope.vm.workInProgress.get("watching").get("issues")
|
||||||
scope.vm.watching = userStories.concat(tasks).concat(issues)
|
scope.vm.watching = userStories.concat(tasks).concat(issues)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -22,7 +29,9 @@ HomeDirective = (homeService) ->
|
||||||
}
|
}
|
||||||
|
|
||||||
HomeDirective.$inject = [
|
HomeDirective.$inject = [
|
||||||
"tgHomeService"
|
"$q",
|
||||||
|
"tgHomeService",
|
||||||
|
"tgProjectsService"
|
||||||
]
|
]
|
||||||
|
|
||||||
angular.module("taigaHome").directive("tgHome", HomeDirective)
|
angular.module("taigaHome").directive("tgHome", HomeDirective)
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
describe "homeDirective", () ->
|
describe "homeDirective", () ->
|
||||||
scope = compile = provide = null
|
scope = compile = provide = timeout = null
|
||||||
|
mockTgHomeService = mockTgProjectsService = null
|
||||||
|
thenStubGetCurrentUserProjectsById = thenStubGetWorkInProgress = null
|
||||||
template = "<div tg-home></div>"
|
template = "<div tg-home></div>"
|
||||||
|
|
||||||
createDirective = () ->
|
createDirective = () ->
|
||||||
|
@ -7,7 +9,10 @@ describe "homeDirective", () ->
|
||||||
return elm
|
return elm
|
||||||
|
|
||||||
_mockTgHomeService = () ->
|
_mockTgHomeService = () ->
|
||||||
|
thenStubGetWorkInProgress = sinon.stub()
|
||||||
|
|
||||||
mockTgHomeService = {
|
mockTgHomeService = {
|
||||||
|
getWorkInProgress: sinon.stub()
|
||||||
workInProgress: Immutable.fromJS({
|
workInProgress: Immutable.fromJS({
|
||||||
assignedTo: {
|
assignedTo: {
|
||||||
userStories: [{"id": 1}]
|
userStories: [{"id": 1}]
|
||||||
|
@ -20,8 +25,12 @@ describe "homeDirective", () ->
|
||||||
issues: [{"id": 6}]
|
issues: [{"id": 6}]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
attachProjectInfoToWorkInProgress: sinon.stub()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mockTgHomeService.getWorkInProgress.returns({
|
||||||
|
then: thenStubGetWorkInProgress
|
||||||
|
})
|
||||||
provide.value "tgHomeService", mockTgHomeService
|
provide.value "tgHomeService", mockTgHomeService
|
||||||
|
|
||||||
_mockTranslateFilter = () ->
|
_mockTranslateFilter = () ->
|
||||||
|
@ -35,6 +44,20 @@ describe "homeDirective", () ->
|
||||||
_mockHomeProjectList = () ->
|
_mockHomeProjectList = () ->
|
||||||
provide.factory 'tgHomeProjectListDirective', () -> {}
|
provide.factory 'tgHomeProjectListDirective', () -> {}
|
||||||
|
|
||||||
|
_mockTgProjectsService = () ->
|
||||||
|
thenStubGetCurrentUserProjectsById = sinon.stub()
|
||||||
|
mockTgProjectsService = {
|
||||||
|
getCurrentUserProjects: sinon.stub()
|
||||||
|
currentUserProjectsById: {
|
||||||
|
get: sinon.stub()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mockTgProjectsService.getCurrentUserProjects.returns({
|
||||||
|
then: thenStubGetCurrentUserProjectsById
|
||||||
|
})
|
||||||
|
provide.value "tgProjectsService", mockTgProjectsService
|
||||||
|
|
||||||
_mocks = () ->
|
_mocks = () ->
|
||||||
module ($provide) ->
|
module ($provide) ->
|
||||||
provide = $provide
|
provide = $provide
|
||||||
|
@ -42,6 +65,7 @@ describe "homeDirective", () ->
|
||||||
_mockHomeProjectList()
|
_mockHomeProjectList()
|
||||||
_mockTgHomeService()
|
_mockTgHomeService()
|
||||||
_mockTranslateFilter()
|
_mockTranslateFilter()
|
||||||
|
_mockTgProjectsService()
|
||||||
return null
|
return null
|
||||||
|
|
||||||
beforeEach ->
|
beforeEach ->
|
||||||
|
@ -50,12 +74,18 @@ describe "homeDirective", () ->
|
||||||
|
|
||||||
_mocks()
|
_mocks()
|
||||||
|
|
||||||
inject ($rootScope, $compile) ->
|
inject ($rootScope, $compile, $timeout) ->
|
||||||
scope = $rootScope.$new()
|
scope = $rootScope.$new()
|
||||||
compile = $compile
|
compile = $compile
|
||||||
|
timeout = $timeout
|
||||||
|
|
||||||
it "home directive content", () ->
|
it "home directive content", () ->
|
||||||
elm = createDirective()
|
elm = createDirective()
|
||||||
scope.$apply()
|
scope.$apply()
|
||||||
|
|
||||||
|
thenStubGetCurrentUserProjectsById.callArg(0)
|
||||||
|
thenStubGetWorkInProgress.callArg(0)
|
||||||
|
timeout.flush()
|
||||||
|
|
||||||
expect(elm.isolateScope().vm.assignedTo.size).to.be.equal(3)
|
expect(elm.isolateScope().vm.assignedTo.size).to.be.equal(3)
|
||||||
expect(elm.isolateScope().vm.watching.size).to.be.equal(3)
|
expect(elm.isolateScope().vm.watching.size).to.be.equal(3)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class HomeService extends taiga.Service
|
class HomeService extends taiga.Service
|
||||||
@.$inject = ["$q", "$tgResources", "$rootScope", "$projectUrl", "$tgAuth"]
|
@.$inject = ["$q", "$tgNavUrls", "$tgResources", "$rootScope", "$projectUrl", "$tgAuth"]
|
||||||
|
|
||||||
constructor: (@q, @rs, @rootScope, @projectUrl, @auth) ->
|
constructor: (@q, @navurls, @rs, @rootScope, @projectUrl, @auth) ->
|
||||||
@._workInProgress = Immutable.Map()
|
@._workInProgress = Immutable.Map()
|
||||||
@._projectPromise = null
|
@._projectPromise = null
|
||||||
@._inProgress = false
|
@._inProgress = false
|
||||||
|
@ -10,6 +10,32 @@ class HomeService extends taiga.Service
|
||||||
|
|
||||||
@.fetchWorkInProgress()
|
@.fetchWorkInProgress()
|
||||||
|
|
||||||
|
attachProjectInfoToWorkInProgress: (projectsById) ->
|
||||||
|
_attachProjectInfoToDuty = (duty) =>
|
||||||
|
project = projectsById.get(String(duty.project))
|
||||||
|
ctx = {
|
||||||
|
project: project.slug
|
||||||
|
ref: duty.ref
|
||||||
|
}
|
||||||
|
Object.defineProperty(duty, "url", {get: () => @navurls.resolve("project-#{duty._name}-detail", ctx)})
|
||||||
|
Object.defineProperty(duty, "projectName", {get: () => project.name})
|
||||||
|
|
||||||
|
@._workInProgress = Immutable.fromJS({
|
||||||
|
assignedTo: {
|
||||||
|
userStories: _.map(_.clone(@.assignedToUserStories), _attachProjectInfoToDuty)
|
||||||
|
tasks: _.map(_.clone(@.assignedToTasks), _attachProjectInfoToDuty)
|
||||||
|
issues: _.map(_.clone(@.assignedToIssues), _attachProjectInfoToDuty)
|
||||||
|
}
|
||||||
|
watching: {
|
||||||
|
userStories: _.map(_.clone(@.watchingUserStories), _attachProjectInfoToDuty)
|
||||||
|
tasks: _.map(_.clone(@.watchingTasks), _attachProjectInfoToDuty)
|
||||||
|
issues: _.map(_.clone(@.watchingIssues), _attachProjectInfoToDuty)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
getWorkInProgress: () ->
|
||||||
|
return @._projectPromise
|
||||||
|
|
||||||
fetchWorkInProgress: () ->
|
fetchWorkInProgress: () ->
|
||||||
userId = @auth.getUser().id
|
userId = @auth.getUser().id
|
||||||
|
|
||||||
|
@ -28,7 +54,6 @@ class HomeService extends taiga.Service
|
||||||
assignedIssuesPromise = @rs.issues.listInAllProjects(params).then (issues) =>
|
assignedIssuesPromise = @rs.issues.listInAllProjects(params).then (issues) =>
|
||||||
@.assignedToIssues = issues
|
@.assignedToIssues = issues
|
||||||
|
|
||||||
|
|
||||||
params = {
|
params = {
|
||||||
status__is_closed: false
|
status__is_closed: false
|
||||||
watchers: userId
|
watchers: userId
|
||||||
|
|
|
@ -76,6 +76,12 @@ describe "tgHome", ->
|
||||||
|
|
||||||
provide.value "$tgAuth", mocks.auth
|
provide.value "$tgAuth", mocks.auth
|
||||||
|
|
||||||
|
_mockTgNavUrls = () ->
|
||||||
|
mocks.tgNavUrls = {
|
||||||
|
resolve: sinon.stub()
|
||||||
|
}
|
||||||
|
provide.value "$tgNavUrls", mocks.tgNavUrls
|
||||||
|
|
||||||
_inject = (callback) ->
|
_inject = (callback) ->
|
||||||
inject (_$q_, _$tgResources_, _$rootScope_, _$projectUrl_, _$timeout_, _tgHomeService_) ->
|
inject (_$q_, _$tgResources_, _$rootScope_, _$projectUrl_, _$timeout_, _tgHomeService_) ->
|
||||||
timeout = _$timeout_
|
timeout = _$timeout_
|
||||||
|
@ -88,6 +94,7 @@ describe "tgHome", ->
|
||||||
_mockResources()
|
_mockResources()
|
||||||
_mockProjectUrl()
|
_mockProjectUrl()
|
||||||
_mockAuth()
|
_mockAuth()
|
||||||
|
_mockTgNavUrls()
|
||||||
return null
|
return null
|
||||||
|
|
||||||
_setup = ->
|
_setup = ->
|
||||||
|
|
|
@ -5,22 +5,25 @@ class ProjectsService extends taiga.Service
|
||||||
@.$inject = ["$tgResources", "$rootScope", "$projectUrl", "tgLightboxFactory"]
|
@.$inject = ["$tgResources", "$rootScope", "$projectUrl", "tgLightboxFactory"]
|
||||||
|
|
||||||
constructor: (@rs, @rootScope, @projectUrl, @lightboxFactory) ->
|
constructor: (@rs, @rootScope, @projectUrl, @lightboxFactory) ->
|
||||||
@._projects = Immutable.Map()
|
@._currentUserProjects = Immutable.Map()
|
||||||
@._projectsById = Immutable.Map()
|
@._currentUserProjectsById = Immutable.Map()
|
||||||
@._inProgress = false
|
@._inProgress = false
|
||||||
@._projectsPromise = null
|
@._currentUserProjectsPromise = null
|
||||||
|
|
||||||
taiga.defineImmutableProperty @, "projects", () => return @._projects
|
taiga.defineImmutableProperty @, "currentUserProjects", () => return @._currentUserProjects
|
||||||
taiga.defineImmutableProperty @, "projectsById", () => return @._projectsById
|
taiga.defineImmutableProperty @, "currentUserProjectsById", () => return @._currentUserProjectsById
|
||||||
|
|
||||||
@.fetchProjects()
|
@.fetchProjects()
|
||||||
|
|
||||||
|
getCurrentUserProjects: ->
|
||||||
|
return @._currentUserProjectsPromise
|
||||||
|
|
||||||
fetchProjects: ->
|
fetchProjects: ->
|
||||||
if not @._inProgress
|
if not @._inProgress
|
||||||
@._inProgress = true
|
@._inProgress = true
|
||||||
|
|
||||||
@._projectsPromise = @rs.projects.listByMember(@rootScope.user?.id)
|
@._currentUserProjectsPromise = @rs.projects.listByMember(@rootScope.user?.id)
|
||||||
@._projectsPromise.then (projects) =>
|
@._currentUserProjectsPromise.then (projects) =>
|
||||||
_.map projects, (project) =>
|
_.map projects, (project) =>
|
||||||
project.url = @projectUrl.get(project)
|
project.url = @projectUrl.get(project)
|
||||||
|
|
||||||
|
@ -33,19 +36,19 @@ class ProjectsService extends taiga.Service
|
||||||
color = project.tags_colors[tag]
|
color = project.tags_colors[tag]
|
||||||
return {name: tag, color: color}
|
return {name: tag, color: color}
|
||||||
|
|
||||||
@._projects = Immutable.fromJS({
|
@._currentUserProjects = Immutable.fromJS({
|
||||||
all: projects,
|
all: projects,
|
||||||
recents: projects.slice(0, 10)
|
recents: projects.slice(0, 10)
|
||||||
})
|
})
|
||||||
|
|
||||||
@._projectsById = Immutable.fromJS(groupBy(projects, (p) -> p.id))
|
@._currentUserProjectsById = Immutable.fromJS(groupBy(projects, (p) -> p.id))
|
||||||
|
|
||||||
return @.projects
|
return @.projects
|
||||||
|
|
||||||
@._projectsPromise.finally =>
|
@._currentUserProjectsPromise.finally =>
|
||||||
@._inProgress = false
|
@._inProgress = false
|
||||||
|
|
||||||
return @._projectsPromise
|
return @._currentUserProjectsPromise
|
||||||
|
|
||||||
newProject: ->
|
newProject: ->
|
||||||
@lightboxFactory.create("tg-lb-create-project", {
|
@lightboxFactory.create("tg-lb-create-project", {
|
||||||
|
|
|
@ -81,8 +81,8 @@ describe "tgProjects", ->
|
||||||
it "all & recents filled", () ->
|
it "all & recents filled", () ->
|
||||||
mocks.thenStub.callArg(0, projects)
|
mocks.thenStub.callArg(0, projects)
|
||||||
|
|
||||||
expect(projectsService.projects.get("all").toJS()).to.be.eql(projects)
|
expect(projectsService.currentUserProjects.get("all").toJS()).to.be.eql(projects)
|
||||||
expect(projectsService.projects.get("recents").toJS()).to.be.eql(projects.slice(0, 10))
|
expect(projectsService.currentUserProjects.get("recents").toJS()).to.be.eql(projects.slice(0, 10))
|
||||||
|
|
||||||
it "_inProgress change to false when tgResources end", () ->
|
it "_inProgress change to false when tgResources end", () ->
|
||||||
expect(projectsService._inProgress).to.be.true
|
expect(projectsService._inProgress).to.be.true
|
||||||
|
@ -103,15 +103,15 @@ describe "tgProjects", ->
|
||||||
it "group projects by id", () ->
|
it "group projects by id", () ->
|
||||||
mocks.thenStub.callArg(0, projects)
|
mocks.thenStub.callArg(0, projects)
|
||||||
|
|
||||||
expect(projectsService.projectsById.size).to.be.equal(12)
|
expect(projectsService.currentUserProjectsById.size).to.be.equal(12)
|
||||||
expect(projectsService.projectsById.toJS()[1].id).to.be.equal(projects[0].id)
|
expect(projectsService.currentUserProjectsById.toJS()[1].id).to.be.equal(projects[0].id)
|
||||||
|
|
||||||
it "add urls in the project object", () ->
|
it "add urls in the project object", () ->
|
||||||
mocks.thenStub.callArg(0, projects)
|
mocks.thenStub.callArg(0, projects)
|
||||||
|
|
||||||
expect(projectsService.projectsById.toJS()[1].url).to.be.equal("url-1")
|
expect(projectsService.currentUserProjectsById.toJS()[1].url).to.be.equal("url-1")
|
||||||
expect(projectsService.projects.get("all").toJS()[0].url).to.be.equal("url-1")
|
expect(projectsService.currentUserProjects.get("all").toJS()[0].url).to.be.equal("url-1")
|
||||||
expect(projectsService.projects.get("recents").toJS()[0].url).to.be.equal("url-1")
|
expect(projectsService.currentUserProjects.get("recents").toJS()[0].url).to.be.equal("url-1")
|
||||||
|
|
||||||
it "add sorted colorized_tags project object", () ->
|
it "add sorted colorized_tags project object", () ->
|
||||||
mocks.thenStub.callArg(0, projects)
|
mocks.thenStub.callArg(0, projects)
|
||||||
|
@ -123,7 +123,7 @@ describe "tgProjects", ->
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
colorized_tags = projectsService.projects.get("all").toJS()[0].colorized_tags
|
colorized_tags = projectsService.currentUserProjects.get("all").toJS()[0].colorized_tags
|
||||||
|
|
||||||
expect(colorized_tags).to.be.eql(tags)
|
expect(colorized_tags).to.be.eql(tags)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue