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

326 lines
11 KiB
JavaScript

'use strict';
Application.Controllers.controller('HomeController', ['$scope', '$stateParams', 'settingsPromise', 'Member', 'uiTourService', '_t',
function ($scope, $stateParams, settingsPromise, Member, uiTourService, _t) {
/* PUBLIC SCOPE */
// Home page HTML content
$scope.homeContent = null;
// Status of the components in the home page (exists or not?)
$scope.status = {
news: false,
projects: false,
twitter: false,
members: false,
events: false
};
/**
* Setup the feature-tour for the home page. (admins only)
* This is intended as a contextual help (when pressing F1)
*/
$scope.setupHomeTour = function () {
if ($scope.currentUser && $scope.currentUser.role === 'admin') {
setupWelcomeTour();
}
};
/* PRIVATE SCOPE */
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
const initialize = function () {
// if we receive a token to reset the password as GET parameter, trigger the
// changePassword modal from the parent controller
if ($stateParams.reset_password_token) {
return $scope.$parent.editPassword($stateParams.reset_password_token);
}
// We set the home page content, with the directives replacing the placeholders
$scope.homeContent = insertDirectives(settingsPromise.home_content);
// listen the $destroy event of the controller to remove the F1 key binding
$scope.$on('$destroy', function () {
window.removeEventListener('keydown', handleF1);
});
// for admins, setup the tour on login
$scope.$watch('currentUser', function (newValue, oldValue) {
if (!oldValue && newValue && newValue.role === 'admin') {
const uitour = uiTourService.getTourByName('welcome');
if (!uitour.hasStep()) {
setupWelcomeTour();
}
}
});
};
/**
* Parse the provided html and replace the elements with special IDs (#news, #projects, #twitter, #members, #events)
* by their respective angular directives
* @param html {String} a valid html string, as defined by the summernote editor in admin/settings/home_page
* @returns {string} a valid html string containing angular directives for the specified plugins
*/
const insertDirectives = function (html) {
const node = document.createElement('div');
node.innerHTML = html.trim();
node.querySelectorAll('div#news').forEach((newsNode) => {
const news = document.createElement('news');
newsNode.parentNode.replaceChild(news, newsNode);
$scope.status.news = true;
});
node.querySelectorAll('div#projects').forEach((projectsNode) => {
const projects = document.createElement('projects');
projectsNode.parentNode.replaceChild(projects, projectsNode);
$scope.status.projects = true;
});
node.querySelectorAll('div#twitter').forEach((twitterNode) => {
const twitter = document.createElement('twitter');
twitterNode.parentNode.replaceChild(twitter, twitterNode);
$scope.status.twitter = true;
});
node.querySelectorAll('div#members').forEach((membersNode) => {
const members = document.createElement('members');
membersNode.parentNode.replaceChild(members, membersNode);
$scope.status.members = true;
});
node.querySelectorAll('div#events').forEach((eventsNode) => {
const events = document.createElement('events');
eventsNode.parentNode.replaceChild(events, eventsNode);
$scope.status.events = true;
});
return node.outerHTML;
};
/**
* Setup the feature-tour for the home page that will present an overview of the whole app.
* This is intended as a contextual help.
*/
const setupWelcomeTour = function () {
// get the tour defined by the ui-tour directive
const uitour = uiTourService.getTourByName('welcome');
// add the steps
uitour.createStep({
selector: 'body',
stepId: 'welcome',
order: 0,
title: _t('app.public.tour.welcome.welcome.title'),
content: _t('app.public.tour.welcome.welcome.content'),
placement: 'bottom',
orphan: true
});
uitour.createStep({
selector: '.nav-primary li.home-link',
stepId: 'home',
order: 1,
title: _t('app.public.tour.welcome.home.title'),
content: _t('app.public.tour.welcome.home.content'),
placement: 'right'
});
uitour.createStep({
selector: '.nav-primary li.public-calendar-link',
stepId: 'calendar',
order: 2,
title: _t('app.public.tour.welcome.calendar.title'),
content: _t('app.public.tour.welcome.calendar.content'),
placement: 'right'
});
uitour.createStep({
selector: '.nav-primary li.reserve-machine-link',
stepId: 'machines',
order: 3,
title: _t('app.public.tour.welcome.machines.title'),
content: _t('app.public.tour.welcome.machines.content'),
placement: 'right'
});
if (!Fablab.withoutSpaces) {
uitour.createStep({
selector: '.nav-primary li.reserve-space-link',
stepId: 'spaces',
order: 4,
title: _t('app.public.tour.welcome.spaces.title'),
content: _t('app.public.tour.welcome.spaces.content'),
placement: 'right'
});
}
uitour.createStep({
selector: '.nav-primary li.reserve-training-link',
stepId: 'trainings',
order: 5,
title: _t('app.public.tour.welcome.trainings.title'),
content: _t('app.public.tour.welcome.trainings.content'),
placement: 'right'
});
uitour.createStep({
selector: '.nav-primary li.reserve-event-link',
stepId: 'events',
order: 6,
title: _t('app.public.tour.welcome.events.title'),
content: _t('app.public.tour.welcome.events.content'),
placement: 'right'
});
uitour.createStep({
selector: '.nav-primary li.projects-gallery-link',
stepId: 'projects',
order: 7,
title: _t('app.public.tour.welcome.projects.title'),
content: _t('app.public.tour.welcome.projects.content'),
placement: 'right'
});
uitour.createStep({
selector: '.nav-primary li.plans-link',
stepId: 'plans',
order: 8,
title: _t('app.public.tour.welcome.plans.title'),
content: _t('app.public.tour.welcome.plans.content'),
placement: 'right'
});
uitour.createStep({
selector: '.nav-primary .admin-section',
stepId: 'admin',
order: 9,
title: _t('app.public.tour.welcome.admin.title'),
content: _t('app.public.tour.welcome.admin.content'),
placement: 'right'
});
uitour.createStep({
selector: '.navbar.header li.about-page-link',
stepId: 'about',
order: 10,
title: _t('app.public.tour.welcome.about.title'),
content: _t('app.public.tour.welcome.about.content'),
placement: 'bottom'
});
uitour.createStep({
selector: '.navbar.header li.notification-center-link',
stepId: 'notifications',
order: 11,
title: _t('app.public.tour.welcome.notifications.title'),
content: _t('app.public.tour.welcome.notifications.content'),
placement: 'bottom'
});
uitour.createStep({
selector: '.navbar.header li.user-menu-dropdown',
stepId: 'profile',
order: 12,
title: _t('app.public.tour.welcome.profile.title'),
content: _t('app.public.tour.welcome.profile.content'),
placement: 'bottom'
});
if ($scope.status.news && settingsPromise.home_blogpost) {
uitour.createStep({
selector: 'news',
stepId: 'news',
order: 13,
title: _t('app.public.tour.welcome.news.title'),
content: _t('app.public.tour.welcome.news.content'),
placement: 'bottom'
});
}
if ($scope.status.projects) {
uitour.createStep({
selector: 'projects',
stepId: 'last_projects',
order: 14,
title: _t('app.public.tour.welcome.last_projects.title'),
content: _t('app.public.tour.welcome.last_projects.content'),
placement: 'top'
});
}
if ($scope.status.twitter) {
uitour.createStep({
selector: 'twitter',
stepId: 'last_tweet',
order: 15,
title: _t('app.public.tour.welcome.last_tweet.title'),
content: _t('app.public.tour.welcome.last_tweet.content'),
placement: 'left'
});
}
if ($scope.status.members) {
uitour.createStep({
selector: 'members',
stepId: 'last_members',
order: 16,
title: _t('app.public.tour.welcome.last_members.title'),
content: _t('app.public.tour.welcome.last_members.content'),
placement: 'left'
});
}
if ($scope.status.events) {
uitour.createStep({
selector: 'events',
stepId: 'next_events',
order: 17,
title: _t('app.public.tour.welcome.next_events.title'),
content: _t('app.public.tour.welcome.next_events.content'),
placement: 'top'
});
}
uitour.createStep({
selector: 'body',
stepId: 'customize',
order: 18,
title: _t('app.public.tour.welcome.customize.title'),
content: _t('app.public.tour.welcome.customize.content'),
placement: 'bottom',
orphan: 'true'
});
uitour.createStep({
selector: '.app-generator .app-version',
stepId: 'version',
order: 19,
title: _t('app.public.tour.welcome.version.title'),
content: _t('app.public.tour.welcome.version.content'),
placement: 'top'
});
uitour.createStep({
selector: 'body',
stepId: 'conclusion',
order: 20,
title: _t('app.public.tour.conclusion.title'),
content: _t('app.public.tour.conclusion.content'),
placement: 'bottom',
orphan: true
});
// on tour end, save the status in database
uitour.on('ended', function () {
if (uitour.getStatus() === uitour.Status.ON && $scope.currentUser.profile.tours.indexOf('welcome') < 0) {
Member.completeTour({ id: $scope.currentUser.id }, { tour: 'welcome' }, function (res) {
$scope.currentUser.profile.tours = res.tours;
});
}
});
// if the user has never seen the tour, show him now
if (Fablab.featureTourDisplay !== 'manual' && $scope.currentUser.profile.tours.indexOf('welcome') < 0) {
uitour.start();
}
// start this tour when an user press F1 - this is contextual help
window.addEventListener('keydown', handleF1);
};
/**
* Callback used to trigger the feature tour when the user press the F1 key.
* @param e {KeyboardEvent}
*/
const handleF1 = function (e) {
if (e.key === 'F1') {
e.preventDefault();
const tour = uiTourService.getTourByName('welcome');
if (tour) { tour.start(); }
}
};
// !!! MUST BE CALLED AT THE END of the controller
return initialize();
}
]);