/** * jQuery asModal v0.3.2 * https://github.com/amazingSurge/jquery-asModal * * Copyright (c) amazingSurge * Released under the LGPL-3.0 license */ (function(global, factory) { if (typeof define === 'function' && define.amd) { define(['jquery'], factory); } else if (typeof exports !== 'undefined') { factory(require('jquery')); } else { var mod = { exports: {} }; factory(global.jQuery); global.jqueryAsModalEs = mod.exports; } })(this, function(_jquery) { 'use strict'; var _jquery2 = _interopRequireDefault(_jquery); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } var _createClass = (function() { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function(Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); var support = (function() { var el = document.createElement('fakeelement'); var transitions = { transition: 'transitionend', WebkitTransition: 'webkitTransitionEnd' }; var css3Support = false; var transition = void 0; // Check transition. for (var t in transitions) { if (transitions.hasOwnProperty(t) && el.style[t] !== undefined) { transition = transitions[t]; } } if (transition) { css3Support = true; } return { css3Support: css3Support, animationEnd: transition }; })(); var DEFAULTS = { namespace: 'modal', // String: Prefix string attached to the class of every element generated by the plugin skin: null, // set plugin skin content: null, // Set the URL, ID or Class. overlay: true, // Show the overlay. closeElement: null, // Element ID or Class to close the modal effect: 'fadeScale', // fadein | slide | newspaper | fall overlaySpeed: 200, // Sets the speed of the overlay, in milliseconds effectFallback: 'fade', // set default jquery animate when css3 animation doesn't support focus: true, // set focus to form element in content errorContent: 'sorry, ajax error.', // set ajax error content loadingContent: 'loading...', // set loading content closeByEscape: true, // Allow the user to close the modal by pressing 'ESC'. closeByOverlayClick: true, // Allow the user to close the modal by clicking the overlay. width: null, // Set a fixed total width. hieght: null, // Set a fixed total height. // Callback API onOpen: null, // Callback: function() - Fires when the modal open onClose: null // Callback: function() - Fires when the modal close //onComplete: null // Callback: function() - Fires when the effect end }; var ANIMATIONS = { fade: { moveIn: function moveIn(instance, dtd) { instance.$content.animate( { opacity: 1 }, { duration: 400, complete: function complete() { dtd.resolve(); } } ); }, moveOut: function moveOut(instance, dtd) { instance.$content.animate( { opacity: 0 }, { duration: 400, complete: function complete() { dtd.resolve(); } } ); } } }; var NAMESPACE$1 = 'asModal'; var asModal = (function() { function asModal(element, options) { _classCallCheck(this, asModal); this.element = element; this.$element = (0, _jquery2.default)(element); this.$container = (0, _jquery2.default)('
'); this.$contentWrap = (0, _jquery2.default)('
').appendTo( this.$container ); this.$content = (0, _jquery2.default)('
').appendTo( this.$contentWrap ); // options var metas = []; _jquery2.default.each(this.$element.data(), function(k, v) { var re = new RegExp('^modal', 'i'); if (re.test(k)) { metas[k.toLowerCase().replace(re, '')] = v; } }); this.options = _jquery2.default.extend( true, {}, DEFAULTS, options, metas ); this.namespace = this.options.namespace; // switch to jquery animate if not support if (!support.css3Support) { this.options.effect = this.options.effectFallback; } this.classes = { overlay: this.namespace + '-overlay', container: this.namespace + '-container', content: this.namespace + '-content', contentWrap: this.namespace + '-contentWrap', skin: this.namespace + '_' + this.options.skin, error: this.namespace + '_error', open: this.namespace + '_open', animateActive: this.namespace + '_animateActive', effect: this.namespace + '_' + this.options.effect, overlayEffect: this.namespace + '_ovrelay_' + this.options.effect, loading: this.namespace + '_loading', disabled: this.namespace + '_disabled' }; // skin if (this.options.skin) { this.$element.addClass(this.classes.skin); this.$container.addClass(this.classes.skin); } this.$container.addClass(this.classes.container); this.$contentWrap.addClass(this.classes.contentWrap); this.$content.addClass(this.classes.content); if (this.options.overlay) { this.$overlay = (0, _jquery2.default)('
'); this.$overlay.addClass(this.classes.overlay); if (this.options.skin) { this.$overlay.addClass(this.classes.skin); } } this.isLoading = false; this.disabled = false; this.isError = false; this.isOpen = false; this._init(); } _createClass( asModal, [ { key: '_init', value: function _init() { // check element href if (this.options.content === null) { this.options.content = this.$element.attr('href'); } // add animation effect this.$contentWrap.addClass(this.classes.effect); this.$overlay.addClass(this.classes.overlayEffect); this.$container.appendTo('body'); this.$overlay.appendTo('body'); // set fixed width/height if (this.options.width) { this.$content.width(this.options.width); } if (this.options.height) { this.$content.height(this.options.height); } this._bindEvents(); this._trigger('ready'); } }, { key: '_bindEvents', value: function _bindEvents() { var _this = this; if (this.options.closeElement) { this.$contentWrap.on( 'click.modal', this.options.closeElement, _jquery2.default.proxy(this.hide, this) ); } this.$element.on('click.modal', function() { if (!_this.disabled) { _this.open(); } return false; }); if (this.options.closeByOverlayClick) { // here not bind in this.$overlay because its zIndex is less then this.$container this.$container.on('click.modal', function(event) { /* eslint consistent-return: "off" */ if ( (0, _jquery2.default)(event.target).hasClass( _this.classes.container ) ) { _this.close(); return false; } }); } } }, { key: '_load', value: function _load() { var _this2 = this; var dtd = _jquery2.default.Deferred(); if ( this.options.content.charAt(0) === '#' || this.options.content.charAt(0) === '.' ) { // element content dtd.resolve((0, _jquery2.default)(this.options.content)); } else { // loading for waiting ajax this._showLoading(); // ajax _jquery2.default .ajax({ type: 'get', cache: true, url: this.options.content }) .then( function(html) { dtd.resolve((0, _jquery2.default)(html)); }, function() { dtd.reject(_this2.options.errorContent); } ); } return dtd.promise(); } }, { key: '_unbindeEvent', value: function _unbindeEvent() { (0, _jquery2.default)(document).off('keydown.modal'); } }, { key: '_showLoading', value: function _showLoading() { this.$loading = (0, _jquery2.default)('
') .html(this.options.loadingContent) .addClass(this.classes.loading); this.$loading.appendTo(this.$overlay); } }, { key: '_hideLoading', value: function _hideLoading() { if (this.$loading) { this.$loading.remove(); this.$loading = null; } } }, { key: '_animate', value: function _animate() { var dtd = _jquery2.default.Deferred(); // keep consistant with css3 animateEnd event // extend jquery animate in Modal.animations if (!support.css3Support) { ANIMATIONS[this.options.effectFallback][this.status](this, dtd); } else { // hand over control to css3 event dtd.reject(); } return dtd.promise(); } }, { key: 'open', value: function open() { var _this3 = this; if (this.isLoading) { this._hideLoading(); } if (this.options.overlay) { this.$overlay.addClass(this.classes.open); // overlay use jquery animation this.$overlay.animate( { opacity: 1 }, { duration: this.options.overlaySpeed } ); } if (this.options.closeByEscape) { (0, _jquery2.default)(document).on('keydown.modal', function( event ) { // any bugs for different browsers, find a better way if (event.keyCode === 27) { _this3.close(); } }); } if (this.content && !this.isError) { // prevent reloading this._afterOpen(); return; } // clear last open info before load this.$content.removeClass(this.classes.error); this._load() .always(function() { _this3._hideLoading(); }) .then( function(content) { _this3.content = content; _this3.$content.empty().html(_this3.content); _this3._afterOpen(); }, function(error) { _this3.$content.addClass(_this3.classes.error); _this3.isError = true; _this3.content = error; _this3.$content.empty().html(_this3.content); _this3._afterOpen(); } ); } }, { key: '_afterOpen', value: function _afterOpen() { var _this4 = this; if (this.options.focus) { // make sure to excute after content show setTimeout(function() { var $input = _jquery2.default.isFunction(_this4.content.find) && _this4.content.find('input'); if ($input.length > 0) { $input.get(0).focus(); } }, 10); } // show container this.$container.addClass(this.classes.open); // active css3 comeIn animation , if browser doesn't support, just ignore it // give some space for css3 animation setTimeout(function() { _this4.$contentWrap.addClass(_this4.classes.animateActive); }, 0); // for animation this.status = 'moveIn'; this._animate().then(function() { // trigger jquery animation end event manually _this4.isOpen = true; _this4.$content.trigger(_this4.animationEnd, _this4); }); } }, { key: 'close', value: function close() { var that = this; // for jquery animation this.status = 'moveOut'; // css3 animationend event listener this.$content.on(support.animationEnd, function() { that._afterClose(); that.$content.off(that.animationEnd); if (typeof that.options.onComplete === 'function') { that.options.onComplete.call(this, this); } that.$container.trigger('modal::complete', this); return false; }); // overlay use jquery animation this.$overlay.animate( { opacity: 0 }, { duration: this.options.overlaySpeed, complete: function complete() { that.$overlay.removeClass(that.classes.open); } } ); // active css3 comeOut animation this.$contentWrap.removeClass(this.classes.animateActive); this._animate().then(function() { that._afterClose(); }); } }, { key: '_afterClose', value: function _afterClose() { this._unbindeEvent(); this.$container.removeClass(this.classes.open); this.isOpen = false; } }, { key: 'enable', value: function enable() { this.disabled = false; this.$element.addClass(this.classes.disabled); this._trigger('enable'); } }, { key: 'disable', value: function disable() { this.disabled = true; this.$element.removeClass(this.classes.disabled); this._trigger('disable'); } }, { key: 'destroy', value: function destroy() { this.$element.off('click.modal'); this.$container.remove(); this.$overlay.remove(); this._trigger('destroy'); } }, { key: '_trigger', value: function _trigger(eventType) { for ( var _len = arguments.length, params = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++ ) { params[_key - 1] = arguments[_key]; } var data = [this].concat(params); // event this.$element.trigger(NAMESPACE$1 + '::' + eventType, data); // callback eventType = eventType.replace(/\b\w+\b/g, function(word) { return word.substring(0, 1).toUpperCase() + word.substring(1); }); var onFunction = 'on' + eventType; if (typeof this.options[onFunction] === 'function') { this.options[onFunction].apply(this, params); } } } ], [ { key: 'setDefaults', value: function setDefaults(options) { _jquery2.default.extend( DEFAULTS, _jquery2.default.isPlainObject(options) && options ); } } ] ); return asModal; })(); var info = { version: '0.3.2' }; var NAMESPACE = 'asModal'; var OtherAsModal = _jquery2.default.fn.asModal; var jQueryAsModal = function jQueryAsModal(options) { for ( var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++ ) { args[_key2 - 1] = arguments[_key2]; } if (typeof options === 'string') { var method = options; if (/^_/.test(method)) { return false; } else if (/^(get)/.test(method)) { var instance = this.first().data(NAMESPACE); if (instance && typeof instance[method] === 'function') { return instance[method].apply(instance, args); } } else { return this.each(function() { var instance = _jquery2.default.data(this, NAMESPACE); if (instance && typeof instance[method] === 'function') { instance[method].apply(instance, args); } }); } } return this.each(function() { if (!(0, _jquery2.default)(this).data(NAMESPACE)) { (0, _jquery2.default)(this).data(NAMESPACE, new asModal(this, options)); } }); }; _jquery2.default.fn.asModal = jQueryAsModal; _jquery2.default.asModal = _jquery2.default.extend( { setDefaults: asModal.setDefaults, noConflict: function noConflict() { _jquery2.default.fn.asModal = OtherAsModal; return jQueryAsModal; } }, info ); });