1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-12 23:09:03 +01:00
fab-manager/app/assets/javascripts/controllers/projects.js.erb

618 lines
22 KiB
Plaintext
Raw Normal View History

/* eslint-disable
handle-callback-err,
no-return-assign,
no-undef,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
2018-11-21 11:08:53 +01:00
'use strict';
/* COMMON CODE */
2018-11-19 16:17:49 +01:00
/**
* Provides a set of common properties and methods to the $scope parameter. They are used
* in the various projects' admin controllers.
*
* Provides :
* - $scope.totalSteps
* - $scope.machines = [{Machine}]
* - $scope.components = [{Component}]
* - $scope.themes = [{Theme}]
* - $scope.licences = [{Licence}]
* - $scope.allowedExtensions = [{String}]
* - $scope.submited(content)
* - $scope.cancel()
* - $scope.addFile()
* - $scope.deleteFile(file)
* - $scope.addStep()
* - $scope.deleteStep(step)
* - $scope.changeStepIndex(step, newIdx)
*
* Requires :
* - $scope.project.project_caos_attributes = []
* - $scope.project.project_steps_attributes = []
* - $state (Ui-Router) [ 'app.public.projects_show', 'app.public.projects_list' ]
*/
class ProjectsController {
constructor ($scope, $state, Project, Machine, Member, Component, Theme, Licence, $document, Diacritics, dialogs, allowedExtensions, _t) {
2018-11-19 16:17:49 +01:00
// Retrieve the list of machines from the server
2018-11-20 12:26:06 +01:00
Machine.query().$promise.then(function (data) {
$scope.machines = data.map(function (d) {
return ({
id: d.id,
name: d.name
2018-11-21 11:08:53 +01:00
});
});
});
2018-11-19 16:17:49 +01:00
// Retrieve the list of components from the server
2018-11-20 12:26:06 +01:00
Component.query().$promise.then(function (data) {
$scope.components = data.map(function (d) {
return ({
id: d.id,
name: d.name
2018-11-21 11:08:53 +01:00
});
});
});
2018-11-19 16:17:49 +01:00
// Retrieve the list of themes from the server
2018-11-20 12:26:06 +01:00
Theme.query().$promise.then(function (data) {
$scope.themes = data.map(function (d) {
return ({
id: d.id,
name: d.name
2018-11-21 11:08:53 +01:00
});
});
});
2018-11-19 16:17:49 +01:00
// Retrieve the list of licences from the server
2018-11-20 12:26:06 +01:00
Licence.query().$promise.then(function (data) {
$scope.licences = data.map(function (d) {
return ({
id: d.id,
name: d.name
2018-11-21 11:08:53 +01:00
});
});
});
2018-11-19 16:17:49 +01:00
// Total number of documentation steps for the current project
2018-11-21 11:08:53 +01:00
$scope.totalSteps = $scope.project.project_steps_attributes.length;
2018-11-19 16:17:49 +01:00
// List of extensions allowed for CAD attachements upload
2018-11-21 11:08:53 +01:00
$scope.allowedExtensions = allowedExtensions;
2018-11-19 16:17:49 +01:00
/**
* For use with ngUpload (https://github.com/twilson63/ngUpload).
* Intended to be the callback when an upload is done: any raised error will be stacked in the
* $scope.alerts array. If everything goes fine, the user is redirected to the project page.
* @param content {Object} JSON - The upload's result
*/
$scope.submited = function (content) {
if ((content.id == null)) {
2018-11-21 11:08:53 +01:00
$scope.alerts = [];
2018-11-20 12:26:06 +01:00
angular.forEach(content, function (v, k) {
angular.forEach(v, function (err) {
$scope.alerts.push({
msg: k + ': ' + err,
2015-05-05 03:10:25 +02:00
type: 'danger'
2018-11-21 11:08:53 +01:00
});
});
});
// using https://github.com/oblador/angular-scroll
2018-11-21 11:08:53 +01:00
$('section[ui-view=main]').scrollTop(0, 200);
} else {
2018-11-21 11:08:53 +01:00
return $state.go('app.public.projects_show', { id: content.slug });
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* For use with 'ng-class', returns the CSS class name for the uploads previews.
* The preview may show a placeholder or the content of the file depending on the upload state.
* @param v {*} any attribute, will be tested for truthiness (see JS evaluation rules)
*/
$scope.fileinputClass = function (v) {
if (v) {
2018-11-21 11:08:53 +01:00
return 'fileinput-exists';
} else {
2018-11-21 11:08:53 +01:00
return 'fileinput-new';
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* This will create a single new empty entry into the project's CAO attachements list.
*/
2018-11-21 11:08:53 +01:00
$scope.addFile = function () { $scope.project.project_caos_attributes.push({}); };
2018-11-19 16:17:49 +01:00
/**
* This will remove the given file from the project's CAO attachements list. If the file was previously uploaded
* to the server, it will be marked for deletion on the server. Otherwise, it will be simply truncated from
* the CAO attachements array.
* @param file {Object} the file to delete
*/
$scope.deleteFile = function (file) {
2018-11-21 11:08:53 +01:00
const index = $scope.project.project_caos_attributes.indexOf(file);
if (file.id != null) {
2018-11-21 11:08:53 +01:00
return file._destroy = true;
} else {
2018-11-21 11:08:53 +01:00
return $scope.project.project_caos_attributes.splice(index, 1);
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* This will create a single new empty entry into the project's steps list.
*/
$scope.addStep = function () {
2018-11-21 11:08:53 +01:00
$scope.totalSteps += 1;
return $scope.project.project_steps_attributes.push({ step_nb: $scope.totalSteps, project_step_images_attributes: [] });
};
2018-11-19 16:17:49 +01:00
/**
* This will remove the given step from the project's steps list. If the step was previously saved
* on the server, it will be marked for deletion for the next saving. Otherwise, it will be simply truncated from
* the steps array.
2018-11-20 12:26:06 +01:00
* @param step {Object} the step to delete
2018-11-19 16:17:49 +01:00
*/
2018-11-20 12:26:06 +01:00
$scope.deleteStep = function (step) {
dialogs.confirm({
2018-11-21 10:59:07 +01:00
resolve: {
object () {
return {
2019-12-18 11:08:14 +01:00
title: _t('app.shared.project.confirmation_required'),
msg: _t('app.shared.project.do_you_really_want_to_delete_this_step')
2018-11-21 11:08:53 +01:00
};
}
}
2018-11-21 10:59:07 +01:00
}
, function () { // deletion confirmed
2018-11-21 11:08:53 +01:00
const index = $scope.project.project_steps_attributes.indexOf(step);
2018-11-21 10:59:07 +01:00
if (step.id != null) {
2018-11-21 11:08:53 +01:00
step._destroy = true;
2018-11-21 10:59:07 +01:00
} else {
2018-11-21 11:08:53 +01:00
$scope.project.project_steps_attributes.splice(index, 1);
2018-11-21 10:59:07 +01:00
}
2018-11-21 10:59:07 +01:00
// update the new total number of steps
2018-11-21 11:08:53 +01:00
$scope.totalSteps -= 1;
2018-11-21 10:59:07 +01:00
// reindex the remaining steps
return (function () {
2018-11-21 11:08:53 +01:00
const result = [];
2018-11-21 10:59:07 +01:00
for (let s of Array.from($scope.project.project_steps_attributes)) {
if (s.step_nb > step.step_nb) {
2018-11-21 11:08:53 +01:00
result.push(s.step_nb -= 1);
2018-11-21 10:59:07 +01:00
} else {
2018-11-21 11:08:53 +01:00
result.push(undefined);
}
2018-11-21 10:59:07 +01:00
}
2018-11-21 11:08:53 +01:00
return result;
})();
});
};
2018-11-19 16:17:49 +01:00
/**
* Change the step_nb property of the given step to the new value provided. The step that was previously at this
* index will be assigned to the old position of the provided step.
* @param event {Object} see https://docs.angularjs.org/guide/expression#-event-
* @param step {Object} the project's step to reindex
* @param newIdx {number} the new index to assign to the step
*/
$scope.changeStepIndex = function (event, step, newIdx) {
2018-11-21 11:08:53 +01:00
if (event) { event.preventDefault(); }
for (let s of Array.from($scope.project.project_steps_attributes)) {
if (s.step_nb === newIdx) {
2018-11-21 11:08:53 +01:00
s.step_nb = step.step_nb;
step.step_nb = newIdx;
break;
}
}
2018-11-21 11:08:53 +01:00
return false;
};
2015-05-05 03:10:25 +02:00
2019-12-18 11:08:14 +01:00
/**
* This function will query the API to autocomplete the typed user's name
* @param nameLookup {string}
*/
$scope.autoCompleteName = function (nameLookup) {
if (!nameLookup) {
2018-11-21 11:08:53 +01:00
return;
}
2018-11-21 11:08:53 +01:00
const asciiName = Diacritics.remove(nameLookup);
2018-11-20 12:26:06 +01:00
Member.search(
{ query: asciiName },
2018-11-21 11:08:53 +01:00
function (users) { $scope.matchingMembers = users; },
function (error) { console.error(error); }
);
};
2018-11-19 16:17:49 +01:00
/**
* This will create a single new empty entry into the project's step image list.
*/
2018-11-21 11:08:53 +01:00
$scope.addProjectStepImage = function (step) { step.project_step_images_attributes.push({}); };
2018-11-19 16:17:49 +01:00
/**
* This will remove the given image from the project's step image list.
* @param step {Object} the project step has images
* @param image {Object} the image to delete
*/
$scope.deleteProjectStepImage = function (step, image) {
2018-11-21 11:08:53 +01:00
const index = step.project_step_images_attributes.indexOf(image);
if (image.id != null) {
2018-11-21 11:08:53 +01:00
return image._destroy = true;
} else {
2018-11-21 11:08:53 +01:00
return step.project_step_images_attributes.splice(index, 1);
}
2018-11-21 11:08:53 +01:00
};
2019-12-18 11:08:14 +01:00
/**
* Returns the text to display on the save button, depending on the current state of the project
*/
$scope.saveButtonValue = function () {
if (!$scope.project.state || $scope.project.state === 'draft') {
return _t('app.shared.project.save_as_draft');
}
return _t('app.shared.buttons.save');
}
}
}
2018-11-19 16:17:49 +01:00
/**
* Controller used on projects listing page
*/
Application.Controllers.controller('ProjectsController', ['$scope', '$state', 'Project', 'machinesPromise', 'themesPromise', 'componentsPromise', 'paginationService', 'OpenlabProject', '$window', 'growl', '_t', '$location', '$timeout',
function ($scope, $state, Project, machinesPromise, themesPromise, componentsPromise, paginationService, OpenlabProject, $window, growl, _t, $location, $timeout) {
/* PRIVATE STATIC CONSTANTS */
// Number of projects added to the page when the user clicks on 'load more projects'
2019-06-06 16:34:53 +02:00
// -- dependency in app/models/project.rb
2018-11-21 11:08:53 +01:00
const PROJECTS_PER_PAGE = 16;
/* PUBLIC SCOPE */
2018-11-19 16:17:49 +01:00
// Fab-manager's instance ID in the openLab network
2018-11-21 11:08:53 +01:00
$scope.openlabAppId = Fablab.openlabAppId;
2018-11-19 16:17:49 +01:00
// Is openLab enabled on the instance?
$scope.openlab = {
projectsActive: Fablab.openlabProjectsActive,
searchOverWholeNetwork: false
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
// default search parameters
$scope.search = {
q: ($location.$$search.q || ''),
from: ($location.$$search.from || undefined),
machine_id: (parseInt($location.$$search.machine_id) || undefined),
component_id: (parseInt($location.$$search.component_id) || undefined),
theme_id: (parseInt($location.$$search.theme_id) || undefined)
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
// list of projects to display
2018-11-21 11:08:53 +01:00
$scope.projects = [];
2018-11-19 16:17:49 +01:00
// list of machines / used for filtering
2018-11-21 11:08:53 +01:00
$scope.machines = machinesPromise;
2018-11-19 16:17:49 +01:00
// list of themes / used for filtering
2018-11-21 11:08:53 +01:00
$scope.themes = themesPromise;
2018-11-19 16:17:49 +01:00
// list of components / used for filtering
2018-11-21 11:08:53 +01:00
$scope.components = componentsPromise;
2015-05-05 03:10:25 +02:00
2018-11-20 12:26:06 +01:00
$scope.searchOverWholeNetworkChanged = function () {
setTimeout(
2018-11-21 11:08:53 +01:00
function () { $scope.resetFiltersAndTriggerSearch(); },
2018-11-20 12:26:06 +01:00
150
2018-11-21 11:08:53 +01:00
);
};
2015-05-05 03:10:25 +02:00
$scope.loadMore = function () {
if ($scope.openlab.searchOverWholeNetwork === true) {
2018-11-21 11:08:53 +01:00
return $scope.projectsPagination.loadMore({ q: $scope.search.q });
} else {
2018-11-21 11:08:53 +01:00
return $scope.projectsPagination.loadMore({ search: $scope.search });
}
2018-11-21 11:08:53 +01:00
};
2015-05-05 03:10:25 +02:00
$scope.resetFiltersAndTriggerSearch = function () {
2018-11-21 11:08:53 +01:00
$scope.search.q = '';
$scope.search.from = undefined;
$scope.search.machine_id = undefined;
$scope.search.component_id = undefined;
$scope.search.theme_id = undefined;
$scope.setUrlQueryParams($scope.search);
return $scope.triggerSearch();
};
2015-05-05 03:10:25 +02:00
$scope.triggerSearch = function () {
2018-11-21 11:08:53 +01:00
const currentPage = parseInt($location.$$search.page) || 1;
if ($scope.openlab.searchOverWholeNetwork === true) {
2018-11-21 11:08:53 +01:00
updateUrlParam('whole_network', 't');
$scope.projectsPagination = new paginationService.Instance(OpenlabProject, currentPage, PROJECTS_PER_PAGE, null, { }, loadMoreOpenlabCallback);
return OpenlabProject.query({ q: $scope.search.q, page: currentPage, per_page: PROJECTS_PER_PAGE }, function (projectsPromise) {
if (projectsPromise.errors != null) {
2019-12-18 11:08:14 +01:00
growl.error(_t('app.public.projects_list.openlab_search_not_available_at_the_moment'));
2018-11-21 11:08:53 +01:00
$scope.openlab.searchOverWholeNetwork = false;
return $scope.triggerSearch();
} else {
2018-11-21 11:08:53 +01:00
$scope.projectsPagination.totalCount = projectsPromise.meta.total;
return $scope.projects = normalizeProjectsAttrs(projectsPromise.projects);
}
2018-11-21 11:08:53 +01:00
});
} else {
2018-11-21 11:08:53 +01:00
updateUrlParam('whole_network', 'f');
$scope.projectsPagination = new paginationService.Instance(Project, currentPage, PROJECTS_PER_PAGE, null, { }, loadMoreCallback, 'search');
return Project.search({ search: $scope.search, page: currentPage, per_page: PROJECTS_PER_PAGE }, function (projectsPromise) {
2018-11-21 11:08:53 +01:00
$scope.projectsPagination.totalCount = projectsPromise.meta.total;
return $scope.projects = projectsPromise.projects;
});
}
2018-11-21 11:08:53 +01:00
};
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
/**
* Callback to switch the user's view to the detailled project page
* @param project {{slug:string}} The project to display
*/
$scope.showProject = function (project) {
if (($scope.openlab.searchOverWholeNetwork === true) && (project.app_id !== Fablab.openlabAppId)) {
2018-11-21 11:08:53 +01:00
$window.open(project.project_url, '_blank');
return true;
} else {
2018-11-21 11:08:53 +01:00
return $state.go('app.public.projects_show', { id: project.slug });
}
2018-11-21 11:08:53 +01:00
};
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
/**
* function to set all url query search parameters from search object
*/
$scope.setUrlQueryParams = function (search) {
2018-11-21 11:08:53 +01:00
updateUrlParam('page', 1);
updateUrlParam('q', search.q);
updateUrlParam('from', search.from);
updateUrlParam('theme_id', search.theme_id);
updateUrlParam('component_id', search.component_id);
return updateUrlParam('machine_id', search.machine_id);
};
2015-05-05 03:10:25 +02:00
/* PRIVATE SCOPE */
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
const initialize = function () {
if ($location.$$search.whole_network === 'f') {
2018-11-21 11:08:53 +01:00
$scope.openlab.searchOverWholeNetwork = false;
} else {
$scope.openlab.searchOverWholeNetwork = ($scope.openlab.projectsActive && Fablab.openlabDefault) || false;
}
2018-11-21 11:08:53 +01:00
return $scope.triggerSearch();
};
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
/**
* function to update url query param, little hack to turn off reloadOnSearch and re-enable it after setting the params
* params example: 'q' , 'presse-purée'
*/
var updateUrlParam = function (name, value) {
2018-11-21 11:08:53 +01:00
$state.current.reloadOnSearch = false;
$location.search(name, value);
return $timeout(function () { $state.current.reloadOnSearch = undefined; });
};
2015-05-05 03:10:25 +02:00
var loadMoreCallback = function (projectsPromise) {
2018-11-21 11:08:53 +01:00
$scope.projects = $scope.projects.concat(projectsPromise.projects);
return updateUrlParam('page', $scope.projectsPagination.currentPage);
};
2016-03-23 18:39:41 +01:00
var loadMoreOpenlabCallback = function (projectsPromise) {
2018-11-21 11:08:53 +01:00
$scope.projects = $scope.projects.concat(normalizeProjectsAttrs(projectsPromise.projects));
return updateUrlParam('page', $scope.projectsPagination.currentPage);
};
2015-05-05 03:10:25 +02:00
2018-11-20 12:26:06 +01:00
var normalizeProjectsAttrs = function (projects) {
return projects.map(function (project) {
2018-11-21 11:08:53 +01:00
project.project_image = project.image_url;
return project;
});
};
2015-05-05 03:10:25 +02:00
2018-11-19 16:17:49 +01:00
// !!! 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
]);
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
/**
* Controller used in the project creation page
*/
Application.Controllers.controller('NewProjectController', ['$scope', '$state', 'Project', 'Machine', 'Member', 'Component', 'Theme', 'Licence', '$document', 'CSRF', 'Diacritics', 'dialogs', 'allowedExtensions', '_t',
function ($scope, $state, Project, Machine, Member, Component, Theme, Licence, $document, CSRF, Diacritics, dialogs, allowedExtensions, _t) {
2018-11-21 11:08:53 +01:00
CSRF.setMetaTags();
2018-11-19 16:17:49 +01:00
// API URL where the form will be posted
2018-11-21 11:08:53 +01:00
$scope.actionUrl = '/api/projects/';
2018-11-19 16:17:49 +01:00
// Form action on the above URL
2018-11-21 11:08:53 +01:00
$scope.method = 'post';
2016-03-23 18:39:41 +01:00
2018-11-19 16:17:49 +01:00
// Default project parameters
$scope.project = {
project_steps_attributes: [],
project_caos_attributes: []
2018-11-21 11:08:53 +01:00
};
2018-11-21 11:08:53 +01:00
$scope.matchingMembers = [];
2018-11-19 16:17:49 +01:00
// Using the ProjectsController
2018-11-21 11:08:53 +01:00
return new ProjectsController($scope, $state, Project, Machine, Member, Component, Theme, Licence, $document, Diacritics, dialogs, allowedExtensions, _t);
}
2018-11-21 11:08:53 +01:00
]);
2018-11-19 16:17:49 +01:00
/**
* Controller used in the project edition page
*/
Application.Controllers.controller('EditProjectController', ['$rootScope', '$scope', '$state', '$stateParams', 'Project', 'Machine', 'Member', 'Component', 'Theme', 'Licence', '$document', 'CSRF', 'projectPromise', 'Diacritics', 'dialogs', 'allowedExtensions', '_t',
function ($rootScope, $scope, $state, $stateParams, Project, Machine, Member, Component, Theme, Licence, $document, CSRF, projectPromise, Diacritics, dialogs, allowedExtensions, _t) {
/* PUBLIC SCOPE */
2018-11-19 16:17:49 +01:00
// API URL where the form will be posted
2018-11-21 11:08:53 +01:00
$scope.actionUrl = `/api/projects/${$stateParams.id}`;
2018-11-19 16:17:49 +01:00
// Form action on the above URL
2018-11-21 11:08:53 +01:00
$scope.method = 'put';
// Retrieve the project's details, if an error occurred, redirect the user to the projects list page
2018-11-21 11:08:53 +01:00
$scope.project = projectPromise;
2018-11-20 12:26:06 +01:00
$scope.matchingMembers = $scope.project.project_users.map(function (u) {
return ({
id: u.id,
name: u.full_name
2018-11-21 11:08:53 +01:00
});
});
/* PRIVATE SCOPE */
/**
* Kind of constructor: these actions will be realized first when the controller is loaded
*/
const initialize = function () {
CSRF.setMetaTags();
if ($scope.project.author_id !== $rootScope.currentUser.id && $scope.project.user_ids.indexOf($rootScope.currentUser.id) === -1 && $scope.currentUser.role !== 'admin') {
$state.go('app.public.projects_show', { id: $scope.project.slug });
console.error('[EditProjectController::initialize] user is not allowed')
}
// Using the ProjectsController
return new ProjectsController($scope, $state, Project, Machine, Member, Component, Theme, Licence, $document, Diacritics, dialogs, allowedExtensions, _t);
}
// !!! MUST BE CALLED AT THE END of the controller
return initialize();
}
2018-11-21 11:08:53 +01:00
]);
2018-11-19 16:17:49 +01:00
/**
* Controller used in the public project's details page
*/
Application.Controllers.controller('ShowProjectController', ['$scope', '$state', 'projectPromise', '$location', '$uibModal', 'dialogs', '_t',
function ($scope, $state, projectPromise, $location, $uibModal, dialogs, _t) {
/* PUBLIC SCOPE */
2018-11-19 16:17:49 +01:00
// Store the project's details
2018-11-21 11:08:53 +01:00
$scope.project = projectPromise;
$scope.projectUrl = $location.absUrl();
$scope.disqusShortname = Fablab.disqusShortname;
2018-11-19 16:17:49 +01:00
/**
* Test if the provided user has the edition rights on the current project
* @param [user] {{id:number}} (optional) the user to check rights
* @returns boolean
*/
$scope.projectEditableBy = function (user) {
2018-11-21 11:08:53 +01:00
if ((user == null)) { return false; }
if ($scope.project.author_id === user.id) { return true; }
let canEdit = false;
angular.forEach($scope.project.project_users, function (u) {
2018-11-21 11:08:53 +01:00
if ((u.id === user.id) && u.is_valid) { return canEdit = true; }
});
return canEdit;
};
2018-11-19 16:17:49 +01:00
/**
* Test if the provided user has the deletion rights on the current project
* @param [user] {{id:number}} (optional) the user to check rights
* @returns boolean
*/
$scope.projectDeletableBy = function (user) {
2018-11-21 11:08:53 +01:00
if ((user == null)) { return false; }
if ($scope.project.author_id === user.id) { return true; }
};
2018-11-19 16:17:49 +01:00
/**
* Callback to delete the current project. Then, the user is redirected to the projects list page,
* which is refreshed. Admins and project owner only are allowed to delete a project
*/
$scope.deleteProject = function () {
// check the permissions
if (($scope.currentUser.role === 'admin') || $scope.projectDeletableBy($scope.currentUser)) {
// delete the project then refresh the projects list
return dialogs.confirm({
resolve: {
object () {
return {
2019-12-18 11:08:14 +01:00
title: _t('app.public.projects_show.confirmation_required'),
msg: _t('app.public.projects_show.do_you_really_want_to_delete_this_project')
2018-11-21 11:08:53 +01:00
};
}
}
}
2018-11-20 12:26:06 +01:00
, function () { // cancel confirmed
2018-11-21 11:08:53 +01:00
$scope.project.$delete(function () { $state.go('app.public.projects_list', {}, { reload: true }); });
});
} else {
2019-12-18 11:08:14 +01:00
return console.error(_t('app.public.projects_show.unauthorized_operation'));
}
2018-11-21 11:08:53 +01:00
};
2018-11-19 16:17:49 +01:00
/**
* Open a modal box containg a form that allow the end-user to signal an abusive content
* @param e {Object} jQuery event
*/
$scope.signalAbuse = function (e) {
2018-11-21 11:08:53 +01:00
if (e) { e.preventDefault(); }
2018-11-20 12:26:06 +01:00
$uibModal.open({
templateUrl: '<%= asset_path "shared/signalAbuseModal.html" %>',
size: 'md',
resolve: {
2018-11-21 11:08:53 +01:00
project () { return $scope.project; }
},
controller: ['$scope', '$uibModalInstance', '_t', 'growl', 'Abuse', 'project', function ($scope, $uibModalInstance, _t, growl, Abuse, project) {
// signaler's profile & signalement infos
$scope.signaler = {
signaled_type: 'Project',
signaled_id: project.id
2018-11-21 11:08:53 +01:00
};
// callback for signaling cancellation
2018-11-21 11:08:53 +01:00
$scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
// callback for form validation
2018-11-21 10:59:07 +01:00
return $scope.ok = function () {
2018-11-20 12:26:06 +01:00
Abuse.save(
{},
{ abuse: $scope.signaler },
function (res) {
// creation successful
2019-12-18 11:08:14 +01:00
growl.success(_t('app.public.projects_show.your_report_was_successful_thanks'));
2018-11-21 11:08:53 +01:00
return $uibModalInstance.close(res);
2018-11-20 12:26:06 +01:00
}
, function (error) {
// creation failed...
2019-12-18 11:08:14 +01:00
growl.error(_t('app.public.projects_show.an_error_occured_while_sending_your_report'));
2018-11-20 12:26:06 +01:00
}
2018-11-21 11:08:53 +01:00
);
};
2018-11-20 12:26:06 +01:00
}]
2018-11-21 11:08:53 +01:00
});
};
2018-11-19 16:17:49 +01:00
/**
* Return the URL allowing to share the current project on the Facebook social network
*/
2018-11-21 11:08:53 +01:00
$scope.shareOnFacebook = function () { return `https://www.facebook.com/share.php?u=${$state.href('app.public.projects_show', { id: $scope.project.slug }, { absolute: true }).replace('#', '%23')}`; };
2018-11-19 16:17:49 +01:00
/**
* Return the URL allowing to share the current project on the Twitter social network
*/
2018-11-21 11:08:53 +01:00
$scope.shareOnTwitter = function () { return `https://twitter.com/intent/tweet?url=${encodeURIComponent($state.href('app.public.projects_show', { id: $scope.project.slug }, { absolute: true }))}&text=${encodeURIComponent($scope.project.name)}`; };
}
2018-11-21 11:08:53 +01:00
]);