2016-02-12 23:52:43 +08:00
|
|
|
// Place all the behaviors and hooks related to the matching controller here.
|
|
|
|
// All this logic will automatically be available in application.js.
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// - error handling of assigning user to project, check XHR data.errors
|
|
|
|
// - error handling of removing user from project, check XHR data.errors
|
|
|
|
// - refresh project users tab after manage user modal is closed
|
|
|
|
// - refactor view handling using library, ex. backbone.js
|
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
/* global Comments CounterBadge animateSpinner initFormSubmitLinks HelperModule
|
2018-11-29 21:20:20 +08:00
|
|
|
I18n setupSidebarTree */
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-01-08 23:30:52 +08:00
|
|
|
(function(global) {
|
2016-02-12 23:52:43 +08:00
|
|
|
var newProjectModal = null;
|
|
|
|
var newProjectModalForm = null;
|
2016-07-21 19:11:15 +08:00
|
|
|
var newProjectModalBody = null;
|
2016-02-12 23:52:43 +08:00
|
|
|
var newProjectBtn = null;
|
|
|
|
|
|
|
|
var editProjectModal = null;
|
|
|
|
var editProjectModalTitle = null;
|
2016-11-14 21:27:35 +08:00
|
|
|
var editProjectModalBody = null;
|
2016-02-12 23:52:43 +08:00
|
|
|
var editProjectBtn = null;
|
|
|
|
|
2018-06-01 22:14:50 +08:00
|
|
|
var projectActionsModal = null;
|
|
|
|
var projectActionsModalHeader = null;
|
|
|
|
var projectActionsModalBody = null;
|
|
|
|
var projectActionsModalFooter = null;
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2018-10-12 04:55:35 +08:00
|
|
|
var exportProjectsModal = null;
|
|
|
|
var exportProjectsModalHeader = null;
|
|
|
|
var exportProjectsModalBody = null;
|
2018-10-16 03:40:59 +08:00
|
|
|
var exportProjectsBtn = null;
|
2018-11-15 22:53:58 +08:00
|
|
|
var exportProjectsSubmit = null;
|
2018-10-12 04:55:35 +08:00
|
|
|
|
2018-09-24 22:25:04 +08:00
|
|
|
var projectsViewMode = 'cards';
|
|
|
|
var projectsViewFilter = $('.projects-view-filter.active').data('filter');
|
2018-10-01 21:39:42 +08:00
|
|
|
var projectsViewFilterChanged = false;
|
|
|
|
var projectsChanged = false;
|
2018-10-10 22:11:52 +08:00
|
|
|
var projectsViewSort = $('#sortMenuDropdown a.disabled').data('sort');
|
2016-11-14 21:27:35 +08:00
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
var TABLE;
|
|
|
|
|
|
|
|
// Array with selected project IDs shared between both views
|
|
|
|
var selectedProjects = [];
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
/**
|
|
|
|
* Initialize the JS for new project modal to work.
|
|
|
|
*/
|
|
|
|
function initNewProjectModal() {
|
2019-04-01 21:02:47 +08:00
|
|
|
newProjectModal.off().on('hidden.bs.modal', function() {
|
2018-09-24 22:25:04 +08:00
|
|
|
var teamSelect = newProjectModalForm.find('select#project_team_id');
|
|
|
|
var teamHidden = newProjectModalForm.find('input#project_visibility_hidden');
|
|
|
|
var teamVisible = newProjectModalForm.find('input#project_visibility_visible');
|
2016-02-12 23:52:43 +08:00
|
|
|
// When closing the new project modal, clear its input vals
|
|
|
|
// and potential errors
|
2016-08-11 22:05:23 +08:00
|
|
|
newProjectModalForm.clearFormErrors();
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
// Clear input fields
|
2016-08-11 22:05:23 +08:00
|
|
|
newProjectModalForm.clearFormFields();
|
2017-01-25 19:12:27 +08:00
|
|
|
teamSelect.val(0);
|
|
|
|
teamSelect.selectpicker('refresh');
|
|
|
|
|
2018-09-24 22:25:04 +08:00
|
|
|
teamHidden.prop('checked', true);
|
|
|
|
teamHidden.attr('checked', 'checked');
|
|
|
|
teamHidden.parent().addClass('active');
|
|
|
|
teamVisible.prop('checked', false);
|
|
|
|
teamVisible.parent().removeClass('active');
|
|
|
|
}).on('show.bs.modal', function() {
|
2017-01-25 19:12:27 +08:00
|
|
|
var teamSelect = newProjectModalForm.find('select#project_team_id');
|
|
|
|
teamSelect.selectpicker('refresh');
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
|
|
|
|
2019-04-01 21:02:47 +08:00
|
|
|
newProjectModalForm.off()
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:beforeSend', function() {
|
|
|
|
animateSpinner(newProjectModalBody);
|
|
|
|
})
|
2018-10-01 21:39:42 +08:00
|
|
|
.on('ajax:success', function(ev, data) {
|
|
|
|
projectsChanged = true;
|
|
|
|
refreshCurrentView();
|
|
|
|
newProjectModal.modal('hide');
|
|
|
|
HelperModule.flashAlertMsg(data.message, 'success');
|
2018-09-24 22:25:04 +08:00
|
|
|
})
|
|
|
|
.on('ajax:error', function(jqxhr, status) {
|
|
|
|
$(this).renderFormErrors('project', status.responseJSON);
|
|
|
|
})
|
|
|
|
.on('ajax:complete', function() {
|
|
|
|
animateSpinner(newProjectModalBody, false);
|
|
|
|
});
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-04-01 21:02:47 +08:00
|
|
|
newProjectBtn.off().click(function() {
|
2016-02-12 23:52:43 +08:00
|
|
|
// Show the modal
|
2018-09-24 22:25:04 +08:00
|
|
|
newProjectModal.modal('show');
|
2019-04-01 21:02:47 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
// init project archive/restore function
|
|
|
|
function initArchiveRestoreButton(el) {
|
2019-04-01 21:02:47 +08:00
|
|
|
el.find('form.edit_project').off()
|
2018-10-01 21:39:42 +08:00
|
|
|
.on('ajax:beforeSend', function() {
|
|
|
|
animateSpinner($('#projects-cards-view').closest('.tab-content'));
|
|
|
|
})
|
|
|
|
.on('ajax:success', function(ev, data) {
|
|
|
|
projectsChanged = true;
|
|
|
|
HelperModule.flashAlertMsg(data.message, 'success');
|
|
|
|
// Project saved, reload view
|
|
|
|
refreshCurrentView();
|
|
|
|
})
|
|
|
|
.on('ajax:error', function(ev, data) {
|
|
|
|
HelperModule.flashAlertMsg(data.responseJSON.message, 'danger');
|
|
|
|
})
|
|
|
|
.on('ajax:complete', function() {
|
|
|
|
animateSpinner($('#projects-cards-view').closest('.tab-content'), false);
|
|
|
|
});
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
function initEditProjectButton(el) {
|
2019-04-01 21:02:47 +08:00
|
|
|
el.find(".dropdown-menu a[data-action='edit']").off()
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:success', function(ev, data) {
|
|
|
|
// Update modal title
|
|
|
|
editProjectModalTitle.html(data.title);
|
|
|
|
|
|
|
|
// Set modal body
|
|
|
|
editProjectModalBody.html(data.html);
|
|
|
|
|
|
|
|
// Add modal body's submit handler
|
2019-04-01 21:02:47 +08:00
|
|
|
editProjectModal.find('form').off()
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:beforeSend', function() {
|
|
|
|
animateSpinner(this);
|
|
|
|
})
|
|
|
|
.on('ajax:success', function(ev2, data2) {
|
2018-10-01 21:39:42 +08:00
|
|
|
projectsChanged = true;
|
2018-09-24 22:25:04 +08:00
|
|
|
// Hide modal
|
|
|
|
editProjectModal.modal('hide');
|
2018-09-27 16:43:44 +08:00
|
|
|
|
|
|
|
HelperModule.flashAlertMsg(data2.message, 'success');
|
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
// Project saved, reload view
|
|
|
|
refreshCurrentView();
|
2018-09-24 22:25:04 +08:00
|
|
|
})
|
|
|
|
.on('ajax:error', function(ev2, data2) {
|
2018-09-27 16:43:44 +08:00
|
|
|
$(this).renderFormErrors('project', data2.responseJSON.errors);
|
2018-09-24 22:25:04 +08:00
|
|
|
})
|
|
|
|
.on('ajax:complete', function() {
|
|
|
|
animateSpinner(this, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Show the modal
|
|
|
|
editProjectModal.modal('show');
|
2016-07-21 19:11:15 +08:00
|
|
|
})
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:error', function() {
|
|
|
|
// TODO
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
/**
|
|
|
|
* Initialize the JS for edit project modal to work.
|
|
|
|
*/
|
|
|
|
function initEditProjectModal() {
|
|
|
|
// Edit button click handler
|
2019-04-01 21:02:47 +08:00
|
|
|
editProjectBtn.off().click(function() {
|
2018-10-01 21:39:42 +08:00
|
|
|
// Submit the modal body's form
|
|
|
|
editProjectModalBody.find('form').submit();
|
|
|
|
});
|
|
|
|
|
|
|
|
// On hide modal handler
|
2019-04-01 21:02:47 +08:00
|
|
|
editProjectModal.off().on('hidden.bs.modal', function() {
|
2018-10-01 21:39:42 +08:00
|
|
|
editProjectModalBody.html('');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
function initManageUsersModal() {
|
|
|
|
// Reload users tab HTML element when modal is closed
|
2019-04-12 15:17:03 +08:00
|
|
|
projectActionsModal.off('hide.bs.modal').on('hide.bs.modal', function() {
|
2018-09-24 22:25:04 +08:00
|
|
|
var projectEl = $('#' + $(this).attr('data-project-id'));
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
// Load HTML to refresh users list
|
|
|
|
$.ajax({
|
2018-09-24 22:25:04 +08:00
|
|
|
url: projectEl.attr('data-project-users-tab-url'),
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
projectEl.find('#users-' + projectEl.attr('id')).html(data.html);
|
|
|
|
CounterBadge.updateCounterBadge(
|
|
|
|
data.counter, data.project_id, 'users'
|
|
|
|
);
|
2016-02-12 23:52:43 +08:00
|
|
|
initUsersEditLink(projectEl);
|
2018-10-01 21:39:42 +08:00
|
|
|
projectsChanged = true;
|
2016-02-12 23:52:43 +08:00
|
|
|
},
|
2018-09-24 22:25:04 +08:00
|
|
|
error: function() {
|
2016-02-12 23:52:43 +08:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Remove modal content when modal window is closed.
|
2019-04-12 15:17:03 +08:00
|
|
|
projectActionsModal.off('hidden.bs.modal').on('hidden.bs.modal', function() {
|
2018-09-24 22:25:04 +08:00
|
|
|
projectActionsModalHeader.html('');
|
|
|
|
projectActionsModalBody.html('');
|
|
|
|
projectActionsModalFooter.html('');
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize users editing modal remote loading.
|
2019-01-08 23:30:52 +08:00
|
|
|
global.initUsersEditLink = function($el) {
|
2019-04-01 21:02:47 +08:00
|
|
|
$el.find('.manage-users-link').off()
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:before', function() {
|
|
|
|
var projectId = $(this).closest('.panel-default').attr('id');
|
|
|
|
projectActionsModal.attr('data-project-id', projectId);
|
|
|
|
projectActionsModal.modal('show');
|
|
|
|
})
|
|
|
|
.on('ajax:success', function(e, data) {
|
|
|
|
$('#manage-users-modal-project').text(data.project.name);
|
|
|
|
initUsersModalBody(data);
|
|
|
|
});
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
|
2018-10-12 04:55:35 +08:00
|
|
|
/**
|
|
|
|
* Initialize the JS for export projects modal to work.
|
|
|
|
*/
|
|
|
|
function initExportProjectsModal() {
|
2018-12-13 23:46:08 +08:00
|
|
|
exportProjectsBtn.off('click').click(function() {
|
2018-10-12 06:48:12 +08:00
|
|
|
// Load HTML to refresh users list
|
|
|
|
$.ajax({
|
2018-11-15 22:53:58 +08:00
|
|
|
url: exportProjectsBtn.data('export-projects-modal-url'),
|
2018-11-16 20:37:32 +08:00
|
|
|
type: 'GET',
|
2018-10-12 06:48:12 +08:00
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
project_ids: selectedProjects
|
|
|
|
},
|
|
|
|
success: function(data) {
|
|
|
|
// Update modal title
|
|
|
|
exportProjectsModalHeader.html(data.title);
|
|
|
|
|
|
|
|
// Set modal body
|
|
|
|
exportProjectsModalBody.html(data.html);
|
|
|
|
|
2018-11-16 20:37:32 +08:00
|
|
|
// Update modal footer (show/hide buttons)
|
2018-11-15 22:53:58 +08:00
|
|
|
if (data.status === 'error') {
|
2018-11-16 20:37:32 +08:00
|
|
|
$('#export-projects-modal-close').show();
|
|
|
|
$('#export-projects-modal-cancel').hide();
|
2018-11-15 22:53:58 +08:00
|
|
|
exportProjectsSubmit.hide();
|
|
|
|
} else {
|
2018-11-16 20:37:32 +08:00
|
|
|
$('#export-projects-modal-close').hide();
|
|
|
|
$('#export-projects-modal-cancel').show();
|
2018-11-15 22:53:58 +08:00
|
|
|
exportProjectsSubmit.show();
|
|
|
|
}
|
2018-10-12 06:48:12 +08:00
|
|
|
// Show the modal
|
|
|
|
exportProjectsModal.modal('show');
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
// TODO
|
|
|
|
}
|
2018-10-12 04:55:35 +08:00
|
|
|
});
|
2018-10-12 06:48:12 +08:00
|
|
|
});
|
2018-10-12 04:55:35 +08:00
|
|
|
|
|
|
|
// Remove modal content when modal window is closed.
|
2019-04-01 21:02:47 +08:00
|
|
|
exportProjectsModal.off().on('hidden.bs.modal', function() {
|
2018-10-12 04:55:35 +08:00
|
|
|
exportProjectsModalHeader.html('');
|
|
|
|
exportProjectsModalBody.html('');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-15 22:53:58 +08:00
|
|
|
function initExportProjects() {
|
|
|
|
// Submit the export projects
|
2018-12-13 23:46:08 +08:00
|
|
|
exportProjectsSubmit.off('click').click(function() {
|
2018-11-15 22:53:58 +08:00
|
|
|
$.ajax({
|
|
|
|
url: exportProjectsSubmit.data('export-projects-submit-url'),
|
|
|
|
type: 'POST',
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
project_ids: selectedProjects
|
|
|
|
},
|
|
|
|
success: function(data) {
|
|
|
|
// Hide modal and show success flash
|
|
|
|
exportProjectsModal.modal('hide');
|
|
|
|
HelperModule.flashAlertMsg(data.flash, 'success');
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
// Initialize reloading manage user modal content after posting new
|
|
|
|
// user.
|
2016-09-27 15:00:02 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
function initAddUserForm() {
|
2019-04-01 21:02:47 +08:00
|
|
|
projectActionsModalBody.find('.add-user-form').off()
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:success', function(e, data) {
|
|
|
|
var errorBlock;
|
2016-02-12 23:52:43 +08:00
|
|
|
initUsersModalBody(data);
|
|
|
|
if (data.status === 'error') {
|
2018-09-24 22:25:04 +08:00
|
|
|
$(this).addClass('has-error');
|
|
|
|
errorBlock = $(this).find('span.help-block');
|
|
|
|
if (errorBlock.length && errorBlock.length > 0) {
|
2016-07-21 19:11:15 +08:00
|
|
|
errorBlock.html(data.error);
|
|
|
|
} else {
|
2018-09-24 22:25:04 +08:00
|
|
|
$(this).append("<span class='help-block col-xs-8'>" + data.error + '</span>');
|
2016-07-21 19:11:15 +08:00
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize remove user from project links.
|
|
|
|
function initRemoveUserLinks() {
|
2019-04-01 21:02:47 +08:00
|
|
|
projectActionsModalBody.find('.remove-user-link').off()
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:success', function(e, data) {
|
2016-02-12 23:52:43 +08:00
|
|
|
initUsersModalBody(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
function initUserRoleForms() {
|
2019-04-01 21:02:47 +08:00
|
|
|
projectActionsModalBody.find('.update-user-form select').off()
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('change', function() {
|
|
|
|
$(this).parents('form').submit();
|
2016-02-12 23:52:43 +08:00
|
|
|
});
|
|
|
|
|
2019-04-01 21:02:47 +08:00
|
|
|
projectActionsModalBody.find('.update-user-form').off()
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:success', function(e, data) {
|
2016-02-12 23:52:43 +08:00
|
|
|
initUsersModalBody(data);
|
|
|
|
})
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:error', function() {
|
2016-02-12 23:52:43 +08:00
|
|
|
// TODO
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize ajax listeners and elements style on modal body. This
|
|
|
|
// function must be called when modal body is changed.
|
|
|
|
function initUsersModalBody(data) {
|
2018-06-01 22:14:50 +08:00
|
|
|
projectActionsModalHeader.html(data.html_header);
|
|
|
|
projectActionsModalBody.html(data.html_body);
|
|
|
|
projectActionsModalFooter.html(data.html_footer);
|
2018-09-24 22:25:04 +08:00
|
|
|
projectActionsModalBody.find('.selectpicker').selectpicker();
|
2016-02-12 23:52:43 +08:00
|
|
|
initAddUserForm();
|
|
|
|
initRemoveUserLinks();
|
|
|
|
initUserRoleForms();
|
|
|
|
}
|
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
function updateSelectedCards() {
|
|
|
|
$('.panel-project').removeClass('selected');
|
|
|
|
$('.project-card-selector').prop('checked', false);
|
|
|
|
$.each(selectedProjects, function(index, value) {
|
|
|
|
var selectedCard = $('.panel-project[id=' + value + ']');
|
|
|
|
selectedCard.addClass('selected');
|
|
|
|
selectedCard.find('.project-card-selector').prop('checked', true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-24 22:25:04 +08:00
|
|
|
/**
|
2018-10-01 21:39:42 +08:00
|
|
|
* Initializes cards view
|
2018-09-24 22:25:04 +08:00
|
|
|
*/
|
|
|
|
function init() {
|
|
|
|
newProjectModal = $('#new-project-modal');
|
|
|
|
newProjectModalForm = newProjectModal.find('form');
|
|
|
|
newProjectModalBody = newProjectModal.find('.modal-body');
|
2018-11-06 18:38:31 +08:00
|
|
|
newProjectBtn = $('.new-project-btn');
|
2018-09-24 22:25:04 +08:00
|
|
|
|
|
|
|
editProjectModal = $('#edit-project-modal');
|
|
|
|
editProjectModalTitle = editProjectModal.find('#edit-project-modal-label');
|
|
|
|
editProjectModalBody = editProjectModal.find('.modal-body');
|
|
|
|
editProjectBtn = editProjectModal.find(".btn[data-action='submit']");
|
|
|
|
|
|
|
|
projectActionsModal = $('#project-actions-modal');
|
|
|
|
projectActionsModalHeader = projectActionsModal.find('.modal-title');
|
|
|
|
projectActionsModalBody = projectActionsModal.find('.modal-body');
|
|
|
|
projectActionsModalFooter = projectActionsModal.find('.modal-footer');
|
|
|
|
|
2018-10-12 04:55:35 +08:00
|
|
|
exportProjectsModal = $('#export-projects-modal');
|
|
|
|
exportProjectsModalHeader = exportProjectsModal.find('.modal-title');
|
|
|
|
exportProjectsModalBody = exportProjectsModal.find('.modal-body');
|
2018-10-16 03:40:59 +08:00
|
|
|
exportProjectsBtn = $('#export-projects-button');
|
2018-11-15 22:53:58 +08:00
|
|
|
exportProjectsSubmit = $('#export-projects-modal-submit');
|
2018-10-12 04:55:35 +08:00
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
updateSelectedCards();
|
2018-09-24 22:25:04 +08:00
|
|
|
initNewProjectModal();
|
|
|
|
initEditProjectModal();
|
|
|
|
initManageUsersModal();
|
2018-10-12 04:55:35 +08:00
|
|
|
initExportProjectsModal();
|
2018-11-15 22:53:58 +08:00
|
|
|
initExportProjects();
|
2018-09-24 22:25:04 +08:00
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
initEditProjectButton($('.panel-project'));
|
|
|
|
initArchiveRestoreButton($('.panel-project'));
|
|
|
|
|
2018-12-13 23:46:08 +08:00
|
|
|
$('.project-card-selector').off('click').click(function() {
|
2018-10-01 21:39:42 +08:00
|
|
|
var projectId = $(this).closest('.panel-project').data('id');
|
|
|
|
// Determine whether ID is in the list of selected project IDs
|
|
|
|
var index = $.inArray(projectId, selectedProjects);
|
|
|
|
|
|
|
|
// If checkbox is checked and row ID is not in list of selected project IDs
|
|
|
|
if (this.checked && index === -1) {
|
2018-09-27 16:43:44 +08:00
|
|
|
$(this).closest('.panel-project').addClass('selected');
|
2018-10-01 21:39:42 +08:00
|
|
|
selectedProjects.push(projectId);
|
2018-11-12 22:51:03 +08:00
|
|
|
exportProjectsBtn.removeAttr('disabled');
|
2018-10-01 21:39:42 +08:00
|
|
|
// Otherwise, if checkbox is not checked and ID is in list of selected IDs
|
|
|
|
} else if (!this.checked && index !== -1) {
|
2018-09-27 16:43:44 +08:00
|
|
|
$(this).closest('.panel-project').removeClass('selected');
|
2018-10-01 21:39:42 +08:00
|
|
|
selectedProjects.splice(index, 1);
|
2018-10-16 03:40:59 +08:00
|
|
|
|
2018-10-16 03:44:24 +08:00
|
|
|
if (selectedProjects.length === 0) {
|
2018-11-12 22:51:03 +08:00
|
|
|
exportProjectsBtn.attr('disabled', 'disabled');
|
2018-10-16 03:44:24 +08:00
|
|
|
}
|
2018-09-27 16:43:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-09-24 22:25:04 +08:00
|
|
|
// initialize project tab remote loading
|
|
|
|
$('.panel-project .active').removeClass('active');
|
2019-04-01 21:02:47 +08:00
|
|
|
$('.panel-project .panel-footer [role=tab]').off()
|
2018-09-24 22:25:04 +08:00
|
|
|
.on('ajax:before', function() {
|
|
|
|
var $this = $(this);
|
|
|
|
var parentNode = $this.parents('li');
|
|
|
|
var targetId = $this.attr('aria-controls');
|
|
|
|
|
|
|
|
if (parentNode.hasClass('active')) {
|
|
|
|
// TODO move to fn
|
|
|
|
parentNode.removeClass('active');
|
|
|
|
$('#' + targetId).removeClass('active');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.on('ajax:success', function(e, data) {
|
|
|
|
var $this = $(this);
|
|
|
|
var targetId = $this.attr('aria-controls');
|
|
|
|
var target = $('#' + targetId);
|
|
|
|
var parentNode = $this.parents('ul').parent();
|
|
|
|
|
|
|
|
target.html(data.html);
|
|
|
|
initUsersEditLink(parentNode);
|
|
|
|
parentNode.find('.active').removeClass('active');
|
|
|
|
$this.parents('li').addClass('active');
|
|
|
|
target.addClass('active');
|
|
|
|
|
2019-06-04 20:40:21 +08:00
|
|
|
Comments.init('simple')
|
2018-09-24 22:25:04 +08:00
|
|
|
})
|
|
|
|
.on('ajax:error', function() {
|
|
|
|
// TODO
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
function refreshCurrentView() {
|
|
|
|
if (projectsViewMode === 'cards') {
|
|
|
|
loadCardsView();
|
|
|
|
} else {
|
|
|
|
TABLE.draw();
|
|
|
|
}
|
2018-11-29 21:20:20 +08:00
|
|
|
// Also refresh sidebar tree navigation
|
|
|
|
$.ajax({
|
|
|
|
url: $('#projects-cards-view').data('projects-sidebar-url'),
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(data) {
|
|
|
|
$('#slide-panel .tree').html('<ul>' + data.html + '</ul>');
|
|
|
|
setupSidebarTree();
|
|
|
|
}
|
|
|
|
});
|
2018-10-01 21:39:42 +08:00
|
|
|
}
|
|
|
|
|
2018-09-27 16:43:44 +08:00
|
|
|
function loadCardsView() {
|
|
|
|
// Load HTML with projects list
|
|
|
|
var viewContainer = $('#projects-cards-view');
|
|
|
|
animateSpinner(viewContainer, true);
|
|
|
|
$.ajax({
|
|
|
|
url: $('#projects-cards-view').data('projects-url'),
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
data: {
|
|
|
|
filter: projectsViewFilter,
|
|
|
|
sort: projectsViewSort
|
|
|
|
},
|
|
|
|
success: function(data) {
|
2018-11-07 20:45:46 +08:00
|
|
|
viewContainer.html(data.html);
|
|
|
|
if (data.count === 0 && projectsViewFilter !== 'archived') {
|
2018-10-24 09:34:13 +08:00
|
|
|
$('#projects-present').hide();
|
|
|
|
$('#projects-absent').show();
|
2018-11-07 20:45:46 +08:00
|
|
|
} else {
|
|
|
|
$('#projects-absent').hide();
|
|
|
|
$('#projects-present').show();
|
2018-10-24 09:34:13 +08:00
|
|
|
}
|
2018-09-27 16:43:44 +08:00
|
|
|
initFormSubmitLinks(viewContainer);
|
|
|
|
init();
|
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
viewContainer.html('Error loading project list');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initProjectsViewFilter() {
|
|
|
|
$('.projects-view-filter').click(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
if ($(this).data('filter') === projectsViewFilter) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-01 21:39:42 +08:00
|
|
|
$('.projects-view-filter').removeClass('active');
|
|
|
|
$(this).addClass('active');
|
|
|
|
selectedProjects = [];
|
2018-09-27 16:43:44 +08:00
|
|
|
projectsViewFilter = $(this).data('filter');
|
2018-10-01 21:39:42 +08:00
|
|
|
projectsViewFilterChanged = true;
|
2018-09-27 16:43:44 +08:00
|
|
|
if ($('#projects-cards-view').hasClass('active')) {
|
|
|
|
loadCardsView();
|
2018-10-01 21:39:42 +08:00
|
|
|
} else if (!$.isEmptyObject(TABLE)) {
|
|
|
|
TABLE.draw();
|
2018-09-27 16:43:44 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initProjectsViewModeSwitch() {
|
2019-04-01 21:02:47 +08:00
|
|
|
$('input[name=projects-view-mode-selector]').off().on('change', function() {
|
2018-09-27 16:43:44 +08:00
|
|
|
if ($(this).val() === projectsViewMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
projectsViewMode = $(this).val();
|
2018-10-01 21:39:42 +08:00
|
|
|
if (projectsChanged) {
|
|
|
|
refreshCurrentView();
|
|
|
|
}
|
|
|
|
projectsChanged = false;
|
2018-09-27 16:43:44 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initSorting() {
|
|
|
|
$('#sortMenuDropdown a').click(function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
if (projectsViewSort !== $(this).data('sort')) {
|
|
|
|
$('#sortMenuDropdown a').removeClass('disabled');
|
|
|
|
projectsViewSort = $(this).data('sort');
|
|
|
|
loadCardsView();
|
|
|
|
$(this).addClass('disabled');
|
|
|
|
$('#sortMenu').dropdown('toggle');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-01 21:39:42 +08:00
|
|
|
// Updates "Select all" control in a data table
|
|
|
|
function updateDataTableSelectAllCtrl() {
|
|
|
|
var $table = TABLE.table().node();
|
|
|
|
var $header = TABLE.table().header();
|
|
|
|
var $chkboxAll = $('.project-row-selector', $table);
|
|
|
|
var $chkboxChecked = $('.project-row-selector:checked', $table);
|
|
|
|
var chkboxSelectAll = $('input[name="select_all"]', $header).get(0);
|
|
|
|
|
|
|
|
// If none of the checkboxes are checked
|
|
|
|
if ($chkboxChecked.length === 0) {
|
|
|
|
chkboxSelectAll.checked = false;
|
|
|
|
if ('indeterminate' in chkboxSelectAll) {
|
|
|
|
chkboxSelectAll.indeterminate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all of the checkboxes are checked
|
|
|
|
} else if ($chkboxChecked.length === $chkboxAll.length) {
|
|
|
|
chkboxSelectAll.checked = true;
|
|
|
|
if ('indeterminate' in chkboxSelectAll) {
|
|
|
|
chkboxSelectAll.indeterminate = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If some of the checkboxes are checked
|
|
|
|
} else {
|
|
|
|
chkboxSelectAll.checked = true;
|
|
|
|
if ('indeterminate' in chkboxSelectAll) {
|
|
|
|
chkboxSelectAll.indeterminate = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function initRowSelection() {
|
|
|
|
// Handle clicks on checkbox
|
|
|
|
$('.dt-body-center .project-row-selector').change(function(e) {
|
|
|
|
// Get row ID
|
|
|
|
var $row = $(this).closest('tr');
|
|
|
|
var data = TABLE.row($row).data();
|
|
|
|
var rowId = data.DT_RowId;
|
|
|
|
|
|
|
|
// Determine whether row ID is in the list of selected project IDs
|
|
|
|
var index = $.inArray(rowId, selectedProjects);
|
|
|
|
|
|
|
|
// If checkbox is checked and row ID is not in list of selected project IDs
|
|
|
|
if (this.checked && index === -1) {
|
|
|
|
selectedProjects.push(rowId);
|
2018-11-12 22:51:03 +08:00
|
|
|
exportProjectsBtn.removeAttr('disabled');
|
2018-10-01 21:39:42 +08:00
|
|
|
// Otherwise, if checkbox is not checked and ID is in list of selected IDs
|
|
|
|
} else if (!this.checked && index !== -1) {
|
|
|
|
selectedProjects.splice(index, 1);
|
2018-10-16 03:40:59 +08:00
|
|
|
|
2018-11-12 22:51:03 +08:00
|
|
|
if (selectedProjects.length === 0) {
|
|
|
|
exportProjectsBtn.attr('disabled', 'disabled');
|
2018-10-16 03:44:24 +08:00
|
|
|
}
|
2018-10-01 21:39:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
updateDataTableSelectAllCtrl();
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle click on "Select all" control
|
|
|
|
$('.dataTables_scrollHead input[name="select_all"]').change(function(e) {
|
|
|
|
if (this.checked) {
|
|
|
|
$('.project-row-selector:not(:checked)').trigger('click');
|
|
|
|
} else {
|
|
|
|
$('.project-row-selector:checked').trigger('click');
|
|
|
|
}
|
|
|
|
// Prevent click event from propagating to parent
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateSelectedRows() {
|
|
|
|
TABLE.rows().every(function() {
|
|
|
|
var rowSelector = $(this.node()).find('input[type="checkbox"]');
|
|
|
|
var rowId = this.data().DT_RowId;
|
|
|
|
|
|
|
|
if ($.inArray(rowId, selectedProjects) !== -1) {
|
|
|
|
rowSelector.prop('checked', true);
|
|
|
|
} else {
|
|
|
|
rowSelector.prop('checked', false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
updateDataTableSelectAllCtrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataTableInit() {
|
|
|
|
var TABLE_ID = '#projects-overview-table';
|
|
|
|
TABLE = $(TABLE_ID).DataTable({
|
2019-08-09 15:41:32 +08:00
|
|
|
dom: "R<'row'<'col-sm-9-custom toolbar'l><'col-sm-3-custom'f>><'row'<'col-sm-12't>><'row'<'col-sm-7'i><'col-sm-5'p>>",
|
2018-10-10 22:11:52 +08:00
|
|
|
stateSave: true,
|
2018-10-12 21:15:43 +08:00
|
|
|
stateDuration: 0,
|
2018-10-01 21:39:42 +08:00
|
|
|
processing: true,
|
|
|
|
serverSide: true,
|
|
|
|
scrollY: '64vh',
|
|
|
|
scrollCollapse: true,
|
|
|
|
destroy: true,
|
|
|
|
ajax: {
|
|
|
|
url: $(TABLE_ID).data('source'),
|
|
|
|
global: false,
|
|
|
|
type: 'POST',
|
|
|
|
data: function(params) {
|
|
|
|
params.filter = projectsViewFilter;
|
|
|
|
// return { ...params, ...{ filter: projectsViewFilter } };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
colReorder: {
|
|
|
|
fixedColumnsLeft: 9
|
|
|
|
},
|
|
|
|
columnDefs: [{
|
|
|
|
// Checkbox column needs special handling
|
|
|
|
targets: 0,
|
|
|
|
searchable: false,
|
|
|
|
orderable: false,
|
|
|
|
className: 'dt-body-center',
|
|
|
|
sWidth: '1%',
|
|
|
|
render: function() {
|
|
|
|
return "<input class='project-row-selector' type='checkbox'>";
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
targets: 8,
|
|
|
|
searchable: false,
|
|
|
|
orderable: false,
|
|
|
|
className: 'dt-body-center',
|
|
|
|
sWidth: '1%'
|
|
|
|
}],
|
|
|
|
oLanguage: {
|
|
|
|
sSearch: I18n.t('general.filter')
|
|
|
|
},
|
|
|
|
rowCallback: function(row, data) {
|
|
|
|
// Get row ID
|
|
|
|
var rowId = data.DT_RowId;
|
|
|
|
var dropdown = $(row).find('.dropdown');
|
|
|
|
var dropdownCell = dropdown.closest('td');
|
|
|
|
// If row ID is in the list of selected row IDs
|
|
|
|
if ($.inArray(rowId, selectedProjects) !== -1) {
|
|
|
|
$(row).find('input[type="checkbox"]').prop('checked', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
initEditProjectButton($(row));
|
|
|
|
initArchiveRestoreButton($(row));
|
|
|
|
|
2019-04-01 21:02:47 +08:00
|
|
|
dropdown.off().on('show.bs.dropdown', function() {
|
2018-10-01 21:39:42 +08:00
|
|
|
$('body').append(dropdown.css({
|
|
|
|
left: dropdown.offset().left,
|
|
|
|
position: 'absolute',
|
|
|
|
top: dropdown.offset().top
|
|
|
|
}).detach());
|
|
|
|
});
|
2019-04-01 21:02:47 +08:00
|
|
|
dropdown.off().on('hidden.bs.dropdown', function() {
|
2018-10-01 21:39:42 +08:00
|
|
|
dropdownCell.append(dropdown.removeAttr('style').detach());
|
|
|
|
});
|
|
|
|
},
|
|
|
|
order: [[2, 'asc']],
|
|
|
|
columns: [
|
|
|
|
{ data: 'checkbox' },
|
|
|
|
{ data: 'status' },
|
|
|
|
{ data: 'name' },
|
|
|
|
{ data: 'start' },
|
|
|
|
{ data: 'visibility' },
|
|
|
|
{ data: 'users' },
|
|
|
|
{ data: 'experiments' },
|
|
|
|
{ data: 'tasks' },
|
|
|
|
{ data: 'actions' }
|
|
|
|
],
|
|
|
|
fnDrawCallback: function() {
|
|
|
|
animateSpinner(this, false);
|
|
|
|
updateDataTableSelectAllCtrl();
|
|
|
|
initRowSelection();
|
|
|
|
initFormSubmitLinks($(this));
|
|
|
|
},
|
2018-10-10 22:11:52 +08:00
|
|
|
stateLoadCallback: function(settings, callback) {
|
|
|
|
$.ajax({
|
|
|
|
url: $(TABLE_ID).data('state-load-source'),
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'GET',
|
|
|
|
success: function(json) {
|
|
|
|
callback(json.state);
|
|
|
|
}
|
|
|
|
});
|
2018-10-01 21:39:42 +08:00
|
|
|
},
|
2018-10-10 22:11:52 +08:00
|
|
|
stateSaveCallback: function() {
|
|
|
|
// Don't do anything, state will be updated at backend, based on params
|
2018-10-01 21:39:42 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle click on table cells with checkboxes
|
2019-04-01 21:02:47 +08:00
|
|
|
$(TABLE_ID).off().on('click', 'tbody td', function(e) {
|
2018-10-01 21:39:42 +08:00
|
|
|
if ($(e.target).is(
|
|
|
|
'.project-row-selector, .active-project-link, button, span'
|
|
|
|
)) {
|
|
|
|
// Skip if clicking on selector checkbox, links and buttons
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$(this).parent().find('.project-row-selector').trigger('click');
|
|
|
|
});
|
|
|
|
|
|
|
|
return TABLE;
|
|
|
|
}
|
|
|
|
|
2019-04-01 21:02:47 +08:00
|
|
|
$('.projects-view-mode-switch a').off().on('shown.bs.tab', function(event) {
|
2018-10-01 21:39:42 +08:00
|
|
|
if ($(event.target).data('mode') === 'table') {
|
|
|
|
// table tab
|
|
|
|
$('#sortMenu').hide();
|
2018-11-07 20:45:46 +08:00
|
|
|
$('#projects-absent').hide();
|
|
|
|
$('#projects-present').show();
|
2018-10-01 21:39:42 +08:00
|
|
|
if ($.isEmptyObject(TABLE)) {
|
|
|
|
dataTableInit();
|
|
|
|
} else if (projectsViewFilterChanged) {
|
|
|
|
TABLE.draw();
|
|
|
|
} else {
|
|
|
|
updateSelectedRows();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// cards tab
|
|
|
|
$('#sortMenu').show();
|
|
|
|
if (projectsViewFilterChanged) {
|
|
|
|
loadCardsView();
|
|
|
|
}
|
|
|
|
updateSelectedCards();
|
|
|
|
}
|
|
|
|
projectsViewFilterChanged = false;
|
|
|
|
});
|
|
|
|
|
2018-09-27 16:43:44 +08:00
|
|
|
initProjectsViewFilter();
|
|
|
|
initProjectsViewModeSwitch();
|
|
|
|
initSorting();
|
|
|
|
loadCardsView();
|
2019-01-08 23:30:52 +08:00
|
|
|
}(window));
|