2015-05-05 03:10:25 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
##
|
|
|
|
# Controller used in notifications page
|
2017-01-05 15:06:54 +01:00
|
|
|
# inherits $scope.$parent.notifications (global notifications state) from ApplicationController
|
2015-05-05 03:10:25 +02:00
|
|
|
##
|
2016-03-23 18:39:41 +01:00
|
|
|
Application.Controllers.controller "NotificationsController", ["$scope", 'Notification', ($scope, Notification) ->
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### PRIVATE STATIC CONSTANTS ###
|
|
|
|
|
|
|
|
# Number of notifications added to the page when the user clicks on 'load next notifications'
|
|
|
|
NOTIFICATIONS_PER_PAGE = 15
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### PUBLIC SCOPE ###
|
|
|
|
|
|
|
|
## Array containg the archived notifications (already read)
|
|
|
|
$scope.notificationsRead = []
|
|
|
|
|
2017-01-05 15:06:54 +01:00
|
|
|
## Array containg the new notifications (not read)
|
|
|
|
$scope.notificationsUnread = []
|
|
|
|
|
|
|
|
## Total number of notifications for the current user
|
|
|
|
$scope.total = 0
|
|
|
|
|
|
|
|
## Total number of unread notifications for the current user
|
|
|
|
$scope.totalUnread = 0
|
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
## By default, the pagination mode is activated to limit the page size
|
|
|
|
$scope.paginateActive = true
|
|
|
|
|
|
|
|
## The currently displayed page number
|
|
|
|
$scope.page = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Mark the provided notification as read, updating its status on the server and moving it
|
|
|
|
# to the already read notifications list.
|
|
|
|
# @param notification {{id:number}} the notification to mark as read
|
2016-03-23 18:39:41 +01:00
|
|
|
# @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
|
2015-05-05 03:10:25 +02:00
|
|
|
##
|
|
|
|
$scope.markAsRead = (notification, e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
Notification.update {id: notification.id},
|
|
|
|
id: notification.id
|
|
|
|
is_read: true
|
2017-01-05 15:06:54 +01:00
|
|
|
, (updatedNotif) ->
|
|
|
|
# remove notif from unreads
|
|
|
|
index = $scope.notificationsUnread.indexOf(notification)
|
|
|
|
$scope.notificationsUnread.splice(index,1)
|
|
|
|
# add update notif to read
|
|
|
|
$scope.notificationsRead.push updatedNotif
|
|
|
|
# update counters
|
|
|
|
$scope.$parent.notifications.unread -= 1
|
|
|
|
$scope.totalUnread -= 1
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
|
|
|
# Mark every unread notifications as read and move them for the unread list to to read array.
|
|
|
|
##
|
|
|
|
$scope.markAllAsRead = ->
|
|
|
|
Notification.update {}
|
|
|
|
, -> # success
|
2017-01-05 15:06:54 +01:00
|
|
|
# add notifs to read
|
|
|
|
angular.forEach $scope.notificationsUnread, (n)->
|
|
|
|
n.is_read = true
|
2015-05-05 03:10:25 +02:00
|
|
|
$scope.notificationsRead.push n
|
2017-01-05 15:06:54 +01:00
|
|
|
# clear unread
|
|
|
|
$scope.notificationsUnread = []
|
|
|
|
# update counters
|
|
|
|
$scope.$parent.notifications.unread = 0
|
|
|
|
$scope.totalUnread = 0
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##
|
2017-01-05 15:06:54 +01:00
|
|
|
# Request the server to retrieve the next notifications and add them
|
|
|
|
# to their corresponding notifications list (read or unread).
|
2015-05-05 03:10:25 +02:00
|
|
|
##
|
2017-01-05 15:06:54 +01:00
|
|
|
$scope.addMoreNotifications = ->
|
|
|
|
Notification.query {page: $scope.page}, (notifications) ->
|
|
|
|
$scope.total = notifications.totals.total
|
|
|
|
$scope.totalUnread = notifications.totals.unread
|
|
|
|
angular.forEach notifications.notifications, (notif) ->
|
|
|
|
if notif.is_read
|
|
|
|
$scope.notificationsRead.push(notif)
|
|
|
|
else
|
|
|
|
$scope.notificationsUnread.push(notif)
|
|
|
|
$scope.paginateActive = (notifications.totals.total > ($scope.notificationsRead.length + $scope.notificationsUnread.length))
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
$scope.page += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### PRIVATE SCOPE ###
|
|
|
|
|
|
|
|
##
|
|
|
|
# Kind of constructor: these actions will be realized first when the controller is loaded
|
|
|
|
##
|
|
|
|
initialize = ->
|
2017-01-05 15:06:54 +01:00
|
|
|
$scope.addMoreNotifications()
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## !!! MUST BE CALLED AT THE END of the controller
|
|
|
|
initialize()
|
|
|
|
]
|