{% endblock %}
{% block jquery_code %}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.lastIndexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
/* Initialize ClipboardJS */
var clipboard = new ClipboardJS('.btn');
clipboard.on('success', function(e) {
showTooltip(e.trigger,'Copied!');
console.log(e);
});
clipboard.on('error', function(e) {
console.error(e);
});
/* Switchery mandatory code */
var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
elems.forEach(function(html) {
var switchery = new Switchery(html, {
'color': '#FA9834',
});
});
function handle_form_errors(data){
if (typeof(data['form_errors']) !== 'undefined'){
$('.errorlist').remove();
$.each(data['form_errors'], function(field_name, error_list){
field_selector = $('#id_'+field_name);
var ul = $('
').insertAfter(field_selector);
$.each(error_list, function(idx, err_msg){
console.log(err_msg);
var li = $('').addClass('errorlist').attr('role', 'menuitem').appendTo(ul);
var group = $('').text(err_msg).appendTo(li);
});
});
}
}
function get_repo_attrs() {
let repo_attrs = new Array();
$('#repo_attributes_table tbody tr').each(function(index, tr) {
// Loop over select and inputs or the current td
$(tr).find('select,input').each(function(i, td) {
name = td.name;
value = td.value;
if( index%2 == 0 && i == 0 )
repo_attrs.push({});
repo_attrs[(index-(index%2))/2][name] = value;
});
});
return repo_attrs;
}
function toggle_condition_var_kind() {
var condition_var_kind = $(this).val();
var tr = $(this).parent().parent();
var condition_var_name = tr.find("#id_condition_var_name:first");
var condition_criterion = tr.find("#id_condition_criterion:first");
var condition_match = tr.find("#id_condition_match:first");
if ( condition_var_kind == "always" ) {
condition_var_name.prop("disabled", true);
condition_criterion.prop("disabled", true);
condition_match.prop("disabled", true);
condition_var_name.val("1");
condition_criterion.val("equals");
condition_match.val("1");
} else {
condition_var_name.prop("disabled", false);
condition_criterion.prop("disabled", false);
condition_match.prop("disabled", false);
}
}
/* All events to refresh (re-apply) after a table is modified */
function refresh_table_events() {
/* Function used to delete an object .btnDelete */
$('.btnDelete').on('click', function(e) {
$(this).parent().parent().remove();
});
/* Function used to delete a double ligne scope entry table */
$('.btnDelete_scope').on('click', function(e) {
// Firstly remove previous line
$(this).parent().parent().prev().remove();
// Then current line
$(this).parent().parent().remove();
});
/* Re-initialize select2 objects */
/* Does not works with same elements ID ... */
Array.prototype.slice.call(document.querySelectorAll('.select2')).forEach(function(html) {
try {
$(html).select2();
} catch(e) { ; }
});
/* If chosen choice is Always, disable first line options */
$('.condition-var-kind').on("change", toggle_condition_var_kind);
}
/* Initialize all custom fields */
refresh_table_events();
/* Add default RepoAttributeForm to repo_attributes_table */
$("#add_repo_attribute").on("click", function(e) {
$('#repo_attributes_table_body').append(`
IF
{{repo_attribute_form.condition_var_kind}}
{{repo_attribute_form.condition_var_name}}
{{repo_attribute_form.condition_criterion}}
{{repo_attribute_form.condition_match}}
THEN
{{repo_attribute_form.assignator}}
KEY
{{repo_attribute_form.action_var_name}}
WITH VALUE FROM
{{repo_attribute_form.action_var_kind}}
NAMED
{{repo_attribute_form.action_var}}
`);
refresh_table_events();
});
/* Convert repo_attributes table to json */
$('#user_scope_edit_form').submit(function(event) {
$('#repo_attributes').val(JSON.stringify(get_repo_attrs()));
});
/* Initialize select2 objects */
$('.select2').select2();
//}); // end of function()
{% endblock %}