1 line
6.6 KiB
JavaScript
1 line
6.6 KiB
JavaScript
!function(s){"use strict";var r;if(!s.fn.popover)throw new Error("Confirmation requires popover.js");function e(t,o){this.init(t,o)}function t(t){for(var o=window,n=t.split("."),i=n.pop(),e=0,s=n.length;e<s;e++)o=o[n[e]];return function(){o[i].call(this)}}e.VERSION="2.4.4",e.KEYMAP={13:"Enter",27:"Escape",39:"ArrowRight",40:"ArrowDown"},(e.DEFAULTS=s.extend({},s.fn.popover.Constructor.DEFAULTS,{placement:"top",title:"Are you sure?",trigger:"click",confirmationEvent:void 0,popout:!1,singleton:!1,copyAttributes:"href target",buttons:null,onConfirm:s.noop,onCancel:s.noop,btnOkClass:"btn-sm btn-primary",btnOkIcon:"icon-ok",btnOkLabel:"Yes",btnCancelClass:"btn-sm btn-danger",btnCancelIcon:"icon-remove",btnCancelLabel:"No",template:'<div class="popover confirmation"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"><p class="confirmation-content"></p><div class="confirmation-buttons text-center"><div class="btn-group"><a href="#" class="btn" data-apply="confirmation"></a><a href="#" class="btn" data-dismiss="confirmation"></a></div></div></div></div>'})).whiteList&&e.DEFAULTS.whiteList["*"].push("data-apply","data-dismiss"),((e.prototype=s.extend({},s.fn.popover.Constructor.prototype)).constructor=e).prototype.getDefaults=function(){return e.DEFAULTS},e.prototype.init=function(t,o){if(s.fn.popover.Constructor.prototype.init.call(this,"confirmation",t,o),(this.options.popout||this.options.singleton)&&!o.rootSelector)throw new Error("The rootSelector option is required to use popout and singleton features since jQuery 3.");this.options._isDelegate=!1,o.selector?this.options._selector=this._options._selector=o.rootSelector+" "+o.selector:o._selector?(this.options._selector=o._selector,this.options._isDelegate=!0):this.options._selector=o.rootSelector,void 0===this.options.confirmationEvent&&(this.options.confirmationEvent=this.options.trigger);var n=this;this.options.selector?this.$element.on(this.options.trigger,this.options.selector,function(t,o){o||(t.preventDefault(),t.stopPropagation(),t.stopImmediatePropagation())}):(this.options._attributes={},this.options.copyAttributes?"string"==typeof this.options.copyAttributes&&(this.options.copyAttributes=this.options.copyAttributes.split(" ")):this.options.copyAttributes=[],this.options.copyAttributes.forEach(function(t){this.options._attributes[t]=this.$element.attr(t)},this),this.$element.on(this.options.trigger,function(t,o){o||(t.preventDefault(),t.stopPropagation(),t.stopImmediatePropagation())}),this.$element.on("show.bs.confirmation",function(t){n.options.singleton&&s(n.options._selector).not(s(this)).filter(function(){return void 0!==s(this).data("bs.confirmation")}).confirmation("hide")})),this.options._isDelegate||(this.eventBody=!1,this.uid=this.$element[0].id||this.getUID("group_"),this.$element.on("shown.bs.confirmation",function(t){n.options.popout&&!n.eventBody&&(n.eventBody=s("body").on("click.bs.confirmation."+n.uid,function(t){s(n.options._selector).is(t.target)||0<s(n.options._selector).has(t.target).length||(s(n.options._selector).filter(function(){return void 0!==s(this).data("bs.confirmation")}).confirmation("hide"),s("body").off("click.bs."+n.uid),n.eventBody=!1)}))}))},e.prototype.hasContent=function(){return!0},e.prototype.setContent=function(){var t,n=this,o=this.tip(),i=this.getTitle(),e=this.getContent();o.find(".popover-title")[this.options.html?"html":"text"](i),o.find(".confirmation-content").toggle(!!e).children().detach().end()[this.options.html?"string"==typeof e?"html":"append":"text"](e),o.on("click",function(t){t.stopPropagation()}),this.options.buttons?(t=o.find(".confirmation-buttons .btn-group").empty(),this.options.buttons.forEach(function(o){t.append(s('<a href="#"></a>').addClass(o.class||"btn btn-xs btn-default").html(o.label||"").attr(o.attr||{}).prepend(s("<i></i>").addClass(o.icon)," ").one("click",function(t){"#"===s(this).attr("href")&&t.preventDefault(),o.onClick&&o.onClick.call(n.$element),o.cancel?(n.getOnCancel().call(n.$element,o.value),n.$element.trigger("canceled.bs.confirmation",[o.value])):(n.getOnConfirm().call(n.$element,o.value),n.$element.trigger("confirmed.bs.confirmation",[o.value])),n.inState&&(n.inState.click=!1),n.hide()}))},this)):(o.find('[data-apply="confirmation"]').addClass(this.options.btnOkClass).html(this.options.btnOkLabel).attr(this.options._attributes).prepend(s("<i></i>").addClass(this.options.btnOkIcon)," ").off("click").one("click",function(t){"#"===s(this).attr("href")&&t.preventDefault(),n.getOnConfirm().call(n.$element),n.$element.trigger("confirmed.bs.confirmation"),n.$element.trigger(n.options.confirmationEvent,[!0]),n.hide()}),o.find('[data-dismiss="confirmation"]').addClass(this.options.btnCancelClass).html(this.options.btnCancelLabel).prepend(s("<i></i>").addClass(this.options.btnCancelIcon)," ").off("click").one("click",function(t){t.preventDefault(),n.getOnCancel().call(n.$element),n.$element.trigger("canceled.bs.confirmation"),n.inState&&(n.inState.click=!1),n.hide()})),o.removeClass("fade top bottom left right in"),o.find(".popover-title").html()||o.find(".popover-title").hide(),r=this,s(window).off("keyup.bs.confirmation").on("keyup.bs.confirmation",this._onKeyup.bind(this))},e.prototype.destroy=function(){r===this&&(r=void 0,s(window).off("keyup.bs.confirmation")),s.fn.popover.Constructor.prototype.destroy.call(this)},e.prototype.hide=function(){r===this&&(r=void 0,s(window).off("keyup.bs.confirmation")),s.fn.popover.Constructor.prototype.hide.call(this)},e.prototype._onKeyup=function(t){if(this.$tip){var o,t=t.key||e.KEYMAP[t.keyCode||t.which],n=this.$tip.find(".confirmation-buttons .btn-group"),i=n.find(".active");switch(t){case"Escape":this.hide();break;case"ArrowRight":o=i.length&&i.next().length?i.next():n.children().first(),i.removeClass("active"),o.addClass("active").focus();break;case"ArrowLeft":o=i.length&&i.prev().length?i.prev():n.children().last(),i.removeClass("active"),o.addClass("active").focus()}}else r=void 0,s(window).off("keyup.bs.confirmation")},e.prototype.getOnConfirm=function(){return this.$element.attr("data-on-confirm")?t(this.$element.attr("data-on-confirm")):this.options.onConfirm},e.prototype.getOnCancel=function(){return this.$element.attr("data-on-cancel")?t(this.$element.attr("data-on-cancel")):this.options.onCancel};var o=s.fn.confirmation;s.fn.confirmation=function(n){var i="object"==typeof n&&n||{};return i.rootSelector=this.selector||i.rootSelector,this.each(function(){var t=s(this),o=t.data("bs.confirmation");(o||"destroy"!=n)&&(o||t.data("bs.confirmation",o=new e(this,i)),"string"==typeof n)&&(o[n](),"hide"==n)&&o.inState&&(o.inState.click=!1)})},s.fn.confirmation.Constructor=e,s.fn.confirmation.noConflict=function(){return s.fn.confirmation=o,this}}(jQuery); |