1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-02 13:24:20 +01:00
fab-manager/app/assets/javascripts/controllers/notifications.js

114 lines
3.6 KiB
JavaScript
Raw Normal View History

/* eslint-disable
no-return-assign,
no-undef,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2018-11-21 11:08:53 +01:00
'use strict';
2015-05-05 03:10:25 +02:00
/**
* Controller used in notifications page
* inherits $scope.$parent.notifications (global notifications state) from ApplicationController
*/
Application.Controllers.controller('NotificationsController', ['$scope', 'Notification', function ($scope, Notification) {
/* PUBLIC SCOPE */
2015-05-05 03:10:25 +02:00
// Array containg the archived notifications (already read)
2018-11-21 11:08:53 +01:00
$scope.notificationsRead = [];
2015-05-05 03:10:25 +02:00
// Array containg the new notifications (not read)
2018-11-21 11:08:53 +01:00
$scope.notificationsUnread = [];
// Total number of notifications for the current user
2018-11-21 11:08:53 +01:00
$scope.total = 0;
// Total number of unread notifications for the current user
2018-11-21 11:08:53 +01:00
$scope.totalUnread = 0;
// By default, the pagination mode is activated to limit the page size
2018-11-21 11:08:53 +01:00
$scope.paginateActive = true;
2015-05-05 03:10:25 +02:00
// The currently displayed page number
2018-11-21 11:08:53 +01:00
$scope.page = 1;
2015-05-05 03:10:25 +02:00
/**
* 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
* @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
*/
$scope.markAsRead = function (notification, e) {
2018-11-21 11:08:53 +01:00
e.preventDefault();
return Notification.update({ id: notification.id }, {
id: notification.id,
2015-05-05 03:10:25 +02:00
is_read: true
}
, function (updatedNotif) {
// remove notif from unreads
2018-11-21 11:08:53 +01:00
const index = $scope.notificationsUnread.indexOf(notification);
$scope.notificationsUnread.splice(index, 1);
// add update notif to read
2018-11-21 11:08:53 +01:00
$scope.notificationsRead.push(updatedNotif);
// update counters
2018-11-21 11:08:53 +01:00
$scope.$parent.notifications.unread -= 1;
return $scope.totalUnread -= 1;
});
};
/**
* Mark every unread notifications as read and move them for the unread list to to read array.
*/
$scope.markAllAsRead = () =>
Notification.update({}
, function () { // success
// add notifs to read
angular.forEach($scope.notificationsUnread, function (n) {
2018-11-21 11:08:53 +01:00
n.is_read = true;
return $scope.notificationsRead.push(n);
});
// clear unread
2018-11-21 11:08:53 +01:00
$scope.notificationsUnread = [];
// update counters
2018-11-21 11:08:53 +01:00
$scope.$parent.notifications.unread = 0;
return $scope.totalUnread = 0;
});
/**
* Request the server to retrieve the next notifications and add them
* to their corresponding notifications list (read or unread).
*/
$scope.addMoreNotifications = function () {
Notification.query({ page: $scope.page }, function (notifications) {
2018-11-21 11:08:53 +01:00
$scope.total = notifications.totals.total;
$scope.totalUnread = notifications.totals.unread;
angular.forEach(notifications.notifications, function (notif) {
if (notif.is_read) {
2018-11-21 11:08:53 +01:00
return $scope.notificationsRead.push(notif);
} else {
2018-11-21 11:08:53 +01:00
return $scope.notificationsUnread.push(notif);
}
2018-11-21 11:08:53 +01:00
});
return $scope.paginateActive = (notifications.totals.total > ($scope.notificationsRead.length + $scope.notificationsUnread.length));
});
2018-11-21 11:08:53 +01:00
return $scope.page += 1;
};
/* PRIVATE SCOPE */
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
2018-11-21 11:08:53 +01:00
const initialize = () => $scope.addMoreNotifications();
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:08:53 +01:00
return initialize();
}
2018-11-21 11:08:53 +01:00
]);