app.util = (function(app){ function getUrlParameter(name) { name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); var results = regex.exec(location.search); return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); }; function actionMessage(message, $target, type, callback){ message = message || ''; $target = $target.closest('div.card').find('.actionMessage'); type = type || 'info'; callback = callback || function(){}; if($target.html() === message) return; if($target.html()){ $target.slideUp('fast', function(){ $target.html('') $target.removeClass (function (index, className) { return (className.match (/(^|\s)ui-\S+/g) || []).join(' '); }); if(message) return actionMessage(message, $target, type, callback); $target.hide() }) }else{ if(type) $target.addClass('ui-' + type); $target.html(message).slideDown('fast'); } setTimeout(callback,10) } $( document ).ready( function () { $('.actionMessage').on('click', 'button.action-close', function(event){ app.util.actionMessage(null, $(this)); }); }); $.fn.serializeObject = function() { var arr = $(this).serializeArray(), obj = {}; for(var i = 0; i < arr.length; i++) { if(obj[arr[i].name] === undefined) { obj[arr[i].name] = arr[i].value; } else { if(!(obj[arr[i].name] instanceof Array)) { obj[arr[i].name] = [obj[arr[i].name]]; } obj[arr[i].name].push(arr[i].value); } } return obj; }; function formAJAX( btn, del ) { event.preventDefault(); // avoid to execute the actual submit of the form. var $form = $(btn).closest( '[action]' ); // gets the 'form' parent var formData = $form.find( '[name]' ).serializeObject(); // builds query formDataing var method = $form.attr('method') || 'post'; // if( !$form.validate()) { // app.util.actionMessage('Please fix the form errors.', $form, 'danger') // return false; // } app.util.actionMessage( '
Loading...
', $form, 'state-highlight' ); app.api[method]($form.attr('action'), formData, function(error, data){ app.util.actionMessage(data.message, $form, error ? 'state-error' : 'priority-primary'); //re-populate table if(!error){ $form.trigger("reset"); eval($form.attr('evalAJAX')); //gets JS to run after completion } }); } function humanFileSize(size) { var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024)); return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; } return { emptyFuction: function(){}, getUrlParameter, actionMessage, humanFileSize, formAJAX, } })(app);