diff --git a/Ply.min.js b/Ply.min.js index cdeacc8..23c8c8f 100644 --- a/Ply.min.js +++ b/Ply.min.js @@ -1,2 +1,2 @@ /*! Ply 0.3.0 - MIT | git://github.com/rubaxa/Ply.git */ -!function(a){window.Ply=a(window)}(function(a){"use strict";function b(a){return"function"==typeof a}function c(a){if(C)return new C(a);var b=B.Deferred();return a(b.resolve,b.reject),b}function d(a){return C?C.all(a):B.when.apply(B,a)}function e(a){return function(){setTimeout(a,1)}}function f(a,b){if(a)for(var c in a)a.hasOwnProperty(c)&&b(a[c],c,a)}function g(a){var c={};return f(a,function(a,d){c[d]=b(a)?a:a instanceof Object?g(a):a}),c}function h(a){for(var b=Array.prototype.slice,c=b.call(arguments,1),d=0,e=c.length;e>d;d++)f(c[d],function(b,c){a[c]=b});return a}function i(a,b){try{return(b||A).querySelector(a)}catch(c){return B(a,b)[0]}}function j(a,b){return a.getElementsByTagName(b)}function k(a,b){a&&b&&a.appendChild(b)}function l(a){a&&a.parentNode&&a.parentNode.removeChild(a)}function m(a,b,c){var d=c.handle=c.handle||function(b){b.target||(b.target=b.srcElement||A),3===b.target.nodeType&&(b.target=b.target.parentNode),b.preventDefault||(b.preventDefault=function(){b.returnValue=!1}),b.stopPropagation||(b.stopPropagation=function(){b.cancelBubble=!0}),c.call(a,b)};a.addEventListener?a.addEventListener(b,d,!1):a.attachEvent("on"+b,d)}function n(a,b,c){var d=c.handle;d&&(a.removeEventListener?a.removeEventListener(b,d,!1):a.detachEvent("on"+b,d))}function o(a,b,c){if(a&&a.style)if(b instanceof Object)for(var d in b)o(a,d,b[d]);else{if(void 0===c)return A.defaultView&&A.defaultView.getComputedStyle?c=A.defaultView.getComputedStyle(a,""):a.currentStyle&&(c=a.currentStyle),void 0===b?c:c[b];a.style[E[b]||b]=c}}function p(a){if(null==a&&(a="div"),a.appendChild)return a;if(a.skip)return A.createDocumentFragment();"string"==typeof a&&(a={tag:a});var b,c=a.children,d=D.exec(a.tag||"");return delete a.children,a.tag=d[1]||"div",a.id=a.id||(d[2]||"").substr(1),d=(d[3]||"").split("."),d[0]=a.className||"",a.className=d.join(" "),b=A.createElement(a.tag),delete a.tag,f(a,function(c,d){c&&("css"===d?o(b,a.css):"text"===d?null!=c&&k(b,A.createTextNode(c)):"html"===d?null!=c&&(b.innerHTML=c):"ply"===d?b.setAttribute(J,c):d in b?b[d]=c:/^data-/.test(d)&&b.setAttribute(d,c))}),c&&c.appendChild?k(b,c):f(c,function(a,c){a&&("string"==typeof a?a={text:a}:"object"!=typeof a&&(a={}),"string"==typeof c&&(a.tag=a.tag||c),k(b,p(a)))}),b}function q(a){for(var b,c,d=j(a,"input"),e=0,f=d.length;f>e;e++)if(b=d[e],"submit"===b.type)!c&&(c=b);else if(!/hidden|check|radio/.test(b.type)&&""==b.value){c=b;break}c||(c=j(a,"button")[0]);try{c.focus()}catch(g){}}function r(a){return s(!0),c(function(b){for(var c,d=j(a,"img"),e=d.length,f=e,g=function(){if(--f<=0){for(e=d.length;e--;)c=d[e],n(c,"load",g),n(c,"error",g);s(!1),b()}};e--;)c=d[e],c.complete?f--:(m(c,"load",g),m(c,"error",g));!f&&g()})}function s(a){var b=s.get();clearTimeout(s.pid),s.pid=a?setTimeout(function(){k(A.body,b)},100):setTimeout(function(){l(b)},100)}function t(a,b){var c=p({css:{padding:"20px 20px 40px",display:"inline-block",position:"relative",textAlign:"left",whiteSpace:"normal",verticalAlign:"middle",transform:"translate3d(0, 0, 0)"},children:a});return b&&o(a,b),o(a,{overflow:"hidden",position:"relative",backfaceVisibility:"hidden"}),c.setAttribute(J,"layer"),k(c,a),c}function u(a){var b=p();return a&&(o(b,a),o(b,{top:0,left:0,right:0,bottom:0,position:"fixed"}),b.setAttribute(J,"overlay")),b}function v(a,b,c){a.wrapEl=p({css:{whiteSpace:"nowrap"}}),c||(a.overlayEl=u(b.overlay),k(a.wrapEl,a.overlayEl));var d=p();o(d,{height:"100%",display:"inline-block",verticalAlign:"middle"}),k(a.wrapEl,d);var e=b.el;return a.el=e&&e.cloneNode?e.cloneNode(!0):p({html:e||""}),a.layerEl=t(a.el,b.layer),a.contentEl=a.layerEl.firstChild,a.context=new x(a.layerEl),k(a.wrapEl,a.layerEl),a.bodyEl=b.body&&i(b.body)||A.body,a.wrapEl.tabIndex=-1,o(a.wrapEl,{top:0,left:0,right:0,bottom:0,position:"fixed",textAlign:"center",overflow:"auto",outline:0}),a}function w(a){var b=this;b.cid="c"+y++,b.options=a=h({layer:K.layer,overlay:K.overlay,init:z,open:z,close:z,destroy:z,callback:z},a),a.flags=h({},K.flags,a.flags),v(b,a),b.setEffect(a.effect),b.fx={queue:c(function(a){a()})},b.fx.add=function(a){return!(b.fx.queue=b.fx.queue.then(a,a).then(function(){return b}))},b.on("click",":close",function(a,c){a.preventDefault(),b.closeBy("BUTTON"===c.nodeName?"cancel":"x")}),b.options.init(this)}function x(a){this.el=a}var y=1,z=function(){},A=a.document,B=a.jQuery||a.Zepto||a.ender||a.$,C=a.Promise,D=/^(\w+)?(#\w+)?((?:\.[\w_-]+)*)/i,E=function(){var a={},b=p().style,c="transition transform perspective transformStyle transformOrigin backfaceVisibility".split(" "),d=["Webkit","Moz","O","MS"];return f(c,function(c,e){if(a[c]=c in b&&c,!a[c])for(e=0;4>e;e++){var f=d[e]+c.charAt(0).toUpperCase()+c.substr(1);if(a[c]=f in b&&f)break}}),a}(),F={esc:27},G=[],H=G.push,I=G.splice,J="data-ply";s.get=function(){return s.el||(s.el=p({tag:".ply-loading",children:{".ply-loading-spinner":!0}}))};var K={layer:{},overlay:{opacity:.6,backgroundColor:"rgb(0, 0, 0)"},flags:{bodyScroll:!1,closeByEsc:!0,closeByOverlay:!0},lang:{ok:"OK",cancel:"Cancel"}};return w.fn=w.prototype={constructor:w,_activate:function(){if(!this.hasFlag("bodyScroll")){var a=this.bodyEl,b=p();a.__of=o(a,"overflow"),a.__pr=o(a,"paddingRight"),k(a,b),o(a,{overflow:"hidden",paddingRight:a.offsetWidth-b.offsetWidth+"px"}),l(b)}this.hasFlag("closeByOverlay")&&m(this.overlayEl,"click",this._getHandleEvent("overlay")),m(this.wrapEl,"submit",this._getHandleEvent("submit"))},_deactivate:function(){if(!this.hasFlag("bodyScroll")){var a=this.bodyEl;o(a,{overflow:a.__of,paddingRight:a.__pr})}n(this.layerEl,"submit",this._getHandleEvent("submit")),n(this.overlayEl,"click",this._getHandleEvent("overlay"))},_getHandleEvent:function(a){var b=this,c=b.__handleEvent||(b.__handleEvent={});return c[a]||(c[a]=function(c){b._handleEvent(a,c)}),c[a]},_handleEvent:function(a,b){b.preventDefault(),this.closeBy(a)},_applyEffect:function(a,b,c){return w.effects.apply.call(c||this.effects,a,b)},closeBy:function(a){var b=this.options.callback({by:a,state:"submit"===a,layer:this,context:this.context});b!==!1&&this.close()},on:function(a,b,c){var d=this;return c||(c=b,b="layer"),c["_"+b]=function(a){var e=a.target;do if(1===e.nodeType&&e.getAttribute(J)===b)return c.call(d,a,e);while(e!==d.wrapEl&&(e=e.parentNode))},m(d.wrapEl,a,c["_"+b]),d},off:function(a,b,c){return c||(c=b,b="layer"),n(this.wrapEl,a,c["_"+b]||z),this},hasFlag:function(a){return!!this.options.flags[a]},setEffect:function(a){return this.effects=w.effects.get(a),this},open:function(){var a=this;return a.visible||(a.visible=!0,a._activate(),w.stack.add(a),a.fx.add(function(){return r(a.wrapEl).then(function(){return k(a.bodyEl,a.wrapEl),a.wrapEl.focus(),a.wrapEl.focus(),q(a.layerEl),a.options.open(a),d([a._applyEffect(a.overlayEl,"open.overlay"),a._applyEffect(a.layerEl,"open.layer")])})})),a.fx.queue},close:function(){var a=this;return a.visible&&(a.visible=!1,a._deactivate(),w.stack.remove(a),a.fx.add(function(){return d([a._applyEffect(a.overlayEl,"close.overlay"),a._applyEffect(a.layerEl,"close.layer")]).then(function(){l(a.wrapEl),a.options.close(a)})})),a.fx.queue},_swap:function(a,b,c,e,f){var g=this;return g.visible?g.fx.add(function(){return r(b).then(function(){return e(),d([g._applyEffect(a,"close.layer",c),g._applyEffect(b,"open.layer",c)]).then(function(){l(a),f(),g.wrapEl.focus(),q(b)})})}):f(),g.fx.queue},swap:function(a,b){a.layer=a.layer||this.options.layer;var c=this,d=v({},a,!0),e=b||a.effect?w.effects.get(b||a.effect):c.effects,f=c.layerEl,g=d.layerEl;return c._swap(f,g,e,function(){k(c.bodyEl,c.wrapEl),k(c.bodyEl,d.wrapEl)},function(){l(d.wrapEl),k(c.wrapEl,g),c.el=d.el,c.layerEl=g,c.contentEl=g.firstChild,c.context.el=g})},innerSwap:function(a,b){a.layer=a.layer||this.options.layer;var c=this,d=v({},a,!0),e=b||a.effect?w.effects.get(b||a.effect):c.effects,f=i(".ply-inside",d.layerEl),g=i(".ply-inside",c.layerEl);return c._swap(g,f,e,function(){o(g,{width:g.offsetWidth+"px",position:"absolute"}),k(g.parentNode,f)},z)},destroy:function(){l(this.wrapEl),this.visible=!1,this._deactivate(),this.options.destroy(this)}},w.stack={_idx:{},last:null,length:0,_pop:function(a){var b=w.stack.last;a.keyCode===F.esc&&b.hasFlag("closeByEsc")&&b.closeBy("esc")},add:function(a){var b=H.call(this,a);this.last=a,this._idx[a.cid]=b-1,1===b&&m(A,"keyup",this._pop)},remove:function(a){var b=this._idx[a.cid];b>=0&&(I.call(this,b),delete this._idx[a.cid],this.last=this[this.length-1],this.last||n(A,"keyup",this._pop))}},w.effects={defaults:{duration:300,open:{layer:null,overlay:null},close:{layer:null,overlay:null}},setup:function(a){this.defaults=this.get(a)},get:function(a){function b(a){var b=/^([\w_-]+)(?::(\d+%?))?(\[[^\]]+\])?/.exec(a)||[];return{name:b[1]||a,duration:b[2]||null,args:JSON.parse(b[3]||"null")||{}}}function c(c,d,e){var f=b(d),g=c[f.name]||e||{},h=f.duration||g.duration||c.duration||a.duration;return"string"==typeof g&&(g=b(g),delete g.args),/%/.test(g.duration)&&(g.duration=parseInt(g.duration,10)/100*h),g.duration=0|(g.duration||h),g}var d=g(this.defaults);if("string"==typeof a){var e=b(a);a=g(this[e.name]||{open:{},close:{}}),a.duration=e.duration||a.duration,a.open.args=e.args[0],a.close.args=e.args[1]}else if(a instanceof Array){var f=b(a[0]),h=b(a[1]),i=this[f.name],j=this[h.name];a={open:g(i&&i.open||{layer:a[0],overlay:a[0]}),close:g(j&&j.close||{layer:a[1],overlay:a[1]})},a.open.args=f.args[0],a.close.args=h.args[0]}a=a||{},a.duration=0|(a.duration||d.duration);for(var k in{open:0,close:0}){var l=a[k]||d[k]||{};"string"==typeof l&&(l={layer:l}),l.layer=c(l,"layer",d[k].layer),l.overlay=c(l,"overlay",d[k].overlay),void 0===l.args&&delete l.args,a[k]=l}return a},apply:function(a,d){d=d.split(".");var h,i,j=this[d[0]],k=a.firstChild,l=[a.getAttribute("style"),k&&k.getAttribute("style")];return j&&(i=j[d[1]])&&(h=w.effects[i.name])&&(h.to||h.from)?(h=g(h),o(a,"transition","none"),o(k,"transition","none"),f(h.to,function(b,c,d){"&"===b&&(d[c]=o(a,c))}),b(h.from)?h.from(a,j.args):h.from&&o(a,h.from),c(function(c){h.width=a.offsetWidth,o(a,"transition","all "+i.duration+"ms"),o(k,"transition","all "+i.duration+"ms"),b(h.to)?h.to(a,j.args):o(a,h.to),setTimeout(c,i.duration)}).then(e(function(){a.setAttribute("style",l[0]),k&&k.setAttribute("style",l[1])}))):void 0},fade:{open:{layer:"fade-in:80%",overlay:"fade-in:100%"},close:{layer:"fade-out:60%",overlay:"fade-out:60%"}},scale:{open:{layer:"scale-in",overlay:"fade-in"},close:{layer:"scale-out",overlay:"fade-out"}},fall:{open:{layer:"fall-in",overlay:"fade-in"},close:{layer:"fall-out",overlay:"fade-out"}},slide:{open:{layer:"slide-in",overlay:"fade-in"},close:{layer:"slide-out",overlay:"fade-out"}},"3d-flip":{open:{layer:"3d-flip-in",overlay:"fade-in"},close:{layer:"3d-flip-out",overlay:"fade-out"}},"3d-sign":{open:{layer:"3d-sign-in",overlay:"fade-in"},close:{layer:"3d-sign-out",overlay:"fade-out"}},inner:{open:{layer:"inner-in"},close:{layer:"inner-out"}},"fade-in":{from:{opacity:0},to:{opacity:"&"}},"fade-out":{to:{opacity:0}},"slide-in":{from:{opacity:0,transform:"translateY(20%)"},to:{opacity:"&",transform:"translateY(0)"}},"slide-out":{to:{opacity:0,transform:"translateY(20%)"}},"fall-in":{from:{opacity:0,transform:"scale(1.3)"},to:{opacity:"&",transform:"scale(1)"}},"fall-out":{to:{opacity:0,transform:"scale(1.3)"}},"scale-in":{from:{opacity:0,transform:"scale(0.7)"},to:{opacity:"&",transform:"scale(1)"}},"scale-out":{to:{opacity:0,transform:"scale(0.7)"}},rotate3d:function(a,b,c,d,e){o(a,{perspective:"1300px"}),o(a.firstChild,{opacity:b,transform:"rotate"+c+"("+d+"deg)",transformStyle:"preserve-3d",transformOrigin:e?"50% 0":"50%"})},"3d-sign-in":{from:function(a){w.effects.rotate3d(a,0,"X",-60,"50% 0")},to:function(a){o(a.firstChild,{opacity:1,transform:"rotateX(0)"})}},"3d-sign-out":{from:function(a){w.effects.rotate3d(a,1,"X",0,"50% 0")},to:function(a){o(a.firstChild,{opacity:0,transform:"rotateX(-60deg)"})}},"3d-flip-in":{from:function(a,b){w.effects.rotate3d(a,0,"Y",b||-70)},to:function(a){o(a.firstChild,{opacity:1,transform:"rotateY(0)"})}},"3d-flip-out":{from:function(a){w.effects.rotate3d(a,1,"Y",0)},to:function(a,b){o(a.firstChild,{opacity:0,transform:"rotateY("+(b||70)+"deg)"})}},"inner-in":{from:function(a){o(a,"transform","translateX(100%)")},to:function(a){o(a,"transform","translateX(0%)")}},"inner-out":{from:function(a){o(a,"transform","translateX(0%)")},to:function(a){o(a,"transform","translateX(-100%)")}}},x.prototype={constructor:x,getEl:function(a){if(this.el)for(var b=j(this.el,"*"),c=b.length;c--;)if(b[c].getAttribute(J+"-name")===a)return b[c]},val:function(a,b){var c=this.getEl(a);return c&&null==c.value&&(c=j(c,"input")[0]||j(c,"textarea")[0]||j(c,"select")[0]),c&&null!=b&&(c.value=b),c&&c.value||""}},w.lang={ok:"OK",cancel:"Cancel"},w.css=o,w.keys=F,w.noop=z,w.each=f,w.extend=h,w.promise=c,w.defaults=K,w.attrName=J,w.Context=x,w.dom={build:p,appendChild:k,removeElement:l,addEvent:m,removeEvent:n},w}),function(a){a(Ply)}(function(a){"use strict";function b(a,c,d){var f,g=b[a];return g||(a=a.split(/\s+/).slice(0,-1).join(" "),g=c&&(b[a+" [name="+c.name+"]"]||b[a+" [type="+c.type+"]"])||b[a+" *"]||b[":default"]),f=j(g(c,d)),c&&c.name&&f.setAttribute(e+"-name",c.name),f.className+=" ply-ui",f}function c(a,d){c["_"+a]=d,c[a]=function(a,c){return i(function(b,e){d(a,c,b,e)}).then(function(a){return a.appendChild||(a=b(":root",a)),a})}}function d(a,b,c,d){return b.effect=b.effect||"slide",{mod:a,header:c.title,content:c.form?{"dialog-form":{children:c.form}}:{el:c.text||c},ctrls:{ok:c.ok||d.ok,cancel:c.cancel||d.cancel}}}var e=a.attrName,f=a.noop,g=a.each,h=a.extend,i=a.promise,j=a.dom.build,k=a.dom.appendChild,l=a.lang,m=function(a,b){return null==a?{skip:!0}:("string"==typeof a&&(a={text:a}),a.name=a.name||b,a)};b.factory=function(a,c,d){b[a.trim().replace(/\s+/g," ")]=function(e,f){var i=document.createDocumentFragment();if(null!=e||":root"===a){e=d?e:m(e),g(d?e:e.children,function(c,d){var e=((f||a)+" "+d).replace(/^:\w+\s+/,""),g=b(e,m(c,d),e);k(i,g)}),d||delete e.children;var j=c(e,i);return j.appendChild||h(j,e),j}return i}},b.factory(":default",function(a,b){return a.children=b,a}),b.factory(":root",function(a){return{tag:"form.ply-layer",className:a.mod,children:[{tag:".ply-inside",children:[b(":header",a.header),b(":content",a.content),a.ctrls&&b(":default",{tag:"div.ply-footer",children:a.ctrls})]}]}}),b.factory(":header",function(a,b){return{tag:".ply-header",text:a.text,children:b}}),b.factory(":content",function(a,b){return{tag:".ply-content",children:b}},!0),b.factory("ok",function(a){return{ply:":ok",tag:"button.ply-ok",text:a===!0?l.ok:a}}),b.factory("cancel",function(a){return{ply:":close",tag:"button.ply-cancel",type:"reset",text:a===!0?l.cancel:a}}),c.use=function(a,b,d,e,f){c["_"+a](b,d,e,f)},c("default",function(a,b,c){c(b||{})}),c("alert",function(a,b,c){c(d("alert",a,b,{ok:!0}))}),c("confirm",function(a,b,c){c(d("confirm",a,b,{ok:!0,cancel:!0}))}),c("prompt",function(a,b,c){c(d("prompt",a,b,{ok:!0,cancel:!0}))}),b.factory("dialog-form *",function(a){return{tag:"input.ply-input",name:a.name,value:a.value,required:!0,placeholder:a.hint||a.text}}),a.create=function(b,d,e){e||(e=d,d={});var f=c[b]||c["default"];return f(d,e).then(function(b){return new a(h(d,{el:b}))})},a.open=function(b,c,d){return a.create(b,c,d).then(function(a){return a.open()})},a.dialog=function(b,c,d){return b instanceof Object?(c=c||{},i(function(d,e){var i,j,k=c.initState,l=b,m={},n=function(a,b){(c.progress||f)(h({name:i.$name,index:i.$index,length:p,stack:l,current:i,layer:b},a),m)},o=function(b,c,d){var e=JSON.parse(JSON.stringify(b.data));i=b,(b.prepare||f)(e,m),a.create(b.ui||"alert",b.options||{},e).then(function(a){var e;j?e=j[/^inner/.test(c)?"innerSwap":"swap"](a,c):(e=a.open(),j=a),e.then(function(){m[b.$name].el=j.layerEl}),d(a)})},p=0;g(l,function(b,c){k=k||c,b.effects=b.effects||{},b.$name=c,b.$index=p++,m[c]=new a.Context}),l.$length=p,o(l[k],null,function(a){n({},a),j.options.callback=function(a){var b=a.state||"next"===i.back,c=b?l[i.next]:l[i.back];return c?(o(c,i[b?"nextEffect":"backEffect"],function(b){n(a,b)}),!1):void(a.state?d:e)(a,m)}})})):(d||(d=c||{},c={}),a.open(b,c,d).then(function(a){return i(function(b){a.options.callback=b})}))},a.ui=b,a.factory=c}); \ No newline at end of file +!function(a){window.Ply=a(window)}(function(a){"use strict";function b(a){return"function"==typeof a}function c(a){if(E)return new E(a);var b=D.Deferred();return a(b.resolve,b.reject),b}function d(a){return E?E.all(a):D.when.apply(D,a)}function e(a){return function(){C(a,1)}}function f(a,b){if(a)for(var c in a)a.hasOwnProperty(c)&&b(a[c],c,a)}function g(a){var c={};return f(a,function(a,d){c[d]=b(a)?a:a instanceof Object?g(a):a}),c}function h(a){for(var b=Array.prototype.slice,c=b.call(arguments,1),d=0,e=c.length;e>d;d++)f(c[d],function(b,c){a[c]=b});return a}function i(a,b){try{return(b||B).querySelector(a)}catch(c){return D(a,b)[0]}}function j(a,b){return a.getElementsByTagName(b)}function k(a,b){a&&b&&a.appendChild(b)}function l(a){a&&a.parentNode&&a.parentNode.removeChild(a)}function m(a,b,c){var d=c.handle=c.handle||function(b){b.target||(b.target=b.srcElement||B),3===b.target.nodeType&&(b.target=b.target.parentNode),b.preventDefault||(b.preventDefault=function(){b.returnValue=!1}),b.stopPropagation||(b.stopPropagation=function(){b.cancelBubble=!0}),c.call(a,b)};a.addEventListener?a.addEventListener(b,d,!1):a.attachEvent("on"+b,d)}function n(a,b,c){var d=c.handle;d&&(a.removeEventListener?a.removeEventListener(b,d,!1):a.detachEvent("on"+b,d))}function o(a,b,c){if(a&&a.style&&b)if(b instanceof Object)for(var d in b)o(a,d,b[d]);else{if(void 0===c)return B.defaultView&&B.defaultView.getComputedStyle?c=B.defaultView.getComputedStyle(a,""):a.currentStyle&&(c=a.currentStyle),void 0===b?c:c[b];a.style[G[b]||b]=c}}function p(a){if(null==a&&(a="div"),a.appendChild)return a;if(a.skip)return B.createDocumentFragment();"string"==typeof a&&(a={tag:a});var b,c=a.children,d=F.exec(a.tag||"");return delete a.children,a.tag=d[1]||"div",a.id=a.id||(d[2]||"").substr(1),d=(d[3]||"").split("."),d[0]=a.className||"",a.className=d.join(" "),b=B.createElement(a.tag),delete a.tag,f(a,function(c,d){c&&("css"===d?o(b,a.css):"text"===d?null!=c&&k(b,B.createTextNode(c)):"html"===d?null!=c&&(b.innerHTML=c):"ply"===d?b.setAttribute(L,c):d in b?b[d]=c:/^data-/.test(d)&&b.setAttribute(d,c))}),c&&c.appendChild?k(b,c):f(c,function(a,c){a&&("string"==typeof a?a={text:a}:"object"!=typeof a&&(a={}),"string"==typeof c&&(a.tag=a.tag||c),k(b,p(a)))}),b}function q(a){for(var b,c,d=j(a,"input"),e=0,f=d.length;f>e;e++)if(b=d[e],"submit"===b.type)!c&&(c=b);else if(!/hidden|check|radio/.test(b.type)&&""==b.value){c=b;break}c||(c=j(a,"button")[0]);try{c.focus()}catch(g){}}function r(a){return s(!0),c(function(b){for(var c,d=j(a,"img"),e=d.length,f=e,g=function(){if(--f<=0){for(e=d.length;e--;)c=d[e],n(c,"load",g),n(c,"error",g);s(!1),b()}};e--;)c=d[e],c.complete?f--:(m(c,"load",g),m(c,"error",g));!f&&g()})}function s(a){var b=s.get();clearTimeout(s.pid),s.pid=a?C(function(){k(B.body,b)},100):C(function(){l(b)},100)}function t(a,b){return p({css:{padding:"20px 20px 40px",display:"inline-block",position:"relative",textAlign:"left",whiteSpace:"normal",verticalAlign:"middle",transform:"translate3d(0, 0, 0)"},children:b.baseHtml?[{ply:":layer",tag:".ply-layer",className:b.mod,css:h({overflow:"hidden",position:"relative",backfaceVisibility:"hidden"},b.layer),children:[b.flags.closeBtn&&{ply:":close",tag:".ply-x",text:x.lang.cross},{tag:".ply-inside",children:a}]}]:a})}function u(a){return p({ply:":overlay",tag:".ply-overlay",css:{top:0,left:0,right:0,bottom:0,position:"fixed"},children:[{tag:"div",css:h({width:"100%",height:"100%"},a)}]})}function v(a,b,c){a.wrapEl=p({css:{whiteSpace:"nowrap",zIndex:b.zIndex}}),c||(a.overlayEl=u(b.overlay),a.overlayBoxEl=a.overlayEl.firstChild,k(a.wrapEl,a.overlayEl));var d=p();o(d,{height:"100%",display:"inline-block",verticalAlign:"middle"}),k(a.wrapEl,d);var e=b.el;return a.el=e&&e.cloneNode?e.cloneNode(!0):p({html:e||""}),a.layerEl=t(a.el,b),a.contentEl=w(a.layerEl),a.context=new y(a.layerEl),k(a.wrapEl,a.layerEl),a.bodyEl=b.body&&i(b.body)||B.body,a.wrapEl.tabIndex=-1,o(a.wrapEl,{top:0,left:0,right:0,bottom:0,position:"fixed",textAlign:"center",overflow:"auto",outline:0}),a}function w(a){return a.firstChild.lastChild.firstChild}function x(a,b){b=a instanceof Object?a:b||{},b.el=b.el||a;var d=this;d.cid="c"+z++,M.zIndex++,d.options=b=h({},M,b),b.flags=h({},M.flags,b.flags),v(d,b),d.setEffect(b.effect),d.fx=function(a){return!(d.fx.queue=d.fx.queue.then(a,a).then(function(){return d}))},d.fx.queue=c(function(a){a()}),d.on("click",":overlay",function(){d.hasFlag("closeByOverlay")&&d.closeBy("overlay")}),d.on("click",":close",function(a,b){a.preventDefault(),d.closeBy("BUTTON"===b.nodeName?"cancel":"x")}),d.options.init(this)}function y(a){this.el=a}var z=1,A=function(){},B=a.document,C=a.setTimeout,D=a.jQuery||a.Zepto||a.ender||a.$,E=a.Promise,F=/^(\w+)?(#\w+)?((?:\.[\w_-]+)*)/i,G=function(){var a={},b=p().style,c="transition transform perspective transformStyle transformOrigin backfaceVisibility".split(" "),d=["Webkit","Moz","O","MS"];return f(c,function(c,e){if(a[c]=c in b&&c,!a[c])for(e=0;4>e;e++){var f=d[e]+c.charAt(0).toUpperCase()+c.substr(1);if(a[c]=f in b&&f)break}}),a}(),H={esc:27},I=[],J=I.push,K=I.splice,L="data-ply";s.get=function(){return s.el||(s.el=p({tag:".ply-loading",children:{".ply-loading-spinner":!0}}))};var M={zIndex:1e4,layer:{},overlay:{opacity:.6,backgroundColor:"rgb(0, 0, 0)"},flags:{closeBtn:!0,bodyScroll:!1,closeByEsc:!0,closeByOverlay:!0,hideLayerInStack:!0,visibleOverlayInStack:!1},baseHtml:!0,init:A,open:A,close:A,destroy:A,callback:A};return x.fn=x.prototype={constructor:x,_activate:function(){if(!this.hasFlag("bodyScroll")){var a=this.bodyEl,b=p();this.__overflow=o(a,"overflow"),this.__paddingRight=o(a,"paddingRight"),k(a,b),o(a,{overflow:"hidden",paddingRight:a.offsetWidth-b.offsetWidth+"px"}),l(b)}m(this.wrapEl,"submit",this._getHandleEvent("submit"))},_deactivate:function(){this.hasFlag("bodyScroll")||o(this.bodyEl,{overflow:this.__overflow,paddingRight:this.__paddingRight}),n(this.layerEl,"submit",this._getHandleEvent("submit"))},_getHandleEvent:function(a){var b=this,c=b.__handleEvent||(b.__handleEvent={});return c[a]||(c[a]=function(c){b._handleEvent(a,c)}),c[a]},_handleEvent:function(a,b){b.preventDefault(),this.closeBy(a)},applyEffect:function(a,b,c){return a.nodeType||(c=b,b=a,a=this.layerEl),c=x.effects.get(c||this.effects),x.effects.apply.call(c,this[a]||a,b)},closeBy:function(a){var b=this.options.callback({by:a,state:"submit"===a,layer:this,context:this.context});b!==!1&&this.close()},on:function(a,b,c){var d=this;return c||(c=b,b=":layer"),c["_"+b]=function(a){var e=a.target;do if(1===e.nodeType&&e.getAttribute(L)===b)return c.call(d,a,e);while(e!==d.wrapEl&&(e=e.parentNode))},m(d.wrapEl,a,c["_"+b]),d},off:function(a,b,c){return c||(c=b,b="layer"),n(this.wrapEl,a,c["_"+b]||A),this},hasFlag:function(a){return!!this.options.flags[a]},setEffect:function(a){return this.effects=x.effects.get(a),this},_toggleState:function(a,b){var c=this,e=a?"open":"close",f=x.stack.last;return c.visible!=a&&(c.visible=a,c[a?"_activate":"_deactivate"](),x.stack[a?"add":"remove"](c),c.fx(function(){return r(c.wrapEl).then(function(){var g=x.stack.length===(a?1:0),h=f&&f.hasFlag("hideLayerInStack"),i=g||c.hasFlag("visibleOverlayInStack");return a?(!i&&l(c.overlayBoxEl),k(c.bodyEl,c.wrapEl),c.wrapEl.focus(),q(c.layerEl),h&&f.applyEffect("close.layer",b).then(function(){l(f.layerEl)})):(f=x.stack.last)&&(k(f.wrapEl,f.layerEl),f.hasFlag("hideLayerInStack")&&f.applyEffect("open.layer",b)),d([c.applyEffect(e+".layer",b),i&&c.applyEffect("overlayEl",e+".overlay",b)]).then(function(){a||(l(c.wrapEl),k(c.overlayEl,c.overlayBoxEl)),c.options[e](c)})})})),c.fx.queue},open:function(a){return this._toggleState(!0,a)},close:function(a){return this._toggleState(!1,a)},_swap:function(a,b,c,e,f){var g=this;return g.visible?g.fx(function(){return r(b).then(function(){return e(),d([g.applyEffect(a,"close.layer",c),g.applyEffect(b,"open.layer",c)]).then(function(){l(a),f(),g.wrapEl.focus(),q(b)})})}):f(),g.fx.queue},swap:function(a,b){a=h({},this.options,a);var c=this,d=v({},a,!0),e=b||a.effect?x.effects.get(b||a.effect):c.effects,f=c.layerEl,g=d.layerEl;return c._swap(f,g,e,function(){k(c.bodyEl,c.wrapEl),k(c.bodyEl,d.wrapEl)},function(){l(d.wrapEl),k(c.wrapEl,g),c.el=d.el,c.layerEl=g,c.contentEl=w(g),c.context.el=g})},innerSwap:function(a,b){a=h({},this.options,a);var c=this,d=v({},a,!0),e=b||a.effect?x.effects.get(b||a.effect):c.effects,f=i(".ply-inside",d.layerEl),g=i(".ply-inside",c.layerEl);return c._swap(g,f,e,function(){o(g,{width:g.offsetWidth+"px",position:"absolute"}),k(g.parentNode,f)},A)},destroy:function(){l(this.wrapEl),this.visible=!1,this._deactivate(),this.options.destroy(this)}},x.stack={_idx:{},last:null,length:0,_pop:function(a){var b=x.stack.last;a.keyCode===H.esc&&b.hasFlag("closeByEsc")&&b.closeBy("esc")},add:function(a){var b=J.call(this,a);this.last=a,this._idx[a.cid]=b-1,1===b&&m(B,"keyup",this._pop)},remove:function(a){var b=this._idx[a.cid];b>=0&&(K.call(this,b),delete this._idx[a.cid],this.last=this[this.length-1],this.last||n(B,"keyup",this._pop))}},x.effects={defaults:{duration:300,open:{layer:null,overlay:null},close:{layer:null,overlay:null}},setup:function(a){this.defaults=this.get(a)},get:function(a){function b(a){var b=/^([\w_-]+)(?::(\d+%?))?(\[[^\]]+\])?/.exec(a)||[];return{name:b[1]||a,duration:b[2]||null,args:JSON.parse(b[3]||"null")||{}}}function c(c,d,e){var f=b(d),g=c[f.name]||e||{},h=f.duration||g.duration||c.duration||a.duration;return"string"==typeof g&&(g=b(g),delete g.args),/%/.test(g.duration)&&(g.duration=parseInt(g.duration,10)/100*h),g.duration=0|(g.duration||h),g}var d=g(this.defaults);if("string"==typeof a){var e=b(a);a=g(this[e.name]||{open:{},close:{}}),a.duration=e.duration||a.duration,a.open.args=e.args[0],a.close.args=e.args[1]}else if(a instanceof Array){var f=b(a[0]),h=b(a[1]),i=this[f.name],j=this[h.name];a={open:g(i&&i.open||{layer:a[0],overlay:a[0]}),close:g(j&&j.close||{layer:a[1],overlay:a[1]})},a.open.args=f.args[0],a.close.args=h.args[0]}a=a||{},a.duration=0|(a.duration||d.duration);for(var k in{open:0,close:0}){var l=a[k]||d[k]||{};"string"==typeof l&&(l={layer:l}),l.layer=c(l,"layer",d[k].layer),l.overlay=c(l,"overlay",d[k].overlay),void 0===l.args&&delete l.args,a[k]=l}return a},apply:function(a,d){d=d.split(".");var h,i,j=this[d[0]],k=a.firstChild,l=[a.getAttribute("style"),k&&k.getAttribute("style")];return j&&(i=j[d[1]])&&(h=x.effects[i.name])&&(h.to||h.from)?(h=g(h),o(a,"transition","none"),o(k,"transition","none"),f(h.to,function(b,c,d){"&"===b&&(d[c]=o(a,c))}),b(h.from)?h.from(a,j.args):h.from&&o(a,h.from),c(function(c){h.width=a.offsetWidth,o(a,"transition","all "+i.duration+"ms"),o(k,"transition","all "+i.duration+"ms"),b(h.to)?h.to(a,j.args):o(a,h.to),C(c,G.transition&&i.duration)}).then(e(function(){return a.setAttribute("style",l[0]),k&&k.setAttribute("style",l[1]),a}))):c(function(a){a()})},fade:{open:{layer:"fade-in:80%",overlay:"fade-in:100%"},close:{layer:"fade-out:60%",overlay:"fade-out:60%"}},scale:{open:{layer:"scale-in",overlay:"fade-in"},close:{layer:"scale-out",overlay:"fade-out"}},fall:{open:{layer:"fall-in",overlay:"fade-in"},close:{layer:"fall-out",overlay:"fade-out"}},slide:{open:{layer:"slide-in",overlay:"fade-in"},close:{layer:"slide-out",overlay:"fade-out"}},"3d-flip":{open:{layer:"3d-flip-in",overlay:"fade-in"},close:{layer:"3d-flip-out",overlay:"fade-out"}},"3d-sign":{open:{layer:"3d-sign-in",overlay:"fade-in"},close:{layer:"3d-sign-out",overlay:"fade-out"}},inner:{open:{layer:"inner-in"},close:{layer:"inner-out"}},"fade-in":{from:{opacity:0},to:{opacity:"&"}},"fade-out":{to:{opacity:0}},"slide-in":{from:{opacity:0,transform:"translateY(20%)"},to:{opacity:"&",transform:"translateY(0)"}},"slide-out":{to:{opacity:0,transform:"translateY(20%)"}},"fall-in":{from:{opacity:0,transform:"scale(1.3)"},to:{opacity:"&",transform:"scale(1)"}},"fall-out":{to:{opacity:0,transform:"scale(1.3)"}},"scale-in":{from:{opacity:0,transform:"scale(0.7)"},to:{opacity:"&",transform:"scale(1)"}},"scale-out":{to:{opacity:0,transform:"scale(0.7)"}},rotate3d:function(a,b,c,d,e){o(a,{perspective:"1300px"}),o(a.firstChild,{opacity:b,transform:"rotate"+c+"("+d+"deg)",transformStyle:"preserve-3d",transformOrigin:e?"50% 0":"50%"})},"3d-sign-in":{from:function(a){x.effects.rotate3d(a,0,"X",-60,"50% 0")},to:function(a){o(a.firstChild,{opacity:1,transform:"rotateX(0)"})}},"3d-sign-out":{from:function(a){x.effects.rotate3d(a,1,"X",0,"50% 0")},to:function(a){o(a.firstChild,{opacity:0,transform:"rotateX(-60deg)"})}},"3d-flip-in":{from:function(a,b){x.effects.rotate3d(a,0,"Y",b||-70)},to:function(a){o(a.firstChild,{opacity:1,transform:"rotateY(0)"})}},"3d-flip-out":{from:function(a){x.effects.rotate3d(a,1,"Y",0)},to:function(a,b){o(a.firstChild,{opacity:0,transform:"rotateY("+(b||70)+"deg)"})}},"inner-in":{from:function(a){o(a,"transform","translateX(100%)")},to:function(a){o(a,"transform","translateX(0%)")}},"inner-out":{from:function(a){o(a,"transform","translateX(0%)")},to:function(a){o(a,"transform","translateX(-100%)")}}},y.fn=y.prototype={constructor:y,getEl:function(a){if(this.el)for(var b=j(this.el,"*"),c=b.length;c--;)if(b[c].getAttribute(L+"-name")===a)return b[c]},val:function(a,b){var c=this.getEl(a);return c&&null==c.value&&(c=j(c,"input")[0]||j(c,"textarea")[0]||j(c,"select")[0]),c&&null!=b&&(c.value=b),c&&c.value||""}},x.lang={ok:"OK",cancel:"Cancel",cross:"✖"},x.css=o,x.keys=H,x.noop=A,x.each=f,x.extend=h,x.promise=c,x.defaults=M,x.attrName=L,x.Context=y,x.dom={build:p,append:k,remove:l,addEvent:m,removeEvent:n},x}),function(a){a(Ply)}(function(a){"use strict";function b(a,c,d){var f,g=b[a];return g||(a=a.split(/\s+/).slice(0,-1).join(" "),g=c&&(b[a+" [name="+c.name+"]"]||b[a+" [type="+c.type+"]"])||b[a+" *"]||b[":default"]),f=j(g(c,d)),c&&c.name&&f.setAttribute(e+"-name",c.name),f.className+=" ply-ui",f}function c(a,d){c["_"+a]=d,c[a]=function(a,c){return i(function(b,e){d(a,c,b,e)}).then(function(a){return a.appendChild||(a=b(":root",a)),a})}}function d(a,b,c,d){return b.mod=a,b.effect=b.effect||"slide",b.flags=h({closeBtn:!1},b.flags),{header:c.title,content:c.form?{"dialog-form":{children:c.form}}:{el:c.text||c},ctrls:{ok:c.ok||d.ok,cancel:c.cancel||d.cancel}}}var e=a.attrName,f=a.noop,g=a.each,h=a.extend,i=a.promise,j=a.dom.build,k=a.dom.append,l=a.lang,m=function(a,b){return null==a?{skip:!0}:("string"==typeof a&&(a={text:a}),"object"==typeof a&&(a.name=a.name||b),a)};b.factory=function(a,c,d){b[a.trim().replace(/\s+/g," ")]=function(e,f){var i=document.createDocumentFragment();if(null!=e||":root"===a){e=d?e:m(e),g(d?e:e.children,function(c,d){var e=((f||a)+" "+d).replace(/^:\w+\s+/,""),g=b(e,m(c,d),e);k(i,g)}),d||delete e.children;var j=c(e,i);return j.appendChild||h(j,e),j}return i}},b.factory(":default",function(a,b){return a.children=b,a}),b.factory(":root",function(a){return{tag:"form.ply-form",className:a.mod,children:[b(":header",a.header),b(":content",a.content),a.ctrls&&b(":default",{tag:"div.ply-footer",children:a.ctrls})]}}),b.factory(":header",function(a,b){return{tag:".ply-header",text:a.text,children:b}}),b.factory(":content",function(a,b){return{tag:".ply-content",children:b}},!0),b.factory("ok",function(a){return{ply:":ok",tag:"button.ply-ok",text:a===!0?l.ok:a}}),b.factory("cancel",function(a){return{ply:":close",tag:"button.ply-cancel",type:"reset",text:a===!0?l.cancel:a}}),c.use=function(a,b,d,e,f){c["_"+a](b,d,e,f)},c("default",function(a,b,c){c(b||{})}),c("alert",function(a,b,c){c(d("alert",a,b,{ok:!0}))}),c("confirm",function(a,b,c){c(d("confirm",a,b,{ok:!0,cancel:!0}))}),c("prompt",function(a,b,c){c(d("prompt",a,b,{ok:!0,cancel:!0}))}),b.factory("dialog-form *",function(a){return{tag:"input.ply-input",type:a.type||"text",name:a.name,value:a.value,required:!0,placeholder:a.hint||a.text}}),a.create=function(b,d,e){e||(e=d,d={});var f=c[b]||c["default"];return f(d,e).then(function(b){return new a(h(d,{el:b}))})},a.open=function(b,c,d){return a.create(b,c,d).then(function(a){return a.open()})},a.dialog=function(b,c,d){return b instanceof Object?(c=c||{},i(function(d,e){var i,j,k,l=c.initState,m=b,n={},o=function(a,b){(c.progress||f)(h({name:i.$name,index:i.$index,length:q,stack:m,current:i,layer:b},a),n)},p=function(b,c,d){var e=JSON.parse(JSON.stringify(b.data));i=b,k=!0,(b.prepare||f)(e,n),a.create(b.ui||"alert",b.options||{},e).then(function(a){var e;j?e=j[/^inner/.test(c)?"innerSwap":"swap"](a,c):(j=a,e=j.open()),e.then(function(){n[b.$name].el=j.layerEl,k=!1}),d(j)})},q=0;g(m,function(b,c){l=l||c,b.effects=b.effects||{},b.$name=c,b.$index=q++,n[c]=new a.Context}),m.$length=q,p(m[l],null,function(a){o({},a),j.options.callback=function(a){if(k)return!1;var b=a.state||"next"===i.back,c=m[i[b?"next":"back"]];return c?(p(c,i[b?"nextEffect":"backEffect"],function(b){o(a,b)}),!1):void(a.state?d:e)(a,n)}})})):(d||(d=c||{},c={}),a.open(b,c,d).then(function(a){return i(function(b){a.options.callback=b})}))},a.ui=b,a.factory=c}); \ No newline at end of file diff --git a/src/Ply.es6 b/src/Ply.es6 index 83f72df..ff96d0d 100644 --- a/src/Ply.es6 +++ b/src/Ply.es6 @@ -103,7 +103,6 @@ function _promise(executor) { /* istanbul ignore if */ if (Promise) { - // @todo: Поткрыть тестами return new Promise(executor); } else { var dfd = $.Deferred(); @@ -559,7 +558,7 @@ /** - * Создать лаер с контентом + * Создать слой с контентом * @param {HTMLElement} contentEl * @param {Object} options * @returns {HTMLElement} @@ -608,13 +607,14 @@ return _buildDOM({ ply: ':overlay', tag: '.ply-overlay', - css: _extend({ + css: { top: 0, left: 0, right: 0, bottom: 0, position: 'fixed' - }, style) + }, + children: [{ tag: 'div', css: _extend({ width: '100%', height: '100%' }, style) }] }); } @@ -629,12 +629,13 @@ */ function _createPly(target, options, onlyLayer) { // Корневой слой - target.wrapEl = _buildDOM({ css: { whiteSpace: 'nowrap' } }); + target.wrapEl = _buildDOM({ css: { whiteSpace: 'nowrap', zIndex: options.zIndex } }); // Затемнение if (!onlyLayer) { target.overlayEl = _createOverlay(options.overlay); + target.overlayBoxEl = target.overlayEl.firstChild; _appendChild(target.wrapEl, target.overlayEl); } @@ -698,8 +699,10 @@ // // Настройки по умолчанию // - var defaults = { - layer: {}, + var _defaults = { + zIndex: 10000, + + layer: {}, // css overlay: { opacity: .6, @@ -710,7 +713,9 @@ closeBtn: true, bodyScroll: false, closeByEsc: true, - closeByOverlay: true + closeByOverlay: true, + hideLayerInStack: true, + visibleOverlayInStack: false }, baseHtml: true, @@ -735,21 +740,30 @@ /** * @class Ply - * @param {Object} options опции слоя + * @param {HTMLElement|Object} el слой или опции + * @param {Object} [options] опции слоя */ - function Ply(options) { + function Ply(el, options) { + options = (el instanceof Object) ? el : (options || {}); + options.el = options.el || el; + + var _this = this; // Локальный идентификатор _this.cid = 'c' + gid++; + // Увеличиваем глобальный zIndex + _defaults.zIndex++; + + // Опции - _this.options = options = _extend({}, defaults, options); + _this.options = options = _extend({}, _defaults, options); // Флаги - options.flags = _extend({}, defaults.flags, options.flags); + options.flags = _extend({}, _defaults.flags, options.flags); // Создаем Ply-элементы @@ -869,11 +883,16 @@ * @param {String} name * @param {String|Object} [effects] * @returns {Promise} - * @private */ - _applyEffect: function (el, name, effects) { + applyEffect: function (el, name, effects) { + if (!el.nodeType) { + effects = name; + name = el; + el = this.layerEl; + } + effects = Ply.effects.get(effects || this.effects); - return Ply.effects.apply.call(effects, el, name); + return Ply.effects.apply.call(effects, this[el] || el, name); }, @@ -973,9 +992,10 @@ * @returns {Promise} * @private */ - toggleState: function (state, effect) { + _toggleState: function (state, effect) { var _this = this, - mode = state ? 'open' : 'close' + mode = state ? 'open' : 'close', + prevLayer = Ply.stack.last ; /* istanbul ignore else */ @@ -989,18 +1009,38 @@ // Очередь эффектов _this.fx(() => { return _preloadImage(_this.wrapEl).then(() => { + var isFirst = Ply.stack.length === (state ? 1 : 0), + hideLayer = prevLayer && prevLayer.hasFlag('hideLayerInStack'), + hasOverlay = isFirst || _this.hasFlag('visibleOverlayInStack'); + if (state) { + // Убрать «затемнение» если мы не первые в стеке + !hasOverlay && _removeElement(_this.overlayBoxEl); + _appendChild(_this.bodyEl, _this.wrapEl); _this.wrapEl.focus(); _autoFocus(_this.layerEl); + + if (hideLayer) { + // Скрыть слой «под» + prevLayer.applyEffect('close.layer', effect).then(() => { + _removeElement(prevLayer.layerEl); + }); + } + } else if (prevLayer = Ply.stack.last) { + // Слой мог быть скрыт, нужно вернуть его + _appendChild(prevLayer.wrapEl, prevLayer.layerEl); + prevLayer.hasFlag('hideLayerInStack') && prevLayer.applyEffect('open.layer', effect); } + // Применяем основные эффекты return _promiseAll([ - _this._applyEffect(_this.overlayEl, mode + '.overlay', effect), - _this._applyEffect(_this.layerEl, mode + '.layer', effect) + _this.applyEffect(mode + '.layer', effect), + hasOverlay && _this.applyEffect('overlayEl', mode + '.overlay', effect) ]).then(() => { if (!state) { _removeElement(_this.wrapEl); + _appendChild(_this.overlayEl, _this.overlayBoxEl); } _this.options[mode](_this); }); @@ -1018,7 +1058,7 @@ * @returns {Promise} */ open: function (effect) { - return this.toggleState(true, effect); + return this._toggleState(true, effect); }, @@ -1028,7 +1068,7 @@ * @returns {Promise} */ close: function (effect) { - return this.toggleState(false, effect); + return this._toggleState(false, effect); }, @@ -1050,8 +1090,8 @@ prepare(); return _promiseAll([ - _this._applyEffect(closeEl, 'close.layer', effects), - _this._applyEffect(openEl, 'open.layer', effects) + _this.applyEffect(closeEl, 'close.layer', effects), + _this.applyEffect(openEl, 'open.layer', effects) ]).then(() => { _removeElement(closeEl); complete(); @@ -1127,7 +1167,7 @@ /** - * Уничтожить лаер + * Уничтожить слой */ destroy: function () { _removeElement(this.wrapEl); @@ -1159,7 +1199,7 @@ /** - * Удаить последний ply-лаер из стека + * Удаить последний ply-слой из стека * @param {Event} evt * @private */ @@ -1392,9 +1432,12 @@ // Возвращаем стили, именно на "then" с разрывом, т.к. «Обещания» могу быть ассинхронными el.setAttribute('style', oldStyle[0]); firstEl && firstEl.setAttribute('style', oldStyle[1]); + return el; })); } } + + return _promise((resolve) => { resolve(); }); }, @@ -1609,7 +1652,7 @@ Ply.each = _each; Ply.extend = _extend; Ply.promise = _promise; - Ply.defaults = defaults; + Ply.defaults = _defaults; Ply.attrName = _plyAttr; Ply.Context = Context; diff --git a/tests/Ply.stack.tests.js b/tests/Ply.stack.tests.js index f062b3e..b9cb6ed 100644 --- a/tests/Ply.stack.tests.js +++ b/tests/Ply.stack.tests.js @@ -2,12 +2,19 @@ module('Ply.stack'); asyncTest('2x', function () { - new Ply({ el: '1', effect: 'fade' }).open().then(function () { - new Ply({ el: '2', effect: 'fade' }).open().then(function () { -// Ply.stack.last.close(); -// Ply.stack.last.close().then(function () { -// start(); -// }); + new Ply('foo', { effect: 'slide' }).open().then(function (foo) { + new Ply('bar', { effect: 'fall' }).open().then(function (bar) { + ok(!foo.layerEl.parentNode, 'foo.parent == null'); + ok(!bar.overlayBoxEl.parentNode, 'bar.parent == null'); + + Ply.stack.last.close().then(function () { + ok(!!foo.layerEl.parentNode, 'foo.parent != null'); + ok(!!bar.overlayBoxEl.parentNode, 'bar.parent != null'); + + Ply.stack.last.close().then(function () { + start(); + }); + }); }); }); }); diff --git a/tests/Ply.tests.js b/tests/Ply.tests.js index 7c585ad..12d65b1 100644 --- a/tests/Ply.tests.js +++ b/tests/Ply.tests.js @@ -29,7 +29,7 @@ layer = new Ply(); for (var key in Ply.defaults.overlay) { - equal(layer.overlayEl.style[key], Ply.defaults.overlay[key], key); + equal(layer.overlayBoxEl.style[key], Ply.defaults.overlay[key], key); } equal(layer.bodyEl, document.body, 'body'); @@ -39,17 +39,17 @@ // Overlay 1 layer = new Ply({ overlay: { opacity: 1 } }); - equal(layer.overlayEl.style.opacity, 1, 'opacity: 1'); - equal(layer.overlayEl.style.backgroundColor, "", 'backgroundColor: ""'); + equal(layer.overlayBoxEl.style.opacity, 1, 'opacity: 1'); + equal(layer.overlayBoxEl.style.backgroundColor, "", 'backgroundColor: ""'); // Overlay 2 layer = new Ply({ overlay: { opacity: 1, backgroundColor: 'rgb(255, 0, 0)' } }); - equal(layer.overlayEl.style.opacity, 1, 'opacity: 1'); - equal(layer.overlayEl.style.backgroundColor, 'rgb(255, 0, 0)', 'backgroundColor: red'); + equal(layer.overlayBoxEl.style.opacity, 1, 'opacity: 1'); + equal(layer.overlayBoxEl.style.backgroundColor, 'rgb(255, 0, 0)', 'backgroundColor: red'); // Overlay 3 layer = new Ply({ overlay: null }); - notEqual(layer.overlayEl.style.position, 'fixed', 'overlay: null'); + notEqual(layer.overlayBoxEl.style.position, 'fixed', 'overlay: null'); // Layer layer = new Ply({ layer: { textAlign: 'center' } }); @@ -75,7 +75,10 @@ var content = document.createElement('b'); content.innerHTML = '!'; - equal(new Ply({ }).contentEl.innerHTML, '', 'conentEl'); + equal(new Ply('').contentEl.innerHTML, '', 'conentEl'); + equal(new Ply('Wow!').contentEl.innerHTML, 'Wow!', 'contentEl'); + equal(new Ply(content).contentEl.innerHTML, '!'); + equal(new Ply({ el: 'Wow!' }).contentEl.innerHTML, 'Wow!', 'contentEl'); equal(new Ply({ el: content }).contentEl.innerHTML, '!'); }); @@ -223,11 +226,11 @@ simulateEvent(layer.contentEl.getElementsByTagName('em')[1], 'click'); equal(log.join('\n'), [ - 'layer:DIV.click->layer', + 'layer:DIV.click->:layer', 'foo:I.click->foo', 'bar:B.click->bar', 'bar:B.click->bar', - 'layer:DIV.click->layer' + 'layer:DIV.click->:layer' ].join('\n')); return layer.close();