scinote-web/app/assets/javascripts/my_modules.js

259 lines
8.8 KiB
JavaScript
Raw Normal View History

/* global I18n dropdownSelector HelperModule animateSpinner */
2019-04-26 21:38:33 +08:00
/* eslint-disable no-use-before-define */
2020-09-16 15:33:52 +08:00
(function() {
function initTaskCollapseState() {
let taskView = '.my-modules-protocols-index';
let taskSection = '.task-section-caret';
let taskId = $(taskView).data('task-id');
2020-09-16 15:33:52 +08:00
function collapseStateSave() {
$(taskView).on('click', taskSection, function() {
let collapsed = $(this).attr('aria-expanded');
let taskSectionType = $(this).attr('aria-controls');
2020-09-16 15:33:52 +08:00
if (collapsed === 'true') {
localStorage.setItem('task_section_collapsed/' + taskId + '/' + taskSectionType, collapsed);
} else {
localStorage.removeItem('task_section_collapsed/' + taskId + '/' + taskSectionType);
}
});
}
2020-09-16 15:33:52 +08:00
function collapseStateLoad() {
$(taskSection).each(function() {
let taskSectionType = $(this).attr('aria-controls');
var collapsed = localStorage.getItem('task_section_collapsed/' + taskId + '/' + taskSectionType);
if (JSON.parse(collapsed)) {
$('#' + taskSectionType).collapse('hide');
}
$(this).closest('.task-section').removeClass('hidden');
});
}
collapseStateSave();
collapseStateLoad();
}
function updateStartDate() {
let updateUrl = $('#startDateContainer').data('update-url');
let val = $('#calendarStartDate').val();
$.ajax({
url: updateUrl,
type: 'PATCH',
dataType: 'json',
data: { my_module: { started_on: val } },
success: function(result) {
$('#startDateLabelContainer').html(result.start_date_label);
},
error: function(response) {
if (response.status === 403) {
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
}
}
});
}
2020-09-16 15:33:52 +08:00
// Bind ajax for editing due dates
function initStartDatePicker() {
2023-10-30 22:14:12 +08:00
$('.datetime-picker-container#start-date').on('dp:ready', () => {
$('#calendarStartDate').data('dateTimePicker').onChange = () => {
updateStartDate();
};
2020-09-16 15:33:52 +08:00
});
2023-11-09 02:32:43 +08:00
window.initDateTimePickerComponent('#calendarStartDateContainer');
2020-09-16 15:33:52 +08:00
}
2020-09-16 15:33:52 +08:00
function updateDueDate() {
let updateUrl = $('#dueDateContainer').data('update-url');
let val = $('#calendarDueDate').val();
$.ajax({
url: updateUrl,
type: 'PATCH',
dataType: 'json',
data: { my_module: { due_date: val } },
success: function(result) {
$('#dueDateLabelContainer').html(result.due_date_label);
},
error: function(response) {
if (response.status === 403) {
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
}
}
});
}
2020-09-16 15:33:52 +08:00
// Bind ajax for editing due dates
function initDueDatePicker() {
2023-10-30 22:14:12 +08:00
$('.datetime-picker-container#due-date').on('dp:ready', () => {
$('#calendarDueDate').data('dateTimePicker').onChange = () => {
updateDueDate();
};
2020-09-16 15:33:52 +08:00
});
2023-11-09 02:32:43 +08:00
window.initDateTimePickerComponent('#calendarDueDateContainer');
2016-02-12 23:52:43 +08:00
}
2020-09-16 15:33:52 +08:00
function initTagsSelector() {
var myModuleTagsSelector = '#module-tags-selector';
dropdownSelector.init($(myModuleTagsSelector), {
2020-09-16 15:33:52 +08:00
closeOnSelect: true,
tagClass: 'my-module-white-tags',
tagStyle: (data) => {
return `background: ${data.params.color}`;
},
customDropdownIcon: () => {
return '';
},
optionLabel: (data) => {
if (data.value > 0) {
return `<span class="my-module-tags-color" style="background:${data.params.color}"></span>
${data.label}`;
}
2023-06-08 14:33:37 +08:00
return `<span class="my-module-tags-color new"><i class="sn-icon sn-icon-new-task"></i></span>
${data.label + ' '}
<span class="my-module-tags-create-new"> ${I18n.t('my_modules.details.create_new_tag')}</span>`;
2020-09-16 15:33:52 +08:00
},
onOpen: function() {
$('.select-container .edit-button-container').removeClass('hidden');
},
onClose: function() {
$('.select-container .edit-button-container').addClass('hidden');
},
onSelect: function() {
var selectElement = $(myModuleTagsSelector);
var lastTag = selectElement.next().find('.ds-tags').last();
var lastTagId = lastTag.find('.tag-label').data('ds-tag-id');
var newTag;
if (lastTagId > 0) {
newTag = { my_module_tag: { tag_id: lastTagId } };
$.post(selectElement.data('update-module-tags-url'), newTag)
.fail(function(response) {
dropdownSelector.removeValue(myModuleTagsSelector, lastTagId, '', true);
if (response.status === 403) {
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
}
});
} else {
newTag = {
tag: {
name: lastTag.find('.tag-label').html(),
project_id: selectElement.data('project-id'),
color: null
},
my_module_id: selectElement.data('module-id'),
simple_creation: true
};
$.post(selectElement.data('tags-create-url'), newTag, function(result) {
dropdownSelector.removeValue(myModuleTagsSelector, 0, '', true);
dropdownSelector.addValue(myModuleTagsSelector, {
value: result.tag.id,
label: result.tag.name,
params: {
color: result.tag.color
}
}, true);
}).fail(function() {
2019-10-18 17:31:13 +08:00
dropdownSelector.removeValue(myModuleTagsSelector, lastTagId, '', true);
2020-09-16 15:33:52 +08:00
});
}
},
onUnSelect: (id) => {
$.post(`${$(myModuleTagsSelector).data('update-module-tags-url')}/${id}/destroy_by_tag_id`)
.success(function() {
dropdownSelector.closeDropdown(myModuleTagsSelector);
})
.fail(function(r) {
if (r.status === 403) {
2020-09-08 21:42:30 +08:00
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
}
2019-10-17 16:06:40 +08:00
});
}
2020-09-16 15:33:52 +08:00
}).getContainer(myModuleTagsSelector).addClass('my-module-tags-container');
}
function initAssignedUsersSelector() {
var myModuleUserSelector = '#module-assigned-users-selector';
2020-04-14 23:25:36 +08:00
dropdownSelector.init(myModuleUserSelector, {
closeOnSelect: true,
labelHTML: true,
tagClass: 'my-module-user-tags',
tagLabel: (data) => {
return `<img class="img-responsive block-inline" src="${data.params.avatar_url}" alt="${data.label}"/>
2023-06-27 17:58:02 +08:00
<span class="user-full-name block-inline">${data.label}</span>`;
},
customDropdownIcon: () => {
return '';
},
optionLabel: (data) => {
if (data.params.avatar_url) {
return `<span class="global-avatar-container" style="margin-top: 10px">
<img src="${data.params.avatar_url}" alt="${data.label}"/></span>
<span style="margin-left: 10px">${data.label}</span>`;
2020-09-16 15:33:52 +08:00
}
return data.label;
},
onSelect: function() {
var selectElement = $(myModuleUserSelector);
var lastUser = selectElement.next().find('.ds-tags').last();
var lastUserId = lastUser.find('.tag-label').data('ds-tag-id');
var newUser;
if (lastUserId > 0) {
newUser = {
user_my_module: {
user_id: lastUserId
}
};
} else {
newUser = {
user_my_module: {
user_id: selectElement.val()
}
};
}
2020-09-16 15:33:52 +08:00
$.post(selectElement.data('users-create-url'), newUser, function(result) {
dropdownSelector.removeValue(myModuleUserSelector, 0, '', true);
dropdownSelector.addValue(myModuleUserSelector, {
value: result.user.id,
label: result.user.full_name,
params: {
avatar_url: result.user.avatar_url,
user_module_id: result.user.user_module_id
}
}, true);
}).fail(function() {
dropdownSelector.removeValue(myModuleUserSelector, lastUserId, '', true);
});
},
onUnSelect: (id) => {
var umID = $(myModuleUserSelector).find(`option[value="${id}"]`).data('params').user_module_id;
$.ajax({
url: `${$(myModuleUserSelector).data('update-module-users-url')}/${umID}`,
type: 'DELETE',
success: () => {
dropdownSelector.closeDropdown(myModuleUserSelector);
},
error: (r) => {
if (r.status === 403) {
HelperModule.flashAlertMsg(I18n.t('general.no_permissions'), 'danger');
}
}
});
}
}).getContainer(myModuleUserSelector).addClass('my-module-users-container');
2020-09-16 15:33:52 +08:00
}
initTaskCollapseState();
initTagsSelector();
initStartDatePicker();
initDueDatePicker();
initAssignedUsersSelector();
}());