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

477 lines
18 KiB
Plaintext
Raw Normal View History

/* eslint-disable
handle-callback-err,
no-return-assign,
no-undef,
standard/no-callback-literal,
*/
// 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
*/
2015-05-05 03:10:25 +02:00
Application.Controllers.controller('ApplicationController', ['$rootScope', '$scope', '$window', '$locale', 'Session', 'AuthService', 'Auth', '$uibModal', '$state', 'growl', 'Notification', '$interval', 'Setting', '_t', 'Version',
function ($rootScope, $scope, $window, $locale, Session, AuthService, Auth, $uibModal, $state, growl, Notification, $interval, Setting, _t, Version) {
/* PRIVATE STATIC CONSTANTS */
2015-05-05 03:10:25 +02:00
// User's notifications will get refreshed every 30s
2018-11-21 11:32:50 +01:00
const NOTIFICATIONS_CHECK_PERIOD = 30000;
2018-11-21 11:32:50 +01:00
/* PUBLIC SCOPE */
// Fab-manager's version
$scope.version =
2018-11-21 11:32:50 +01:00
{ version: '' };
// currency symbol for the current locale (cf. angular-i18n)
2018-11-21 11:32:50 +01:00
$rootScope.currencySymbol = $locale.NUMBER_FORMATS.CURRENCY_SYM;
/**
* Set the current user to the provided value and initialize the session
* @param user {Object} Rails/Devise user
*/
$scope.setCurrentUser = function (user) {
if (!angular.isUndefinedOrNull(user)) {
2018-11-21 11:32:50 +01:00
$rootScope.currentUser = user;
Session.create(user);
getNotifications();
// fab-manager's app-version
if (user.role === 'admin') {
2018-11-21 11:32:50 +01:00
return $scope.version = Version.get();
} else {
2018-11-21 11:32:50 +01:00
return $scope.version = { version: '' };
}
}
2018-11-21 11:32:50 +01:00
};
/**
* Login callback
* @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
* @param callback {function}
*/
$scope.login = function (e, callback) {
2018-11-21 11:32:50 +01:00
if (e) { e.preventDefault(); }
return openLoginModal(null, null, callback);
};
/**
* Logout callback
* @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
*/
$scope.logout = function (e) {
2018-11-21 11:32:50 +01:00
e.preventDefault();
return Auth.logout().then(function () {
2018-11-21 11:32:50 +01:00
Session.destroy();
$rootScope.currentUser = null;
$rootScope.toCheckNotifications = false;
$scope.notifications = {
total: 0,
unread: 0
2018-11-21 11:32:50 +01:00
};
return $state.go('app.public.home');
}, function (error) {
2018-11-21 11:32:50 +01:00
console.error(`An error occurred logging out: ${error}`);
});
};
/**
* Open the modal window allowing the user to create an account.
* @param e {Object} see https://docs.angularjs.org/guide/expression#-event-
*/
$scope.signup = function (e) {
2018-11-21 11:32:50 +01:00
if (e) { e.preventDefault(); }
return $uibModal.open({
templateUrl: '<%= asset_path "shared/signupModal.html" %>',
size: 'md',
controller: ['$scope', '$uibModalInstance', 'Group', 'CustomAsset', 'growl', '_t', function ($scope, $uibModalInstance, Group, CustomAsset, growl, _t) {
// default parameters for the date picker in the account creation modal
$scope.datePicker = {
format: Fablab.uibDateFormat,
opened: false,
options: {
startingDay: Fablab.weekStartingDay
}
2018-11-21 11:32:50 +01:00
};
2015-05-05 03:10:25 +02:00
// callback to open the date picker (account creation modal)
$scope.openDatePicker = function ($event) {
2018-11-21 11:32:50 +01:00
$event.preventDefault();
$event.stopPropagation();
return $scope.datePicker.opened = true;
};
2015-05-05 03:10:25 +02:00
// retrieve the groups (standard, student ...)
2018-11-21 11:32:50 +01:00
Group.query(function (groups) {
$scope.groups = groups;
2018-11-21 11:32:50 +01:00
});
2015-05-05 03:10:25 +02:00
// retrieve the CGU
2018-11-21 11:32:50 +01:00
CustomAsset.get({ name: 'cgu-file' }, function (cgu) {
$scope.cgu = cgu.custom_asset;
2018-11-21 11:32:50 +01:00
});
2016-03-23 18:39:41 +01:00
// default user's parameters
$scope.user = {
is_allow_contact: true,
is_allow_newsletter: false
2018-11-21 11:32:50 +01:00
};
// Errors display
2018-11-21 11:32:50 +01:00
$scope.alerts = [];
$scope.closeAlert = function (index) {
$scope.alerts.splice(index, 1);
};
// callback for form validation
$scope.ok = function () {
// try to create the account
$scope.alerts = [];
// remove 'organization' attribute
const orga = $scope.user.organization;
delete $scope.user.organization;
// register on server
2018-11-21 11:32:50 +01:00
return Auth.register($scope.user).then(function (user) {
if (user.id) {
// creation successful
$uibModalInstance.close(user);
} else {
// the user was not saved in database, something wrong occurred
growl.error(_t('unexpected_error_occurred'));
}
}, function (error) {
// creation failed...
// restore organization param
2018-11-21 11:32:50 +01:00
$scope.user.organization = orga;
// display errors
2018-11-21 11:32:50 +01:00
angular.forEach(error.data.errors, function (v, k) {
angular.forEach(v, function (err) {
$scope.alerts.push({
msg: k + ': ' + err,
type: 'danger'
});
});
});
2018-11-21 11:32:50 +01:00
});
};
}]
2018-11-21 11:32:50 +01:00
}).result['finally'](null).then(function (user) {
// when the account was created successfully, set the session to the newly created account
2018-11-21 11:32:50 +01:00
$scope.setCurrentUser(user);
});
};
/**
* Open the modal window allowing the user to change his password.
* @param token {string} security token for password changing. The user should have recieved it by mail
*/
2018-11-21 11:32:50 +01:00
$scope.editPassword = function (token) {
$uibModal.open({
templateUrl: '<%= asset_path "shared/passwordEditModal.html" %>',
size: 'md',
controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
$scope.user = { reset_password_token: token };
$scope.alerts = [];
$scope.closeAlert = function (index) {
2018-11-21 11:32:50 +01:00
$scope.alerts.splice(index, 1);
};
return $scope.changePassword = function () {
2018-11-21 11:32:50 +01:00
$scope.alerts = [];
return $http.put('/users/password.json', { user: $scope.user }).then(function () { $uibModalInstance.close(); }).catch(function (data) {
angular.forEach(data.data.errors, function (v, k) {
angular.forEach(v, function (err) {
$scope.alerts.push({
msg: k + ': ' + err,
type: 'danger'
});
});
});
2018-11-21 11:32:50 +01:00
});
};
}]
}).result['finally'](null).then(function () {
2018-11-21 11:32:50 +01:00
growl.success(_t('your_password_was_successfully_changed'));
return Auth.login().then(function (user) {
$scope.setCurrentUser(user);
}, function (error) {
console.error(`Authentication failed: ${error}`);
}
2018-11-21 11:32:50 +01:00
);
});
};
/**
* Compact/Expend the width of the left navigation bar
* @param event {Object} see https://docs.angularjs.org/guide/expression#-event-
*/
$scope.toggleNavSize = function (event) {
2018-11-21 11:32:50 +01:00
let $classes, $targets;
if (typeof event === 'undefined') {
2018-11-21 11:32:50 +01:00
console.error('[ApplicationController::toggleNavSize] Missing event parameter');
return;
}
2018-11-21 11:32:50 +01:00
let toggler = $(event.target);
if (!toggler.data('toggle')) { toggler = toggler.closest('[data-toggle^="class"]'); }
2018-11-21 11:32:50 +01:00
const $class = toggler.data()['toggle'];
const $target = toggler.data('target') || toggler.attr('data-link');
if ($class) {
2018-11-21 11:32:50 +01:00
const $tmp = $class.split(':')[1];
if ($tmp) { $classes = $tmp.split(','); }
}
if ($target) {
2018-11-21 11:32:50 +01:00
$targets = $target.split(',');
}
if ($classes && $classes.length) {
$.each($targets, function (index) {
if ($classes[index].indexOf('*') !== -1) {
const patt = new RegExp('\\s',
+$classes[index].replace(/\*/g, '[A-Za-z0-9-_]+').split(' ').join('\\s|\\s'),
2018-11-21 11:32:50 +01:00
+'\\s', 'g');
$(toggler).each(function (i, it) {
2018-11-21 11:32:50 +01:00
let cn = ` ${it.className} `;
while (patt.test(cn)) {
2018-11-21 11:32:50 +01:00
cn = cn.replace(patt, ' ');
}
2018-11-21 11:32:50 +01:00
return it.className = $.trim(cn);
});
}
2018-11-21 11:32:50 +01:00
return (($targets[index] !== '#') && $($targets[index]).toggleClass($classes[index])) || toggler.toggleClass($classes[index]);
});
}
2018-11-21 11:32:50 +01:00
toggler.toggleClass('active');
};
2018-11-21 11:32:50 +01:00
/* PRIVATE SCOPE */
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
const initialize = function () {
// try to retrieve any currently logged user
Auth.login().then(function (user) {
2018-11-21 11:32:50 +01:00
$scope.setCurrentUser(user);
// force users to complete their profile if they are not
if (user.need_completion) {
2018-11-21 11:32:50 +01:00
return $state.transitionTo('app.logged.profileCompletion');
}
2018-11-27 16:26:21 +01:00
}, function () {
console.info('No users currently logged');
2018-11-21 11:32:50 +01:00
$rootScope.toCheckNotifications = false;
});
// bind to the $stateChangeStart event (AngularJS/UI-Router)
$rootScope.$on('$stateChangeStart', function (event, toState, toParams) {
2018-11-21 11:32:50 +01:00
if (!toState.data) { return; }
2018-11-21 11:32:50 +01:00
const { authorizedRoles } = toState.data;
if (!AuthService.isAuthorized(authorizedRoles)) {
2018-11-21 11:32:50 +01:00
event.preventDefault();
if (AuthService.isAuthenticated()) {
// user is not allowed
2018-11-21 11:32:50 +01:00
console.error('[ApplicationController::initialize] user is not allowed');
} else {
// user is not logged in
2018-11-21 11:32:50 +01:00
openLoginModal(toState, toParams);
}
}
2018-11-21 11:32:50 +01:00
});
// we stop polling notifications when the page is not in foreground
2018-11-21 11:32:50 +01:00
onPageVisible(function (state) { $rootScope.toCheckNotifications = (state === 'visible'); });
2018-11-21 11:32:50 +01:00
Setting.get({ name: 'fablab_name' }, function (data) { $scope.fablabName = data.setting.value; });
Setting.get({ name: 'name_genre' }, function (data) { $scope.nameGenre = data.setting.value; });
// shorthands
2018-11-21 11:32:50 +01:00
$scope.isAuthenticated = Auth.isAuthenticated;
$scope.isAuthorized = AuthService.isAuthorized;
2018-11-27 16:26:21 +01:00
$rootScope.login = $scope.login;
2018-11-21 11:32:50 +01:00
};
/**
* Retreive once the notifications from the server and display a message popup for each new one.
* Then, periodically check for new notifications.
*/
var getNotifications = function () {
2018-11-21 11:32:50 +01:00
$rootScope.toCheckNotifications = true;
if (!$rootScope.checkNotificationsIsInit && !!$rootScope.currentUser) {
2018-11-21 11:32:50 +01:00
setTimeout(function () {
// we request the most recent notifications
Notification.last_unread(function (notifications) {
2018-11-21 11:32:50 +01:00
$rootScope.lastCheck = new Date();
$scope.notifications = notifications.totals;
const toDisplay = [];
2018-11-21 11:32:50 +01:00
angular.forEach(notifications.notifications, function (n) { toDisplay.push(n); });
if (toDisplay.length < notifications.totals.unread) {
toDisplay.push({ message: { description: _t('and_NUMBER_other_notifications', { NUMBER: notifications.totals.unread - toDisplay.length }, 'messageformat') } });
}
2018-11-21 11:32:50 +01:00
angular.forEach(toDisplay, function (notification) { growl.info(notification.message.description); });
});
}, 2000);
const checkNotifications = function () {
if ($rootScope.toCheckNotifications) {
return Notification.polling({ last_poll: $rootScope.lastCheck }).$promise.then(function (data) {
$rootScope.lastCheck = new Date();
$scope.notifications = data.totals;
2018-11-21 11:32:50 +01:00
angular.forEach(data.notifications, function (notification) { growl.info(notification.message.description); });
}).catch(function (error) {
console.error('Error while polling notifications', error);
2018-11-21 11:32:50 +01:00
});
}
};
$interval(checkNotifications, NOTIFICATIONS_CHECK_PERIOD);
$rootScope.checkNotificationsIsInit = true;
}
2018-11-21 11:32:50 +01:00
};
/**
* Open the modal window allowing the user to log in.
*/
var openLoginModal = function (toState, toParams, callback) {
2018-10-25 17:08:13 +02:00
<% active_provider = AuthProvider.active %>
<% if active_provider.providable_type != DatabaseProvider.name %>
$window.location.href = '<%="/users/auth/#{active_provider.strategy_name}"%>';
2018-10-25 17:08:13 +02:00
<% else %>
return $uibModal.open({
templateUrl: '<%= asset_path "shared/deviseModal.html" %>',
size: 'sm',
controller: ['$scope', '$uibModalInstance', '_t', function ($scope, $uibModalInstance, _t) {
2018-11-21 11:32:50 +01:00
const user = ($scope.user = {});
$scope.login = function () {
Auth.login(user).then(function (user) {
// Authentication succeeded ...
2018-11-21 11:32:50 +01:00
$uibModalInstance.close(user);
if (callback && (typeof callback === 'function')) {
2018-11-21 11:32:50 +01:00
return callback(user);
}
}
, function (error) {
console.error(`Authentication failed: ${error}`);
$scope.alerts = [];
return $scope.alerts.push({
msg: _t('wrong_email_or_password'),
type: 'danger'
2018-11-21 11:32:50 +01:00
});
});
};
// handle modal behaviors. The provided reason will be used to define the following actions
2018-11-21 11:32:50 +01:00
$scope.dismiss = function () { $uibModalInstance.dismiss('cancel'); };
$scope.openSignup = function (e) {
2018-11-21 11:32:50 +01:00
e.preventDefault();
return $uibModalInstance.dismiss('signup');
};
return $scope.openResetPassword = function (e) {
2018-11-21 11:32:50 +01:00
e.preventDefault();
return $uibModalInstance.dismiss('resetPassword');
};
}]
}).result['finally'](null).then(function (user) {
2018-11-21 11:32:50 +01:00
// what to do when the modal is closed
2018-11-21 11:32:50 +01:00
// authentication succeeded, set the session, gather the notifications and redirect
$scope.setCurrentUser(user);
2018-11-21 11:32:50 +01:00
if ((toState !== null) && (toParams !== null)) {
return $state.go(toState, toParams);
}
}, function (reason) {
// authentication did not ended successfully
if (reason === 'signup') {
// open signup modal
$scope.signup();
} else if (reason === 'resetPassword') {
// open the 'reset password' modal
return $uibModal.open({
templateUrl: '<%= asset_path "shared/passwordNewModal.html" %>',
size: 'sm',
controller: ['$scope', '$uibModalInstance', '$http', function ($scope, $uibModalInstance, $http) {
$scope.user = { email: '' };
return $scope.sendReset = function () {
$scope.alerts = [];
return $http.post('/users/password.json', { user: $scope.user }).then(function () { $uibModalInstance.close(); }).catch(function () {
2018-11-21 11:32:50 +01:00
$scope.alerts.push({
msg: _t('your_email_address_is_unknown'),
type: 'danger'
});
});
};
}]
}).result['finally'](null).then(function () { growl.info(_t('you_will_receive_in_a_moment_an_email_with_instructions_to_reset_your_password')); });
}
});
// otherwise the user just closed the modal
<% end %>
2018-11-21 11:32:50 +01:00
};
/**
* Detect if the current page (tab/window) is active of put as background.
* When the status changes, the callback is triggered with the new status as parameter
* Inspired by http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active#answer-1060034
*/
var onPageVisible = function (callback) {
2018-11-21 11:32:50 +01:00
let hidden = 'hidden';
const onchange = function (evt) {
2018-11-21 11:32:50 +01:00
const v = 'visible';
const h = 'hidden';
const evtMap = {
focus: v,
focusin: v,
pageshow: v,
blur: h,
focusout: h,
pagehide: h
2018-11-21 11:32:50 +01:00
};
evt = evt || window.event;
if (evt.type in evtMap) {
2018-11-21 11:32:50 +01:00
if (typeof callback === 'function') { callback(evtMap[evt.type]); }
} else {
2018-11-21 11:32:50 +01:00
if (typeof callback === 'function') { callback(this[hidden] ? 'hidden' : 'visible'); }
}
2018-11-21 11:32:50 +01:00
};
// Standards:
if (hidden in document) {
2018-11-21 11:32:50 +01:00
document.addEventListener('visibilitychange', onchange);
} else if ((hidden = 'mozHidden') in document) {
2018-11-21 11:32:50 +01:00
document.addEventListener('mozvisibilitychange', onchange);
} else if ((hidden = 'webkitHidden') in document) {
2018-11-21 11:32:50 +01:00
document.addEventListener('webkitvisibilitychange', onchange);
} else if ((hidden = 'msHidden') in document) {
2018-11-21 11:32:50 +01:00
document.addEventListener('msvisibilitychange', onchange);
// IE 9 and lower
} else if ('onfocusin' in document) {
2018-11-21 11:32:50 +01:00
document.onfocusin = (document.onfocusout = onchange);
// All others
} else {
2018-11-21 11:32:50 +01:00
window.onpageshow = (window.onpagehide = (window.onfocus = (window.onblur = onchange)));
}
// set the initial state (but only if browser supports the Page Visibility API)
if (document[hidden] !== undefined) {
2018-11-21 11:32:50 +01:00
return onchange({ type: document[hidden] ? 'blur' : 'focus' });
}
2018-11-21 11:32:50 +01:00
};
2018-11-19 16:17:49 +01:00
// !!! MUST BE CALLED AT THE END of the controller
2018-11-21 11:32:50 +01:00
return initialize();
}
2018-11-21 11:32:50 +01:00
]);