2018-03-23 23:00:33 +08:00
|
|
|
(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// @TODO refactor that eventually
|
|
|
|
function initEditCoumnModal() {
|
|
|
|
var modalID = '#manageRepositoryColumn';
|
|
|
|
var colRadID = '#repository_column_data_type_repositorylistvalue';
|
|
|
|
var tagsInputID = '[data-role="tagsinput"]';
|
|
|
|
var formID = '[data-role="manage-repository-column-form"]';
|
|
|
|
|
|
|
|
$('[data-action="edit"]').off('click').on('click', function() {
|
|
|
|
var editUrl = $(this).closest('li').attr('data-edit-url');
|
|
|
|
$.get(editUrl, function(data) {
|
|
|
|
$(data.html).appendTo('body').promise().done(function() {
|
|
|
|
$(modalID).modal('show').promise().done(function() {
|
|
|
|
|
|
|
|
$(modalID).on('hidden.bs.modal', function () {
|
|
|
|
// remove edit modal window
|
|
|
|
$(modalID).remove();
|
|
|
|
$('.modal-backdrop').remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
_initTagInput();
|
|
|
|
setTimeout(function() {
|
|
|
|
$('#repository_column_name').focus();
|
|
|
|
}, 500)
|
|
|
|
|
|
|
|
if($(modalID).attr('data-edit-type') === 'RepositoryListValue') {
|
|
|
|
var values = JSON.parse($(tagsInputID).attr('data-value'));
|
|
|
|
$(colRadID).click().promise().done(function() {
|
|
|
|
$.each(values, function(index, element) {
|
|
|
|
$(tagsInputID).tagsinput('add', element);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$('[data-action="save"]').on('click', function() {
|
|
|
|
if($(colRadID).is(':checked')) {
|
|
|
|
$('#list_items').val($(tagsInputID).val());
|
|
|
|
}
|
|
|
|
|
2018-04-05 16:47:47 +08:00
|
|
|
_processResponse($(formID), 'update', modalID);
|
2018-03-23 23:00:33 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-05-25 20:36:15 +08:00
|
|
|
}).fail(function(error) {
|
|
|
|
HelperModule.flashAlertMsg(
|
|
|
|
"<%= I18n.t("libraries.repository_columns.no_permissions") %>",
|
|
|
|
'danger');
|
2018-03-23 23:00:33 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initDeleteColumnModal() {
|
|
|
|
$('[data-action="destroy"]').off('click').on('click', function() {
|
|
|
|
var element = $(this);
|
|
|
|
var modal_html = $("#deleteRepositoryColumn");
|
|
|
|
$.get(element.closest('li').attr('data-destroy-url'), function(data) {
|
|
|
|
modal_html.find('.modal-body').html(data.html)
|
|
|
|
.promise()
|
|
|
|
.done(function() {
|
|
|
|
modal_html.modal('show');
|
|
|
|
_initSubmitAction(modal_html, $(modal_html.find('form')));
|
|
|
|
});
|
2018-05-25 20:36:15 +08:00
|
|
|
}).fail(function(error) {
|
|
|
|
HelperModule.flashAlertMsg(
|
|
|
|
"<%= I18n.t("libraries.repository_columns.no_permissions") %>",
|
|
|
|
'danger');
|
2018-03-23 23:00:33 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO refactor that eventually
|
|
|
|
function initNewColumnModal() {
|
|
|
|
var modalID = '#manageRepositoryColumn';
|
|
|
|
$('[data-action="new-column-modal"]').off('click').on('click', function() {
|
|
|
|
var modalUrl = $(this).attr('data-modal-url');
|
|
|
|
$.get(modalUrl, function(data) {
|
|
|
|
$(data.html).appendTo('body').promise().done(function() {
|
|
|
|
$(modalID).modal('show').promise().done(function() {
|
|
|
|
|
|
|
|
$(modalID).on('hidden.bs.modal', function () {
|
|
|
|
// remove create new modal window
|
|
|
|
$(modalID).remove();
|
|
|
|
$('.modal-backdrop').remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
_initTagInput();
|
|
|
|
setTimeout(function() {
|
|
|
|
$('#repository_column_name').focus();
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
$('[data-action="save"]').on('click', function() {
|
|
|
|
var colRad = '#repository_column_data_type_repositorylistvalue';
|
|
|
|
if($(colRad).is(':checked')) {
|
|
|
|
$('#list_items')
|
|
|
|
.val($('[data-role="tagsinput"]').val());
|
|
|
|
}
|
|
|
|
var form = $('[data-role="manage-repository-column-form"]');
|
2018-04-05 16:47:47 +08:00
|
|
|
_processResponse(form, 'create', modalID);
|
2018-03-23 23:00:33 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/* *********************************
|
|
|
|
Helper methods
|
|
|
|
********************************* */
|
|
|
|
|
|
|
|
function _insertNewListItem(column) {
|
2018-04-06 23:02:10 +08:00
|
|
|
// remove element if already persent
|
|
|
|
$('[data-id="' + column.id + '"]').remove();
|
2018-06-12 16:22:48 +08:00
|
|
|
var html = '<li class="list-group-item row" data-id="' + column.id + '" ';
|
2018-03-23 23:00:33 +08:00
|
|
|
html += 'data-destroy-url="' + column.destroy_html_url + '"';
|
2018-06-12 16:22:48 +08:00
|
|
|
html += 'data-edit-url="' + column.edit_url + '"><div class="col-xs-8">';
|
|
|
|
html += '<span class="pull-left column-name">' + column.name + '</span>';
|
|
|
|
html += '</div><div class="col-xs-4"><span class="controlls pull-right">';
|
2018-04-25 19:57:12 +08:00
|
|
|
html += '<button class="btn btn-default" data-action="edit">';
|
2018-05-22 22:55:58 +08:00
|
|
|
html += '<span class="fas fa-pencil-alt"></span> ';
|
2018-04-25 19:57:12 +08:00
|
|
|
html += '<%= I18n.t "libraries.repository_columns.index.edit_column" %></button> ';
|
2018-06-12 16:22:48 +08:00
|
|
|
html += '<button class="btn btn-default delete-column" data-action="destroy">';
|
2018-07-09 19:13:44 +08:00
|
|
|
html += '<span class="fas fa-trash-alt"></span> ';
|
2018-04-25 19:57:12 +08:00
|
|
|
html += '<%= I18n.t "libraries.repository_columns.index.delete_column" %>';
|
2018-06-12 16:22:48 +08:00
|
|
|
html += '</button></span></div></li>';
|
2018-03-23 23:00:33 +08:00
|
|
|
$(html).insertBefore('.repository-columns-body ul li:first')
|
|
|
|
.promise()
|
|
|
|
.done(function() {
|
|
|
|
initDeleteColumnModal();
|
|
|
|
initEditCoumnModal();
|
|
|
|
// remove create new modal window
|
|
|
|
$('#manageRepositoryColumn').remove();
|
|
|
|
$('.modal-backdrop').remove();
|
|
|
|
});
|
|
|
|
// remove 'no column' list item
|
|
|
|
$('[data-attr="no-columns"]').remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
function _replaceListItem(column) {
|
|
|
|
$('.list-group-item[data-id="' + column.id + '"]')
|
|
|
|
.find('span.pull-left').text(column.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _initTagInput() {
|
|
|
|
$('[name="repository_column[data_type]"]')
|
|
|
|
.on('click', function() {
|
|
|
|
var listValueId = 'repository_column_data_type_repositorylistvalue';
|
|
|
|
if($(this).attr('id') === listValueId) {
|
|
|
|
$('[data-role="tagsinput"]').tagsinput({
|
|
|
|
maxChars: <%= Constants::NAME_MAX_LENGTH %>,
|
|
|
|
trimValue: true
|
|
|
|
});
|
|
|
|
$('.bootstrap-tagsinput').show();
|
2018-03-26 17:28:14 +08:00
|
|
|
$('[data-role="tagsimput-label"]').show();
|
2018-03-23 23:00:33 +08:00
|
|
|
} else {
|
|
|
|
$('.bootstrap-tagsinput').hide();
|
2018-03-26 17:28:14 +08:00
|
|
|
$('[data-role="tagsimput-label"]').hide();
|
2018-03-23 23:00:33 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _removeElementFromDom(column) {
|
|
|
|
$('.list-group-item[data-id="' + column.id + '"]').remove();
|
|
|
|
if($('.list-group-item').length === 0) {
|
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _initSubmitAction(modal, form) {
|
|
|
|
modal.find('[data-action="delete"]').on('click', function() {
|
|
|
|
form.submit();
|
|
|
|
modal.modal('hide')
|
|
|
|
animateSpinner();
|
|
|
|
_processResponse(form, 'destroy');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-05 16:47:47 +08:00
|
|
|
function _processResponse(form, action, modalID) {
|
2018-03-23 23:00:33 +08:00
|
|
|
form.on('ajax:success', function(e, data) {
|
|
|
|
switch(action) {
|
|
|
|
case 'destroy':
|
|
|
|
_removeElementFromDom(data);
|
|
|
|
break;
|
|
|
|
case 'create':
|
|
|
|
_insertNewListItem(data);
|
|
|
|
break;
|
|
|
|
case 'update':
|
|
|
|
_replaceListItem(data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
location.reload();
|
|
|
|
}
|
|
|
|
HelperModule.flashAlertMsg(data.message, 'success');
|
|
|
|
animateSpinner(null, false);
|
2018-04-05 16:47:47 +08:00
|
|
|
if (modalID) {
|
|
|
|
$(modalID).modal('hide');
|
|
|
|
}
|
2018-03-23 23:00:33 +08:00
|
|
|
}).on('ajax:error', function(e, xhr) {
|
|
|
|
animateSpinner(null, false);
|
2018-04-05 16:47:47 +08:00
|
|
|
if (modalID) {
|
2018-04-19 20:02:42 +08:00
|
|
|
if(xhr.responseJSON.message.hasOwnProperty('repository_list_items')) {
|
|
|
|
var message = xhr.responseJSON.message['repository_list_items'];
|
|
|
|
$('.dnd-error').remove();
|
2018-04-19 21:32:14 +08:00
|
|
|
$('#manageRepositoryColumn ').find('.bootstrap-tagsinput').after(
|
2018-04-19 20:02:42 +08:00
|
|
|
"<i class='dnd-error'>" + message + "</i>"
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
var field = { "name": xhr.responseJSON.message }
|
|
|
|
$(form).renderFormErrors('repository_column', field, true, e);
|
|
|
|
}
|
2018-04-05 16:47:47 +08:00
|
|
|
} else {
|
|
|
|
HelperModule.flashAlertMsg(xhr.responseJSON.message, 'danger');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (modalID) {
|
|
|
|
form.submit();
|
|
|
|
}
|
2018-03-23 23:00:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* *********************************
|
|
|
|
Initializers
|
|
|
|
********************************* */
|
|
|
|
|
2018-07-19 23:56:42 +08:00
|
|
|
initEditCoumnModal();
|
|
|
|
initDeleteColumnModal();
|
|
|
|
initNewColumnModal();
|
2018-03-23 23:00:33 +08:00
|
|
|
})();
|