Move generic filters related functions to separated mixin.

stable
Andrey Antukh 2014-06-26 22:47:18 +02:00
parent 735d3f6e49
commit 96ff32f3d4
3 changed files with 52 additions and 44 deletions

View File

@ -41,5 +41,42 @@ class PageMixin
.value() .value()
return results return results
# This mixin requires @location and @scope
class FiltersMixin
selectFilter: (name, value, load=false) ->
params = @location.search()
if params[name] != undefined and name != "page"
existing = _.map(params[name].split(","), trim)
existing.push(value)
value = joinStr(",", _.uniq(existing))
location = if load then @location else @location.noreload(@scope)
location.search(name, value)
unselectFilter: (name, value, load=false) ->
params = @location.search()
if params[name] is undefined
return
if value is undefined or value is null
delete params[name]
parsedValues = _.map(params[name].split(","), trim)
newValues = _.reject(parsedValues, (x) -> x == toString(value))
if _.isEmpty(newValues)
value = null
else
value = joinStr(",", _.uniq(newValues))
location = if load then @location else @location.noreload(@scope)
location.search(name, value)
taiga = @.taiga taiga = @.taiga
taiga.PageMixin = PageMixin taiga.PageMixin = PageMixin
taiga.FiltersMixin = FiltersMixin

View File

@ -34,7 +34,7 @@ module = angular.module("taigaIssues")
## Issues Controller ## Issues Controller
############################################################################# #############################################################################
class IssuesController extends mixOf(taiga.Controller, taiga.PageMixin) class IssuesController extends mixOf(taiga.Controller, taiga.PageMixin, taiga.FiltersMixin)
@.$inject = [ @.$inject = [
"$scope", "$scope",
"$rootScope", "$rootScope",
@ -62,49 +62,12 @@ class IssuesController extends mixOf(taiga.Controller, taiga.PageMixin)
loadProject: -> loadProject: ->
return @rs.projects.get(@scope.projectId).then (project) => return @rs.projects.get(@scope.projectId).then (project) =>
@scope.project = project @scope.project = project
# @scope.points = _.sortBy(project.points, "order")
# @scope.issueStatusList = _.sortBy(project.issue_statuses, "order")
# @scope.severityList = _.sortBy(project.severities, "order")
# @scope.priorityList = _.sortBy(project.priorities, "order")
@scope.issueStatusById = groupBy(project.issue_statuses, (x) -> x.id) @scope.issueStatusById = groupBy(project.issue_statuses, (x) -> x.id)
@scope.severityById = groupBy(project.severities, (x) -> x.id) @scope.severityById = groupBy(project.severities, (x) -> x.id)
@scope.priorityById = groupBy(project.priorities, (x) -> x.id) @scope.priorityById = groupBy(project.priorities, (x) -> x.id)
@scope.membersById = groupBy(project.memberships, (x) -> x.id) @scope.membersById = groupBy(project.memberships, (x) -> x.id)
console.log @scope.membersById
return project return project
selectFilter: (name, value, load=false) ->
params = @location.search()
if params[name] != undefined
existing = _.map(params[name].split(","), trim)
existing.push(value)
value = joinStr(",", _.uniq(existing))
location = if load then @location else @location.noreload()
location.search(name, value)
unselectFilter: (name, value, load=false) ->
params = @location.search()
if params[name] is undefined
return
if value is undefined
delete params[name]
parsedValues = _.map(params[name].split(","), trim)
newValues = _.reject(parsedValues, (x) -> x == toString(value))
if _.isEmpty(newValues)
value = null
else
value = joinStr(",", _.uniq(newValues))
location = if load then @location else @location.noreload()
location.search(name, value)
getFilters: -> getFilters: ->
filters = _.pick(@location.search(), "page", "tags", "status", "type") filters = _.pick(@location.search(), "page", "tags", "status", "type")
filters.page = 1 if not filters.page filters.page = 1 if not filters.page
@ -235,29 +198,35 @@ IssuesDirective = ($log, $location) ->
event.preventDefault() event.preventDefault()
$scope.$apply -> $scope.$apply ->
$scope.page += 1 $ctrl.selectFilter("page", $scope.page + 1)
$location.noreload($scope).search("page", $scope.page)
$ctrl.loadIssues() $ctrl.loadIssues()
$el.on "click", ".issues-paginator a.previous", (event) -> $el.on "click", ".issues-paginator a.previous", (event) ->
event.preventDefault() event.preventDefault()
$scope.$apply -> $scope.$apply ->
$scope.page -= 1 $ctrl.selectFilter("page", $scope.page - 1)
$location.noreload($scope).search("page", $scope.page)
$ctrl.loadIssues() $ctrl.loadIssues()
#########################
## Issues Filters
#########################
linkFilters = ($scope, $el, $attrs, $ctrl) ->
$log.debug "IssuesDirective:linkFilters"
######################### #########################
## Issues Link ## Issues Link
######################### #########################
link = ($scope, $el, $attrs) -> link = ($scope, $el, $attrs) ->
$ctrl = $el.controller() $ctrl = $el.controller()
linkFilters($scope, $el, $attrs, $ctrl)
linkPagination($scope, $el, $attrs, $ctrl) linkPagination($scope, $el, $attrs, $ctrl)
return {link:link} return {link:link}
IssueStatusDirective = -> IssueStatusDirective = ->
link = ($scope, $el, $attrs) -> link = ($scope, $el, $attrs) ->
issue = $scope.$eval($attrs.tgIssueStatus) issue = $scope.$eval($attrs.tgIssueStatus)

View File

@ -6,7 +6,9 @@ block head
block content block content
div.wrapper(tg-issues, ng-controller="IssuesController as ctrl", div.wrapper(tg-issues, ng-controller="IssuesController as ctrl",
ng-init="section='issues'") ng-init="section='issues'")
sidebar.menu-secondary.sidebar sidebar.menu-secondary.sidebar.filters-container
// TODO: maybe everything related to this section
// should be done in one unique include?
header header
h1 Filters h1 Filters
include views/modules/search-in include views/modules/search-in