LocalCDN-Firefox-Chrome-Brave/resources/angular-bootstrap-colorpicker/3.0.32/js/bootstrap-colorpicker-modul...

1 line
7.8 KiB
JavaScript

angular.module("colorpicker.module",[]).factory("Helper",function(){"use strict";return{closestSlider:function(e){var o=e.matches||e.webkitMatchesSelector||e.mozMatchesSelector||e.msMatchesSelector;return o.bind(e)("I")?e.parentNode:e},getOffset:function(e,o){for(var t=0,r=0,n=e.getBoundingClientRect();e&&!isNaN(e.offsetLeft)&&!isNaN(e.offsetTop);)o||"BODY"!==e.tagName?(t+=e.scrollLeft,r+=e.scrollTop):(t+=document.documentElement.scrollLeft||e.scrollLeft,r+=document.documentElement.scrollTop||e.scrollTop),e=e.offsetParent;return{top:n.top+window.pageYOffset,left:n.left+window.pageXOffset,scrollX:t,scrollY:r}},stringParsers:[{re:/rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,parse:function(e){return[e[1],e[2],e[3],e[4]]}},{re:/rgba?\(\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*,\s*(\d+(?:\.\d+)?)\%\s*(?:,\s*(\d+(?:\.\d+)?)\s*)?\)/,parse:function(e){return[2.55*e[1],2.55*e[2],2.55*e[3],e[4]]}},{re:/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/,parse:function(e){return[parseInt(e[1],16),parseInt(e[2],16),parseInt(e[3],16)]}},{re:/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/,parse:function(e){return[parseInt(e[1]+e[1],16),parseInt(e[2]+e[2],16),parseInt(e[3]+e[3],16)]}}]}}).factory("Color",["Helper",function(e){"use strict";return{value:{h:1,s:1,b:1,a:1},rgb:function(){var e=this.toRGB();return"rgb("+e.r+","+e.g+","+e.b+")"},rgba:function(){var e=this.toRGB();return"rgba("+e.r+","+e.g+","+e.b+","+e.a+")"},hex:function(){return this.toHex()},RGBtoHSB:function(e,o,t,r){e/=255,o/=255,t/=255;var n,i,l,s;return l=Math.max(e,o,t),s=l-Math.min(e,o,t),n=0===s?null:l===e?(o-t)/s:l===o?(t-e)/s+2:(e-o)/s+4,n=(n+360)%6*60/360,i=0===s?0:s/l,{h:n||1,s:i,b:l,a:r||1}},setColor:function(o){o=o?o.toLowerCase():o;for(var t in e.stringParsers)if(e.stringParsers.hasOwnProperty(t)){var r=e.stringParsers[t],n=r.re.exec(o),i=n&&r.parse(n);if(i)return this.value=this.RGBtoHSB.apply(null,i),!1}},setHue:function(e){this.value.h=1-e},setSaturation:function(e){this.value.s=e},setLightness:function(e){this.value.b=1-e},setAlpha:function(e){this.value.a=parseInt(100*(1-e),10)/100},toRGB:function(e,o,t,r){e||(e=this.value.h,o=this.value.s,t=this.value.b),e*=360;var n,i,l,s,c;return e=e%360/60,c=t*o,s=c*(1-Math.abs(e%2-1)),n=i=l=t-c,e=~~e,n+=[c,s,0,0,s,c][e],i+=[s,c,c,s,0,0][e],l+=[0,0,s,c,c,s][e],{r:Math.round(255*n),g:Math.round(255*i),b:Math.round(255*l),a:r||this.value.a}},toHex:function(e,o,t,r){var n=this.toRGB(e,o,t,r);return"#"+(1<<24|parseInt(n.r,10)<<16|parseInt(n.g,10)<<8|parseInt(n.b,10)).toString(16).substr(1)}}}]).factory("Slider",["Helper",function(e){"use strict";var o={maxLeft:0,maxTop:0,callLeft:null,callTop:null,knob:{top:0,left:0}},t={};return{getSlider:function(){return o},getLeftPosition:function(e){return Math.max(0,Math.min(o.maxLeft,o.left+((e.pageX||t.left)-t.left)))},getTopPosition:function(e){return Math.max(0,Math.min(o.maxTop,o.top+((e.pageY||t.top)-t.top)))},setSlider:function(r,n){var i=e.closestSlider(r.target),l=e.getOffset(i,n),s=i.getBoundingClientRect(),c=r.clientX-s.left,a=r.clientY-s.top;o.knob=i.children[0].style,o.left=r.pageX-l.left-window.pageXOffset+l.scrollX,o.top=r.pageY-l.top-window.pageYOffset+l.scrollY,t={left:r.pageX-(c-o.left),top:r.pageY-(a-o.top)}},setSaturation:function(e,t,r){o={maxLeft:r,maxTop:r,callLeft:"setSaturation",callTop:"setLightness"},this.setSlider(e,t)},setHue:function(e,t,r){o={maxLeft:0,maxTop:r,callLeft:!1,callTop:"setHue"},this.setSlider(e,t)},setAlpha:function(e,t,r){o={maxLeft:0,maxTop:r,callLeft:!1,callTop:"setAlpha"},this.setSlider(e,t)},setKnob:function(e,t){o.knob.top=e+"px",o.knob.left=t+"px"}}}]).directive("colorpicker",["$document","$compile","Color","Slider","Helper",function(e,o,t,r,n){"use strict";return{require:"?ngModel",restrict:"A",link:function(i,l,s,c){function a(){e.on("mousemove",u),e.on("mouseup",f)}function p(){try{Y.css("backgroundColor",M[x]())}catch(e){Y.css("backgroundColor",M.toHex())}D.css("backgroundColor",M.toHex(M.value.h,1,1,1)),"rgba"===x&&(b.css.backgroundColor=M.toHex())}function u(e){var o=r.getLeftPosition(e),t=r.getTopPosition(e),n=r.getSlider();r.setKnob(t,o),n.callLeft&&M[n.callLeft].call(M,o/L),n.callTop&&M[n.callTop].call(M,t/L),p();var s=M[x]();return l.val(s),c&&i.$apply(c.$setViewValue(s)),C&&V.val(s),!1}function f(){m("colorpicker-selected"),e.off("mousemove",u),e.off("mouseup",f)}function d(e){M.value=A,M.setColor(l.val()),C&&!e&&V.val(l.val()),F.eq(0).css({left:M.value.s*L+"px",top:L-M.value.b*L+"px"}),F.eq(1).css("top",L*(1-M.value.h)+"px"),F.eq(2).css("top",L*(1-M.value.a)+"px"),A=M.value,p()}function h(){var e,o=n.getOffset(l[0]),t=2;return angular.isDefined(s.colorpickerParent)&&(o.left=0,o.top=0),"top"===w?e={top:o.top-X-t,left:o.left}:"right"===w?e={top:o.top,left:o.left+l[0].offsetWidth+t}:"bottom"===w?e={top:o.top+l[0].offsetHeight+t,left:o.left}:"left"===w&&(e={top:o.top,left:o.left-R-t}),{top:e.top+"px",left:e.left+"px"}}function g(){v()}function k(){y.hasClass("colorpicker-visible")||(d(),y.addClass("colorpicker-visible").css(h()),m("colorpicker-shown"),$===!1&&e.on("mousedown",g),s.colorpickerIsOpen&&(i[s.colorpickerIsOpen]=!0,i.$$phase&&i.$root.$$phase||i.$digest()))}function m(e){c&&i.$emit(e,{name:s.ngModel,value:c.$modelValue})}function v(){y.hasClass("colorpicker-visible")&&(y.removeClass("colorpicker-visible"),m("colorpicker-closed"),e.off("mousedown",g),s.colorpickerIsOpen&&(i[s.colorpickerIsOpen]=!1,i.$$phase&&i.$root.$$phase||i.$digest()))}var b,x=s.colorpicker?s.colorpicker:"hex",w=angular.isDefined(s.colorpickerPosition)?s.colorpickerPosition:"bottom",$=!!angular.isDefined(s.colorpickerInline)&&s.colorpickerInline,S=!!angular.isDefined(s.colorpickerFixedPosition)&&s.colorpickerFixedPosition,I=angular.isDefined(s.colorpickerParent)?l.parent():angular.element(document.body),C=!!angular.isDefined(s.colorpickerWithInput)&&s.colorpickerWithInput,L=angular.isDefined(s.colorpickerSize)?s.colorpickerSize:100,P=L+"px",H=C?'<input type="text" name="colorpicker-input" spellcheck="false">':"",T=$?"":'<button type="button" class="close close-colorpicker">&times;</button>',O='<div class="colorpicker dropdown"><div class="dropdown-menu"><colorpicker-saturation><i></i></colorpicker-saturation><colorpicker-hue><i></i></colorpicker-hue><colorpicker-alpha><i></i></colorpicker-alpha><colorpicker-preview></colorpicker-preview>'+H+T+"</div></div>",y=angular.element(O),M=t,A={h:1,s:0,b:1,a:1},B=y.find("colorpicker-hue"),D=y.find("colorpicker-saturation"),Y=y.find("colorpicker-preview"),F=y.find("i"),R=parseInt(L)+29+("rgba"===x?15:0),X=parseInt(L)+55;if(o(y)(i),y.css("min-width",R+"px"),D.css({width:P,height:P}),B.css("height",P),C){var V=y.find("input");V.css("width",P),V.on("mousedown",function(e){e.stopPropagation()}).on("keyup",function(){var e=this.value;l.val(e),c&&c.$modelValue!==e&&(i.$apply(c.$setViewValue(e)),d(!0))})}"rgba"===x&&(y.addClass("alpha"),b=y.find("colorpicker-alpha"),b.css("height",P),b.on("click",function(e){r.setAlpha(e,S,L),u(e)}).on("mousedown",function(e){r.setAlpha(e,S,L),a()}).on("mouseup",function(e){m("colorpicker-selected-alpha")})),B.on("click",function(e){r.setHue(e,S,L),u(e)}).on("mousedown",function(e){r.setHue(e,S,L),a()}).on("mouseup",function(e){m("colorpicker-selected-hue")}),D.on("click",function(e){r.setSaturation(e,S,L),u(e),angular.isDefined(s.colorpickerCloseOnSelect)&&v()}).on("mousedown",function(e){r.setSaturation(e,S,L),a()}).on("mouseup",function(e){m("colorpicker-selected-saturation")}),S&&y.addClass("colorpicker-fixed-position"),y.addClass("colorpicker-position-"+w),"true"===$&&y.addClass("colorpicker-inline"),I.append(y),c&&(c.$render=function(){l.val(c.$viewValue),d()}),l.on("blur keyup change",function(){d()}),l.on("$destroy",function(){y.remove()}),$===!1?l.on("click",k):k(),y.on("mousedown",function(e){e.stopPropagation(),e.preventDefault()}),y.find("button").on("click",function(){v()}),s.colorpickerIsOpen&&i.$watch(s.colorpickerIsOpen,function(e){e===!0?k():e===!1&&v()})}}}]);