File size: 3,536 Bytes
e1d589b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | Trestle.Dialog = function(options) {
options = options || {};
this.el = Trestle.Dialog.createElement();
if (options.modalClass) {
this.el.find('.modal-dialog').addClass(options.modalClass);
}
};
Trestle.Dialog.TEMPLATE =
'<div class="modal fade" tabindex="-1">' +
'<div class="modal-dialog">' +
'<div class="modal-content" data-context></div>' +
'</div>' +
'</div>';
Trestle.Dialog.createElement = function() {
var el = $(Trestle.Dialog.TEMPLATE).appendTo('body');
el.modal({ show: false });
// Remove dialog elements once hidden
el.on('hidden.bs.modal', function() {
el.remove();
});
// Restore the next visible modal to the foreground
el.on('hide.bs.modal', function() {
el.prevAll('.modal.in').last().removeClass('background');
});
// 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.showError = function(title, errorText) {
var dialog = new Trestle.Dialog({ modalClass: 'modal-lg' });
dialog.showError(title, $('<pre>').addClass('exception').text(errorText));
dialog.show();
return dialog;
};
Trestle.Dialog.prototype.load = function(url, callback) {
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);
if (callback) {
callback.apply(dialog);
}
},
error: function(xhr, status, error) {
var errorMessage = Trestle.i18n['trestle.dialog.error'] || 'The request could not be completed.';
var title = error || errorMessage;
var content = $('<p>').text(errorMessage);
dialog.showError(title, content);
}
});
};
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(title, content) {
this.el.addClass('error');
var container = this.el.find('.modal-content').empty();
$('<div class="modal-header">')
.append('<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>')
.append('<h4 class="modal-title"></h4>').find('h4').text(title).end()
.appendTo(container);
$('<div class="modal-body">')
.append(content)
.appendTo(container);
$('<div class="modal-footer">')
.append('<button type="button" class="btn btn-default" data-dismiss="modal" aria-label="OK">').find('button').text(Trestle.i18n['admin.buttons.ok'] || 'OK').end()
.appendTo(container);
};
Trestle.Dialog.prototype.show = function() {
this.el.modal('show');
// Background any existing visible modals
this.el.prevAll('.modal.in').addClass('background');
};
Trestle.Dialog.prototype.hide = function() {
this.el.modal('hide');
};
$(document).on('click', '[data-behavior="dialog"]', function(e) {
e.preventDefault();
var url = $(this).data('url') || $(this).attr('href');
var dialog = new Trestle.Dialog({
modalClass: $(this).data('dialog-class')
});
dialog.load(url);
});
|