2022-12-23 00:39:51 +08:00
|
|
|
/* global dropdownSelector initBSTooltips I18n */
|
2022-12-05 19:55:57 +08:00
|
|
|
|
2022-09-29 05:18:36 +08:00
|
|
|
(function() {
|
|
|
|
function initNewMyModuleModal() {
|
2022-11-24 21:49:42 +08:00
|
|
|
let experimentWrapper = '.experiment-new-my_module';
|
2022-09-29 05:18:36 +08:00
|
|
|
let newMyModuleModal = '#new-my-module-modal';
|
2022-12-05 19:55:57 +08:00
|
|
|
let myModuleUserSelector = '#my_module_user_ids';
|
2022-12-23 00:39:51 +08:00
|
|
|
var myModuleTagsSelector = '#module-tags-selector';
|
2022-12-19 18:55:38 +08:00
|
|
|
|
2022-09-29 05:18:36 +08:00
|
|
|
|
|
|
|
// Modal's submit handler function
|
|
|
|
$(experimentWrapper)
|
|
|
|
.on('ajax:success', newMyModuleModal, function() {
|
|
|
|
$(newMyModuleModal).modal('hide');
|
|
|
|
})
|
|
|
|
.on('ajax:error', newMyModuleModal, function(ev, data) {
|
|
|
|
$(this).renderFormErrors('my_module', data.responseJSON);
|
2022-12-05 19:55:57 +08:00
|
|
|
})
|
|
|
|
.on('submit', newMyModuleModal, function() {
|
|
|
|
// To submit correct assigned user ids to new modal
|
|
|
|
// Clear default selected user in dropdown
|
|
|
|
$(`${myModuleUserSelector} option[value=${$('#new-my-module-modal').data('user-id')}]`)
|
|
|
|
.prop('selected', false);
|
|
|
|
$.map(dropdownSelector.getValues(myModuleUserSelector), function(val) {
|
|
|
|
$(`${myModuleUserSelector} option[value=${val}]`).prop('selected', true);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.on('ajax:success', '.new-my-module-button', function(ev, result) {
|
2022-09-29 05:18:36 +08:00
|
|
|
// Add and show modal
|
2022-12-05 19:55:57 +08:00
|
|
|
$(experimentWrapper).append($.parseHTML(result.html));
|
2022-09-29 05:18:36 +08:00
|
|
|
$(newMyModuleModal).modal('show');
|
|
|
|
$(newMyModuleModal).find("input[type='text']").focus();
|
|
|
|
|
|
|
|
// Remove modal when it gets closed
|
|
|
|
$(newMyModuleModal).on('hidden.bs.modal', function() {
|
|
|
|
$(newMyModuleModal).remove();
|
|
|
|
});
|
2022-12-05 19:55:57 +08:00
|
|
|
|
|
|
|
// initiaize user assing dropdown menu
|
|
|
|
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}"/>
|
|
|
|
<span style="user-full-name block-inline">${data.label}</span>`;
|
|
|
|
},
|
|
|
|
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>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.label;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
dropdownSelector.selectValues(myModuleUserSelector, $('#new-my-module-modal').data('user-id'));
|
2022-12-19 18:55:38 +08:00
|
|
|
|
|
|
|
dropdownSelector.init($(myModuleTagsSelector), {
|
|
|
|
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-01-10 20:07:10 +08:00
|
|
|
return `<span class="my-module-tags-color new"><i class="fas fa-plus"></i></span>
|
2022-12-19 18:55:38 +08:00
|
|
|
${data.label + ' '}
|
2023-01-10 20:07:10 +08:00
|
|
|
<span class="my-module-tags-create-new"> ${I18n.t('my_modules.details.create_new_tag')}</span>`;
|
2022-12-19 18:55:38 +08:00
|
|
|
},
|
2022-12-23 00:39:51 +08:00
|
|
|
ajaxParams: function(params) {
|
|
|
|
let newParams = params;
|
|
|
|
newParams.selected_tags = JSON.stringify(dropdownSelector.getValues(myModuleTagsSelector));
|
|
|
|
return newParams;
|
2022-12-19 18:55:38 +08:00
|
|
|
},
|
|
|
|
onSelect: function() {
|
|
|
|
var selectElement = $(myModuleTagsSelector);
|
|
|
|
var lastTag = selectElement.next().find('.ds-tags').last();
|
|
|
|
var lastTagId = lastTag.find('.tag-label').data('ds-tag-id');
|
|
|
|
|
|
|
|
if (lastTagId > 0) {
|
2022-12-23 00:39:51 +08:00
|
|
|
$('#my_module_tag_ids').val(JSON.stringify(dropdownSelector.getValues(myModuleTagsSelector)));
|
2022-12-19 18:55:38 +08:00
|
|
|
} else {
|
2022-12-23 00:39:51 +08:00
|
|
|
let newTag = {
|
2022-12-19 18:55:38 +08:00
|
|
|
tag: {
|
|
|
|
name: lastTag.find('.tag-label').html(),
|
|
|
|
project_id: selectElement.data('project-id'),
|
|
|
|
color: null
|
|
|
|
},
|
|
|
|
simple_creation: true
|
|
|
|
};
|
2022-12-20 14:37:40 +08:00
|
|
|
$.post(selectElement.data('tags-create-url'), newTag, function(res) {
|
2022-12-19 18:55:38 +08:00
|
|
|
dropdownSelector.removeValue(myModuleTagsSelector, 0, '', true);
|
|
|
|
dropdownSelector.addValue(myModuleTagsSelector, {
|
2022-12-20 14:37:40 +08:00
|
|
|
value: res.tag.id,
|
|
|
|
label: res.tag.name,
|
2022-12-19 18:55:38 +08:00
|
|
|
params: {
|
2022-12-20 14:37:40 +08:00
|
|
|
color: res.tag.color
|
2022-12-19 18:55:38 +08:00
|
|
|
}
|
|
|
|
}, true);
|
2022-12-23 00:39:51 +08:00
|
|
|
$('#my_module_tag_ids').val(JSON.stringify(dropdownSelector.getValues(myModuleTagsSelector)));
|
2022-12-19 18:55:38 +08:00
|
|
|
}).fail(function() {
|
|
|
|
dropdownSelector.removeValue(myModuleTagsSelector, lastTagId, '', true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-12-20 14:37:40 +08:00
|
|
|
});
|
2022-09-29 05:18:36 +08:00
|
|
|
});
|
2022-12-08 17:16:37 +08:00
|
|
|
initBSTooltips();
|
2022-09-29 05:18:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
initNewMyModuleModal();
|
|
|
|
}());
|