dreamjob/app/webroot/js/dreamjob.js

156 lines
5.9 KiB
JavaScript
Raw Normal View History

2014-02-18 12:10:29 +01:00
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
2014-03-23 10:16:15 +01:00
var global_load = null;
2014-02-18 12:10:29 +01:00
!function($) {
$(function() {
$body = $("body");
$(document).on({
ajaxStart: function() { $body.addClass("loading"); },
ajaxStop: function() { $body.removeClass("loading"); }
});
function event(where){
$("#alert .alert").alert();
$(where+'.dropdown-toggle').dropdown();
$(where+" a").each(function(that){
if($(this).attr("rel")!=="external")
$(this).click(function(e){
load($(this).attr('href'),{},($(this).attr('rel')=="tab"));
return false;
});
});
2014-03-15 01:52:07 +01:00
2014-03-26 09:15:43 +01:00
$(where+" form").submit(function(e){
if(!$(this).hasClass("hide-form") && $(this).attr('enctype')!='multipart/form-data' ){
e.stopPropagation();
//e.preventDefault();
2014-03-15 01:52:07 +01:00
if($(this).attr("back"))
back=window.location.href;
else
back=null;
load($(this).attr("action"),{data:$(this).serialize(), type:$(this).attr("method"),back:back},($(this).attr('rel')=="tab"));
return false;
2014-03-26 09:15:43 +01:00
}
});
$("form[ enctype *= 'multipart/form-data' ]").each(function(evt){
if (!$('#fileUpload').length)
$('body').append('<iframe id="fileUpload" name="fileUpload" style="display:none" />');
$(this).attr('target','fileUpload');
$(this).submit(function(){
frame = $('#fileUpload').load(function(){
setContent(frame.contents().find('body').html());
frame.unbind('load');
setTimeout(function (){frame.html('');}, 1);
});
});
});
2014-03-15 01:52:07 +01:00
$(where+" form.hide-form").each(function(evt){
that = this;
$('span.edit.label',this).click(function(evt){
$(that).removeClass('hide-input');
$(this).addClass('hide');
$('span.save.label',that).removeClass('hide');
$('input',that).removeAttr('disabled');
});
function handleSend(evt){
that2=this;
$.getJSON($(that).attr("action"),$(that).serialize(),function(evt){
if(!evt.error){
$(that).addClass('hide-input');
$('input',that).attr('disabled','disabled');
$('span.save.label',that).addClass('hide');
$('span.edit.label',that).removeClass('hide');
}else{
//TODO Handle error
console.log("TODO Handle Error");
}
});
}
$('span.save.label',this).addClass('hide');
$('input',this).attr('disabled','disabled');
$('span.save.label',this).click(handleSend);
$(that).submit(function(evt){
evt.stopPropagation();
handleSend(evt);
return false;
});
});
//$(where+" form label.has-error").parent(".form-group").addClass("has-error")
2014-02-18 12:10:29 +01:00
}
2014-03-26 09:15:43 +01:00
function setContent(data,back,tab){
2014-03-18 22:48:22 +01:00
alert = data.split('<!-- alert -->')[1].split('<!-- /alert -->')[0];
2014-03-23 10:16:15 +01:00
if( alert.trim().length > 0){
$('#alert').html(alert);
}
2014-02-18 12:10:29 +01:00
if(back){
load(back,{},tab);
}else if(tab){
navbar = data.split('<!-- tab -->')[1].split('<!-- /tab -->')[0];
$('#tab').html(navbar);
event("#tab");
}else{
navbar = data.split('<!-- navbar -->')[1].split('<!-- /navbar -->')[0];
$('#navbar').html(navbar);
event("#navbar");
container = data.split('<!-- container -->')[1].split('<!-- /container -->')[0];
$('#container').html(container);
event("#container");
barright = data.split('<!-- barright -->')[1].split('<!-- /barright -->')[0];
$('#barright').html(barright);
event("#barright");
barleft = data.split('<!-- barleft -->')[1].split('<!-- /barleft -->')[0];
$('#barleft').html(barleft);
event("#barleft");
}
2014-03-23 10:16:15 +01:00
if(!back){
navbar = data.split('<!-- extraScript -->')[1].split('<!-- /extraScript -->')[0];
eval($(navbar).html());
}
2014-03-26 09:15:43 +01:00
}
function setContentError(data,back,tab){
2014-03-23 10:16:15 +01:00
alert = "<div class=\"alert alert-danger\">"+data.statusText+
"<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>"
+ "</div>";
2014-02-18 12:10:29 +01:00
$('#alert').html(alert);
2014-03-23 10:16:15 +01:00
event("#alert");
$('#container').html(data.responseText);
2014-02-18 12:10:29 +01:00
event("#container");
2014-03-26 09:15:43 +01:00
}
function load(pageurl,conf,tab){
data = {};
typemethode = "GET";
back = false;
if(conf['data'])
data = conf['data'];
if(conf['type'])
typemethode = conf['type'];
if(conf['back'] && conf['back'].indexOf("logout")== -1 && conf['back'].indexOf("login")== -1 )
back = conf['back'];
//e.preventDefault();
/*
if uncomment the above line, html5 nonsupported browers won't change the url but will display the ajax content;
if commented, html5 nonsupported browers will reload the page to the specified link.
*/
$.ajax({url:pageurl,data:data,type:typemethode,success:function(data){setContent(data,back,tab);} ,error:function(data){setContentError(data,back,tab);}});
2014-02-18 12:10:29 +01:00
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
};
event("");
2014-03-23 10:16:15 +01:00
global_load = load;
2014-02-18 12:10:29 +01:00
});
}(window.jQuery);