146 lines
5.4 KiB
JavaScript
146 lines
5.4 KiB
JavaScript
jQuery(function($) {
|
|
$.fn.adminInputFormat = function() {
|
|
function sanitizeNumber(number, precision, preserveSign) {
|
|
let result;
|
|
let maxlength = 11;
|
|
number = number.replace(',', '.');
|
|
number = number.split('.', 2);
|
|
number[0] = number[0].slice(0, maxlength - 3);
|
|
const sign = preserveSign && (number[0][0] == '+' || number[0][0] == '-') ? number[0][0] : '';
|
|
number = number.join('.').replace(/[^0-9\.]/ig, '');
|
|
|
|
if (number.length) {
|
|
result = sign + stPrice.round(number, precision);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
function format(input) {
|
|
console.log(input);
|
|
let value = input.val();
|
|
let format = input.data('format');
|
|
|
|
if (typeof format !== 'object') {
|
|
format = { type: format, decimals: 2, signed: false };
|
|
}
|
|
|
|
console.log('format', format);
|
|
|
|
let min = undefined !== input.data('format-min-value') ? input.data('format-min-value') : format.min;
|
|
let max = undefined !== input.data('format-max-value') ? input.data('format-min-value') : format.max;
|
|
let preserveSign = undefined !== input.data('format-preserve-sign') ? input.data('format-preserve-sign') : format.signed;
|
|
let decimals = undefined !== input.data('format-decimals') ? input.data('format-decimals') : format.decimals;
|
|
|
|
switch (format.type) {
|
|
case 'price':
|
|
value = sanitizeNumber(value, 2, preserveSign);
|
|
break;
|
|
|
|
case 'float':
|
|
case 'decimal':
|
|
value = sanitizeNumber(value, decimals, preserveSign);
|
|
break;
|
|
|
|
case 'integer':
|
|
value = sanitizeNumber(value, 0, preserveSign);
|
|
break;
|
|
}
|
|
|
|
if (min && min > value) {
|
|
value = min;
|
|
}
|
|
|
|
if (max && max < value) {
|
|
value = max;
|
|
}
|
|
|
|
input.val(value);
|
|
}
|
|
|
|
$(this).each(function() {
|
|
const input = $(this);
|
|
if (!input.data('data-format-init')) {
|
|
input.change(function() {
|
|
format(input);
|
|
});
|
|
input.data('data-format-init', true);
|
|
}
|
|
});
|
|
}
|
|
|
|
$(document).ready(function($) {
|
|
$('#viewport-width').click(function() {
|
|
const container = $(this);
|
|
const layout = $('#content-viewport');
|
|
|
|
if (!container.hasClass('viewport-expanded')) {
|
|
container.addClass('viewport-expanded');
|
|
layout.addClass('viewport-expanded');
|
|
} else {
|
|
container.removeClass('viewport-expanded');
|
|
layout.removeClass('viewport-expanded');
|
|
}
|
|
|
|
$.post(container.prop('href'), {
|
|
'expanded': Number(container.hasClass('viewport-expanded'))
|
|
});
|
|
|
|
return false;
|
|
});
|
|
|
|
$('body').on('submit', '.admin_form', function(e) {
|
|
const activeElement = $(document.activeElement);
|
|
|
|
if (undefined === activeElement.data('admin-action-show-preloader') || activeElement.data('admin-action-show-preloader')) {
|
|
$(document).trigger('preloader', 'show');
|
|
}
|
|
});
|
|
|
|
$('input[data-format]').adminInputFormat();
|
|
|
|
$('body').on('click', '[data-admin-confirm], [data-admin-action]', function(e) {
|
|
const action = $(this);
|
|
|
|
if (action.is('[data-admin-confirm]')) {
|
|
console.log('admin-confirm-action!');
|
|
if (action.data('admin-confirm').length) {
|
|
if (window.confirm(action.data('admin-confirm'))) {
|
|
$(document).trigger('preloader', 'show');
|
|
if (action.data('admin-action-url')) {
|
|
window.location = action.data('admin-action-url');
|
|
} else if (action.data('admin-action-post')) {
|
|
if (action.is(':submit')) {
|
|
return true;
|
|
}
|
|
|
|
action.closest('form').submit();
|
|
} else {
|
|
window.location = action.attr('href');
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} else if (action.is('[data-admin-action]')) {
|
|
if (!action.data('admin-confirm')) {
|
|
console.log('admin-action!');
|
|
if (action.data('admin-action') == 'delete' || action.data('admin-action') == 'action-delete' || action.is('[data-admin-action-show-preloader]')) {
|
|
$(document).trigger('preloader', 'show');
|
|
}
|
|
|
|
if (action.data('admin-action-url')) {
|
|
var link = $('<a href=\"' + action.data('admin-action-url') + '\"></a>');
|
|
if (action.data('admin-action-blank')) {
|
|
link.attr('target', '_blank');
|
|
}
|
|
$('body').append(link);
|
|
link.get(0).click();
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
});
|
|
});
|
|
}); |