var common = {
clearForm: function (obj) {
var form = $('#' + obj);
form.find('input.form-control').val('');
form.find('textarea.form-control').val('');
form.find('select.form-control').val('');
form.find(':checked').prop('checked', false);
},
slitIds: function (text, c = '|') {
var tmp = text.split(c);
var res = [];
for (var i = 0; i < tmp.length; i++) {
if (tmp[i]) res.push(tmp[i]);
}
return res;
},
item_price: function (price) {
price = parseFloat(price).toFixed(2);
return '$' + price;
},
clear_price: function (text) {
if (!text) return 0;
var price = text.replace('$', '');
price = parseFloat(price).toFixed(2);
return price;
},
showUpMsg: function (msg) {
$.iGrowl({
type: 'info',
title: "Notify",
delay: 4000,
message: msg,
icon: 'vicons-bell'
})
},
showUpErr: function (err) {
$.iGrowl({
type: 'error',
title: "Error",
delay: 4000,
message: err,
icon: 'vicons-cancel'
})
},
formatNumber: function (n) {
// format number 1000000 to 1,234,567
return n.toString().replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",")
},
formatCurrency: function (input, blur) {
// get input value
var input_val = input.val();
// don't validate empty input
if (input_val === "") {
return;
}
// original length
var original_len = input_val.length;
// initial caret position
var caret_pos = input.prop("selectionStart");
// check for decimal
if (input_val.indexOf(".") >= 0) {
// get position of first decimal
// this prevents multiple decimals from
// being entered
var decimal_pos = input_val.indexOf(".");
// split number by decimal point
var left_side = input_val.substring(0, decimal_pos);
var right_side = input_val.substring(decimal_pos);
// add commas to left side of number
left_side = common.formatNumber(left_side);
// validate right side
right_side = common.formatNumber(right_side);
// On blur make sure 2 numbers after decimal
if (blur === "blur") {
right_side += "00";
}
// Limit decimal to only 2 digits
right_side = right_side.substring(0, 2);
// join number by .
input_val = "$" + left_side + "." + right_side;
} else {
// no decimal entered
// add commas to number
// remove all non-digits
input_val = common.formatNumber(input_val);
input_val = "$" + input_val;
// final formatting
if (blur === "blur") {
input_val += ".00";
}
}
// send updated string to input
input.val(input_val);
// put caret back in the right position
var updated_len = input_val.length;
caret_pos = updated_len - original_len + caret_pos;
input[0].setSelectionRange(caret_pos, caret_pos);
},
sortUsingNestedText: function (parent, childSelector, keySelector) {
var items = parent.children(childSelector).sort(function (a, b) {
var vA = $(keySelector, a).text();
var vB = $(keySelector, b).text();
return (vA < vB) ? -1 : (vA > vB) ? 1 : 0;
});
parent.append(items);
},
printFree: function (divId) {
var divToPrint = document.getElementById(divId);
var newWin = window.open('', 'Print-Window');
newWin.document.open();
newWin.document.write('<html><head>' +
'<link href="'+ROOT_URL+'assets/bootstap.space.css" rel="stylesheet" type="text/css"/>' +
'<link href="'+ROOT_URL+'front/css/bootstrap-grid.min.css" rel="stylesheet" type="text/css"/>' +
'<link href="'+ROOT_URL+'front/css/style.css" rel="stylesheet">' +
'<style>.text-center{text-align: center;}.text-right{text-align: right;}</style>' +
'</head><body onload="window.print()">' + divToPrint.innerHTML + '</body></html>');
newWin.document.close();
setTimeout(function () {
newWin.close();
}, 1000);
},
printDiv: function (divId) {
var divToPrint = document.getElementById(divId);
var newWin = window.open('', 'Print-Window');
newWin.document.open();
newWin.document.write('<html><head>' +
'<link href="'+ROOT_URL+'assets/bootstap.space.css" rel="stylesheet" type="text/css"/>' +
'<link href="'+ROOT_URL+'front/css/bootstrap-grid.min.css" rel="stylesheet" type="text/css"/>' +
'<style>body{ font-size:11px;} .text-center{text-align: center;}.text-right{text-align: right;} .hide-print{display: none;}</style>' +
'</head><body onload="window.print()"><div style="width: 200px;">' + divToPrint.innerHTML + '</div></body></html>');
newWin.document.close();
setTimeout(function () {
newWin.close();
}, 1000);
},
printObject: function ($obj) {
var content = $obj.html();
var newWin = window.open('', 'Print-Window');
newWin.document.open();
newWin.document.write('<html><head>' +
'<link href="'+ROOT_URL+'assets/bootstap.space.css" rel="stylesheet" type="text/css"/>' +
'<link href="'+ROOT_URL+'front/css/bootstrap-grid.min.css" rel="stylesheet" type="text/css"/>' +
'<style>body{ font-size:11px;} .text-center{text-align: center;}.text-right{text-align: right;} .hide-print{display: none;}</style>' +
'</head><body onload="window.print()"><div style="width: 200px;">' + content + '</div></body></html>');
newWin.document.close();
setTimeout(function () {
newWin.close();
}, 1000);
}
}
String.prototype.slitIds = function (c) {
return common.slitIds(this, c);
}
$(document).on('keyup', '.input_currency', function () {
common.formatCurrency($(this));
});
$(document).on('blur', '.input_currency', function () {
common.formatCurrency($(this), "blur");
});