import $ from 'jquery' import { i18n } from '../core/i18n' import { triggerInit } from '../core/events' const TEMPLATE = `
').addClass('exception').text(errorText)) dialog.show() return dialog } export default class Dialog { constructor (options = {}) { this.options = options this.$el = createElement() if (options.modalClass) { this.$el.find('.modal-dialog').addClass(options.modalClass) } } load (url, callback) { this.show() this.setLoading(true) $.ajax({ url: url, dataType: 'html', headers: { 'X-Trestle-Dialog': true }, complete: () => { this.setLoading(false) }, success: (content) => { this.setContent(content) if (callback) { callback.apply(this) } }, error: (xhr, status, error) => { const errorMessage = i18n['trestle.dialog.error'] || 'The request could not be completed.' const title = error || errorMessage const content = $('').text(errorMessage) this.showError(title, content) } }) } setLoading (loading) { this.$el.toggleClass('loading', loading) } setContent (content) { this.$el.find('.modal-content').html(content) triggerInit(this.$el) } showError (title, content) { this.$el.addClass('error') this.setContent(`
${title}
${content.prop('outerHTML')}`) } show () { this.$el.modal('show') // Background any existing visible modals this.$el.prevAll('.modal.in').addClass('background') } hide () { this.$el.modal('hide') } } $(document).on('click', '[data-behavior="dialog"]', function (e) { e.preventDefault() const url = $(this).data('url') || $(this).attr('href') const dialog = new Dialog({ modalClass: $(this).data('dialog-class') }) dialog.load(url) })