1 Star 0 Fork 0

taoqibao / jQuery-Impromptu

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
jquery-impromptu.min.js 10.96 KB
一键复制 编辑 原始数据 按行查看 历史
Trent Richardson 提交于 2015-11-14 10:18 . New Version
/*! jQuery-Impromptu - v6.2.2 - 2015-11-14
* http://trentrichardson.com/Impromptu
* Copyright (c) 2015 Trent Richardson; Licensed MIT */
!function(a,b){"function"==typeof define&&define.amd?define(["jquery"],b):b(a.jQuery)}(this,function(a){"use strict";var b=function(a,c){var d=this;return d.id=b.count++,b.lifo.push(d),a&&d.open(a,c),d};b.defaults={prefix:"jqi",classes:{box:"",fade:"",prompt:"",form:"",close:"",title:"",message:"",buttons:"",button:"",defaultButton:""},title:"",closeText:"&times;",buttons:{Ok:!0},buttonTimeout:1e3,loaded:function(a){},submit:function(a,b,c,d){},close:function(a,b,c,d){},statechanging:function(a,b,c){},statechanged:function(a,b){},opacity:.6,zIndex:999,overlayspeed:"slow",promptspeed:"fast",show:"fadeIn",hide:"fadeOut",focus:0,defaultButton:0,useiframe:!1,top:"15%",position:{container:null,x:null,y:null,arrow:null,width:null},persistent:!0,timeout:0,states:{},initialState:0,state:{name:null,title:"",html:"",buttons:{Ok:!0},focus:0,defaultButton:0,position:{container:null,x:null,y:null,arrow:null,width:null},submit:function(a,b,c,d){return!0}}},b.setDefaults=function(c){b.defaults=a.extend({},b.defaults,c)},b.setStateDefaults=function(c){b.defaults.state=a.extend({},b.defaults.state,c)},b.count=0,b.lifo=[],b.getLast=function(){var a=b.lifo.length;return a>0?b.lifo[a-1]:!1},b.removeFromStack=function(a){for(var c=b.lifo.length-1;c>=0;c--)if(b.lifo[c].id===a)return b.lifo.splice(c,1)[0]},b.prototype={id:null,open:function(c,d){var e=this;e.options=a.extend({},b.defaults,d),e.timeout&&clearTimeout(e.timeout),e.timeout=!1;var f=e.options,g=a(document.body),h=a(window),i='<div class="'+f.prefix+"box "+f.classes.box+'">';i+=f.useiframe&&a("object, applet").length>0?'<iframe src="javascript:false;" class="'+f.prefix+"fade "+f.classes.fade+'"></iframe>':'<div class="'+f.prefix+"fade "+f.classes.fade+'"></div>',i+='<div class="'+f.prefix+" "+f.classes.prompt+'"><form action="#" class="'+f.prefix+"form "+f.classes.form+'"><div class="'+f.prefix+"close "+f.classes.close+'">'+f.closeText+'</div><div class="'+f.prefix+'states"></div></form></div></div>',e.jqib=a(i).appendTo(g),e.jqi=e.jqib.children("."+f.prefix),e.jqif=e.jqib.children("."+f.prefix+"fade"),c.constructor===String&&(c={state0:{title:f.title,html:c,buttons:f.buttons,position:f.position,focus:f.focus,defaultButton:f.defaultButton,submit:f.submit}}),e.options.states={};var j,k;for(j in c)k=a.extend({},b.defaults.state,{name:j},c[j]),e.addState(k.name,k),""===e.currentStateName&&(e.currentStateName=k.name);e.jqi.on("click","."+f.prefix+"buttons button",function(b){var c=a(this),d=c.parents("."+f.prefix+"state"),g=d.data("jqi-name"),h=e.options.states[g],i=d.children("."+f.prefix+"message"),j=h.buttons[c.text()]||h.buttons[c.html()],k={};if(e.options.buttonTimeout>0&&(e.disableStateButtons(g),setTimeout(function(){e.enableStateButtons(g)},e.options.buttonTimeout)),void 0===j)for(var l in h.buttons)(h.buttons[l].title===c.text()||h.buttons[l].title===c.html())&&(j=h.buttons[l].value);a.each(e.jqi.children("form").serializeArray(),function(a,b){void 0===k[b.name]?k[b.name]=b.value:typeof k[b.name]===Array||"object"==typeof k[b.name]?k[b.name].push(b.value):k[b.name]=[k[b.name],b.value]});var m=new a.Event("impromptu:submit");m.stateName=h.name,m.state=d,d.trigger(m,[j,i,k]),m.isDefaultPrevented()||e.close(!0,j,i,k)});var l=function(){if(f.persistent){var b=f.top.toString().indexOf("%")>=0?h.height()*(parseInt(f.top,10)/100):parseInt(f.top,10),c=parseInt(e.jqi.css("top").replace("px",""),10)-b;a("html,body").animate({scrollTop:c},"fast",function(){var a=0;e.jqib.addClass(f.prefix+"warning");var b=setInterval(function(){e.jqib.toggleClass(f.prefix+"warning"),a++>1&&(clearInterval(b),e.jqib.removeClass(f.prefix+"warning"))},100)})}else e.close(!0)},m=function(b){var c=window.event?event.keyCode:b.keyCode;if(27===c&&l(),13===c){var d=e.getCurrentState().find("."+f.prefix+"defaultbutton"),g=a(b.target);g.is("textarea,."+f.prefix+"button")===!1&&d.length>0&&(b.preventDefault(),d.click())}if(9===c){var h=a("input,select,textarea,button",e.getCurrentState()),i=!b.shiftKey&&b.target===h[h.length-1],j=b.shiftKey&&b.target===h[0];if(i||j)return setTimeout(function(){if(h){var a=h[j===!0?h.length-1:0];a&&a.focus()}},10),!1}};return e.position(),e.style(),e._windowResize=function(a){e.position(a)},h.resize({animate:!1},e._windowResize),e.jqif.click(l),e.jqi.find("."+f.prefix+"close").click(function(){e.close()}),e.jqi.find("."+f.prefix+"form").submit(function(){return!1}),e.jqib.on("keydown",m).on("impromptu:loaded",f.loaded).on("impromptu:close",f.close).on("impromptu:statechanging",f.statechanging).on("impromptu:statechanged",f.statechanged),e.jqif[f.show](f.overlayspeed),e.jqi[f.show](f.promptspeed,function(){e.goToState(isNaN(f.initialState)?f.initialState:e.jqi.find("."+f.prefix+"states ."+f.prefix+"state").eq(f.initialState).data("jqi-name")),e.jqib.trigger("impromptu:loaded")}),f.timeout>0&&(e.timeout=setTimeout(function(){e.close(!0)},f.timeout)),e},close:function(c,d,e,f){var g=this;return b.removeFromStack(g.id),g.timeout&&(clearTimeout(g.timeout),g.timeout=!1),g.jqib&&g.jqib[g.options.hide]("fast",function(){g.jqib.trigger("impromptu:close",[d,e,f]),g.jqib.remove(),a(window).off("resize",g._windowResize),"function"==typeof c&&c()}),g.currentStateName="",g},addState:function(c,d,e){var f,g,h,i,j,k=this,l="",m=null,n="",o="",p=k.options,q=a.isFunction(d.position)?d.position():d.position,r=k.jqi.find("."+p.prefix+"states"),s=[],t=0;if(d=a.extend({},b.defaults.state,{name:c},d),a.isPlainObject(q)&&null!==q.arrow&&(n='<div class="'+p.prefix+"arrow "+p.prefix+"arrow"+q.arrow+'"></div>'),d.title&&""!==d.title&&(o='<div class="lead '+p.prefix+"title "+p.classes.title+'">'+d.title+"</div>"),f=d.html,"function"==typeof d.html&&(f="Error: html function must return text"),l+='<div class="'+p.prefix+'state" data-jqi-name="'+c+'">'+n+o+'<div class="'+p.prefix+"message "+p.classes.message+'">'+f+'</div><div class="'+p.prefix+"buttons"+(a.isEmptyObject(d.buttons)?"hide ":" ")+p.classes.buttons+'">',a.isArray(d.buttons))s=d.buttons;else if(a.isPlainObject(d.buttons))for(h in d.buttons)d.buttons.hasOwnProperty(h)&&s.push({title:h,value:d.buttons[h]});for(t=0,j=s.length;j>t;t++)i=s[t],g=d.focus===t||isNaN(d.focus)&&d.defaultButton===t?p.prefix+"defaultbutton "+p.classes.defaultButton:"",l+='<button class="'+p.classes.button+" "+p.prefix+"button "+g,"undefined"!=typeof i.classes&&(l+=" "+(a.isArray(i.classes)?i.classes.join(" "):i.classes)+" "),l+='" name="'+p.prefix+"_"+c+"_button"+i.title.replace(/[^a-z0-9]+/gi,"")+'" value="'+i.value+'">'+i.title+"</button>";return l+="</div></div>",m=a(l).css({display:"none"}),m.on("impromptu:submit",d.submit),void 0!==e?k.getState(e).after(m):r.append(m),k.options.states[c]=d,m},removeState:function(a,b){var c=this,d=c.getState(a),e=function(){d.remove()};return 0===d.length?!1:("none"!==d.css("display")?void 0!==b&&c.getState(b).length>0?c.goToState(b,!1,e):d.next().length>0?c.nextState(e):d.prev().length>0?c.prevState(e):c.close():d.slideUp("slow",e),!0)},getApi:function(){return this},getBox:function(){return this.jqib},getPrompt:function(){return this.jqi},getState:function(a){return this.jqi.find('[data-jqi-name="'+a+'"]')},getCurrentState:function(){return this.getState(this.getCurrentStateName())},getCurrentStateName:function(){return this.currentStateName},disableStateButtons:function(b,c,d){var e=this;a.isArray(b)&&(c=b,b=null),e.getState(b||e.getCurrentStateName()).find("."+e.options.prefix+"button").each(function(b,e){(void 0===c||-1!==a.inArray(e.value,c))&&(e.disabled=!d)})},enableStateButtons:function(a,b){this.disableStateButtons(a,b,!0)},position:function(b){var c=this,d=a.fx.off,e=c.getCurrentState(),f=c.options.states[e.data("jqi-name")],g=f?a.isFunction(f.position)?f.position():f.position:void 0,h=a(window),i=document.body.scrollHeight,j=a(window).height(),k=(a(document).height(),i>j?i:j),l=parseInt(h.scrollTop(),10),m=l+(c.options.top.toString().indexOf("%")>=0?j*(parseInt(c.options.top,10)/100):parseInt(c.options.top,10));if(void 0!==b&&b.data.animate===!1&&(a.fx.off=!0),c.jqib.css({position:"absolute",height:k,width:"100%",top:0,left:0,right:0,bottom:0}),c.jqif.css({position:"fixed",height:k,width:"100%",top:0,left:0,right:0,bottom:0}),g&&g.container){var n=a(g.container).offset(),o=!1;a.isPlainObject(n)&&void 0!==n.top&&(m=n.top+g.y-(c.options.top.toString().indexOf("%")>=0?j*(parseInt(c.options.top,10)/100):parseInt(c.options.top,10)),c.jqi.css({position:"absolute"}),c.jqi.animate({top:n.top+g.y,left:n.left+g.x,marginLeft:0,width:void 0!==g.width?g.width:null},function(){!o&&n.top+g.y+c.jqi.outerHeight(!0)>l+j&&(a("html,body").animate({scrollTop:m},"slow","swing",function(){}),o=!0)}),(l>m||m>l+j)&&(a("html,body").animate({scrollTop:m},"slow","swing",function(){}),o=!0))}else g&&g.width?(c.jqi.css({position:"absolute",left:"50%"}),c.jqi.animate({top:g.y||m,left:g.x||"50%",marginLeft:g.width/2*-1,width:g.width})):c.jqi.css({position:"absolute",top:m,left:"50%",marginLeft:c.jqi.outerWidth(!1)/2*-1});void 0!==b&&b.data.animate===!1&&(a.fx.off=d)},style:function(){var a=this;a.jqif.css({zIndex:a.options.zIndex,display:"none",opacity:a.options.opacity}),a.jqi.css({zIndex:a.options.zIndex+1,display:"none"}),a.jqib.css({zIndex:a.options.zIndex})},goToState:function(b,c,d){var e=this,f=(e.jqi,e.options),g=e.getState(b),h=f.states[g.data("jqi-name")],i=new a.Event("impromptu:statechanging"),j=e.options;if(void 0!==h){if("function"==typeof h.html){var k=h.html;g.find("."+j.prefix+"message ").html(k())}"function"==typeof c&&(d=c,c=!1),e.jqib.trigger(i,[e.getCurrentStateName(),b]),!i.isDefaultPrevented()&&g.length>0&&(e.jqi.find("."+j.prefix+"parentstate").removeClass(j.prefix+"parentstate"),c?(e.jqi.find("."+j.prefix+"substate").not(g).slideUp(f.promptspeed).removeClass("."+j.prefix+"substate").find("."+j.prefix+"arrow").hide(),e.jqi.find("."+j.prefix+"state:visible").addClass(j.prefix+"parentstate"),g.addClass(j.prefix+"substate")):e.jqi.find("."+j.prefix+"state").not(g).slideUp(f.promptspeed).find("."+j.prefix+"arrow").hide(),e.currentStateName=h.name,g.slideDown(f.promptspeed,function(){var c=a(this);e.enableStateButtons(),"string"==typeof h.focus?c.find(h.focus).eq(0).focus():c.find("."+j.prefix+"defaultbutton").focus(),c.find("."+j.prefix+"arrow").show(f.promptspeed),"function"==typeof d&&e.jqib.on("impromptu:statechanged",d),e.jqib.trigger("impromptu:statechanged",[b]),"function"==typeof d&&e.jqib.off("impromptu:statechanged",d)}),c||e.position())}return g},nextState:function(a){var b=this,c=b.getCurrentState().next();return c.length>0&&b.goToState(c.data("jqi-name"),a),c},prevState:function(a){var b=this,c=b.getCurrentState().prev();return c.length>0&&b.goToState(c.data("jqi-name"),a),c}},a.prompt=function(a,c){var d=new b(a,c);return d.jqi},a.each(b,function(b,c){a.prompt[b]=c}),a.each(b.prototype,function(c,d){a.prompt[c]=function(){var a=b.getLast();return a&&"function"==typeof a[c]?a[c].apply(a,arguments):void 0}}),a.fn.prompt=function(b){void 0===b&&(b={}),void 0===b.withDataAndEvents&&(b.withDataAndEvents=!1),a.prompt(a(this).clone(b.withDataAndEvents).html(),b)},window.Impromptu=b});
1
https://gitee.com/yizhitaoqibao/jQuery-Impromptu.git
git@gitee.com:yizhitaoqibao/jQuery-Impromptu.git
yizhitaoqibao
jQuery-Impromptu
jQuery-Impromptu
master

搜索帮助