/* * Ajax Popup plugin * * Options: * - content: content HTML string or callback * * Data attributes: * - data-control="popup" - enables the ajax popup plugin * - data-ajax="popup-content.htm" - ajax content to load * - data-handler="widget:pluginName" - October ajax request name * - data-keyboard="false" - Allow popup to be closed with the keyboard * - data-request-data="file_id: 1" - October ajax request data * * JavaScript API: * $('a#someLink').popup({ ajax: 'popup-content.htm' }) * $('a#someLink').popup({ handler: 'onOpenPopupForm' }) * * Dependences: * - Bootstrap Modal (modal.js) */ +function ($) { "use strict"; // POPUP CLASS DEFINITION // ============================ var Popup = function(element, options) { var self = this this.options = options this.$el = $(element) this.$container = null this.$modal = null this.$backdrop = null this.isOpen = false this.firstDiv = null this.allowHide = true this.$container = this.createPopupContainer() this.$content = this.$container.find('.modal-content:first') this.$modal = this.$container.modal({ show: false, backdrop: false, keyboard: this.options.keyboard }) /* * Duplicate the popup reference on the .control-popup container */ this.$container.data('oc.popup', this) /* * Hook in to BS Modal events */ this.$modal.on('hide.bs.modal', function(){ self.isOpen = false self.setBackdrop(false) }) this.$modal.on('hidden.bs.modal', function(){ self.$container.remove() self.$el.data('oc.popup', null) }) this.$modal.on('show.bs.modal', function(){ self.isOpen = true self.setBackdrop(true) }) this.$modal.on('shown.bs.modal', function(){ self.triggerEvent('shown.oc.popup') }) this.$modal.on('close.oc.popup', function(){ self.hide() return false }) this.init() } Popup.DEFAULTS = { ajax: null, handler: null, keyboard: true, extraData: {}, content: null } Popup.prototype.init = function(){ var self = this /* * Do not allow the same popup to open twice */ if (self.isOpen) return /* * Show loading panel */ this.setBackdrop(true) if (!this.options.content) this.setLoading(true) /* * October AJAX */ if (this.options.handler) { this.$el.request(this.options.handler, { data: this.options.extraData, success: function(data, textStatus, jqXHR) { this.success(data, textStatus, jqXHR).done(function(){ self.setContent(data.result) $(window).trigger('ajaxUpdateComplete', [this, data, textStatus, jqXHR]) self.triggerEvent('popupComplete') // Deprecated self.triggerEvent('complete.oc.popup') }) }, error: function(jqXHR, textStatus, errorThrown) { this.error(jqXHR, textStatus, errorThrown).done(function(){ self.hide() self.triggerEvent('popupError') // Deprecated self.triggerEvent('error.oc.popup') }) } }) } /* * Regular AJAX */ else if (this.options.ajax) { $.ajax({ url: this.options.ajax, data: this.options.extraData, success: function(data) { self.setContent(data) }, cache: false }) } /* * Specified content */ else if (this.options.content) { var content = typeof this.options.content == 'function' ? this.options.content.call(this.$el[0], this) : this.options.content this.setContent(content) } } Popup.prototype.createPopupContainer = function() { var modal = $('
').prop({ class: 'control-popup modal fade', role: 'dialog', tabindex: -1 }), modalDialog = $('
').addClass('modal-dialog'), modalContent = $('
').addClass('modal-content') return modal.append(modalDialog.append(modalContent)) } Popup.prototype.setContent = function(contents) { this.$content.html(contents) this.setLoading(false) this.show() // Duplicate the popup object reference on to the first div // inside the popup. Eg: $('#firstDiv').popup('hide') this.firstDiv = this.$content.find('>div:first') if (this.firstDiv.length > 0) this.firstDiv.data('oc.popup', this) } Popup.prototype.setBackdrop = function(val) { if (val && !this.$backdrop) { this.$backdrop = $('