/*
EXAMPLE USES:
InvModal.showModal({
title: 'text for title bar' ,
content: 'html' ,
footer: true ,
closebutton: true ,
savebutton: false,
savebuttonlabel: 'text' ,
success: function(){
alert('here');
return true;
} ,
url: 'string' ,
urlargs: {
}
});
$('.delete').click(function(){
InvModal.confirm('
Are you sure you want to delete this record?
' , $(this).attr('href'));
}
*/
var InvModal = {
modalid: 'InvModal' ,
settings: {},
init: function () {
// Add auto event listener for standard confirm links
$('body').on('click' , '.confirm' , function(){
InvModal.confirm('' + $(this).attr('data-confirmmessage') + '
' , $(this).attr('href'));
return false;
});
// Add auto event listener for standard modal links
$('body').on('click' , '.damodal' , function(e){
InvModal.showModal({
'title': '' ,
'footer': false ,
'url': $(this).attr('href')
});
return false;
});
},
showModal: function (settings){
if(typeof(settings.content) == 'undefined'){
settings.content = '';
}
if(typeof(settings.title) == 'undefined'){
settings.title = false;
}
if(typeof(settings.closebutton) == 'undefined'){
settings.closebutton = true;
}
if(typeof(settings.closebuttonlabel) == 'undefined'){
settings.closebuttonlabel = 'Cancel';
}
if(typeof(settings.footer) == 'undefined'){
settings.footer = true;
}
if(typeof(settings.savebutton) == 'undefined'){
settings.savebutton = false;
}
if(typeof(settings.savebuttonlabel) == 'undefined'){
settings.savebuttonlabel = 'Save';
}
if(typeof(settings.classes) == 'undefined'){
settings.classes = '';
}
if(typeof(settings.onLoad) != 'function'){
settings.onLoad = InvModal.defaultOnLoad;
}
if(typeof(settings.success) != 'function'){
settings.success = InvModal.defaultSuccessCallback;
}
this.settings = settings;
/*
available settings
{
title: 'text for title bar' , // DEFAULT NULL // If null, no title bar displayed
content: 'html' , // HTML content for the modal
footer: bool , // DEFAULT true // Show close / done buttons in footer
closebutton: bool , // DEFAULT true // Show close buttons in title bar
savebuttonlabel: 'text' , // Text label for save button
success: function() // callback method for a save event
}
*/
this.createModal();
this.updateContent();
$('#' + this.modalid).modal('show');
$(document).keyup(function(e) {
if (e.keyCode == 27){
InvModal.hideModal();
}
});
},
hideModal: function (){
$('#' + this.modalid).modal('hide');
this.destroyModal();
if(typeof(this.settings.close) == 'function'){
this.settings.close();
}
},
confirm: function (message , callback){
if(typeof(callback) == 'function'){
var success = callback;
} else {
var success = function(){
window.location.replace(callback);
return true;
}
}
this.showModal({
title: false ,
content: message ,
footer: true ,
closebutton: false ,
savebuttonlabel: 'Yes' ,
savebutton : true ,
success: success
});
return false;
},
alert: function (message) {
this.showModal({
title: false ,
content: message ,
footer: true ,
closebutton: false,
closebuttonlabel: 'OK'
});
},
createModal: function () {
if($('#' + this.modalid).length < 1){
$('body').append('');
}
},
updateContent: function() {
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').html('');
this.addTitleBar(this.settings);
this.addContent(this.settings);
this.addFooter(this.settings);
$('body').on('click', '#' + this.modalid + ' .InvModalCancel', function(){
InvModal.hideModal();
});
},
loadContent: function(url , args){
//this.show();
this.addSpinner();
args.ajax = 1;
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body').load(url , args , function(response, status, xhr) {
if (status == "error") {
InvModal.addFailed();
} else {
//$('#' + InvModal.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body').html(response);
}
// Update all the links in the modal
InvModal.settings.onLoad();
});
},
addSpinner: function(){
var spinnerhtml = 'Loading…
';
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body').html(spinnerhtml);
},
addFailed: function(){
var spinnerhtml = 'Failed
';
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body').html(spinnerhtml);
},
addTitleBar: function () {
if(this.settings.title || this.settings.closebutton){
var html = '';
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').append(html);
}
},
addContent: function () {
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').append('');
if(typeof(this.settings.url) == 'string'){
var args = {};
if(typeof(this.settings.urlargs) == 'object'){
args = this.settings.urlargs
}
this.loadContent(this.settings.url , args);
} else if(typeof(this.settings.content) == 'string'){
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body').html(this.settings.content);
}
},
addFooter: function () {
if(this.settings.footer){
var html = '';
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').append(html);
}
if(this.settings.savebutton){
$('body').off('click', '#' + this.modalid + ' .InvModalDone');
$('body').one('click', '#' + this.modalid + ' .InvModalDone', this.settings.success);
}
},
destroyModal: function () {
//$('#InvModal').remove();
this.settings = {};
},
defaultOnLoad: function () {
if(typeof(formCheck) == 'object'){
formCheck.antiSpamForms();
}
InvModal.updateLinks();
},
defaultSuccessCallback: function () {
this.destroyModal();
return true;
},
updateLinks: function(){
if(typeof initFormfields === "function") {
initFormfields($('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body'));
}
if(typeof(formCheck) === 'object'){
if(typeof(formCheck.initFormCheck) == 'function'){
formCheck.initFormCheck($('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body'));
}
}
// Ajax windows
// Add AJAX windows to ajaxmodalotggles
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body').find('.ajaxmodaltoggle').find('.modaltogglecontent').each(function(){
if(!$(this).hasClass('ajaxwindow')){
$(this).addClass('ajaxwindow');
}
});
// create all AJAX window objects
ajaxwindows = new Object;
var ajaxwindowcount = 0;
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body').find('.ajaxwindow').each(function(){
var ajaxwindowid = 'ajaxwindow' + String(ajaxwindowcount);
$(this).attr('id' , ajaxwindowid);
ajaxwindows[ajaxwindowid] = new FXAjaxWindow(ajaxwindowid);
ajaxwindows[ajaxwindowid].initLoad();
ajaxwindowcount ++;
});
// Fix links and form.
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body').find('a').each(function(){
if($(this).attr('href') && $(this).attr('href') != '#' && !$(this).hasClass('ajaxwindowlink') && !$(this).hasClass('internallink') && !$(this).data('eventhandled')){
$(this).unbind('click');
$(this).bind('click' , function(){
InvModal.loadContent($(this).attr('href') , {'ajax': 1});
return false;
});
}
});
$('#' + this.modalid).children('.modal-dialog').children('.modal-content').children('.modal-body').find('form').each(function(){
if($(this).attr('action') && $(this).attr('action') != '#' && !$(this).hasClass('ajaxwindowlink') && !$(this).hasClass('internallink') && !$(this).data('eventhandled')){
$(this).unbind('submit');
$(this).bind('submit' , function(e){
e.preventDefault();
if (typeof formCheck !== 'undefined') {
formCheck.antiSpamForms();
// validate form
if($(this).hasClass('da-validate')) {
var $form = $(this);
formCheck.checkForm($form, function() {
formvalues = InvModal.serializeObject($form);
formvalues.ajax = 1;
InvModal.loadContent($form.attr('action') , formvalues);
});
return false;
}
}
formvalues = InvModal.serializeObject($(this));
formvalues.ajax = 1;
InvModal.loadContent($(this).attr('action') , formvalues);
return false;
});
}
});
},
serializeObject: function(element){
responseobject = {};
data = $(element).serializeArray();
$(data).each(function(i , field){
if(typeof(field) === 'object'){
if(typeof(responseobject[field.name]) == 'string'){
var oldval = responseobject[field.name];
responseobject[field.name] = [oldval];
}
if(typeof(responseobject[field.name]) == 'object'){
responseobject[field.name].push(field.value);
} else {
responseobject[field.name] = field.value;
}
}
});
return responseobject;
}
}
$(document).ready(function(){
InvModal.init();
});