Trestle.Dialog = function() {
this.el = Trestle.Dialog.getElement();
};
Trestle.Dialog.TEMPLATE =
'
';
Trestle.Dialog.getElement = function() {
var el = $('#dialog');
if (el.length == 0) {
el = $(Trestle.Dialog.TEMPLATE).appendTo('body');
el.modal({ show: false });
// Remove dialog elements once hidden
el.on('hidden.bs.modal', function() {
el.remove();
});
// Set X-Trestle-Dialog header on AJAX requests initiated from the dialog
el.on('ajax:beforeSend', '[data-remote]', function(e, xhr, options) {
xhr.setRequestHeader("X-Trestle-Dialog", true);
});
}
return el;
};
Trestle.Dialog.prototype.load = function(url) {
var dialog = this;
dialog.show();
dialog.setLoading(true);
$.ajax({
url: url,
dataType: 'html',
headers: {
"X-Trestle-Dialog": true
},
complete: function() {
dialog.setLoading(false);
},
success: function(content) {
dialog.setContent(content);
},
error: function(xhr, status, error) {
dialog.showError(error);
}
});
};
Trestle.Dialog.prototype.setLoading = function(loading) {
if (loading) {
this.el.addClass('loading');
} else {
this.el.removeClass('loading');
}
}
Trestle.Dialog.prototype.setContent = function(content) {
this.el.find('.modal-content').html(content);
$(Trestle).trigger('init', this.el);
};
Trestle.Dialog.prototype.showError = function(error) {
this.el.addClass('error');
var container = this.el.find('.modal-content').empty();
var errorMessage = Trestle.i18n['trestle.dialog.error'] || 'The request could not be completed.';
$('