123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- /*!
- * jquery-confirm v1.7.5 (http://craftpip.github.io/jquery-confirm/)
- * Author: Boniface Pereira
- * Website: www.craftpip.com
- * Contact: hey@craftpip.com
- *
- * Copyright 2013-2015 jquery-confirm
- * Licensed under MIT (https://github.com/craftpip/jquery-confirm/blob/master/LICENSE)
- */
- if (typeof jQuery === 'undefined') {
- throw new Error('jquery-confirm requires jQuery');
- }
- var jconfirm, Jconfirm;
- (function ($) {
- "use strict";
- $.confirm = function (options) {
- /*
- * Alias of jconfirm
- */
- return jconfirm(options);
- };
- $.alert = function (options) {
- /*
- * Alias of jconfirm
- */
- options.cancelButton = false;
- return jconfirm(options);
- };
- $.dialog = function (options) {
- /*
- * Alias of jconfirm
- */
- options.cancelButton = false;
- options.confirmButton = false;
- return jconfirm(options);
- };
- jconfirm = function (options) {
- /*
- * initial function for calling.
- */
- if (jconfirm.defaults) {
- /*
- * Merge global defaults with plugin defaults
- */
- $.extend(jconfirm.pluginDefaults, jconfirm.defaults);
- }
- /*
- * merge options with plugin defaults.
- */
- var options = $.extend({}, jconfirm.pluginDefaults, options);
- return new Jconfirm(options);
- };
- Jconfirm = function (options) {
- /*
- * constructor function Jconfirm,
- * options = user options.
- */
- $.extend(this, options);
- this._init();
- };
- Jconfirm.prototype = {
- _init: function () {
- var that = this;
- this._rand = Math.round(Math.random() * 99999);
- this._buildHTML();
- this._bindEvents();
- setTimeout(function () {
- that.open();
- }, 0);
- },
- animations: ['anim-scale', 'anim-top', 'anim-bottom', 'anim-left', 'anim-right', 'anim-zoom', 'anim-opacity', 'anim-none', 'anim-rotate', 'anim-rotatex', 'anim-rotatey', 'anim-scalex', 'anim-scaley'],
- _buildHTML: function () {
- var that = this;
- /*
- * Cleaning animations.
- */
- this.animation = 'anim-' + this.animation.toLowerCase();
- if (this.animation === 'none')
- this.animationSpeed = 0;
- /*
- * Append html to body.
- */
- this.$el = $(this.template).appendTo(this.container).addClass(this.theme);
- this.$el.find('.jconfirm-box-container').addClass(this.columnClass);
- this.CSS = {
- '-webkit-transition-duration': this.animationSpeed / 1000 + 's',
- 'transition-duration': this.animationSpeed / 1000 + 's',
- '-webkjit-transition-timing-function': 'cubic-bezier(0.27, 1.12, 0.32, ' + this.animationBounce + ')',
- 'transition-timing-function': 'cubic-bezier(0.27, 1.12, 0.32, ' + this.animationBounce + ')',
- };
- this.$el.find('.jconfirm-bg').css(this.CSS);
- this.$b = this.$el.find('.jconfirm-box').css(this.CSS).addClass(this.animation);
- /*
- * Setup title contents
- */
- this.setTitle();
- this.contentDiv = this.$el.find('div.content');
- /*
- * Settings up buttons
- */
- this.$btnc = this.$el.find('.buttons');
- if (this.confirmButton && this.confirmButton.trim() !== '') {
- this.$confirmButton = $('<button class="btn">' + this.confirmButton + '</button>')
- .appendTo(this.$btnc)
- .addClass(this.confirmButtonClass);
- }
- if (this.cancelButton && this.cancelButton.trim() !== '') {
- this.$cancelButton = $('<button class="btn">' + this.cancelButton + '</button>')
- .appendTo(this.$btnc)
- .addClass(this.cancelButtonClass);
- }
- if (!this.confirmButton && !this.cancelButton) {
- this.$btnc.remove();
- }
- if(!this.confirmButton && !this.cancelButton && this.closeIcon == null){
- this.$closeButton = this.$b.find('.closeIcon').show();
- }
- if (this.closeIcon === true){
- this.$closeButton = this.$b.find('.closeIcon').show();
- }
- this.setContent();
- if (this.autoClose)
- this._startCountDown();
- },
- setTitle: function(string){
- this.title = (typeof string !== 'undefined') ? string : this.title;
- if (this.title) {
- this.$el.find('div.title').html('<i class="' + this.icon + '"></i> ' + this.title);
- } else {
- this.$el.find('div.title').remove();
- }
- },
- setContent: function (string) {
- var that = this;
- this.content = (string) ? string : this.content;
- var animate = (string) ? true : false;
- /*
- * Set content.
- */
- if (typeof this.content === 'boolean') {
- if (!this.content)
- this.contentDiv.remove();
- else
- console.error('Invalid option for property content: passed TRUE');
- } else if (typeof this.content === 'string') {
- if (this.content.substr(0, 4).toLowerCase() === 'url:') {
- this.contentDiv.html('');
- this.$btnc.find('button').prop('disabled', true);
- var url = this.content.substring(4, this.content.length);
- $.get(url).done(function (html) {
- that.contentDiv.html(html);
- }).always(function (data, status, xhr) {
- if (typeof that.contentLoaded === 'function')
- that.contentLoaded(data, status, xhr);
- that.$btnc.find('button').prop('disabled', false);
- that.setDialogCenter();
- });
- } else {
- this.contentDiv.html(this.content);
- }
- } else if (typeof this.content === 'function') {
- this.contentDiv.html('');
- this.$btnc.find('button').attr('disabled', 'disabled');
- var promise = this.content(this);
- if (typeof promise !== 'object') {
- console.error('The content function must return jquery promise.');
- } else if (typeof promise.always !== 'function') {
- console.error('The object returned is not a jquery promise.');
- } else {
- promise.always(function (data, status) {
- that.$btnc.find('button').removeAttr('disabled');
- that.setDialogCenter();
- });
- }
- } else {
- console.error('Invalid option for property content, passed: ' + typeof this.content);
- }
- this.setDialogCenter(animate);
- },
- _startCountDown: function () {
- var opt = this.autoClose.split('|');
- if (/cancel/.test(opt[0]) && this.type === 'alert') {
- return false;
- } else if (/confirm|cancel/.test(opt[0])) {
- this.$cd = $('<span class="countdown">').appendTo(this['$' + opt[0] + 'Button']);
- var that = this;
- that.$cd.parent().click();
- var time = opt[1] / 1000;
- this.interval = setInterval(function () {
- that.$cd.html(' [' + (time -= 1) + ']');
- if (time === 0) {
- that.$cd.parent().trigger('click');
- clearInterval(that.interval);
- }
- }, 1000);
- } else {
- console.error('Invalid option ' + opt[0] + ', must be confirm/cancel');
- }
- },
- _bindEvents: function () {
- var that = this;
-
- this.$el.find('.jconfirm-scrollpane').click(function (e) {
- if (that.backgroundDismiss) {
- that.cancel();
- that.close();
- } else {
- that.$b.addClass('hilight');
- setTimeout(function () {
- that.$b.removeClass('hilight');
- }, 400);
- }
- });
-
- this.$el.find('.jconfirm-box').click(function (e) {
- e.stopPropagation();
- });
-
- if (this.$confirmButton) {
- this.$confirmButton.click(function (e) {
- e.preventDefault();
- var r = that.confirm(that.$b);
- that.onAction();
- if (typeof r === 'undefined' || r)
- that.close();
- });
- }
- if (this.$cancelButton) {
- this.$cancelButton.click(function (e) {
- e.preventDefault();
- var r = that.cancel(that.$b);
- that.onAction();
- if (typeof r === 'undefined' || r)
- that.close();
- });
- }
- if (this.$closeButton) {
- this.$closeButton.click(function (e) {
- e.preventDefault();
- that.cancel();
- that.onAction();
- that.close();
- });
- }
- if (this.keyboardEnabled) {
- setTimeout(function () {
- $(window).on('keyup.' + this._rand, function (e) {
- that.reactOnKey(e);
- });
- }, 500);
- }
- $(window).on('resize.' + this._rand, function () {
- that.setDialogCenter(true);
- });
- },
- reactOnKey: function key(e) {
-
- /*
- * prevent keyup event if the dialog is not last!
- */
- var a = $('.jconfirm');
- if (a.eq(a.length - 1)[0] !== this.$el[0])
- return false;
- var key = e.which;
- if (key === 27) {
- /*
- * if ESC key
- */
- if (!this.backgroundDismiss) {
- /*
- * If background dismiss is false, Glow the modal.
- */
- this.$el.find('.jconfirm-bg').click();
- return false;
- }
- if (this.$cancelButton) {
- this.$cancelButton.click();
- } else {
- this.close();
- }
- }
- if (key === 13 || key == 32) {
- /*
- * if ENTER or SPACE key
- */
- if (this.$confirmButton) {
- this.$confirmButton.click();
- } else {
- }
- }
- },
- setDialogCenter: function (animate) {
- var windowHeight = $(window).height();
- var boxHeight = this.$b.outerHeight();
- var topMargin = (windowHeight - boxHeight) / 2;
- var minMargin = 100;
- if (boxHeight > (windowHeight - minMargin)) {
- var style = {
- 'margin-top': minMargin / 2,
- 'margin-bottom': minMargin / 2,
- }
- } else {
- var style = {
- 'margin-top': topMargin,
- }
- }
- if (animate) {
- this.$b.animate(style, {
- duration: this.animationSpeed,
- queue: false
- });
- } else {
- this.$b.css(style);
- }
- },
- close: function () {
- var that = this;
- if(this.isClosed())
- return false;
- if(typeof this.onClose === 'function')
- this.onClose();
- /*
- unbind the window resize & keyup event.
- */
- $(window).unbind('resize.' + this._rand);
- if (this.keyboardEnabled)
- $(window).unbind('keyup.' + this._rand);
- that.$el.find('.jconfirm-bg').removeClass('seen');
- this.$b.addClass(this.animation);
- setTimeout(function () {
- that.$el.remove();
- }, this.animationSpeed + 10); // wait 10 miliseconds more, ensure everything is done.
- jconfirm.record.closed += 1;
- jconfirm.record.currentlyOpen -= 1;
- if(jconfirm.record.currentlyOpen < 1)
- $('body').removeClass('jconfirm-noscroll');
- return true;
- },
- open: function () {
- var that = this;
- if (this.isClosed())
- return false;
- that.$el.find('.jconfirm-bg').addClass('seen');
- $('body').addClass('jconfirm-noscroll');
- this.$b.removeClass(this.animations.join(' '));
- /**
- * Blur the focused elements, prevents re-execution with button press.
- */
- $('body :focus').trigger('blur');
- this.$b.find('input[autofocus]:visible:first').focus();
- jconfirm.record.opened += 1;
- jconfirm.record.currentlyOpen += 1;
- if(typeof this.onOpen === 'function')
- this.onOpen();
- return true;
- },
- isClosed: function () {
- return (this.$el.css('display') === '') ? true : false;
- }
- };
- jconfirm.pluginDefaults = {
- template: '<div class="jconfirm"><div class="jconfirm-bg"></div><div class="jconfirm-scrollpane"><div class="container"><div class="row"><div class="jconfirm-box-container span6 offset3"><div class="jconfirm-box"><div class="closeIcon"><span class="glyphicon glyphicon-remove"></span></div><div class="title"></div><div class="content"></div><div class="buttons"></div><div class="jquery-clear"></div></div></div></div></div></div></div>',
- title: 'Hello',
- content: 'Are you sure to continue?',
- contentLoaded: function () {
- },
- icon: '',
- confirmButton: '确定',
- cancelButton: '取消',
- confirmButtonClass: 'btn-default',
- cancelButtonClass: 'btn-default',
- theme: 'white',
- animation: 'scale',
- animationSpeed: 400,
- animationBounce: 1.5,
- keyboardEnabled: false,
- container: 'body',
- confirm: function () {
- },
- cancel: function () {
- },
- backgroundDismiss: true,
- autoClose: false,
- closeIcon: null,
- columnClass: 'col-md-6 col-md-offset-3',
- onOpen: function(){
- },
- onClose: function(){
- },
- onAction: function(){
- }
- };
- jconfirm.record = {
- opened: 0,
- closed: 0,
- currentlyOpen: 0,
- };
- })(jQuery);
|