ht-contextmenu.js 16 KB

1
  1. !function(v,k){"use strict";var b="ht",s=v[b],h="position",Y="absolute",n="px",c="left",N="top",R="innerHTML",t="className",I="width",O="height",z="0",j="display",B="none",w="visibility",d="user-select",H="margin",e="padding",A=null,X=s.Color,J=s.Default,K=J.getInternal(),G=v.setTimeout,E=v.setInterval,$=v.clearTimeout,V=v.clearInterval,Q=v.parseInt,q=J.isLeftButton,u=J.isDragging,o=J.startDragging,x=J.getDistance,l=J.isTouchable,a=X.widgetIconHighlight,_=X.widgetIconBorder,W=X.widgetIconGradient,Z=function(){return document},i=function(l,x){return l.querySelectorAll(x)},U=function(m, borderColor){var f=Z().createElement(m);return"ul"===m&&(D(f,h,"relative"),D(f,H,z),D(f,e,z),D(f,"list-style",B),D(f,"box-sizing","border-box"),D(f,"-moz-box-sizing","border-box"),D(f,j,"inline-block"),D(f,"vertical-align","text-bottom"),D(f,"border","1px solid "+(borderColor || J.contextMenuBorderColor)),D(f,"box-shadow","0 0 16px 1px "+J.contextMenuShadowColor),D(f,"overflow","hidden"),J.contextMenuBorderRadius&&D(f,"border-radius",J.contextMenuBorderRadius+n)),f},M=function(f){var T=f.touches[0];return T?T:f.changedTouches[0]},m=function(){return U("div")},C=function(){return U("canvas")},D=function(p,w,S){p.style.setProperty(w,S,A)},g=function(j,f,g){J.def(s.widget[j],f,g)},y=function(L,J){L.appendChild(J)},f=function(b,t){b.removeChild(t)},S=function(){return Z().body},L=function(U,i,T,B){U.addEventListener(i,T,!!B)},r=function(O,R,E,C){O.removeEventListener(R,E,!!C)};K.addMethod(J,{contextMenuCheckIcon:{width:16,height:16,comps:[{type:"border",rect:[1,1,14,14],width:1,color:_},{type:"shape",points:[13,3,7,12,4,8],borderWidth:2,borderColor:a}]},contextMenuRadioIcon:{width:16,height:16,comps:[{type:"circle",rect:[2,2,12,12],borderWidth:1,borderColor:_},{type:"circle",rect:[4,4,8,8],borderWidth:1,borderColor:a,gradient:J.imageGradient,gradientColor:W,background:a}]},contextMenuLabelFont:(l?"16":"13")+"px arial, sans-serif",contextMenuLabelColor:"#000",contextMenuBackground:"#fff",contextMenuDisabledLabelColor:"#888",contextMenuHoverBackground:"#648BFE",contextMenuHoverLabelColor:"#fff",contextMenuSeparatorWidth:1,contextMenuSeparatorColor:"#E5E5E5",contextMenuScrollerColor1:"#FDFDFD",contextMenuScrollerColor2:"#D3D3D3",contextMenuScrollerBorderColor:"#C3C3C3",contextMenuBorderColor:"#C3C3C3",contextMenuShadowColor:"rgba(128, 128, 128, 0.5)",contextMenuBorderRadius:5,contextMenuSubmenuMark:"▶"},!0);var P=function(F){var W=this,n=F._view;if(W.$11b=F,W.addListeners(),L(n,"contextmenu",function(U){U.preventDefault()}),!l){var I=W.$37b=W.$36b.bind(W);L(n,"mouseover",I),L(n,"mouseout",I)}};J.def(P,k,{ms_listener:1,getView:function(){return this.$11b._view},handle_touchstart:function(e){if(J.preventDefault(e),q(e)){for(var w=this,i=w.$11b,C=w.getView(),R=C.children,L=0;L<R.length;L++){var a=R[L],O=a.$24b,H=a.$25b;if(O&&O.contains(e.target))return i.scrollUp(a),w.$28b=G(function(){w.$29b=E(function(){i.scrollUp(a)},100)},500),o(w,e),void 0;if(H&&H.contains(e.target))return i.scrollDown(a),w.$28b=G(function(){w.$29b=E(function(){i.scrollDown(a)},100)},500),o(w,e),void 0}l&&(e=M(e)),w.$30b={x:e.pageX,y:e.pageY}}},handle_mousedown:function(M){this.handle_touchstart(M)},handle_touchend:function(P){if(q(P)){var w=this,I=w.$30b,C=l?{x:M(P).pageX,y:M(P).pageY}:{x:P.pageX,y:P.pageY};if(!I||x(I,C)>1)return delete w.$30b,void 0;for(var g=w.getView(),Z=w.$11b,H=P.target,z=A,b=A,n=Z._items,k=g.$26b,J=0;J<k.length;J++)if(b=k[J],b.contains(H)){z=b.getAttribute("data-id");break}if(z&&n){var W=Z.$17b(n,function(K){return K._id===z});if(W){var c=!1;W.disabled instanceof Function?c=W.disabled.call(Z,W):W.disabled===!0&&(c=!0),c||(W.items?l&&w.$36b(b,!0):Z.$1b(W,P))}}delete w.$30b}},$36b:function(q,A){if(!u()){var $,F=this,o=F.$11b,W=F.getView(),T=o.$20b||W.children[0],B=o.$19b,k=W.$26b,r=W.children,S=q.target,i=W.getBoundingClientRect(),a=J.getWindowInfo(),l=a.width,K=a.height,I=function(o){for(var j=0;j<r.length;j++){var A=r[j],S=new RegExp(o+"$"),h=A[t];if(h&&(S.test(h)||h.indexOf(o+" ")>=0))return A}},s=function(T){for(var L=0;L<k.length;L++){var N=k[L],n=new RegExp(T+"$"),f=N[t];if(f&&(n.test(f)||f.indexOf(T+" ")>=0))return N}},z=function(y){var F=s("menu-item"+y.$45b),j=F.getBoundingClientRect(),m=j.top-i.top,A=j.left-i.left;D(y,N,m+n),D(y,c,A+j.width+n);var v=y.getBoundingClientRect(),V=v.top,r=v.left,E=v.height,X=v.width,b=V+E+2,x=r+X+2;b>K&&D(y,N,m+K-b+n),x>l&&D(y,c,A-X+n)};if(A)$=q;else{if("mouseover"===q.type){for(var _=0;_<k.length;_++){var j=k[_];if(j.contains(S)){$=j;break}}if(!$&&B){var Z=B.parentNode,R=I("submenu"+B.getAttribute("data-id"));(R&&R.contains(S)||Z&&Z.contains(S))&&($=B)}}else if("mouseout"===q.type){for(var p=!1,d=q.relatedTarget,_=0;_<r.length;_++){var V=r[_];if("hidden"!==V.style.visibility&&V.contains(d)){p=!0;break}}if(p)return}!$&&T&&($=s("menu-item"+(T.$45b||"NaN")))}if($!=B){if(B)for(var H=B;H;){H[t]=H[t].replace(" menu-item-hover",""),H[t].indexOf("disabled")<0&&(D(H,"background-color",o.contextMenuBackground||J.contextMenuBackground),D(H,"color",o.contextMenuLabelColor||J.contextMenuLabelColor));var L=I("submenu"+H.getAttribute("data-id"));L&&D(L,w,"hidden");var f=H.parentNode;H=s("menu-item"+(f.$45b||"NaN"))}if($){for(var P=$,e=[];P;){P[t]+=" menu-item-hover",P[t].indexOf("disabled")<0&&(D(P,"background-color",o.contextMenuHoverBackground||J.contextMenuHoverBackground),D(P,"color",o.contextMenuHoverLabelColor||J.contextMenuHoverLabelColor));var y=I("submenu"+P.getAttribute("data-id"));y&&(D(y,w,"visible"),e.push(y));var f=P.parentNode;P=s("menu-item"+(f.$45b||"NaN"))}e.reverse(),e.forEach(function(H){z(H)})}}o.$19b=$,o.$20b=$?$.parentNode:W.children[0]}},handle_mouseup:function(u){this.handle_touchend(u)},handleWindowTouchEnd:function(){var w=this;w.$28b!=A&&($(w.$28b),delete w.$28b),w.$29b!=A&&(V(w.$29b),delete w.$29b),delete w.$34b,delete w.$30b,delete w.$35b},handleWindowMouseUp:function(J){this.handleWindowTouchEnd(J)},handle_mousemove:function(f){this.handle_touchmove(f)},handle_touchmove:function(s){if(!u()&&q(s)){for(var K=this,w=K.getView().children,X=A,U=0;U<w.length;U++){var p=w[U];if(p.contains(s.target)){X=p;break}}var Y=K.$30b,W=l?{x:M(s).pageX,y:M(s).pageY}:{x:s.pageX,y:s.pageY};X&&Y&&x(Y,W)>2&&(o(K,s),K.$34b=X,K.$35b=X.$18b)}},handleWindowTouchMove:function(G){G.preventDefault();var z=this,O=z.$11b,n=z.$34b,U=z.$35b,W=z.$30b;if(W&&n){var b=l?{x:M(G).pageX,y:M(G).pageY}:{x:G.pageX,y:G.pageY},i=b.y-W.y;i>0?O.scrollUp(n,n.$18b-(U-i)):O.scrollDown(n,U-i-n.$18b)}},handleWindowMouseMove:function(V){this.handleWindowTouchMove(V)},$10b:function($,O){$.preventDefault();for(var T=this,Z=T.getView().children,M=A,R=0;R<Z.length;R++){var j=Z[R];if(j.contains($.target)){M=j;break}}if(M){var H=this.$11b,s=H.getRowHeight();Math.abs(O)>.05&&(O>0?H.scrollUp(M,O*s):0>O&&H.scrollDown(M,-O*s))}},handle_mousewheel:function(n){this.$10b(n,n.wheelDelta/40)},handle_DOMMouseScroll:function(o){this.$10b(o,-o.detail)},$44b:function(k){this.getView().contains(k.target)||this.$11b.hide()},$41b:function(l){this.$11b.show(l)},$4b:function(A,E){var x=this.$11b;if(E=E||x._items,E&&E.length&&A.keyCode){var Y=[A.keyCode];A.shiftKey&&Y.push(16),A.ctrlKey&&Y.push(17),A.altKey&&Y.push(18),/mac/.test(v.navigator?v.navigator.userAgent.toLowerCase():"")?A.metaKey&&Y.push(17):A.metaKey&&Y.push(91),Y.sort();var C=Y.join(),D=x.$17b(E,function($){if($.key){var p=$.key.slice(0);return p.sort(),C===p.join()}});if(D){D.preventDefault!==!1&&A.preventDefault();var W=!1;D.disabled instanceof Function?W=D.disabled.call(x,D):D.disabled===!0&&(W=!0),W||x.$1b(D,A)}}},$39b:function(h){var W=this,V=M(h);W.$32b={x:V.pageX,y:V.pageY}},$38b:function(F){var Q=this,K=M(F);Q.$31b={x:K.pageX,y:K.pageY},Q.$33b=G(function(){Q.$31b&&(Q.$32b?x(Q.$31b,Q.$32b)<10&&Q.$11b.show(F):Q.$11b.show(F)),delete Q.$33b,delete Q.$31b,delete Q.$32b},600)},$40b:function(){var J=this;J.$33b!=A&&($(J.$33b),delete J.$33b,delete J.$31b,delete J.$32b)}}),s.widget.ContextMenu=function(l){var i=this,c=i._view=K.createView(null,i);c[t]="ht-widget-contextmenu",i.setItems(l),i.$13b=new P(i),D(c,"font",i.contextMenuLabelFont||J.contextMenuLabelFont),D(c,h,Y),D(c,"cursor","default"),D(c,"-webkit-"+d,B),D(c,"-moz-"+d,B),D(c,"-ms-"+d,B),D(c,d,B),D(c,"box-sizing","border-box"),D(c,"-moz-box-sizing","border-box"),J.baseZIndex!=A&&D(c,"z-index",Q(J.baseZIndex)+2+""),i.$3b=function(n){i.$13b.$4b(n)}},g("ContextMenu",k,{$5b:0,_items:A,$21b:A,_enableGlobalKey:!1,ms_v:1,enableGlobalKey:function(){var J=this,z=J._enableGlobalKey;z===!1&&(L(Z(),"keydown",J.$3b),J._enableGlobalKey=!0)},disableGlobalKey:function(){this._enableGlobalKey=!1,r(Z(),"keydown",this.$3b)},setItems:function(s){this._items=s},getItems:function(){return this._items},setVisibleFunc:function(W){this.$16b=W},setLabelMaxWidth:function(v){this.$43b=v},$1b:function(q,J){var Z=this;if("check"===q.type)q.selected=!q.selected;else if("radio"===q.type){var W=q.groupId;Z.$17b(Z._items,function(G){G.groupId===W&&(G.selected=!1)}),q.selected=!0}if(Z.hide(),q.action)q.action.apply(q.scope||Z,[q,J]);else if(q.href){var _=q.linkTarget||"_self";v.open(q.href,_)}},getItemByProperty:function(k,T,l){var o=this;if(l=l||o._items,!l||0===l.length)return A;var Z=o.$17b(l,function(j){return j[k]===T});return Z||A},scrollUp:function(v,t){var B=this;if(t=t==A?20:t,t=Q(t),0!==t){var e=0;v.$18b>t&&(e=v.$18b-t),B.$42b(v,e),v.scrollTop=e,v.$18b=e}},scrollDown:function(z,P){var $=this;if(P=P==A?20:P,P=Q(P),0!==P){var y=z.$22b,x=z.$23b,V=y-x;x+z.$18b+P<y&&(V=z.$18b+P),$.$42b(z,V),z.scrollTop=V,z.$18b=V}},$42b:function(v,P){P=P==A?v.$18b:P;var d=v.$24b,F=v.$25b;d&&(D(d,"top",P+n),0==P?D(d,j,B):D(d,j,"block")),F&&(D(F,"bottom",-P+n),P==v.$22b-v.$23b?D(F,j,B):D(F,j,"block"))},getRowHeight:function(){return this._view.querySelector(".menu-item").offsetHeight},$17b:function(W,g){for(var H=0;H<W.length;H++){var C=W[H];if(g(C))return C;if(C.items){var s=this.$17b(C.items,g);if(s)return s}}},$2b:function(A,z){for(var c=this,P=0;P<A.length;P++){c.$5b++;var v=A[P];if(!c.$16b||c.$16b.call(c,v)){var d=U("li"),B=c.$5b+"";if(D(d,"white-space","nowrap"),D(d,j,"block"),"separator"===v||v.separator===!0){var S=m();S[t]="separator",D(S,O,J.contextMenuSeparatorWidth+n),D(S,"background",J.contextMenuSeparatorColor),y(d,S)}else{v._id=B,d.setAttribute("data-id",B);var b=U("span"),L=U("span"),k=C(),r=m();if(D(b,j,"inline-block"),D(L,j,"inline-block"),k[t]="prefix",D(k,j,"inline-block"),D(k,I,"1em"),D(k,O,"1em"),D(k,"vertical-align","middle"),D(k,H,"0 0.2em"),"check"===v.type&&v.selected?k[t]+=" check-prefix":"radio"===v.type&&v.selected&&(k[t]+=" radio-prefix"),y(d,k),v.icon){var i=C();i[t]="contextmenu-item-icon",D(i,j,"inline-block"),D(i,"vertical-align","middle"),D(i,O,"1.2em"),D(i,I,"1.2em"),D(i,"margin-right","0.2em"),i.$50b=v.icon,y(b,i)}if(L[R]=v.label,y(b,L),y(d,b),r[t]="suffix",D(r,j,"inline-block"),D(r,"margin-left","1em"),D(r,"margin-right","0.4em"),D(r,"text-align","right"),D(r,"font-size","75%"),v.items&&(r[R]=J.contextMenuSubmenuMark),v.suffix&&(r[R]=v.suffix),y(d,r),d[t]="menu-item menu-item"+B,D(d,"background-color",c.contextMenuBackground||J.contextMenuBackground),D(d,"color",c.contextMenuLabelColor||J.contextMenuLabelColor),D(d,e,"3px 0"),v.height&&D(d,"height",v.height+"px"),v.height&&D(d,"line-height",v.height+"px"),v.disabled instanceof Function){var W=v.disabled.call(c,v);W&&(d[t]+=" disabled",D(d,"color",J.contextMenuDisabledLabelColor))}else v.disabled&&(d[t]+=" disabled",D(d,"color",J.contextMenuDisabledLabelColor));if(v.items){c.$21b||(c.$21b=new s.List);var Z=U("ul",c.contextMenuBorderColor);Z[t]="submenu"+B,Z.$45b=B,D(Z,w,"hidden"),D(Z,h,Y),y(c._view,Z),c.$21b.add(Z),c.$2b(v.items,Z)}}y(z,d)}}},rebuild:function(){var D=this,m=D._items,c=D._view;if(c&&(c[R]="",D.$21b=A,D.$5b=0,D.$19b=A,D.$20b=A,c.$26b=A,m&&0!==m.length)){var H=U("ul",D.contextMenuBorderColor);y(c,H),D.$2b(m,H)}},addTo:function(n){if(n){var u=this,y=u.$13b;if(u.$12b=n,u.$9b=function(j){y.$44b(j)},u.$27b=function(w){y.$41b(w)},l){var q=u.$6b=function(N){y.$38b(N)},C=u.$7b=function(K){y.$39b(K)},i=u.$8b=function(n){y.$40b(n)};L(n,"touchstart",q,!0),L(n,"touchmove",C),L(n,"touchend",i)}else L(n,"contextmenu",u.$27b)}},show:function(X,w,g,wf){var U=this,g=g==A?!0:!1,P=U._view;if(P){if(U.invalidate(),1===arguments.length){var H=X;if(l){var u=M(H);X=u.pageX,w=u.pageY}else X=H.pageX,w=H.pageY}var O=J.getWindowInfo(),s=wf&&wf.width||O.width,T=wf&&wf.height||O.height,p=O.left,C=O.top,Q={pageX:X,pageY:w,clientX:X-p,clientY:w-C,target:1,originEvent:H},G=Q.clientX,F=Q.clientY,d=function(o){o.style.height=T-6+n;var r=m(),q=m(),C=function(H){D(H,h,Y),D(H,"text-align","center"),D(H,I,"100%"),D(H,"font-size",10+n),D(H,"padding","2px 0"),D(H,"border","0px solid "+J.contextMenuScrollerBorderColor),D(H,"background-color",J.contextMenuScrollerColor1),H.style.backgroundImage="-webkit-linear-gradient(top, "+J.contextMenuScrollerColor1+", "+J.contextMenuScrollerColor2+")",H.style.backgroundImage="linear-gradient(to bottom, "+J.contextMenuScrollerColor1+", "+J.contextMenuScrollerColor2+")"};r[t]="menu-arrow-item menu-arrow-item-top",q[t]="menu-arrow-item menu-arrow-item-bottom",C(r),D(r,"top",z),D(r,"left",z),D(r,"border-bottom-width",1+n),r[R]="▲",C(q),D(q,"bottom",z),D(q,"left",z),D(q,"border-top-width",1+n),q[R]="▼",o.$24b=r,o.$25b=q,o.$18b=o.scrollTop,o.$22b=o.scrollHeight,o.$23b=o.clientHeight,y(o,r),y(o,q),U.$42b(o)};U.beforeShow&&U.beforeShow(Q);var f=U._items;if(f&&(H&&H.preventDefault(),f.length)){U.rebuild(),y(S(),P),P.$26b=i(P,".menu-item");var K=P.children[0];K.offsetHeight>T&&d(K);var $=F+(g?1:0),E=G+(g?1:0),B=function(o){for(var y=0,A=0,l=0,d=U.$43b;l<o.children.length;l++){var Z=o.children[l];if(Z.getAttribute("data-id")){var i=Z.children[1],v=Z.children[2],V=i.children;if(d){var Y=V[0];V.length>1&&(Y=V[1]),Y.offsetWidth>d&&(Y[R]="<marquee scrollamount='3'>"+Y[R]+"</marquee>",Y.children[0].style.verticalAlign="text-bottom",D(Y,I,d+n),D(Y,j,"inline-block"))}var b=i.offsetWidth,x=v.offsetWidth;b>y&&(y=b),x>A&&(A=x)}}for(l=0;l<o.children.length;l++){var Z=o.children[l];if(Z.getAttribute("data-id")){var i=Z.children[1],v=Z.children[2],p=i.children[0],a=i.children[1];!a&&p.style.width&&D(p,I,y+n),D(i,I,y+n),D(v,I,A+n)}}};B(K);var r=F+3+P.offsetHeight,k=G+3+P.offsetWidth;r>T&&!wf?D(P,N,$-(r-T)+C+n):D(P,N,$+C+n),k>s?D(P,c,E-(k-s)+p+n):D(P,c,E+p+n);var b=U.$21b;b&&b.each(function(N){B(N),N.offsetHeight>T&&d(N)}),U.$9b&&L(Z(),l?"touchstart":"mousedown",U.$9b,!0),U.afterShow&&U.afterShow(Q),U.$47b()}}},isShowing:function(){return this._view?this._view.parentNode!=A:!1},getRelatedView:function(){return this.$12b},hide:function(){var J=this,F=J._view;F&&F.parentNode&&(f(F.parentNode,F),r(Z(),l?"touchstart":"mousedown",J.$9b,!0),J.afterHide&&J.afterHide())},dispose:function(){var j=this,Z=j.$12b,v=j._view;v&&(v.parentNode&&f(v.parentNode,v),j.disableGlobalKey(),Z&&(l?(r(Z,"touchstart",j.$6b,!0),r(Z,"touchmove",j.$7b),r(Z,"touchend",j.$8b)):r(Z,"contextmenu",j.$27b)),j._view=j._items=j.$21b=j.$19b=j.$12b=j.beforeShow=j.afterShow=j.afterHide=j.$9b=j.$3b=j.$6b=j.$7b=j.$8b=j.$27b=A)},$46b:function(p,L,$,O){var N=K.initContext(p);K.translateAndScale(N,0,0,1),N.clearRect(0,0,$,O),J.drawStretchImage(N,J.getImage(L),"fill",0,0,$,O),N.restore()},$47b:function(){var I,q,F,X=this,T=X._view;if(X.isShowing()){var v=i(T,".check-prefix");for(F=0;F<v.length;F++){var y=v[F];I=y.clientWidth,q=y.clientHeight,y.$48b=I,y.$49b=q,K.setCanvas(y,I,q)}var A=i(T,".radio-prefix");for(F=0;F<A.length;F++){var b=A[F];I=b.clientWidth,q=b.clientHeight,b.$48b=I,b.$49b=q,K.setCanvas(b,I,q)}var D=i(T,".contextmenu-item-icon");for(F=0;F<D.length;F++){var p=D[F];I=p.clientWidth,q=p.clientHeight,p.$48b=I,p.$49b=q,K.setCanvas(p,I,q)}}},validateImpl:function(){var g,N,W,k=this,U=k._view;if(k.isShowing()){var b=i(U,".check-prefix");for(W=0;W<b.length;W++){var B=b[W];g=B.$48b,N=B.$49b,g&&N&&k.$46b(B,J.contextMenuCheckIcon,g,N)}var F=i(U,".radio-prefix");for(W=0;W<F.length;W++){var l=F[W];g=l.$48b,N=l.$49b,g&&N&&k.$46b(l,J.contextMenuRadioIcon,g,N)}var E=i(U,".contextmenu-item-icon");for(W=0;W<E.length;W++){var s=E[W];g=s.$48b,N=s.$49b,g&&N&&k.$46b(s,J.getImage(s.$50b),g,N)}}}})}("undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:this,Object);