function uniqueId() {
return Math.random().toString(36).substr(2, 9);
}
// colorise les labels au chargement de l'edition
function colorLabel(fields) {
for (var key in fields) {
var selector = $('.form-group[id$="' + key + '"]');
var input = selector.find('input');
if (input.attr('type') == 'text') {
if (input.val() != '') {
selector.addClass(fields[key]);
} else {
selector.removeClass(fields[key]);
}
}
if (input.attr('type') == 'checkbox') {
if (input.is(':checked')) {
selector.addClass(fields[key]);
} else {
selector.removeClass(fields[key]);
}
}
}
}
// toggle sur les labels si remplissage ou cochage
function colorCheck(fields) {
$('body').on('change keyup', '.form-row input', function () {
var self = $(this);
if (self.attr('id').split('_')[1] in fields) {
if (self.attr('type') == 'text') {
if (self.val() != '') {
self.closest('.form-group').addClass(fields[self.attr('id').split('_')[1]]);
} else {
self.closest('.form-group').removeClass(fields[self.attr('id').split('_')[1]]);
}
}
if (self.attr('type') == 'checkbox') {
if (self.is(':checked')) {
self.closest('.form-group').addClass(fields[self.attr('id').split('_')[1]]);
} else {
self.closest('.form-group').removeClass(fields[self.attr('id').split('_')[1]]);
}
}
}
});
}
// permet de relancer iCheck et Select2 apres le chargement AJAX
function reloadIcheckSelect2() {
$('select').select2({
allowClear: true,
placeholder: "-- Choisir --"
});
$('input[type="checkbox"], input[type="radio"]').iCheck({
checkboxClass: 'icheckbox_square-blue',
radioClass: 'iradio_square-blue'
}).on('ifToggled', function (e) {
$(e.target).trigger('change');
});
}
var coreModalBreadcrumb = [];
var coreModalOpen = [];
function injectResponseInModal(response, size) {
if(typeof size === 'undefined') {
size = 'xl';
}
var modalIndex = uniqueId();
$('#modal-container').append('{{ include('admin/core/admin_modal.html.twig') }}');
$('#modal-container .modal').last().attr('id', 'admin-modal-' + modalIndex);
$('#admin-modal-' + modalIndex + ' .modal-body').html(response);
$('#admin-modal-' + modalIndex + ' .modal-dialog').addClass('modal-'+size);
$('#admin-modal-' + modalIndex).modal();
reloadIcheckSelect2();
}
function closePopover() {
$("*").each(function () {
var popover = $.data(this, "bs.popover");
if (popover) {
$(this).popover('hide');
}
});
}
function modalDetail() {
$('body').on('click', '[class*=get-view-]', function (e) {
if ($('#modal-container').html() === '') {
coreModalBreadcrumb = [];
}
e.preventDefault();
var modalIndex = uniqueId();
var self = $(this);
var data = {'object': self.data('object')};
$.ajax({
type: 'GET',
url: Routing.generate('admin_ajax_get_' + self.data('type'), data),
success: function (response) {
$('#modal-container').append('{{ include('admin/core/admin_modal.html.twig') }}');
$('#modal-container .modal').last().attr('id', 'admin-modal-' + modalIndex);
$('#modal-container .modal').last().data('name', self.data('name'));
if (self.data('size')) {
$('#admin-modal-' + modalIndex + ' .modal-dialog').addClass('modal-' + self.data('size'));
}
$('#admin-modal-' + modalIndex + ' .modal-body').html(response);
$('#admin-modal-' + modalIndex).modal();
coreModalBreadcrumb.push('<span class="badge modal-close" data-modalindex="#admin-modal-' + modalIndex + '">' + self.data('name') + '</span>');
coreModalOpen.push("#admin-modal-"+modalIndex);
$('#admin-modal-' + modalIndex + ' .modal-breadcrumb').html(coreModalBreadcrumb);
}
});
});
}
function modalDelete() {
$('#modal-container').on('hidden.bs.modal', '.modal', function (e) {
var self = $(this);
$("#" + self.attr('id')).remove();
coreModalOpen = [];
coreModalBreadcrumb = [];
$('#modal-container .modal').each(function (index, element) {
coreModalOpen.push('#'+$(element).attr('id'));
coreModalBreadcrumb.push('<span class="badge modal-close" data-modalindex="#'+$(element).attr('id')+'">' + $(element).data('name') + '</span>');
});
});
}
function modalClose() {
$('body').on('click', '.modal-close', function (e) {
e.preventDefault();
var self = $(this);
var id = self.data('modalindex');
var index = coreModalOpen.findIndex(function(element) {
return element == id;
});
for (var i = index+1; i < coreModalOpen.length; i++) {
$(coreModalOpen[i]).modal('hide');
}
});
}
function showGraph(dataRoute, route, canvasSelector, type, configA, optionsA) {
$.ajax({
type: 'GET',
url: Routing.generate(route, dataRoute),
dataType: 'json'
}).success(function (json) {
if (typeof(chart) !== 'undefined') {
chart.destroy();
}
var data = {};
data.datasets = [];
data.labels = json['data']['label'];
var config = {};
config.data = json['data']['data'];
config.label = {% if chart_label == '' %}false{% else %}{{ chart_label }}{% endif %};
;
config.backgroundColor = '{{ chart_background_color|raw }}';
config.borderColor = '{{ chart_border_color|raw }}';
config.borderWidth = '{{ chart_border_width|raw }}';
for (key in configA) {
config[key] = configA[key];
}
data.datasets.push(config);
var ctx = document.getElementById(canvasSelector);
chart = new Chart(ctx, {
type: type,
data: data,
options: optionsA
});
});
}
$(function () {
modalDetail();
modalClose();
modalDelete();
{% include 'admin/core/update_text_relation.js.twig' %}
$('.modal').on('show.bs.modal', function (event) {
var idx = $('.modal:visible').length;
$(this).css('z-index', 1040 + (10 * idx));
});
$('.modal').on('shown.bs.modal', function (event) {
var idx = ($('.modal:visible').length) - 1; // raise backdrop after animation.
$('.modal-backdrop').not('.stacked').css('z-index', 1039 + (10 * idx));
$('.modal-backdrop').not('.stacked').addClass('stacked');
});
$('body').on('click', '[data-action="close-popover"]', function() {
closePopover();
});
if ($('.help-block:contains("trans")').hide().closest('.form-group').find('label').before('<i class="fa fa-globe" style="color:#D6261E;"></i> ').length == 0) {
$('.help-block:contains("trans")').hide().closest('div[class*="box box-primary"]').find('h4[class*="box-title"]').before('<i class="fa fa-globe" style="color:#D6261E;"></i> ');
}
$('label[class|="help-trans"]').append(' <i class="fa fa-globe" style="color:#D6261E;"></i>');
$('body').on('click', '.confirm', function () {
return confirm('Confirmation ?');
});
});