demarches-normaliennes/app/assets/javascripts/old_design/admin_procedures_modal.js

74 lines
2 KiB
JavaScript
Raw Normal View History

2018-10-02 16:59:30 +02:00
/* globals $ */
2017-03-30 17:00:35 +02:00
$(document).on('turbolinks:load', init_path_modal);
var PROCEDURE_PATH_SELECTOR = 'input[data-autocomplete=path]';
function init_path_modal() {
2017-04-04 16:15:33 +02:00
path_modal_action();
path_validation_action();
path_type_init();
path_validation($(PROCEDURE_PATH_SELECTOR));
}
function path_modal_action() {
2018-10-02 16:59:30 +02:00
$('#publish-modal').on('show.bs.modal', function(event) {
$('#publish-modal .modal-body .table .tr-content').hide();
2018-10-02 16:59:30 +02:00
var button = $(event.relatedTarget); // Button that triggered the modal
2017-04-04 16:15:33 +02:00
var modal_title = button.data('modal_title'); // Extract info from data-* attributes
var modal_index = button.data('modal_index'); // Extract info from data-* attributes
2018-10-02 16:59:30 +02:00
var modal = $(this);
modal.find('#publish-modal-title').html(modal_title);
2018-10-02 16:59:30 +02:00
$('#publish-modal .modal-body .table #' + modal_index).show();
});
}
function path_validation_action() {
2018-10-02 16:59:30 +02:00
$(PROCEDURE_PATH_SELECTOR).keyup(function(key) {
if (key.keyCode != 13) path_validation(this);
2017-04-04 16:15:33 +02:00
});
}
function togglePathMessage(valid, mine) {
$('#path-messages .message').hide();
2017-04-04 16:15:33 +02:00
if (valid === true && mine === true) {
$('#path_is_mine').show();
} else if (valid === true && mine === false) {
$('#path_is_not_mine').show();
} else if (valid === false && mine === null) {
$('#path_is_invalid').show();
}
if ((valid && mine === null) || mine === true)
2018-10-02 16:59:30 +02:00
$('#publish-modal #publish').removeAttr('disabled');
else $('#publish-modal #publish').attr('disabled', 'disabled');
}
function path_validation(el) {
2017-04-04 16:15:33 +02:00
var valid = validatePath($(el).val());
toggleErrorClass(el, valid);
togglePathMessage(valid, null);
}
2018-10-02 16:59:30 +02:00
function toggleErrorClass(node, boolean) {
if (boolean) $(node).removeClass('input-error');
else $(node).addClass('input-error');
}
function validatePath(path) {
2018-10-02 16:59:30 +02:00
var re = /^[a-z0-9_-]{3,50}$/;
2017-04-04 16:15:33 +02:00
return re.test(path);
}
function path_type_init() {
2018-10-02 16:59:30 +02:00
$(PROCEDURE_PATH_SELECTOR).bind('autocomplete:select', function(
ev,
suggestion
) {
2017-04-04 16:15:33 +02:00
togglePathMessage(true, suggestion['mine']);
});
}