// --------------------------------------------------------------------- // Document ready $(document).ready(function() { $(document).tooltip(); $.notification.options( { animation:'fade', delay:6000, position:'tr' }); // $('input[type="file"]').each(function() // { // if($(this).attr('required') && $(this).val() == '' && !$(this).attr('disabled')) // { // $(this).addClass('error'); // } // }); $('input[type="file"]').on('change', function(e) { var currValue = $(this).val(); $(this).removeClass('error'); if(currValue == '') { currValue = 'Choose file ...'; if($(this).attr('required') && !$(this).attr('disabled')) { $(this).addClass('error'); } } $(this).attr('placeholder',currValue.replace(/^.*[\\\/]/, '')); }); // Panel title actions $('.panel-title:not(.no-arrow)').append('
'); $('.panel-title-arrow').click(function(e) { if($(this).parent().hasClass('closed')) { $(this).parent().removeClass('closed'); $(this).parent().next().stop().slideDown(200); } else { $(this).parent().addClass('closed'); $(this).parent().next().stop().slideUp(200); } }); // Search input field $('input.search').wrap(''); $('input.search').after(''); // wrap all selects $(document).find('select').each(function() { if (!$(this).hasClass('ignore') && !$(this).parent().hasClass('divSelect')) { $(this).wrap('').attr('style','width:100%'); } }); }); // -------------------------------------------------------------------------- // Vuejs touch events global settings // Vue.use(Vue2TouchEvents, { // touchClass: '', // tapTolerance: 10, // swipeTolerance: 30, // longTapTimeInterval: 400 // }) // -------------------------------------------------------------------------- // jquery validation methods $.validator.setDefaults( { errorElement: "span", errorClass: "validation-error", errorPlacement: function(error,element) // Nema span poruka { return true; } }); $.validator.addMethod("passwordPattern", function (value, element) { return value.match(/^(?=.*\d)(?=.*[A-Z])(?!.*\s).{8,30}$/); }, "Password must be at least 8 characters long and contains at least: 1 digit, 1 letter and 1 one upper letter"); // -------------------------------------------------------------------------- // Set Globaly auth token with every ajax request $.ajaxSetup( { contentType: 'application/json; charset=UTF-8', processData: false, headers: { 'Accept': 'application/json, text/plain, */*', 'X-Authorization': '', "If-Modified-Since": 'Mon, 26 Jul 1997 05:00:00 GMT', "Cache-Control": 'no-cache', "Pragma": 'no-cache', }, beforeSend: function(jqXHR, options) { if ( options.contentType == "application/json; charset=UTF-8" ) { ParseRequestDatesToString(options.data, true); options.data = JSON.stringify(options.data); } }, // dataFilter: function(response, json) // { // var resp = JSON.parse(response); // ParseResponseDatesToObject(resp); // return JSON.stringify(resp); // } }); // -------------------------------------------------------------------------- // Before success transform all date strings to date objects $(document).ajaxSuccess(function(e,jqXHR, options, data) { ParseResponseDatesToObject(data); }); // --------------------------------------------------------------------------- // Parse all english date strings into Date Object function ParseResponseDatesToObject(response) { //console.log(JSON.stringify(response)); // yyyy-mm-dd //var regexGB = /^([0-9]{4})-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])( (0?[0-9]|2[0-3]|[0-1][0-9]):(0?[0-9]|[0-5][0-9]):(0?[0-9]|[0-5][0-9]))?$/; // dd/mm/yyyy var regexGB = /^(0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/]([0-9]{4})( (0?[0-9]|2[0-3]|[0-1][0-9]):(0?[0-9]|[0-5][0-9]):(0?[0-9]|[0-5][0-9]))?$/; if (typeof response !== "object") { return; } $.each(response, function (key, value) { if (typeof value === "string") { //console.log('aaa:'+value); var ttt = value.match(regexGB); //console.log(JSON.stringify(ttt)); if (ttt != null) { try { //response[key] = new Date(ttt[1], ttt[2] - 1, ttt[3], (ttt[5] != null) ? ttt[5] : '00', (ttt[6] != null) ? ttt[6] : '00', (ttt[7] != null) ? ttt[7] : '00', 0); response[key] = new Date(ttt[3], ttt[2] - 1, ttt[1], (ttt[5] != null) ? ttt[5] : '00', (ttt[6] != null) ? ttt[6] : '00', (ttt[7] != null) ? ttt[7] : '00', 0); } catch (e) { //console.log('ParseResponseDatesToObject ERROR (' + key + '):' + e); } //console.log(response[key].toString()); } } else if (typeof value === "object" && value != null) { //console.log('key:'+key+', value:'+value); ParseResponseDatesToObject(value); } }); } // Parse all request Date Objects into english date strings function ParseRequestDatesToString(request, onlyConvertDate) { //console.log(typeof request, JSON.stringify(request)); if (typeof request !== "object") { return; } $.each(request, function (key, value) { if (value instanceof Date) { //console.log('before:'+value); // yyyy-mm-dd if (onlyConvertDate != undefined && onlyConvertDate == true) { request[key] = value.getFullYear() + '-' + (((value.getMonth() + 1) < 10) ? '0' : '') + (value.getMonth() + 1) + '-' + ((value.getDate() < 10) ? '0' : '') + value.getDate(); } else { request[key] = value.getFullYear() + '-' + (((value.getMonth() + 1) < 10) ? '0' : '') + (value.getMonth() + 1) + '-' + ((value.getDate() < 10) ? '0' : '') + value.getDate() + ' ' + ((value.getHours() < 10) ? '0' : '') + value.getHours() + ':' + ((value.getMinutes() < 10) ? '0' : '') + value.getMinutes() + ':' + ((value.getSeconds() < 10) ? '0' : '') + value.getSeconds(); } // dd/mm/yyyy // if(onlyConvertDate != undefined && onlyConvertDate == true) // { // request[key] = ((value.getDate() < 10) ? '0' : '') + value.getDate() + '/' + (((value.getMonth() + 1) < 10) ? '0' : '') + (value.getMonth() + 1) + '/' + value.getFullYear(); // } // else // { // request[key] = ((value.getDate() < 10) ? '0' : '') + value.getDate() + '/' + (((value.getMonth() + 1) < 10) ? '0' : '') + (value.getMonth() + 1) + '/' + value.getFullYear() + ' ' + ((value.getHours() < 10) ? '0' : '') + value.getHours() + ':' + ((value.getMinutes() < 10) ? '0' : '') + value.getMinutes() + ':' + ((value.getSeconds() < 10) ? '0' : '') + value.getSeconds(); // } //console.log('after:'+request[key]); } else if (typeof value === "object" && value != null) { ParseRequestDatesToString(value,onlyConvertDate); } }); } // Parse date object to string function DateToString(objDate) { return ((objDate.getDate() < 10) ? '0' : '') + objDate.getDate() + '/' + (((objDate.getMonth() + 1) < 10) ? '0' : '') + (objDate.getMonth() + 1) + '/' + objDate.getFullYear(); } // Parse date time object to string function DateTimeToString(objDate) { return ((objDate.getDate() < 10) ? '0' : '') + objDate.getDate() + '/' + (((objDate.getMonth() + 1) < 10) ? '0' : '') + (objDate.getMonth() + 1) + '/' + objDate.getFullYear() + ' ' + ((objDate.getHours() < 10) ? '0' : '') + objDate.getHours() + ':' + ((objDate.getMinutes() < 10) ? '0' : '') + objDate.getMinutes() + ':' + ((objDate.getSeconds() < 10) ? '0' : '') + objDate.getSeconds(); } // -------------------------------------------------------------------------- // Set Globaly ajax error response $( document ).ajaxError(function(event, jqxhr, settings) { if(jqxhr.status != 0) { console.error('Error $http (Status '+((jqxhr.status != undefined) ? jqxhr.status : '-' )+') ' +'\n - Method: '+ ((settings.type != undefined) ? settings.type : '-' ) +'\n - Url: '+ ((settings.url != undefined) ? settings.url : '-' ) +'\n - Input parameters: '+ ((settings.data != undefined) ? '(POST) '+JSON.stringify(settings.data) : '(GET) look in url') +'\n - Response: '+ ((jqxhr.responseJSON != undefined) ? JSON.stringify(jqxhr.responseJSON) : ((jqxhr.responseText != undefined) ? JSON.stringify(jqxhr.responseText) : '-') ) ); } }); // --------------------------------------------------------------------- // CUSTOM FUNCTIONS // replaceAll method String.prototype.replaceAll = function(search, replacement) { var target = this; return target.replace(new RegExp(search, 'g'), replacement); }; function pad(number, length) { var str = '' + number; while (str.length < length) { str = '0' + str; } return str; } function ToDateObject(stringDate) { var stringArray = stringDate.split('/'); return new Date(stringArray[2],((stringArray[1] == '0') ? 0 : parseInt(stringArray[1]-1)),stringArray[0],0,0,0); } // --------------------------------------------------------------------- // Cookie methods function setCookie(key, value, expireInMin) { var expires = new Date(); expires.setTime(expires.getTime() + ((expireInMin != undefined) ? expireInMin*60*1000 : 31536000000 ) ); //1 year document.cookie = key + '=' + value + ';expires=' + expires.toUTCString() + ';path=/;SameSite=Lax' + (location.protocol == 'http:' ? '' : ';secure'); } function getCookie(key) { var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)'); return keyValue ? keyValue[2] : null; } function delCookie( key ) { var expires = new Date(); expires = new Date(expires.setDate(expires.getDate() - 1)); // go yesterday document.cookie = key + '=; expires=' + expires.toUTCString() + ';path=/'; }