How to use this.nativeClick method in Appium Xcuitest Driver

Best JavaScript code snippet using appium-xcuitest-driver

wc-color-picker.js

Source:wc-color-picker.js Github

copy

Full Screen

1import './wc-button.js';2import './wc-popover.js';3import message from './wc-message.js';4import { rgbToHsv,hslToHsv,parseToHSVA } from '../utils/color.js';5import { HSVaColor } from '../utils/hsvacolor.js';6const Material_colors = ['#f44336','#E91E63','#9C27B0','#673AB7','#3F51B5','#2196F3','#03A9F4','#00BCD4','#009688','#4CAF50','#8BC34A','#CDDC39','#FFEB3B','#FFC107','#FF9800','#FF5722','#795548','#9E9E9E','#607D8B','rgba(0,0,0,.65)','transparent']7class WcColorPane extends HTMLElement {8 constructor() {9 super();10 const shadowRoot = this.attachShadow({ mode: 'open' });11 shadowRoot.innerHTML = `12 <style>13 :host{14 display: block;15 min-width: 300px;16 }17 .color-pane{18 padding:.8em;19 }20 .color-palette{21 position:relative;22 height:150px;23 background:linear-gradient(to top, hsla(0,0%,0%,calc(var(--a))), transparent), linear-gradient(to left, hsla(calc(var(--h)),100%,50%,calc(var(--a))),hsla(0,0%,100%,calc(var(--a)))),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );24 background-position:0 0, 0 0,0 0,5px 5px;25 background-size:100% 100%, 100% 100%, 10px 10px, 10px 10px;26 user-select: none;27 cursor: crosshair;28 opacity:1;29 transition:opacity .1s;30 }31 .color-palette:active{32 opacity:.99;33 }34 .color-palette::after{35 pointer-events:none;36 position:absolute;37 content:'';38 box-sizing:border-box;39 width:10px;40 height:10px;41 border-radius:50%;42 border:2px solid #fff;43 left:calc(var(--s) * 1%);44 top:calc((100 - var(--v)) * 1%);45 transform:translate(-50%,-50%);46 }47 .color-chooser{48 display:flex;49 padding:10px 0;50 }51 .color-show{52 display:flex;53 position: relative;54 width:32px;55 height:32px;56 background:var(--c);57 transition:none;58 border-radius:50%;59 overflow:hidden;60 cursor:pointer;61 }62 .color-show .icon-file{63 width:1em;64 height:1em;65 margin: auto;66 fill: hsl(0, 0%, calc( ((2 - var(--s) / 100) * var(--v) / 200 * var(--a) - 0.6 ) * -999999% ));67 opacity: 0;68 transition: .3s;69 }70 .color-show:hover .icon-file{71 opacity:1;72 }73 .color-show input{74 position:absolute;75 clip:rect(0,0,0,0);76 }77 .color-show::after{78 content:'';79 position:absolute;80 width:32px;81 height:32px;82 background:linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );83 background-position:0 0,5px 5px;84 background-size:10px 10px;85 z-index:-1;86 }87 .color-range{88 flex:1;89 margin-left:10px;90 }91 input[type="range"]{92 display: block;93 pointer-events:all;94 width:100%;95 -webkit-appearance: none;96 outline : 0;97 height: 10px;98 border-radius:5px;99 margin:0;100 }101 input[type="range"]::-webkit-slider-runnable-track{102 display: flex;103 align-items: center;104 position: relative;105 }106 input[type="range"]::-webkit-slider-thumb{107 -webkit-appearance: none;108 position: relative;109 width:10px;110 height:10px;111 transform:scale(1.2);112 border-radius: 50%;113 box-shadow: 0 0 10px rgba(0,0,0,0.1);114 background:#fff;115 transition:.2s cubic-bezier(.12, .4, .29, 1.46);116 }117 input[type="range"]::-moz-range-thumb{118 box-sizing:border-box;119 pointer-events:none;120 position: relative;121 width:10px;122 height:10px;123 transform:scale(1.2);124 border-radius: 50%;125 border:0;126 box-shadow: 0 0 10px rgba(0,0,0,0.1);127 background:#fff;128 transition:.2s cubic-bezier(.12, .4, .29, 1.46);129 }130 input[type="range"]::-webkit-slider-thumb:active,131 input[type="range"]:focus::-webkit-slider-thumb{132 transform:scale(1.5);133 }134 input[type="range"]::-moz-range-thumb:active,135 input[type="range"]:focus::-moz-range-thumb{136 transform:scale(1.5);137 }138 input[type="range"]+input[type="range"]{139 margin-top:10px;140 }141 .color-hue{142 background:linear-gradient(to right, red, yellow, lime, cyan, blue, magenta, red)143 }144 .color-opacity{145 background:linear-gradient(to right, hsla(calc(var(--h)),100%,50%,0), hsla(calc(var(--h)),100%,50%,1)),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );146 background-position:0 0,0 0,5px 5px;147 background-size:100% 100%,10px 10px,10px 10px;148 }149 .color-label{150 position:absolute;151 display:flex;152 visibility:hidden;153 opacity:0;154 left:0;155 right:0;156 top:0;157 bottom:0;158 transition: .3s;159 }160 .color-label input{161 flex:1;162 margin-right:.8em;163 outline:0;164 min-width:0;165 width: 0;166 border-radius:var(--borderRadius,.25em);167 border:1px solid #ddd;168 padding:0 5px;169 line-height:28px;170 text-align:center;171 -moz-appearance: textfield;172 transition:.3s;173 }174 input[type="number"]::-webkit-inner-spin-button{175 display:none;176 }177 ::-moz-focus-inner,::-moz-focus-outer{178 border:0;179 outline : 0;180 }181 .color-label input:focus{182 border-color:var(--themeColor,#42b983);183 }184 .color-footer{185 display:flex186 }187 .btn-switch{188 position:relative;189 border-radius:var(--borderRadius,.25em);190 background:none;191 border:0;192 outline:0;193 line-height:30px;194 width: 60px;195 padding: 0;196 color:var(--themeColor,#42b983);197 overflow:hidden;198 }199 .btn-switch::before{200 content:'';201 position:absolute;202 left:0;203 top:0;204 right:0;205 bottom:0;206 background:var(--themeBackground,var(--themeColor,#42b983));207 opacity:.2;208 transition:.3s;209 }210 .btn-switch:hover::before,.btn-switch:focus::before{211 opacity:.3;212 }213 .color-input{214 position:relative;215 flex:1;216 height:30px;217 overflow:hidden;218 }219 .color-footer[data-type="HEXA"] .color-label:nth-child(1),220 .color-footer[data-type="RGBA"] .color-label:nth-child(2),221 .color-footer[data-type="HSLA"] .color-label:nth-child(3){222 opacity:1;223 visibility:inherit;224 z-index:2;225 }226 .color-sign{227 padding-top:10px;228 display:grid;229 grid-template-columns: repeat(auto-fit, minmax(15px, 1fr));230 grid-gap: 10px;231 }232 .color-sign>button{233 position:relative;234 cursor:pointer;235 width:100%;236 padding-bottom:0;237 padding-top:100%;238 border-radius:4px;239 border:0;240 outline:0;241 }242 .color-sign>button::before{243 content:'';244 position:absolute;245 left:0;246 top:0;247 width:100%;248 height:100%;249 z-index:-1;250 background:linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );251 background-position:0 0,5px 5px;252 background-size:10px 10px;253 border-radius: 4px;254 }255 .color-sign>button::after{256 content:'';257 position:absolute;258 opacity:.5;259 z-index:-2;260 left:0;261 top:0;262 width:100%;263 height:100%;264 background:inherit;265 border-radius:4px;266 transition:.3s;267 }268 .color-sign>button:hover::after,.color-sign>button:focus::after{269 transform:translate(2px,2px)270 }271 </style>272 <div class="color-pane" id="color-pane">273 <div class="color-palette" id="color-palette"></div>274 <div class="color-chooser">275 <a class="color-show" id="copy-btn"><svg class="icon-file" viewBox="0 0 1024 1024"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32z"></path><path d="M704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg><input></a>276 <div class="color-range">277 <input class="color-hue" value="0" min="0" max="360" type="range" id="range-hue">278 <input class="color-opacity" value="1" min="0" max="1" step="0.01" type="range" id="range-opacity">279 </div>280 </div>281 <div class="color-footer" data-type="HEXA">282 <div class="color-input">283 <div class="color-label" id="color-hexa">284 <input spellcheck="false" />285 </div>286 <div class="color-label" id="color-rgba">287 <input type="number" min="0" max="255" spellcheck="false" />288 <input type="number" min="0" max="255" spellcheck="false" />289 <input type="number" min="0" max="255" spellcheck="false" />290 <input type="number" min="0" max="1" step="0.01" spellcheck="false" />291 </div>292 <div class="color-label" id="color-hlsa">293 <input type="number" min="0" max="360" spellcheck="false" />294 <input type="number" min="0" max="100" spellcheck="false" />295 <input type="number" min="0" max="100" spellcheck="false" />296 <input type="number" min="0" max="1" step="0.01" spellcheck="false" />297 </div>298 </div>299 <button class="btn-switch" id="btn-switch" type="flat">HEXA</button>300 </div>301 <div class="color-sign" id="colors">302 ${303 Material_colors.map(el=>'<button style="background-color:'+el+'" data-color='+el+'></button>').join('')304 }305 </div>306 </div>307 `308 }309 choose(ev){310 const {x,y,width:w,height:h} = this.palette.getBoundingClientRect();311 const value = [...this.$value];312 const _x = Math.min(Math.max(0,(ev.clientX-x)/w*100),100);313 const _y = Math.min(Math.max(0,(ev.clientY-y)/h*100),100);314 value[1] = _x;315 value[2] = 100-_y;316 this.value = `hsva(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;317 }318 connectedCallback() {319 this.type = ['HEXA','RGBA','HSLA'];320 this.typeindex = 0;321 this.palette = this.shadowRoot.getElementById('color-palette');322 this.colors = this.shadowRoot.getElementById('colors');323 this.pane = this.shadowRoot.getElementById('color-pane');324 this.rangeHue = this.shadowRoot.getElementById('range-hue');325 this.rangeOpacity = this.shadowRoot.getElementById('range-opacity');326 this.copyBtn = this.shadowRoot.getElementById('copy-btn');327 this.copyinfo = this.copyBtn.querySelector('input');328 this.switch = this.shadowRoot.getElementById('btn-switch');329 this.colorHexa = this.shadowRoot.getElementById('color-hexa').querySelectorAll('input');330 this.colorRgba = this.shadowRoot.getElementById('color-rgba').querySelectorAll('input');331 this.colorHlsa = this.shadowRoot.getElementById('color-hlsa').querySelectorAll('input');332 this.value = this.defaultvalue;333 this.rangeHue.addEventListener('input',()=>{334 const value = [...this.$value];335 value[0] = Number(this.rangeHue.value);336 this.nativeclick = true;337 this.value = `hsva(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;338 })339 this.palette.addEventListener('mousedown',(ev)=>{340 this.start = true;341 this.choose(ev);342 })343 document.addEventListener('mousemove',this.mousemove);344 document.addEventListener('mouseup',this.mouseup);345 this.rangeOpacity.addEventListener('input',()=>{346 const value = [...this.$value];347 value[3] = Number(this.rangeOpacity.value);348 this.nativeclick = true;349 this.value = `hsva(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;350 })351 this.colors.addEventListener('click',(ev)=>{352 const item = ev.target.closest('button');353 if(item){354 this.nativeclick = true;355 this.value = item.dataset.color;356 }357 })358 this.switch.addEventListener('click',()=>{359 this.typeindex ++;360 this.typeindex %= 3;361 this.switch.textContent = this.type[this.typeindex];362 this.nativeclick = true;363 this.value = this.value;364 this.switch.parentNode.dataset.type = this.type[this.typeindex];365 })366 this.copyBtn.addEventListener('click',()=>{367 this.copyinfo.select();368 if (document.execCommand('copy')) {369 document.execCommand('copy');370 message.success(this.value);371 }372 })373 this.colorHexa.forEach(el=>{374 el.addEventListener('change',()=>{375 this.nativeclick = true;376 this.value = el.value;377 })378 })379 this.colorRgba.forEach((el,i)=>{380 el.addEventListener('change',()=>{381 const value = HSVaColor(...this.$value).toRGBA();382 value[i] = Number(el.value);383 this.nativeclick = true;384 this.value = `rgba(${value[0]}, ${value[1]}, ${value[2]}, ${value[3]})`;385 })386 })387 this.colorHlsa.forEach((el,i)=>{388 el.addEventListener('change',()=>{389 const value = HSVaColor(...this.$value).toHSLA();390 value[i] = Number(el.value);391 this.nativeclick = true;392 this.value = `hsla(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;393 })394 })395 }396 mousemove = (ev) => {397 if(this.start){398 this.choose(ev);399 }400 }401 mouseup = () => {402 if(getComputedStyle(this.palette).opacity!==1 && this.start){403 this.dispatchEvent(new CustomEvent('change', {404 detail: {405 value: this.value,406 color: this.color407 }408 }));409 }410 this.start = false;411 }412 disconnectedCallback() {413 document.removeEventListener('mousemove', this.mousemove);414 document.removeEventListener('mouseup', this.mouseup);415 }416 get value() {417 return HSVaColor(...this.$value)['to'+this.type[this.typeindex]]().toString();418 }419 get color() {420 return HSVaColor(...this.$value);421 }422 get defaultvalue() {423 return this.getAttribute('defaultvalue')||'#ff0000';424 }425 set defaultvalue(value) {426 this.setAttribute("defaultvalue",value);427 }428 set value(value) {429 this.$value = parseToHSVA(value).values;430 //[h,s,v,a]431 const [h,s,v,a=1] = this.$value;432 this.pane.style.setProperty('--h',h);433 this.pane.style.setProperty('--s',s);434 this.pane.style.setProperty('--v',v);435 this.pane.style.setProperty('--a',a);436 this.pane.style.setProperty('--c',this.value);437 this.copyinfo.value = this.value;438 this.rangeHue.value = h;439 this.rangeOpacity.value = a.toFixed(2);440 const COLOR = HSVaColor(...this.$value);441 this.colorHexa[0].value = COLOR.toHEXA().toString();442 const RGBA = COLOR.toRGBA();443 this.colorRgba[0].value = RGBA[0].toFixed(0);444 this.colorRgba[1].value = RGBA[1].toFixed(0);445 this.colorRgba[2].value = RGBA[2].toFixed(0);446 this.colorRgba[3].value = RGBA[3].toFixed(2);447 const HSLA = COLOR.toHSLA();448 this.colorHlsa[0].value = HSLA[0].toFixed(0);449 this.colorHlsa[1].value = HSLA[1].toFixed(0);450 this.colorHlsa[2].value = HSLA[2].toFixed(0);451 this.colorHlsa[3].value = HSLA[3].toFixed(2);452 if(this.nativeclick){453 this.nativeclick = false;454 this.dispatchEvent(new CustomEvent('change', {455 detail: {456 value: this.value,457 color: this.color458 }459 }));460 }461 }462}463if(!customElements.get('wc-color-pane')){464 customElements.define('wc-color-pane', WcColorPane);465}466export default class WcColorPicker extends HTMLElement {467 static get observedAttributes() { return ['disabled','dir'] }468 constructor() {469 super();470 const shadowRoot = this.attachShadow({ mode: 'open' });471 shadowRoot.innerHTML = `472 <style>473 :host{474 display:inline-block;475 width:30px;476 height:30px;477 font-size:14px;478 }479 :host([block]){480 display:block;481 }482 :host([disabled]){483 pointer-events:none;484 }485 486 :host(:focus-within) wc-popover,:host(:hover) wc-popover{ 487 z-index: 2;488 }489 wc-popover{490 width:100%;491 height:100%;492 }493 .color-btn{494 width:100%;495 height:100%;496 padding:5px;497 background-clip: content-box;498 background-color:var(--themeColor,#42b983);499 }500 .color-btn:hover{501 z-index: auto;502 }503 wc-popover{504 display:block;505 }506 wc-popcon{507 min-width:100%;508 }509 .pop-footer{510 display:flex;511 justify-content:flex-end;512 padding:0 .8em .8em;513 }514 .pop-footer wc-button{515 font-size: .8em;516 margin-left: .8em;517 }518 .color-btn::before{519 content:'';520 position:absolute;521 left:5px;522 top:5px;523 right:5px;524 bottom:5px;525 z-index:-1;526 background:linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );527 background-position:0 0,5px 5px;528 background-size:10px 10px;529 }530 </style>531 <wc-popover id="popover" ${this.dir? "dir='"+this.dir+"'" : ""}>532 <wc-button class="color-btn" id="color-btn" ${this.disabled? "disabled" : ""}></wc-button>533 <wc-popcon id="popcon">534 <div class="pop-footer">535 <wc-button autoclose>取 消</wc-button>536 <wc-button type="primary" id="btn-submit" autoclose>确 认</wc-button>537 </div>538 </wc-popcon>539 </wc-popover>540 `541 }542 focus() {543 this.colorBtn.focus();544 }545 connectedCallback() {546 this.popover = this.shadowRoot.getElementById('popover');547 this.popcon = this.shadowRoot.getElementById('popcon');548 this.colorBtn = this.shadowRoot.getElementById('color-btn');549 this.btnSubmit = this.shadowRoot.getElementById('btn-submit');550 this.colorBtn.addEventListener('click',()=>{551 if(!this.colorPane){552 this.colorPane = new WcColorPane();553 this.colorPane.defaultvalue = this.defaultvalue;554 this.popcon.prepend(this.colorPane);555 }556 })557 this.btnSubmit.addEventListener('click',()=>{558 this.nativeclick = true;559 this.value = this.colorPane.value;560 })561 this.popcon.addEventListener('close',()=>{562 this.colorPane.value = this.value;563 })564 this.value = this.defaultvalue;565 }566 567 get defaultvalue() {568 return this.getAttribute('defaultvalue')||'#42b983';569 }570 get value() {571 return this.$value;572 }573 get color() {574 return HSVaColor(...parseToHSVA(this.$value).values);575 }576 get type() {577 return this.getAttribute('type');578 }579 get disabled() {580 return this.getAttribute('disabled')!==null;581 }582 get dir() {583 return this.getAttribute('dir');584 }585 set dir(value){586 this.setAttribute('dir', value);587 }588 set disabled(value) {589 if (value === null || value === false) {590 this.removeAttribute('disabled');591 } else {592 this.setAttribute('disabled', '');593 }594 }595 set defaultvalue(value){596 this.setAttribute('defaultvalue', value);597 }598 set value(value) {599 this.colorBtn.style.setProperty('--themeColor',value);600 this.$value = value;601 if(this.nativeclick){602 this.nativeclick = false;603 this.dispatchEvent(new CustomEvent('change', {604 detail: {605 value: this.value,606 color: this.color607 }608 }));609 }else{610 if(this.colorPane){611 this.colorPane.value = this.value;612 }else{613 this.defaultvalue = this.value;614 }615 }616 }617 attributeChangedCallback(name, oldValue, newValue) {618 if (name == 'disabled' && this.colorBtn) {619 if (newValue != null) {620 this.colorBtn.setAttribute('disabled', 'disabled');621 } else {622 this.colorBtn.removeAttribute('disabled');623 }624 }625 if (name == 'dir' && this.popover) {626 if (newValue != null) {627 this.popover.dir = newValue;628 }629 }630 }631}632if (!customElements.get('wc-color-picker')) {633 customElements.define('wc-color-picker', WcColorPicker);...

Full Screen

Full Screen

xy-color-picker.js

Source:xy-color-picker.js Github

copy

Full Screen

1import './xy-button.js';2import './xy-popover.js';3import message from './xy-message.js';4import { rgbToHsv,hslToHsv,parseToHSVA } from '../utils/color.js';5import { HSVaColor } from '../utils/hsvacolor.js';6const Material_colors = ['#f44336','#E91E63','#9C27B0','#673AB7','#3F51B5','#2196F3','#03A9F4','#00BCD4','#009688','#4CAF50','#8BC34A','#CDDC39','#FFEB3B','#FFC107','#FF9800','#FF5722','#795548','#9E9E9E','#607D8B','rgba(0,0,0,.65)','transparent']7class XyColorPane extends HTMLElement {8 constructor() {9 super();10 const shadowRoot = this.attachShadow({ mode: 'open' });11 shadowRoot.innerHTML = `12 <style>13 :host{14 display: block;15 min-width: 300px;16 }17 .color-pane{18 padding:.8em;19 }20 .color-palette{21 position:relative;22 height:150px;23 background:linear-gradient(to top, hsla(0,0%,0%,calc(var(--a))), transparent), linear-gradient(to left, hsla(calc(var(--h)),100%,50%,calc(var(--a))),hsla(0,0%,100%,calc(var(--a)))),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );24 background-position:0 0, 0 0,0 0,5px 5px;25 background-size:100% 100%, 100% 100%, 10px 10px, 10px 10px;26 user-select: none;27 cursor: crosshair;28 opacity:1;29 transition:opacity .1s;30 }31 .color-palette:active{32 opacity:.99;33 }34 .color-palette::after{35 pointer-events:none;36 position:absolute;37 content:'';38 box-sizing:border-box;39 width:10px;40 height:10px;41 border-radius:50%;42 border:2px solid #fff;43 left:calc(var(--s) * 1%);44 top:calc((100 - var(--v)) * 1%);45 transform:translate(-50%,-50%);46 }47 .color-chooser{48 display:flex;49 padding:10px 0;50 }51 .color-show{52 display:flex;53 position: relative;54 width:32px;55 height:32px;56 background:var(--c);57 transition:none;58 border-radius:50%;59 overflow:hidden;60 cursor:pointer;61 }62 .color-show .icon-file{63 width:1em;64 height:1em;65 margin: auto;66 fill: hsl(0, 0%, calc( ((2 - var(--s) / 100) * var(--v) / 200 * var(--a) - 0.6 ) * -999999% ));67 opacity: 0;68 transition: .3s;69 }70 .color-show:hover .icon-file{71 opacity:1;72 }73 .color-show input{74 position:absolute;75 clip:rect(0,0,0,0);76 }77 .color-show::after{78 content:'';79 position:absolute;80 width:32px;81 height:32px;82 background:linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );83 background-position:0 0,5px 5px;84 background-size:10px 10px;85 z-index:-1;86 }87 .color-range{88 flex:1;89 margin-left:10px;90 }91 input[type="range"]{92 display: block;93 pointer-events:all;94 width:100%;95 -webkit-appearance: none;96 outline : 0;97 height: 10px;98 border-radius:5px;99 margin:0;100 }101 input[type="range"]::-webkit-slider-runnable-track{102 display: flex;103 align-items: center;104 position: relative;105 }106 input[type="range"]::-webkit-slider-thumb{107 -webkit-appearance: none;108 position: relative;109 width:10px;110 height:10px;111 transform:scale(1.2);112 border-radius: 50%;113 box-shadow: 0 0 10px rgba(0,0,0,0.1);114 background:#fff;115 transition:.2s cubic-bezier(.12, .4, .29, 1.46);116 }117 input[type="range"]::-moz-range-thumb{118 box-sizing:border-box;119 pointer-events:none;120 position: relative;121 width:10px;122 height:10px;123 transform:scale(1.2);124 border-radius: 50%;125 border:0;126 box-shadow: 0 0 10px rgba(0,0,0,0.1);127 background:#fff;128 transition:.2s cubic-bezier(.12, .4, .29, 1.46);129 }130 input[type="range"]::-webkit-slider-thumb:active,131 input[type="range"]:focus::-webkit-slider-thumb{132 transform:scale(1.5);133 }134 input[type="range"]::-moz-range-thumb:active,135 input[type="range"]:focus::-moz-range-thumb{136 transform:scale(1.5);137 }138 input[type="range"]+input[type="range"]{139 margin-top:10px;140 }141 .color-hue{142 background:linear-gradient(to right, red, yellow, lime, cyan, blue, magenta, red)143 }144 .color-opacity{145 background:linear-gradient(to right, hsla(calc(var(--h)),100%,50%,0), hsla(calc(var(--h)),100%,50%,1)),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );146 background-position:0 0,0 0,5px 5px;147 background-size:100% 100%,10px 10px,10px 10px;148 }149 .color-label{150 position:absolute;151 display:flex;152 visibility:hidden;153 opacity:0;154 left:0;155 right:0;156 top:0;157 bottom:0;158 transition: .3s;159 }160 .color-label input{161 flex:1;162 margin-right:.8em;163 outline:0;164 min-width:0;165 width: 0;166 border-radius:var(--borderRadius,.25em);167 border:1px solid #ddd;168 padding:0 5px;169 line-height:28px;170 text-align:center;171 -moz-appearance: textfield;172 transition:.3s;173 }174 input[type="number"]::-webkit-inner-spin-button{175 display:none;176 }177 ::-moz-focus-inner,::-moz-focus-outer{178 border:0;179 outline : 0;180 }181 .color-label input:focus{182 border-color:var(--themeColor,#42b983);183 }184 .color-footer{185 display:flex186 }187 .btn-switch{188 position:relative;189 border-radius:var(--borderRadius,.25em);190 background:none;191 border:0;192 outline:0;193 line-height:30px;194 width: 60px;195 padding: 0;196 color:var(--themeColor,#42b983);197 overflow:hidden;198 }199 .btn-switch::before{200 content:'';201 position:absolute;202 left:0;203 top:0;204 right:0;205 bottom:0;206 background:var(--themeBackground,var(--themeColor,#42b983));207 opacity:.2;208 transition:.3s;209 }210 .btn-switch:hover::before,.btn-switch:focus::before{211 opacity:.3;212 }213 .color-input{214 position:relative;215 flex:1;216 height:30px;217 overflow:hidden;218 }219 .color-footer[data-type="HEXA"] .color-label:nth-child(1),220 .color-footer[data-type="RGBA"] .color-label:nth-child(2),221 .color-footer[data-type="HSLA"] .color-label:nth-child(3){222 opacity:1;223 visibility:inherit;224 z-index:2;225 }226 .color-sign{227 padding-top:10px;228 display:grid;229 grid-template-columns: repeat(auto-fit, minmax(15px, 1fr));230 grid-gap: 10px;231 }232 .color-sign>button{233 position:relative;234 cursor:pointer;235 width:100%;236 padding-bottom:0;237 padding-top:100%;238 border-radius:4px;239 border:0;240 outline:0;241 }242 .color-sign>button::before{243 content:'';244 position:absolute;245 left:0;246 top:0;247 width:100%;248 height:100%;249 z-index:-1;250 background:linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );251 background-position:0 0,5px 5px;252 background-size:10px 10px;253 border-radius: 4px;254 }255 .color-sign>button::after{256 content:'';257 position:absolute;258 opacity:.5;259 z-index:-2;260 left:0;261 top:0;262 width:100%;263 height:100%;264 background:inherit;265 border-radius:4px;266 transition:.3s;267 }268 .color-sign>button:hover::after,.color-sign>button:focus::after{269 transform:translate(2px,2px)270 }271 </style>272 <div class="color-pane" id="color-pane">273 <div class="color-palette" id="color-palette"></div>274 <div class="color-chooser">275 <a class="color-show" id="copy-btn"><svg class="icon-file" viewBox="0 0 1024 1024"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32z"></path><path d="M704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg><input></a>276 <div class="color-range">277 <input class="color-hue" value="0" min="0" max="360" type="range" id="range-hue">278 <input class="color-opacity" value="1" min="0" max="1" step="0.01" type="range" id="range-opacity">279 </div>280 </div>281 <div class="color-footer" data-type="HEXA">282 <div class="color-input">283 <div class="color-label" id="color-hexa">284 <input spellcheck="false" />285 </div>286 <div class="color-label" id="color-rgba">287 <input type="number" min="0" max="255" spellcheck="false" />288 <input type="number" min="0" max="255" spellcheck="false" />289 <input type="number" min="0" max="255" spellcheck="false" />290 <input type="number" min="0" max="1" step="0.01" spellcheck="false" />291 </div>292 <div class="color-label" id="color-hlsa">293 <input type="number" min="0" max="360" spellcheck="false" />294 <input type="number" min="0" max="100" spellcheck="false" />295 <input type="number" min="0" max="100" spellcheck="false" />296 <input type="number" min="0" max="1" step="0.01" spellcheck="false" />297 </div>298 </div>299 <button class="btn-switch" id="btn-switch" type="flat">HEXA</button>300 </div>301 <div class="color-sign" id="colors">302 ${303 Material_colors.map(el=>'<button style="background-color:'+el+'" data-color='+el+'></button>').join('')304 }305 </div>306 </div>307 `308 }309 choose(ev){310 const {x,y,width:w,height:h} = this.palette.getBoundingClientRect();311 const value = [...this.$value];312 const _x = Math.min(Math.max(0,(ev.clientX-x)/w*100),100);313 const _y = Math.min(Math.max(0,(ev.clientY-y)/h*100),100);314 value[1] = _x;315 value[2] = 100-_y;316 this.value = `hsva(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;317 }318 connectedCallback() {319 this.type = ['HEXA','RGBA','HSLA'];320 this.typeindex = 0;321 this.palette = this.shadowRoot.getElementById('color-palette');322 this.colors = this.shadowRoot.getElementById('colors');323 this.pane = this.shadowRoot.getElementById('color-pane');324 this.rangeHue = this.shadowRoot.getElementById('range-hue');325 this.rangeOpacity = this.shadowRoot.getElementById('range-opacity');326 this.copyBtn = this.shadowRoot.getElementById('copy-btn');327 this.copyinfo = this.copyBtn.querySelector('input');328 this.switch = this.shadowRoot.getElementById('btn-switch');329 this.colorHexa = this.shadowRoot.getElementById('color-hexa').querySelectorAll('input');330 this.colorRgba = this.shadowRoot.getElementById('color-rgba').querySelectorAll('input');331 this.colorHlsa = this.shadowRoot.getElementById('color-hlsa').querySelectorAll('input');332 this.value = this.defaultvalue;333 this.rangeHue.addEventListener('input',()=>{334 const value = [...this.$value];335 value[0] = Number(this.rangeHue.value);336 this.nativeclick = true;337 this.value = `hsva(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;338 })339 this.palette.addEventListener('mousedown',(ev)=>{340 this.start = true;341 this.choose(ev);342 })343 document.addEventListener('mousemove',this.mousemove);344 document.addEventListener('mouseup',this.mouseup);345 this.rangeOpacity.addEventListener('input',()=>{346 const value = [...this.$value];347 value[3] = Number(this.rangeOpacity.value);348 this.nativeclick = true;349 this.value = `hsva(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;350 })351 this.colors.addEventListener('click',(ev)=>{352 const item = ev.target.closest('button');353 if(item){354 this.nativeclick = true;355 this.value = item.dataset.color;356 }357 })358 this.switch.addEventListener('click',()=>{359 this.typeindex ++;360 this.typeindex %= 3;361 this.switch.textContent = this.type[this.typeindex];362 this.nativeclick = true;363 this.value = this.value;364 this.switch.parentNode.dataset.type = this.type[this.typeindex];365 })366 this.copyBtn.addEventListener('click',()=>{367 this.copyinfo.select();368 if (document.execCommand('copy')) {369 document.execCommand('copy');370 message.success(this.value);371 }372 })373 this.colorHexa.forEach(el=>{374 el.addEventListener('change',()=>{375 this.nativeclick = true;376 this.value = el.value;377 })378 })379 this.colorRgba.forEach((el,i)=>{380 el.addEventListener('change',()=>{381 const value = HSVaColor(...this.$value).toRGBA();382 value[i] = Number(el.value);383 this.nativeclick = true;384 this.value = `rgba(${value[0]}, ${value[1]}, ${value[2]}, ${value[3]})`;385 })386 })387 this.colorHlsa.forEach((el,i)=>{388 el.addEventListener('change',()=>{389 const value = HSVaColor(...this.$value).toHSLA();390 value[i] = Number(el.value);391 this.nativeclick = true;392 this.value = `hsla(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;393 })394 })395 }396 mousemove = (ev) => {397 if(this.start){398 this.choose(ev);399 }400 }401 mouseup = () => {402 if(getComputedStyle(this.palette).opacity!==1 && this.start){403 this.dispatchEvent(new CustomEvent('change', {404 detail: {405 value: this.value,406 color: this.color407 }408 }));409 }410 this.start = false;411 }412 disconnectedCallback() {413 document.removeEventListener('mousemove', this.mousemove);414 document.removeEventListener('mouseup', this.mouseup);415 }416 get value() {417 return HSVaColor(...this.$value)['to'+this.type[this.typeindex]]().toString();418 }419 get color() {420 return HSVaColor(...this.$value);421 }422 get defaultvalue() {423 return this.getAttribute('defaultvalue')||'#ff0000';424 }425 set defaultvalue(value) {426 this.setAttribute("defaultvalue",value);427 }428 set value(value) {429 this.$value = parseToHSVA(value).values;430 //[h,s,v,a]431 const [h,s,v,a=1] = this.$value;432 this.pane.style.setProperty('--h',h);433 this.pane.style.setProperty('--s',s);434 this.pane.style.setProperty('--v',v);435 this.pane.style.setProperty('--a',a);436 this.pane.style.setProperty('--c',this.value);437 this.copyinfo.value = this.value;438 this.rangeHue.value = h;439 this.rangeOpacity.value = a.toFixed(2);440 const COLOR = HSVaColor(...this.$value);441 this.colorHexa[0].value = COLOR.toHEXA().toString();442 const RGBA = COLOR.toRGBA();443 this.colorRgba[0].value = RGBA[0].toFixed(0);444 this.colorRgba[1].value = RGBA[1].toFixed(0);445 this.colorRgba[2].value = RGBA[2].toFixed(0);446 this.colorRgba[3].value = RGBA[3].toFixed(2);447 const HSLA = COLOR.toHSLA();448 this.colorHlsa[0].value = HSLA[0].toFixed(0);449 this.colorHlsa[1].value = HSLA[1].toFixed(0);450 this.colorHlsa[2].value = HSLA[2].toFixed(0);451 this.colorHlsa[3].value = HSLA[3].toFixed(2);452 if(this.nativeclick){453 this.nativeclick = false;454 this.dispatchEvent(new CustomEvent('change', {455 detail: {456 value: this.value,457 color: this.color458 }459 }));460 }461 }462}463if(!customElements.get('xy-color-pane')){464 customElements.define('xy-color-pane', XyColorPane);465}466export default class XyColorPicker extends HTMLElement {467 static get observedAttributes() { return ['disabled','dir'] }468 constructor() {469 super();470 const shadowRoot = this.attachShadow({ mode: 'open' });471 shadowRoot.innerHTML = `472 <style>473 :host{474 display:inline-block;475 width:30px;476 height:30px;477 font-size:14px;478 }479 :host([block]){480 display:block;481 }482 :host([disabled]){483 pointer-events:none;484 }485 486 :host(:focus-within) xy-popover,:host(:hover) xy-popover{ 487 z-index: 2;488 }489 xy-popover{490 width:100%;491 height:100%;492 }493 .color-btn{494 width:100%;495 height:100%;496 padding:5px;497 background-clip: content-box;498 background-color:var(--themeColor,#42b983);499 }500 .color-btn:hover{501 z-index: auto;502 }503 xy-popover{504 display:block;505 }506 xy-popcon{507 min-width:100%;508 }509 .pop-footer{510 display:flex;511 justify-content:flex-end;512 padding:0 .8em .8em;513 }514 .pop-footer xy-button{515 font-size: .8em;516 margin-left: .8em;517 }518 .color-btn::before{519 content:'';520 position:absolute;521 left:5px;522 top:5px;523 right:5px;524 bottom:5px;525 z-index:-1;526 background:linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );527 background-position:0 0,5px 5px;528 background-size:10px 10px;529 }530 </style>531 <xy-popover id="popover" ${this.dir? "dir='"+this.dir+"'" : ""}>532 <xy-button class="color-btn" id="color-btn" ${this.disabled? "disabled" : ""}></xy-button>533 <xy-popcon id="popcon">534 <div class="pop-footer">535 <xy-button autoclose>取 消</xy-button>536 <xy-button type="primary" id="btn-submit" autoclose>确 认</xy-button>537 </div>538 </xy-popcon>539 </xy-popover>540 `541 }542 focus() {543 this.colorBtn.focus();544 }545 connectedCallback() {546 this.popover = this.shadowRoot.getElementById('popover');547 this.popcon = this.shadowRoot.getElementById('popcon');548 this.colorBtn = this.shadowRoot.getElementById('color-btn');549 this.btnSubmit = this.shadowRoot.getElementById('btn-submit');550 this.colorBtn.addEventListener('click',()=>{551 if(!this.colorPane){552 this.colorPane = new XyColorPane();553 this.colorPane.defaultvalue = this.defaultvalue;554 this.popcon.prepend(this.colorPane);555 }556 })557 this.btnSubmit.addEventListener('click',()=>{558 this.nativeclick = true;559 this.value = this.colorPane.value;560 })561 this.popcon.addEventListener('close',()=>{562 this.colorPane.value = this.value;563 })564 this.value = this.defaultvalue;565 }566 567 get defaultvalue() {568 return this.getAttribute('defaultvalue')||'#42b983';569 }570 get value() {571 return this.$value;572 }573 get color() {574 return HSVaColor(...parseToHSVA(this.$value).values);575 }576 get type() {577 return this.getAttribute('type');578 }579 get disabled() {580 return this.getAttribute('disabled')!==null;581 }582 get dir() {583 return this.getAttribute('dir');584 }585 set dir(value){586 this.setAttribute('dir', value);587 }588 set disabled(value) {589 if (value === null || value === false) {590 this.removeAttribute('disabled');591 } else {592 this.setAttribute('disabled', '');593 }594 }595 set defaultvalue(value){596 this.setAttribute('defaultvalue', value);597 }598 set value(value) {599 this.colorBtn.style.setProperty('--themeColor',value);600 this.$value = value;601 if(this.nativeclick){602 this.nativeclick = false;603 this.dispatchEvent(new CustomEvent('change', {604 detail: {605 value: this.value,606 color: this.color607 }608 }));609 }else{610 if(this.colorPane){611 this.colorPane.value = this.value;612 }else{613 this.defaultvalue = this.value;614 }615 }616 }617 attributeChangedCallback(name, oldValue, newValue) {618 if (name == 'disabled' && this.colorBtn) {619 if (newValue != null) {620 this.colorBtn.setAttribute('disabled', 'disabled');621 } else {622 this.colorBtn.removeAttribute('disabled');623 }624 }625 if (name == 'dir' && this.popover) {626 if (newValue != null) {627 this.popover.dir = newValue;628 }629 }630 }631}632if (!customElements.get('xy-color-picker')) {633 customElements.define('xy-color-picker', XyColorPicker);...

Full Screen

Full Screen

com-color-picker.js

Source:com-color-picker.js Github

copy

Full Screen

1import './com-button.js';2import './com-popover.js';3import message from './com-message.js';4import { rgbToHsv, hslToHsv, parseToHSVA } from '../color.js';5import { HSVaColor } from '../hsvacolor.js';6const Material_colors = ['#f44336', '#E91E63', '#9C27B0', '#673AB7', '#3F51B5', '#2196F3', '#03A9F4', '#15bbf2', '#00BCD4', '#009688', '#4CAF50', '#8BC34A',7 '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800', '#FF5722', '#795548', '#9E9E9E', '#607D8B', 'rgba(0,0,0,.65)', 'transparent']8class ComColorPane extends HTMLElement {9 constructor() {10 super();11 const shadowRoot = this.attachShadow({ mode: 'open' });12 shadowRoot.innerHTML = `13 <style>14 :host{15 display: block;16 min-width: 300px;17 }18 .color-pane{19 padding:.8em;20 }21 .color-palette{22 position:relative;23 height:150px;24 background:linear-gradient(to top, hsla(0,0%,0%,calc(var(--a))), transparent), linear-gradient(to left, hsla(calc(var(--h)),100%,50%,calc(var(--a))),hsla(0,0%,100%,calc(var(--a)))),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );25 background-position:0 0, 0 0,0 0,5px 5px;26 background-size:100% 100%, 100% 100%, 10px 10px, 10px 10px;27 user-select: none;28 cursor: crosshair;29 opacity:1;30 transition:opacity .1s;31 }32 .color-palette:active{33 opacity:.99;34 }35 .color-palette::after{36 pointer-events:none;37 position:absolute;38 content:'';39 box-sizing:border-box;40 width:10px;41 height:10px;42 border-radius:50%;43 border:2px solid #fff;44 left:calc(var(--s) * 1%);45 top:calc((100 - var(--v)) * 1%);46 transform:translate(-50%,-50%);47 }48 .color-chooser{49 display:flex;50 padding:10px 0;51 }52 .color-show{53 display:flex;54 position: relative;55 width:32px;56 height:32px;57 background:var(--c);58 transition:none;59 border-radius:50%;60 overflow:hidden;61 cursor:pointer;62 }63 .color-show .icon-file{64 width:1em;65 height:1em;66 margin: auto;67 fill: hsl(0, 0%, calc( ((2 - var(--s) / 100) * var(--v) / 200 * var(--a) - 0.6 ) * -999999% ));68 opacity: 0;69 transition: var(--transitionTime,.3s);70 }71 .color-show:hover .icon-file{72 opacity:1;73 }74 .color-show input{75 position:absolute;76 clip:rect(0,0,0,0);77 }78 .color-show::after{79 content:'';80 position:absolute;81 width:32px;82 height:32px;83 background:linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );84 background-position:0 0,5px 5px;85 background-size:10px 10px;86 z-index:-1;87 }88 .color-range{89 flex:1;90 margin-left:10px;91 }92 input[type="range"]{93 display: block;94 pointer-events:all;95 width:100%;96 -webkit-appearance: none;97 outline : 0;98 height: 10px;99 border-radius:5px;100 margin:0;101 }102 input[type="range"]::-webkit-slider-runnable-track{103 display: flex;104 align-items: center;105 position: relative;106 }107 input[type="range"]::-webkit-slider-thumb{108 -webkit-appearance: none;109 position: relative;110 width:10px;111 height:10px;112 transform:scale(1.2);113 border-radius: 50%;114 box-shadow: 0 0 10px rgba(0,0,0,0.1);115 background:#fff;116 transition:.2s cubic-bezier(.12, .4, .29, 1.46);117 }118 input[type="range"]::-moz-range-thumb{119 box-sizing:border-box;120 pointer-events:none;121 position: relative;122 width:10px;123 height:10px;124 transform:scale(1.2);125 border-radius: 50%;126 border:0;127 box-shadow: 0 0 10px rgba(0,0,0,0.1);128 background:#fff;129 transition:.2s cubic-bezier(.12, .4, .29, 1.46);130 }131 input[type="range"]::-webkit-slider-thumb:active,132 input[type="range"]:focus::-webkit-slider-thumb{133 transform:scale(1.5);134 }135 input[type="range"]::-moz-range-thumb:active,136 input[type="range"]:focus::-moz-range-thumb{137 transform:scale(1.5);138 }139 input[type="range"]+input[type="range"]{140 margin-top:10px;141 }142 .color-hue{143 background:linear-gradient(to right, red, yellow, lime, cyan, blue, magenta, red)144 }145 .color-opacity{146 background:linear-gradient(to right, hsla(calc(var(--h)),100%,50%,0), hsla(calc(var(--h)),100%,50%,1)),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );147 background-position:0 0,0 0,5px 5px;148 background-size:100% 100%,10px 10px,10px 10px;149 }150 .color-label{151 position:absolute;152 display:flex;153 visibility:hidden;154 opacity:0;155 left:0;156 right:0;157 top:0;158 bottom:0;159 transition: .3s;160 }161 .color-label input{162 flex:1;163 margin-right:.8em;164 outline:0;165 min-width:0;166 width: 0;167 border-radius:var(--borderRadius,.25em);168 border:1px solid #ddd;169 padding:0 5px;170 line-height:28px;171 text-align:center;172 -moz-appearance: textfield;173 transition:.3s;174 }175 input[type="number"]::-webkit-inner-spin-button{176 display:none;177 }178 ::-moz-focus-inner,::-moz-focus-outer{179 border:0;180 outline : 0;181 }182 .color-label input:focus{183 border-color:var(--themeColor,#15bbf2);184 }185 .color-footer{186 display:flex187 }188 .btn-switch{189 position:relative;190 border-radius:var(--borderRadius,.25em);191 background:none;192 border:0;193 outline:0;194 line-height:30px;195 width: 60px;196 padding: 0;197 color:var(--themeColor,#15bbf2);198 overflow:hidden;199 }200 .btn-switch::before{201 content:'';202 position:absolute;203 left:0;204 top:0;205 right:0;206 bottom:0;207 background:var(--themeBackground,var(--themeColor,#15bbf2));208 opacity:.2;209 transition:.3s;210 }211 .btn-switch:hover::before,.btn-switch:focus::before{212 opacity:.3;213 }214 .color-input{215 position:relative;216 flex:1;217 height:30px;218 overflow:hidden;219 }220 .color-footer[data-type="HEXA"] .color-label:nth-child(1),221 .color-footer[data-type="RGBA"] .color-label:nth-child(2),222 .color-footer[data-type="HSLA"] .color-label:nth-child(3){223 opacity:1;224 visibility:inherit;225 z-index:2;226 }227 .color-sign{228 padding-top:10px;229 display:grid;230 grid-template-columns: repeat(auto-fit, minmax(15px, 1fr));231 grid-gap: 10px;232 }233 .color-sign>button{234 position:relative;235 cursor:pointer;236 width:100%;237 padding-bottom:0;238 padding-top:100%;239 border-radius:4px;240 border:0;241 outline:0;242 }243 .color-sign>button::before{244 content:'';245 position:absolute;246 left:0;247 top:0;248 width:100%;249 height:100%;250 z-index:-1;251 background:linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );252 background-position:0 0,5px 5px;253 background-size:10px 10px;254 border-radius: 4px;255 }256 .color-sign>button::after{257 content:'';258 position:absolute;259 opacity:.5;260 z-index:-2;261 left:0;262 top:0;263 width:100%;264 height:100%;265 background:inherit;266 border-radius:4px;267 transition:.3s;268 }269 .color-sign>button:hover::after,.color-sign>button:focus::after{270 transform:translate(2px,2px)271 }272 </style>273 <div class="color-pane" id="color-pane">274 <div class="color-palette" id="color-palette"></div>275 <div class="color-chooser">276 <a class="color-show" id="copy-btn"><svg class="icon-file" viewBox="0 0 1024 1024"><path d="M832 64H296c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h496v688c0 4.4 3.6 8 8 8h56c4.4 0 8-3.6 8-8V96c0-17.7-14.3-32-32-32z"></path><path d="M704 192H192c-17.7 0-32 14.3-32 32v530.7c0 8.5 3.4 16.6 9.4 22.6l173.3 173.3c2.2 2.2 4.7 4 7.4 5.5v1.9h4.2c3.5 1.3 7.2 2 11 2H704c17.7 0 32-14.3 32-32V224c0-17.7-14.3-32-32-32zM350 856.2L263.9 770H350v86.2zM664 888H414V746c0-22.1-17.9-40-40-40H232V264h432v624z"></path></svg><input></a>277 <div class="color-range">278 <input class="color-hue" value="0" min="0" max="360" type="range" id="range-hue">279 <input class="color-opacity" value="1" min="0" max="1" step="0.01" type="range" id="range-opacity">280 </div>281 </div>282 <div class="color-footer" data-type="HEXA">283 <div class="color-input">284 <div class="color-label" id="color-hexa">285 <input spellcheck="false" />286 </div>287 <div class="color-label" id="color-rgba">288 <input type="number" min="0" max="255" spellcheck="false" />289 <input type="number" min="0" max="255" spellcheck="false" />290 <input type="number" min="0" max="255" spellcheck="false" />291 <input type="number" min="0" max="1" step="0.01" spellcheck="false" />292 </div>293 <div class="color-label" id="color-hlsa">294 <input type="number" min="0" max="360" spellcheck="false" />295 <input type="number" min="0" max="100" spellcheck="false" />296 <input type="number" min="0" max="100" spellcheck="false" />297 <input type="number" min="0" max="1" step="0.01" spellcheck="false" />298 </div>299 </div>300 <button class="btn-switch" id="btn-switch" type="flat">HEXA</button>301 </div>302 <div class="color-sign" id="colors">303 ${304 Material_colors.map(el => '<button style="background-color:' + el + '" data-color=' + el + '></button>').join('')305 }306 </div>307 </div>308 `309 }310 choose(ev) {311 const { x, y, width: w, height: h } = this.palette.getBoundingClientRect();312 const value = [...this.$value];313 const _x = Math.min(Math.max(0, (ev.clientX - x) / w * 100), 100);314 const _y = Math.min(Math.max(0, (ev.clientY - y) / h * 100), 100);315 value[1] = _x;316 value[2] = 100 - _y;317 this.value = `hsva(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;318 }319 connectedCallback() {320 this.type = ['HEXA', 'RGBA', 'HSLA'];321 this.typeindex = 0;322 this.palette = this.shadowRoot.getElementById('color-palette');323 this.colors = this.shadowRoot.getElementById('colors');324 this.pane = this.shadowRoot.getElementById('color-pane');325 this.rangeHue = this.shadowRoot.getElementById('range-hue');326 this.rangeOpacity = this.shadowRoot.getElementById('range-opacity');327 this.copyBtn = this.shadowRoot.getElementById('copy-btn');328 this.copyinfo = this.copyBtn.querySelector('input');329 this.switch = this.shadowRoot.getElementById('btn-switch');330 this.colorHexa = this.shadowRoot.getElementById('color-hexa').querySelectorAll('input');331 this.colorRgba = this.shadowRoot.getElementById('color-rgba').querySelectorAll('input');332 this.colorHlsa = this.shadowRoot.getElementById('color-hlsa').querySelectorAll('input');333 this.value = this.defaultvalue;334 this.rangeHue.addEventListener('input', () => {335 const value = [...this.$value];336 value[0] = Number(this.rangeHue.value);337 this.nativeclick = true;338 this.value = `hsva(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;339 })340 this.palette.addEventListener('mousedown', (ev) => {341 this.start = true;342 this.choose(ev);343 })344 document.addEventListener('mousemove', (ev) => {345 if (this.start) {346 this.choose(ev);347 }348 })349 document.addEventListener('mouseup', (ev) => {350 if (getComputedStyle(this.palette).opacity !== 1 && this.start) {351 this.dispatchEvent(new CustomEvent('change', {352 detail: {353 value: this.value,354 color: this.color355 }356 }));357 }358 this.start = false;359 })360 this.rangeOpacity.addEventListener('input', () => {361 const value = [...this.$value];362 value[3] = Number(this.rangeOpacity.value);363 this.nativeclick = true;364 this.value = `hsva(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;365 })366 this.colors.addEventListener('click', (ev) => {367 const item = ev.target.closest('button');368 if (item) {369 this.nativeclick = true;370 this.value = item.dataset.color;371 }372 })373 this.switch.addEventListener('click', () => {374 this.typeindex++;375 this.typeindex %= 3;376 this.switch.textContent = this.type[this.typeindex];377 this.nativeclick = true;378 this.value = this.value;379 this.switch.parentNode.dataset.type = this.type[this.typeindex];380 })381 this.copyBtn.addEventListener('click', () => {382 this.copyinfo.select();383 if (document.execCommand('copy')) {384 document.execCommand('copy');385 message.success(this.value);386 }387 })388 this.colorHexa.forEach(el => {389 el.addEventListener('change', () => {390 this.nativeclick = true;391 this.value = el.value;392 })393 })394 this.colorRgba.forEach((el, i) => {395 el.addEventListener('change', () => {396 const value = HSVaColor(...this.$value).toRGBA();397 value[i] = Number(el.value);398 this.nativeclick = true;399 this.value = `rgba(${value[0]}, ${value[1]}, ${value[2]}, ${value[3]})`;400 })401 })402 this.colorHlsa.forEach((el, i) => {403 el.addEventListener('change', () => {404 const value = HSVaColor(...this.$value).toHSLA();405 value[i] = Number(el.value);406 this.nativeclick = true;407 this.value = `hsla(${value[0]}, ${value[1]}%, ${value[2]}%, ${value[3]})`;408 })409 })410 }411 get value() {412 return HSVaColor(...this.$value)['to' + this.type[this.typeindex]]().toString();413 }414 get color() {415 return HSVaColor(...this.$value);416 }417 get defaultvalue() {418 return this.getAttribute('defaultvalue') || '#ff0000';419 }420 set defaultvalue(value) {421 this.setAttribute("defaultvalue", value);422 }423 set value(value) {424 this.$value = parseToHSVA(value).values;425 //[h,s,v,a]426 const [h, s, v, a = 1] = this.$value;427 this.pane.style.setProperty('--h', h);428 this.pane.style.setProperty('--s', s);429 this.pane.style.setProperty('--v', v);430 this.pane.style.setProperty('--a', a);431 this.pane.style.setProperty('--c', this.value);432 this.copyinfo.value = this.value;433 this.rangeHue.value = h;434 this.rangeOpacity.value = a.toFixed(2);435 const COLOR = HSVaColor(...this.$value);436 this.colorHexa[0].value = COLOR.toHEXA().toString();437 const RGBA = COLOR.toRGBA();438 this.colorRgba[0].value = RGBA[0].toFixed(0);439 this.colorRgba[1].value = RGBA[1].toFixed(0);440 this.colorRgba[2].value = RGBA[2].toFixed(0);441 this.colorRgba[3].value = RGBA[3].toFixed(2);442 const HSLA = COLOR.toHSLA();443 this.colorHlsa[0].value = HSLA[0].toFixed(0);444 this.colorHlsa[1].value = HSLA[1].toFixed(0);445 this.colorHlsa[2].value = HSLA[2].toFixed(0);446 this.colorHlsa[3].value = HSLA[3].toFixed(2);447 if (this.nativeclick) {448 this.nativeclick = false;449 this.dispatchEvent(new CustomEvent('change', {450 detail: {451 value: this.value,452 color: this.color453 }454 }));455 }456 }457}458customElements.define('com-color-pane', ComColorPane);459export default class ComColorPicker extends HTMLElement {460 static get observedAttributes() { return ['disabled', 'dir'] }461 constructor() {462 super();463 const shadowRoot = this.attachShadow({ mode: 'open' });464 shadowRoot.innerHTML = `465 <style>466 :host{467 display:inline-block;468 width:30px;469 height:30px;470 font-size:14px;471 }472 :host([block]){473 display:block;474 }475 :host([disabled]){476 pointer-events:none;477 }478 479 :host(:focus-within) com-popover,:host(:hover) com-popover{ 480 z-index: 2;481 }482 com-popover{483 width:100%;484 height:100%;485 }486 .color-btn{487 width:100%;488 height:100%;489 padding:5px;490 background-clip: content-box;491 background-color:var(--themeColor,#15bbf2);492 }493 .color-btn:hover{494 z-index: auto;495 }496 com-popover{497 display:block;498 }499 com-popcon{500 min-width:100%;501 }502 .pop-footer{503 display:flex;504 justify-content:flex-end;505 padding:0 .8em .8em;506 }507 .pop-footer com-button{508 font-size: .8em;509 margin-left: .8em;510 }511 .color-btn::before{512 content:'';513 position:absolute;514 left:5px;515 top:5px;516 right:5px;517 bottom:5px;518 z-index:-1;519 background:linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ),linear-gradient( 45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 );520 background-position:0 0,5px 5px;521 background-size:10px 10px;522 }523 </style>524 <com-popover id="popover" ${this.dir ? "dir='" + this.dir + "'" : ""}>525 <com-button class="color-btn" id="color-btn" ${this.disabled ? "disabled" : ""}></com-button>526 <com-popcon id="popcon">527 <div class="pop-footer">528 <com-button autoclose>取 消</com-button>529 <com-button type="primary" id="btn-submit" autoclose>确 认</com-button>530 </div>531 </com-popcon>532 </com-popover>533 `534 }535 focus() {536 this.colorBtn.focus();537 }538 connectedCallback() {539 this.popover = this.shadowRoot.getElementById('popover');540 this.popcon = this.shadowRoot.getElementById('popcon');541 this.colorBtn = this.shadowRoot.getElementById('color-btn');542 this.btnSubmit = this.shadowRoot.getElementById('btn-submit');543 this.colorBtn.addEventListener('click', () => {544 if (!this.colorPane) {545 this.colorPane = new ComColorPane();546 this.colorPane.defaultvalue = this.defaultvalue;547 this.popcon.prepend(this.colorPane);548 }549 })550 this.btnSubmit.addEventListener('click', () => {551 this.nativeclick = true;552 this.value = this.colorPane.value;553 })554 this.popcon.addEventListener('close', () => {555 this.colorPane.value = this.value;556 })557 this.value = this.defaultvalue;558 }559 get defaultvalue() {560 return this.getAttribute('defaultvalue') || '#15bbf2';561 }562 get value() {563 return this.$value;564 }565 get color() {566 return HSVaColor(...parseToHSVA(this.$value).values);567 }568 get type() {569 return this.getAttribute('type');570 }571 get disabled() {572 return this.getAttribute('disabled') !== null;573 }574 get dir() {575 return this.getAttribute('dir');576 }577 set dir(value) {578 this.setAttribute('dir', value);579 }580 set disabled(value) {581 if (value === null || value === false) {582 this.removeAttribute('disabled');583 } else {584 this.setAttribute('disabled', '');585 }586 }587 set defaultvalue(value) {588 this.setAttribute('defaultvalue', value);589 }590 set value(value) {591 this.colorBtn.style.setProperty('--themeColor', value);592 this.$value = value;593 if (this.nativeclick) {594 this.nativeclick = false;595 this.dispatchEvent(new CustomEvent('change', {596 detail: {597 value: this.value,598 color: this.color599 }600 }));601 } else {602 if (this.colorPane) {603 this.colorPane.value = this.value;604 } else {605 this.defaultvalue = this.value;606 }607 }608 }609 attributeChangedCallback(name, oldValue, newValue) {610 if (name == 'disabled' && this.colorBtn) {611 if (newValue != null) {612 this.colorBtn.setAttribute('disabled', 'disabled');613 } else {614 this.colorBtn.removeAttribute('disabled');615 }616 }617 if (name == 'dir' && this.popover) {618 if (newValue != null) {619 this.popover.dir = newValue;620 }621 }622 }623}624if (!customElements.get('com-color-picker')) {625 customElements.define('com-color-picker', ComColorPicker);...

Full Screen

Full Screen

wc-select.js

Source:wc-select.js Github

copy

Full Screen

1import './wc-option.js';2import './wc-popover.js';3export default class WcSelect extends HTMLElement {4 static get observedAttributes() { return ['value', 'disabled', 'type'] }5 constructor() {6 super();7 const shadowRoot = this.attachShadow({ mode: 'open' });8 shadowRoot.innerHTML = `9 <style>10 :host{11 display:inline-block;12 font-size: 14px;13 border-radius: var(--borderRadius,.25em);14 }15 :host([block]){16 display:block;17 }18 19 :host(:not([disabled]):not([type="primary"]):focus-within) #select,20 :host(:not([disabled]):not([type="primary"]):hover) #select{21 border-color:var(--themeColor,#42b983);22 color:var(--themeColor,#42b983);23 }24 :host([search]:focus-within:not([disabled])) #select, 25 :host([search]:not([disabled]):hover) #select{26 color: var(--themeColor,#42b983);27 }28 :host([disabled]){29 pointer-events:none;30 }31 32 :host(:focus-within) wc-popover,:host(:active) wc-popover{ 33 z-index: 2;34 }35 wc-tips{36 display:block;37 width: 100%;38 height: 100%;39 border-radius: inherit;40 }41 42 #select:not([type="primary"]){43 display:flex;44 width:100%;45 height:100%;46 font-size: inherit;47 color: currentColor;48 border-radius: inherit;49 }50 :host([search]) #select{51 color:currentColor;52 }53 wc-tips[show=show]{54 --themeColor:var(--errorColor,#f4615c);55 --borderColor:var(--errorColor,#f4615c);56 }57 :host([invalid]) #select:not([type="primary"]){58 color:var(--errorColor,#f4615c);59 }60 #select span{61 flex:1;62 text-align:left;63 }64 65 wc-input::after{66 content:'';67 position:absolute;68 left:0;69 top:0;70 right:0;71 bottom:0;72 cursor:default;73 pointer-events:none;74 }75 #select[readonly]::after{76 pointer-events:all;77 }78 .arrow{79 position:relative;80 font-size:.9em;81 transform:scaleY(.8);82 margin-left:.5em;83 pointer-events:none;84 width:1em;85 height:1em;86 fill:currentColor;87 transition:.3s transform cubic-bezier(.645, .045, .355, 1);88 }89 :host([search]) .arrow{90 transition:color .3s cubic-bezier(.645, .045, .355, 1),.3s transform cubic-bezier(.645, .045, .355, 1);91 }92 wc-popover[open] .arrow{93 transform:scaleY(-.8);94 }95 wc-popover{96 display:block;97 height:inherit;98 border-radius: inherit;99 }100 wc-popcon{101 min-width:100%;102 overflow:auto;103 max-height:50vh;104 scroll-behavior: smooth;105 }106 :host([search]) wc-popcon::before{107 display:none;108 box-sizing: border-box;109 width:100%;110 content:'没有匹配到任何选项';111 padding: .25em .625em;112 line-height: 1.8;113 color:var(--fontColor,#333);114 white-space:nowrap;115 opacity:.5;116 }117 :host([empty]) wc-popcon::before{118 display:block;119 }120 </style>121 <style id="filter"></style>122 <wc-popover id="root" ${this.search?"accomplish":""}>123 <wc-tips id="tip" type="error">124 <${this.search?'wc-input':'wc-button'} id="select" debounce="200" readonly ${this.disabled? "disabled" : ""} ${this.type?("type="+this.type):""}>${this.search?"":'<span id="value"></span>'}<svg class="arrow" viewBox="0 0 1024 1024"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3 0.1-12.7-6.4-12.7z"></path></svg></${this.search?'wc-input':'wc-button'}>125 </wc-tips>126 <wc-popcon id="options">127 <slot id="slot"></slot>128 </wc-popcon>129 </wc-popover>130 `131 }132 move(dir) {133 const pre = this.nodes[this.focusIndex];134 const focusIndex = dir + this.focusIndex;135 const current = this.nodes[focusIndex];136 if (current) {137 if(pre){138 pre.focusin = false;139 }140 current.focusin = true;141 this.focusIndex = focusIndex;142 }143 }144 movein(dir) {145 this.focusIndex = dir + this.focusIndex;146 if(this.focusIndex<0){147 this.focusIndex = this.nodes.length-1;148 }149 if(this.focusIndex===this.nodes.length){150 this.focusIndex = 0;151 }152 this.nativeclick = true;153 this.value = this.nodes[this.focusIndex].value; 154 }155 focus() {156 this.select.focus();157 }158 reset() {159 this.value = this.defaultvalue;160 this.tip.show = false;161 this.invalid = false;162 }163 checkValidity(){164 if(this.novalidate||this.disabled||this.form&&this.form.novalidate){165 return true;166 }167 if(this.validity){168 this.tip.show = false;169 this.invalid = false;170 return true;171 }else{172 this.focus();173 this.tip.show = 'show';174 this.invalid = true;175 this.tip.tips = this.errortips;176 return false;177 }178 }179 connectedCallback() {180 this.form = this.closest('wc-form');181 this.root = this.shadowRoot.getElementById('root');182 this.select = this.shadowRoot.getElementById('select');183 this.options = this.shadowRoot.getElementById('options');184 this.slots = this.shadowRoot.getElementById('slot');185 this.txt = this.shadowRoot.getElementById('value');186 this.tip = this.shadowRoot.getElementById('tip');187 this.focusIndex = -1;188 this.addEventListener('keydown', (ev) => {189 if (this.options.open) {190 switch (ev.keyCode) {191 case 9://Tab192 ev.preventDefault();193 break;194 case 38://ArrowUp195 ev.preventDefault();196 this.move(-1);197 break;198 case 40://ArrowDown199 ev.preventDefault();200 this.move(1);201 break;202 case 27://Esc203 ev.preventDefault();204 this.options.open = false;205 break;206 default:207 break;208 }209 }else{210 switch (ev.keyCode) {211 case 38://ArrowUp212 ev.preventDefault();213 this.movein(-1);214 break;215 case 40://ArrowDown216 ev.preventDefault();217 this.movein(1);218 break;219 default:220 break;221 }222 }223 })224 this.select.addEventListener('focus',(ev)=>{225 ev.stopPropagation();226 if(!this.isfocus){227 this.checkValidity();228 this.dispatchEvent(new CustomEvent('focus',{229 detail:{230 value:this.value231 }232 }));233 }234 })235 this.options.addEventListener('click',(ev)=>{236 this.focus();237 const item = ev.target.closest('wc-option');238 if(item){239 this.nativeclick = true;240 this.value = item.value;241 }242 })243 this.options.addEventListener('close',(ev)=>{244 if(this.search){245 this.select.readonly = true;246 this.select.value = this.$text;247 //this.value = this.$value;248 this.nodes = [...this.querySelectorAll(`wc-option:not([disabled])`)];249 this.filter.textContent = '';250 this.empty = false;251 }252 const place = this.querySelector(`wc-option[focusin]`);253 const current = this.querySelector(`wc-option[selected]`);254 if(place){255 place.focusin = false;256 }257 if(current){258 current.focusin = true;259 this.focusIndex = this.nodes.indexOf(current);260 }else{261 this.focusIndex = -1;262 }263 })264 this.options.addEventListener('open',(ev)=>{265 if(this.search){266 this.select.value = '';267 this.select.readonly = false;268 this.focus();269 }270 })271 if(this.search){272 this.filter = this.shadowRoot.getElementById('filter');273 this.select.addEventListener('input',(ev)=>{274 const value = this.select.value.trim();275 if(value===""){276 this.nodes = [...this.querySelectorAll(`wc-option:not([disabled])`)];277 this.filter.textContent = '';278 }else{279 this.nodes = [...this.querySelectorAll(`wc-option[key*="${value}" i]:not([disabled])`)];280 this.filter.textContent = `281 :host([search]) ::slotted(wc-option:not([key*="${value}" i]))282 {283 display:none;284 }285 `286 }287 const place = this.querySelector(`wc-option[focusin]`);288 if(place){289 place.focusin = false;290 }291 if(this.nodes[0]){292 this.nodes[0].focusin = true;293 this.empty = false;294 }else{295 this.empty = true;296 }297 this.focusIndex = 0;298 })299 this.select.addEventListener('submit',(ev)=>{300 if(!this.options.open){301 this.options.open = true;302 }else{303 const item = this.nodes[this.focusIndex];304 this.nativeclick = true;305 if(item){306 this.value = item.value;307 }else{308 this.value = this.$value;309 this.options.open = false;310 }311 }312 })313 }else{314 this.addEventListener('click',(ev)=>{315 if(!this.options.open){316 const item = this.nodes[this.focusIndex];317 if(item){318 this.nativeclick = true;319 this.value = item.value;320 }321 }322 })323 }324 this.select.addEventListener('blur',(ev)=>{325 ev.stopPropagation();326 if(getComputedStyle(this.root).zIndex==2){327 this.isfocus = true;328 }else{329 this.isfocus = false;330 this.dispatchEvent(new CustomEvent('blur',{331 detail:{332 value:this.value333 }334 }));335 }336 })337 this.slots.addEventListener('slotchange', () => {338 this.nodes = [...this.querySelectorAll(`wc-option:not([disabled])`)];339 if (!this.defaultvalue) {340 this.value = '';341 } else {342 this.value = this.defaultvalue;343 }344 this.init = true;345 });346 }347 348 get defaultvalue() {349 return this.getAttribute('defaultvalue')||'';350 }351 get value() {352 return this.$value||'';353 }354 get text() {355 return this.$text||this.placeholder;356 }357 get name() {358 return this.getAttribute('name');359 }360 get empty() {361 return this.getAttribute('empty')!==null;362 }363 get type() {364 return this.getAttribute('type');365 }366 get validity() {367 return this.required?this.value!=='':true;368 }369 get novalidate() {370 return this.getAttribute('novalidate')!==null;371 }372 get errortips() {373 return this.getAttribute('errortips')||'请选择一项';374 }375 get required() {376 return this.getAttribute('required')!==null;377 }378 get invalid() {379 return this.getAttribute('invalid')!==null;380 }381 get disabled() {382 return this.getAttribute('disabled')!==null;383 }384 get search() {385 return this.getAttribute('search')!==null;386 }387 get placeholder() {388 return this.getAttribute('placeholder')||'请选择';389 }390 set required(value) {391 if(value===null||value===false){392 this.removeAttribute('required');393 }else{394 this.setAttribute('required', '');395 }396 }397 set invalid(value) {398 if(value===null||value===false){399 this.removeAttribute('invalid');400 }else{401 this.setAttribute('invalid', '');402 }403 }404 set novalidate(value) {405 if(value===null||value===false){406 this.removeAttribute('novalidate');407 }else{408 this.setAttribute('novalidate', '');409 }410 }411 set disabled(value) {412 if (value === null || value === false) {413 this.removeAttribute('disabled');414 } else {415 this.setAttribute('disabled', '');416 }417 }418 set empty(value) {419 if (value === null || value === false) {420 this.removeAttribute('empty');421 } else {422 this.setAttribute('empty', '');423 }424 }425 set defaultvalue(value){426 this.setAttribute('defaultvalue', value);427 }428 set value(value) {429 if(value === ''){430 this.$value = '';431 this.$text = this.placeholder;432 if(this.focusIndex>=0){433 const current = this.nodes[this.focusIndex];434 if(current){435 this.focusIndex = -1;436 current.selected = false;437 current.focusin = false;438 }439 }440 if(this.search){441 this.select.placeholder = this.placeholder;442 this.select.value = '';443 }else{444 this.txt.textContent = this.placeholder;445 }446 return447 }448 if (value !== this.value) {449 this.$value = value;450 const pre = this.querySelector(`wc-option[selected]`);451 if(pre){452 pre.selected = false;453 pre.focusin = false;454 }455 const cur = this.querySelector(`wc-option[value="${value}"]`)||this.querySelector(`wc-option`);456 this.focusIndex = this.nodes.indexOf(cur);457 cur.selected = true;458 cur.focusin = true;459 this.$text = cur.textContent;460 if(this.search){461 this.select.placeholder = this.$text;462 this.select.value = this.$text;463 }else{464 this.txt.textContent = this.$text;465 }466 if(this.nativeclick){467 this.nativeclick = false;468 this.checkValidity();469 this.dispatchEvent(new CustomEvent('change', {470 detail: {471 value: value,472 text: this.$text473 }474 }));475 }476 }477 this.options.open = false;478 }479 attributeChangedCallback(name, oldValue, newValue) {480 if (name == 'disabled' && this.select) {481 if (newValue != null) {482 this.select.disabled = true;483 } else {484 this.select.disabled = false;485 }486 }487 }488}489if (!customElements.get('wc-select')) {490 customElements.define('wc-select', WcSelect);...

Full Screen

Full Screen

xy-select.js

Source:xy-select.js Github

copy

Full Screen

1import './xy-option.js';2import './xy-popover.js';3export default class XySelect extends HTMLElement {4 static get observedAttributes() { return ['value', 'disabled', 'type'] }5 constructor() {6 super();7 const shadowRoot = this.attachShadow({ mode: 'open' });8 shadowRoot.innerHTML = `9 <style>10 :host{11 display:inline-block;12 font-size: 14px;13 border-radius: var(--borderRadius,.25em);14 }15 :host([block]){16 display:block;17 }18 19 :host(:not([disabled]):not([type="primary"]):focus-within) #select,20 :host(:not([disabled]):not([type="primary"]):hover) #select{21 border-color:var(--themeColor,#42b983);22 color:var(--themeColor,#42b983);23 }24 :host([search]:focus-within:not([disabled])) #select, 25 :host([search]:not([disabled]):hover) #select{26 color: var(--themeColor,#42b983);27 }28 :host([disabled]){29 pointer-events:none;30 }31 32 :host(:focus-within) xy-popover,:host(:active) xy-popover{ 33 z-index: 2;34 }35 xy-tips{36 display:block;37 width: 100%;38 height: 100%;39 border-radius: inherit;40 }41 42 #select:not([type="primary"]){43 display:flex;44 width:100%;45 height:100%;46 font-size: inherit;47 color: currentColor;48 border-radius: inherit;49 }50 :host([search]) #select{51 color:currentColor;52 }53 xy-tips[show=show]{54 --themeColor:var(--errorColor,#f4615c);55 --borderColor:var(--errorColor,#f4615c);56 }57 :host([invalid]) #select:not([type="primary"]){58 color:var(--errorColor,#f4615c);59 }60 #select span{61 flex:1;62 text-align:left;63 }64 65 xy-input::after{66 content:'';67 position:absolute;68 left:0;69 top:0;70 right:0;71 bottom:0;72 cursor:default;73 pointer-events:none;74 }75 #select[readonly]::after{76 pointer-events:all;77 }78 .arrow{79 position:relative;80 font-size:.9em;81 transform:scaleY(.8);82 margin-left:.5em;83 pointer-events:none;84 width:1em;85 height:1em;86 fill:currentColor;87 transition:.3s transform cubic-bezier(.645, .045, .355, 1);88 }89 :host([search]) .arrow{90 transition:color .3s cubic-bezier(.645, .045, .355, 1),.3s transform cubic-bezier(.645, .045, .355, 1);91 }92 xy-popover[open] .arrow{93 transform:scaleY(-.8);94 }95 xy-popover{96 display:block;97 height:inherit;98 border-radius: inherit;99 }100 xy-popcon{101 min-width:100%;102 overflow:auto;103 max-height:50vh;104 scroll-behavior: smooth;105 }106 :host([search]) xy-popcon::before{107 display:none;108 box-sizing: border-box;109 width:100%;110 content:'没有匹配到任何选项';111 padding: .25em .625em;112 line-height: 1.8;113 color:var(--fontColor,#333);114 white-space:nowrap;115 opacity:.5;116 }117 :host([empty]) xy-popcon::before{118 display:block;119 }120 </style>121 <style id="filter"></style>122 <xy-popover id="root" ${this.search?"accomplish":""}>123 <xy-tips id="tip" type="error">124 <${this.search?'xy-input':'xy-button'} id="select" debounce="200" readonly ${this.disabled? "disabled" : ""} ${this.type?("type="+this.type):""}>${this.search?"":'<span id="value"></span>'}<svg class="arrow" viewBox="0 0 1024 1024"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3 0.1-12.7-6.4-12.7z"></path></svg></${this.search?'xy-input':'xy-button'}>125 </xy-tips>126 <xy-popcon id="options">127 <slot id="slot"></slot>128 </xy-popcon>129 </xy-popover>130 `131 }132 move(dir) {133 const pre = this.nodes[this.focusIndex];134 const focusIndex = dir + this.focusIndex;135 const current = this.nodes[focusIndex];136 if (current) {137 if(pre){138 pre.focusin = false;139 }140 current.focusin = true;141 this.focusIndex = focusIndex;142 }143 }144 movein(dir) {145 this.focusIndex = dir + this.focusIndex;146 if(this.focusIndex<0){147 this.focusIndex = this.nodes.length-1;148 }149 if(this.focusIndex===this.nodes.length){150 this.focusIndex = 0;151 }152 this.nativeclick = true;153 this.value = this.nodes[this.focusIndex].value; 154 }155 focus() {156 this.select.focus();157 }158 reset() {159 this.value = this.defaultvalue;160 this.tip.show = false;161 this.invalid = false;162 }163 checkValidity(){164 if(this.novalidate||this.disabled||this.form&&this.form.novalidate){165 return true;166 }167 if(this.validity){168 this.tip.show = false;169 this.invalid = false;170 return true;171 }else{172 this.focus();173 this.tip.show = 'show';174 this.invalid = true;175 this.tip.tips = this.errortips;176 return false;177 }178 }179 connectedCallback() {180 this.form = this.closest('xy-form');181 this.root = this.shadowRoot.getElementById('root');182 this.select = this.shadowRoot.getElementById('select');183 this.options = this.shadowRoot.getElementById('options');184 this.slots = this.shadowRoot.getElementById('slot');185 this.txt = this.shadowRoot.getElementById('value');186 this.tip = this.shadowRoot.getElementById('tip');187 this.focusIndex = -1;188 this.addEventListener('keydown', (ev) => {189 if (this.options.open) {190 switch (ev.keyCode) {191 case 9://Tab192 ev.preventDefault();193 break;194 case 38://ArrowUp195 ev.preventDefault();196 this.move(-1);197 break;198 case 40://ArrowDown199 ev.preventDefault();200 this.move(1);201 break;202 case 27://Esc203 ev.preventDefault();204 this.options.open = false;205 break;206 default:207 break;208 }209 }else{210 switch (ev.keyCode) {211 case 38://ArrowUp212 ev.preventDefault();213 this.movein(-1);214 break;215 case 40://ArrowDown216 ev.preventDefault();217 this.movein(1);218 break;219 default:220 break;221 }222 }223 })224 this.select.addEventListener('focus',(ev)=>{225 ev.stopPropagation();226 if(!this.isfocus){227 this.checkValidity();228 this.dispatchEvent(new CustomEvent('focus',{229 detail:{230 value:this.value231 }232 }));233 }234 })235 this.options.addEventListener('click',(ev)=>{236 this.focus();237 const item = ev.target.closest('xy-option');238 if(item){239 this.nativeclick = true;240 this.value = item.value;241 }242 })243 this.options.addEventListener('close',(ev)=>{244 if(this.search){245 this.select.readonly = true;246 this.select.value = this.$text;247 //this.value = this.$value;248 this.nodes = [...this.querySelectorAll(`xy-option:not([disabled])`)];249 this.filter.textContent = '';250 this.empty = false;251 }252 const place = this.querySelector(`xy-option[focusin]`);253 const current = this.querySelector(`xy-option[selected]`);254 if(place){255 place.focusin = false;256 }257 if(current){258 current.focusin = true;259 this.focusIndex = this.nodes.indexOf(current);260 }else{261 this.focusIndex = -1;262 }263 })264 this.options.addEventListener('open',(ev)=>{265 if(this.search){266 this.select.value = '';267 this.select.readonly = false;268 this.focus();269 }270 })271 if(this.search){272 this.filter = this.shadowRoot.getElementById('filter');273 this.select.addEventListener('input',(ev)=>{274 const value = this.select.value.trim();275 if(value===""){276 this.nodes = [...this.querySelectorAll(`xy-option:not([disabled])`)];277 this.filter.textContent = '';278 }else{279 this.nodes = [...this.querySelectorAll(`xy-option[key*="${value}" i]:not([disabled])`)];280 this.filter.textContent = `281 :host([search]) ::slotted(xy-option:not([key*="${value}" i]))282 {283 display:none;284 }285 `286 }287 const place = this.querySelector(`xy-option[focusin]`);288 if(place){289 place.focusin = false;290 }291 if(this.nodes[0]){292 this.nodes[0].focusin = true;293 this.empty = false;294 }else{295 this.empty = true;296 }297 this.focusIndex = 0;298 })299 this.select.addEventListener('submit',(ev)=>{300 if(!this.options.open){301 this.options.open = true;302 }else{303 const item = this.nodes[this.focusIndex];304 this.nativeclick = true;305 if(item){306 this.value = item.value;307 }else{308 this.value = this.$value;309 this.options.open = false;310 }311 }312 })313 }else{314 this.addEventListener('click',(ev)=>{315 if(!this.options.open){316 const item = this.nodes[this.focusIndex];317 if(item){318 this.nativeclick = true;319 this.value = item.value;320 }321 }322 })323 }324 this.select.addEventListener('blur',(ev)=>{325 ev.stopPropagation();326 if(getComputedStyle(this.root).zIndex==2){327 this.isfocus = true;328 }else{329 this.isfocus = false;330 this.dispatchEvent(new CustomEvent('blur',{331 detail:{332 value:this.value333 }334 }));335 }336 })337 this.slots.addEventListener('slotchange', () => {338 this.nodes = [...this.querySelectorAll(`xy-option:not([disabled])`)];339 if (!this.defaultvalue) {340 this.value = '';341 } else {342 this.value = this.defaultvalue;343 }344 this.init = true;345 });346 }347 348 get defaultvalue() {349 return this.getAttribute('defaultvalue')||'';350 }351 get value() {352 return this.$value||'';353 }354 get text() {355 return this.$text||this.placeholder;356 }357 get name() {358 return this.getAttribute('name');359 }360 get empty() {361 return this.getAttribute('empty')!==null;362 }363 get type() {364 return this.getAttribute('type');365 }366 get validity() {367 return this.required?this.value!=='':true;368 }369 get novalidate() {370 return this.getAttribute('novalidate')!==null;371 }372 get errortips() {373 return this.getAttribute('errortips')||'请选择一项';374 }375 get required() {376 return this.getAttribute('required')!==null;377 }378 get invalid() {379 return this.getAttribute('invalid')!==null;380 }381 get disabled() {382 return this.getAttribute('disabled')!==null;383 }384 get search() {385 return this.getAttribute('search')!==null;386 }387 get placeholder() {388 return this.getAttribute('placeholder')||'请选择';389 }390 set required(value) {391 if(value===null||value===false){392 this.removeAttribute('required');393 }else{394 this.setAttribute('required', '');395 }396 }397 set invalid(value) {398 if(value===null||value===false){399 this.removeAttribute('invalid');400 }else{401 this.setAttribute('invalid', '');402 }403 }404 set novalidate(value) {405 if(value===null||value===false){406 this.removeAttribute('novalidate');407 }else{408 this.setAttribute('novalidate', '');409 }410 }411 set disabled(value) {412 if (value === null || value === false) {413 this.removeAttribute('disabled');414 } else {415 this.setAttribute('disabled', '');416 }417 }418 set empty(value) {419 if (value === null || value === false) {420 this.removeAttribute('empty');421 } else {422 this.setAttribute('empty', '');423 }424 }425 set defaultvalue(value){426 this.setAttribute('defaultvalue', value);427 }428 set value(value) {429 if(value === ''){430 this.$value = '';431 this.$text = this.placeholder;432 if(this.focusIndex>=0){433 const current = this.nodes[this.focusIndex];434 if(current){435 this.focusIndex = -1;436 current.selected = false;437 current.focusin = false;438 }439 }440 if(this.search){441 this.select.placeholder = this.placeholder;442 this.select.value = '';443 }else{444 this.txt.textContent = this.placeholder;445 }446 return447 }448 if (value !== this.value) {449 this.$value = value;450 const pre = this.querySelector(`xy-option[selected]`);451 if(pre){452 pre.selected = false;453 pre.focusin = false;454 }455 const cur = this.querySelector(`xy-option[value="${value}"]`)||this.querySelector(`xy-option`);456 this.focusIndex = this.nodes.indexOf(cur);457 cur.selected = true;458 cur.focusin = true;459 this.$text = cur.textContent;460 if(this.search){461 this.select.placeholder = this.$text;462 this.select.value = this.$text;463 }else{464 this.txt.textContent = this.$text;465 }466 if(this.nativeclick){467 this.nativeclick = false;468 this.checkValidity();469 this.dispatchEvent(new CustomEvent('change', {470 detail: {471 value: value,472 text: this.$text473 }474 }));475 }476 }477 this.options.open = false;478 }479 attributeChangedCallback(name, oldValue, newValue) {480 if (name == 'disabled' && this.select) {481 if (newValue != null) {482 this.select.disabled = true;483 } else {484 this.select.disabled = false;485 }486 }487 }488}489if (!customElements.get('xy-select')) {490 customElements.define('xy-select', XySelect);...

Full Screen

Full Screen

awc-dropdown.js

Source:awc-dropdown.js Github

copy

Full Screen

1"use strict";2import '../awc-option/awc-option'3import '../awc-popover/awc-popover';4import html from './awc-dropdown.html';5export default class AwcDropdown extends HTMLElement {6 static get observedAttributes() {7 return ['value', 'disabled', 'type'];8 }9 constructor() {10 super();11 this._render();12 }13 get defaultvalue() {14 return this.getAttribute('defaultvalue') || '';15 }16 set defaultvalue(value) {17 this.setAttribute('defaultvalue', value);18 }19 get value() {20 return this._value || '';21 }22 set value(value) {23 if (value === '') {24 this._value = '';25 this._text = this.placeholder;26 if (this._focusIndex >= 0) {27 const current = this.nodes[this._focusIndex];28 if (current) {29 this._focusIndex = -1;30 current.selected = false;31 current.focusin = false;32 }33 }34 if (this.search) {35 this.selectEl.placeholder = this.placeholder;36 this.selectEl.value = '';37 } else {38 this.valueEl.textContent = this.placeholder;39 }40 return41 }42 if (value !== this.value) {43 this._value = value;44 const pre = this.querySelector(`awc-option[selected]`);45 if (pre) {46 pre.selected = false;47 pre.focusin = false;48 }49 const cur = this.querySelector(`awc-option[value="${value}"]`) || this.querySelector(`awc-option`);50 this._focusIndex = this.nodes.indexOf(cur);51 cur.selected = true;52 cur.focusin = true;53 this._text = cur.textContent;54 if (this.search) {55 this.selectEl.placeholder = this._text;56 this.selectEl.value = this._text;57 } else {58 this.valueEl.textContent = this._text;59 }60 if (this._nativeclick) {61 this._nativeclick = false;62 this.dispatchEvent(63 new CustomEvent('change', {64 detail: {65 value: value,66 text: this._text,67 },68 })69 );70 }71 }72 this.optionsEl.open = false;73 }74 get empty() {75 return this.getAttribute('empty') !== null;76 }77 set empty(value) {78 if (value === null || value === false) {79 this.removeAttribute('empty');80 } else {81 this.setAttribute('empty', '');82 }83 }84 get disabled() {85 return this.getAttribute('disabled') !== null;86 }87 set disabled(value) {88 if (value === null || value === false) {89 this.removeAttribute('disabled');90 } else {91 this.setAttribute('disabled', '');92 }93 }94 get type() {95 return this.getAttribute('type');96 }97 set type(value) {98 this.setAttribute('type', value);99 }100 get search() {101 return this.getAttribute('search') !== null;102 }103 set search(value) {104 this.setAttribute("search", value);105 }106 get placeholder() {107 return this.getAttribute('placeholder') || 'Please Select Item';108 }109 set placeholder(value) {110 this.setAttribute("placeholder", value);111 }112 focus() {113 this.selectEl.focus();114 }115 reset() {116 this.value = this.defaultvalue;117 }118 attributeChangedCallback(name, oldValue, newValue) {119 if (name == 'disabled' && this.selectEl) {120 if (newValue != null) {121 this.selectEl.disabled = true;122 } else {123 this.selectEl.disabled = false;124 }125 }126 }127 connectedCallback() {128 this.rootEl = this.shadowRoot.getElementById('root');129 this.optionsEl = this.shadowRoot.getElementById('options');130 this.slotEl = this.shadowRoot.getElementById('slot');131 this.valueEl = this.shadowRoot.getElementById('value');132 const selectInputEl = this.shadowRoot.getElementById('selectInput');133 const selectButtonEl = this.shadowRoot.getElementById('selectButton');134 if (this.search) {135 this.selectEl = selectInputEl;136 selectInputEl.style.display = "flex";137 selectButtonEl.style.display = "none";138 } else {139 this.selectEl = selectButtonEl;140 selectInputEl.style.display = "none";141 selectButtonEl.style.display = "flex";142 }143 this._focusIndex = -1;144 this.disabled = this.disabled;145 this.selectEl.disabled = this.disabled;146 this.selectEl.setAttribute('type', this.type);147 this.addEventListener('keydown', (ev) => {148 if (this.optionsEl.open) {149 switch (ev.key) {150 case 'Tab':151 ev.preventDefault();152 break;153 case 'ArrowUp':154 ev.preventDefault();155 this._move(-1)156 break;157 case 'ArrowDown':158 ev.preventDefault();159 this._move(1)160 break;161 case 'Escape':162 ev.preventDefault();163 this.optionsEl.open = false;164 break;165 default:166 break;167 }168 } else {169 switch (ev.key) {170 case 'ArrowUp':171 ev.preventDefault();172 this._movein(-1);173 break;174 case 'ArrowDown':175 ev.preventDefault();176 this._movein(1);177 break;178 default:179 break;180 }181 }182 })183 this.selectEl.addEventListener('focus', (ev) => {184 ev.stopPropagation();185 if (!this.isfocus) {186 this.dispatchEvent(187 new CustomEvent('focus', {188 detail: {189 value: this.value,190 },191 })192 );193 }194 })195 this.optionsEl.addEventListener('click', (ev) => {196 this.focus();197 const item = ev.target.closest('awc-option');198 if (item) {199 this._nativeclick = true;200 this.value = item.value;201 }202 })203 this.optionsEl.addEventListener('close', (ev) => {204 if (this.search) {205 this.selectEl.readonly = true;206 this.selectEl.value = this._text;207 this.nodes = [...this.querySelectorAll(`awc-option:not([disabled])`)];208 this.filterEl.textContent = '';209 this.empty = false;210 }211 const place = this.querySelector(`awc-option[focusin]`);212 const current = this.querySelector(`awc-option[selected]`);213 if (place) {214 place.focusin = false;215 }216 if (current) {217 current.focusin = true;218 this._focusIndex = this.nodes.indexOf(current);219 } else {220 this._focusIndex = -1;221 }222 })223 this.optionsEl.addEventListener('open', (ev) => {224 if (this.search) {225 this.selectEl.value = '';226 this.selectEl.readonly = false;227 this.focus();228 }229 })230 if (this.search) {231 this.filterEl = this.shadowRoot.getElementById('filter');232 this.selectEl.addEventListener('input', (ev) => {233 const value = this.selectEl.value.trim();234 if (value === '') {235 this.nodes = [...this.querySelectorAll(`awc-option:not([disabled])`)];236 this.filterEl.textContent = '';237 } else {238 this.nodes = [239 ...this.querySelectorAll(240 `awc-option[value*="${value}" i]:not([disabled])`241 ),242 ];243 this.filterEl.textContent = `244 :host([search]) ::slotted(awc-option:not([value*="${value}" i]))245 {246 display:none;247 }248 `;249 }250 const place = this.querySelector(`awc-option[focusin]`);251 if (place) {252 place.focusin = false;253 }254 if (this.nodes[0]) {255 this.nodes[0].focusin = true;256 this.empty = false;257 } else {258 this.empty = true;259 }260 this._focusIndex = 0;261 })262 this.selectEl.addEventListener('submit', (ev) => {263 if (!this.optionsEl.open) {264 this.optionsEl.open = true;265 } else {266 const item = this.nodes[this._focusIndex];267 this._nativeclick = true;268 if (item) {269 this.value = item.value;270 } else {271 this.value = this._value;272 this.optionsEl.open = false;273 }274 }275 })276 } else {277 this.addEventListener('click', (ev) => {278 if (!this.optionsEl.open) {279 const item = this.nodes[this._focusIndex];280 if (item) {281 this._nativeclick = true;282 this.value = item.value;283 }284 }285 })286 }287 this.selectEl.addEventListener('blur', (ev) => {288 ev.stopPropagation();289 if (getComputedStyle(this.rootEl).zIndex == 2) {290 this.isfocus = true;291 } else {292 this.isfocus = false;293 this.dispatchEvent(294 new CustomEvent('blur', {295 detail: {296 value: this.value,297 },298 })299 )300 }301 })302 this.slotEl.addEventListener('slotchange', () => {303 this.nodes = [...this.querySelectorAll(`awc-option:not([disabled])`)];304 if (!this.defaultvalue) {305 this.value = '';306 } else {307 this.value = this.defaultvalue;308 }309 })310 }311 _move(dir) {312 const pre = this.nodes[this._focusIndex];313 const focusIndex = dir + this._focusIndex;314 const current = this.nodes[focusIndex];315 if (current) {316 if (pre) {317 pre.focusin = false;318 }319 current.focusin = true;320 this._focusIndex = focusIndex;321 }322 }323 _movein(dir) {324 this._focusIndex = dir + this._focusIndex;325 if (this._focusIndex < 0) {326 this._focusIndex = this.nodes.length - 1;327 }328 if (this._focusIndex === this.nodes.length) {329 this._focusIndex = 0;330 }331 this._nativeclick = true;332 this.value = this.nodes[this._focusIndex].value;333 }334 _render() {335 const shadowRoot = this.attachShadow({ mode: 'open' });336 shadowRoot.innerHTML = html;337 }338}339if (!customElements.get('awc-dropdown')) {340 customElements.define('awc-dropdown', AwcDropdown)...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

1import './components/xy-popover.js';2import MARKER from '../picker/components/icon';3import { handleCSSVariables, setDefaultColorCache, getDefaultColorCache } from './utils/main';4const ColorCollections = ['#ff1300','#EC7878','#9C27B0','#673AB7','#3F51B5','#0070FF','#03A9F4','#00BCD4','#4CAF50','#8BC34A','#CDDC39','#FFE500','#FFBF00','#FF9800','#795548','#9E9E9E','#5A5A5A','#FFF'];5class ColorPlugin extends HTMLElement {6 static get observedAttributes() { return ['disabled','dir'] }7 constructor(options) {8 super();9 const shadowRoot = this.attachShadow({ mode: 'open' });10 this.colorCollections = options.colorCollections || ColorCollections;11 this.onColorPicked = options.onColorPicked;12 this.defaulColor = handleCSSVariables(options.defaultColor || this.colorCollections[0]);13 this.pluginType = options.type;14 shadowRoot.innerHTML = `15 <style>16 :host{17 display:inline-block;18 width:15px;19 font-size:14px;20 border: none;21 }22 :host([block]){23 display:block;24 }25 :host([disabled]){26 pointer-events:none;27 }28 29 :host(:focus-within) xy-popover,:host(:hover) xy-popover{ 30 z-index: 2;31 }32 xy-popover{33 width:100%;34 height:35px;35 margin-right: 3px;36 }37 xy-popover:hover {38 border-radius: 0 5px 5px 0;39 background: rgba(203, 203, 203, 0.49);40 }41 .color-btn {42 border: 1px solid #cab9b9;43 margin: 18px 2px 2px 2px;44 width: 6px;45 height: 6px;46 opacity: 0.9;47 padding: 1px 0 1px 0;48 color: var(--themeColor, #42b983);49 background: var(--themeColor, #42b983);50 font-weight: bolder;51 border-radius: 2px;52 }53 .color-btn:hover {54 opacity: 1;55 z-index: auto;56 }57 xy-popover{58 display:block;59 }60 xy-popcon{61 position: fixed;62 min-width:100%;63 }64 .pop-footer{65 display:flex;66 justify-content:flex-end;67 padding:0 .8em .8em;68 }69 .pop-footer xy-button{70 font-size: .8em;71 margin-left: .8em;72 }73 .color-btn::before{74 content:'';75 position:absolute;76 left:5px;77 top:5px;78 right:5px;79 bottom:5px;80 z-index:-1;81 background: linear-gradient(45deg, #ddd 25%,transparent 0,transparent 75%,#ddd 0 ), linear-gradient(45deg, #ddd 25%, transparent 0, transparent 75%, #ddd 0);82 background-position: 0 0,5px 5px;83 background-size: 10px 10px;84 }85 .color-sign {86 max-width: 220px;87 padding: 10px;88 display:grid;89 cursor: default;90 grid-template-columns: repeat(auto-fit, minmax(15px, 1fr));91 grid-gap: 10px; 92 }93 .color-sign>button {94 position: relative;95 width: 16px;96 height: 16px;97 border-radius: 6px;98 border: 1px solid #b8b9b49e;99 outline: 0;100 opacity: 0.9;101 }102 .color-sign>button:hover {103 cursor: pointer;104 opacity: 1;105 }106 .color-section {107 display: flex;108 align-items: center;109 justify-content: center;110 }111 .color-fire-btn {112 font-size: 17px;113 font-weight: bold;114 height: 28px;115 padding-top: 8px;116 padding-right: 1px;117 margin-left: 3px;118 text-shadow: 2px 0 0 #cab9b9;119 padding-left: 3px;120 border-radius: 5px 0 0 5px;121 }122 .color-fire-btn:hover {123 font-size: 17px;124 font-weight: bold;125 text-shadow: 2px 0 0 #cab9b9;126 background: rgba(203, 203, 203, 0.49);127 border-radius: 5px 0 0 5px;128 }129 .color-fire-btn-text {130 margin-right: 2px;131 }132 </style>133 <section class="color-section">134 <div class="color-fire-btn" id="color-fire-btn">135 ${this.pluginType === 'marker' ? MARKER : '<div class="color-fire-btn-text">A</div>' }136 </div>137 <xy-popover id="popover" ${this.dir ? "dir='" + this.dir + "'" : ""}>138 <xy-button class="color-btn" id="color-btn" ${this.disabled ? "disabled" : ""}>_</xy-button>139 <xy-popcon id="popcon">140 <div class="color-sign" id="colors">${this.colorCollections.map(el => '<button style="background-color:' + el + '" data-color=' + el + '></button>').join('')}</div>141 </xy-popcon>142 </xy-popover>143 </section>`;144 }145 focus() {146 this.colorBtn.focus();147 }148 connectedCallback() {149 this.popover = this.shadowRoot.getElementById('popover');150 this.popcon = this.shadowRoot.getElementById('popcon');151 this.colorBtn = this.shadowRoot.getElementById('color-btn');152 this.colors = this.shadowRoot.getElementById('colors');153 this.colors.addEventListener('click',(ev) => {154 const item = ev.target.closest('button');155 if (item) {156 this.nativeclick = true;157 this.value = item.dataset.color;158 this.value = handleCSSVariables(this.value);159 this.onColorPicked(this.value);160 }161 });162 this.value = this.defaultvalue;163 }164 get defaultvalue() {165 return this.defaulColor;166 }167 get value() {168 return this.$value;169 }170 get type() {171 return this.getAttribute('type');172 }173 get disabled() {174 return this.getAttribute('disabled') !== null;175 }176 get dir() {177 return this.getAttribute('dir');178 }179 set dir(value){180 this.setAttribute('dir', value);181 }182 set disabled(value) {183 if (value === null || value === false) {184 this.removeAttribute('disabled');185 } else {186 this.setAttribute('disabled', '');187 }188 }189 set defaultvalue(value){190 this.setAttribute('defaultvalue', value);191 }192 set value(value) {193 this.$value = value;194 this.colorBtn.style.setProperty(195 '--themeColor',196 this.nativeclick197 ? setDefaultColorCache(value, this.pluginType)198 : getDefaultColorCache(value, this.pluginType)199 );200 if (this.nativeclick) {201 this.nativeclick = false;202 this.dispatchEvent(new CustomEvent('change', {203 detail: {204 value: this.value,205 }206 }));207 } else {208 if (this.colorPane) {209 this.colorPane.value = this.value;210 } else {211 this.defaultvalue = this.value;212 }213 }214 }215 attributeChangedCallback(name, oldValue, newValue) {216 if (name == 'disabled' && this.colorBtn) {217 if (newValue != null) {218 this.colorBtn.setAttribute('disabled', 'disabled');219 } else {220 this.colorBtn.removeAttribute('disabled');221 }222 }223 if (name == 'dir' && this.popover) {224 if (newValue != null) {225 this.popover.dir = newValue;226 }227 }228 }229}230if (!customElements.get('xy-color-picker')) {231 customElements.define('xy-color-picker', ColorPlugin);232}233export {234 ColorPlugin,...

Full Screen

Full Screen

pointerMock.js

Source:pointerMock.js Github

copy

Full Screen

...89 clickEvent.misc.requestAnimationFrame = function(callback) { requestAnimationFrameCallback = callback; };90 }91 triggerEvent('dxpointerup');92 requestAnimationFrameCallback();93 this.nativeClick();94 return this;95 },96 cancel: function() {97 triggerEvent('dxpointercancel');98 return this;99 },100 click: function(clickOnly) {101 if(!clickOnly) {102 this.down();103 this.up();104 } else {105 triggerEvent('dxclick');106 }107 return this;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { remote } = require('webdriverio');2(async () => {3 const browser = await remote({4 capabilities: {5 }6 })7 const el = await browser.$('~myButton')8 await el.nativeClick()9 await browser.deleteSession()10})().catch((e) => console.error(e))

Full Screen

Using AI Code Generation

copy

Full Screen

1driver.init({2}).then(function() {3 return driver.elementByAccessibilityId('Buttons').click();4}).then(function() {5 return driver.elementByAccessibilityId('UICatalog').click();6}).then(function() {7 return driver.elementByAccessibilityId('Buttons').click();8}).then(function() {9 return driver.elementByAccessibilityId('OK, Great!').click();10}).then(function() {11 return driver.elementByAccessibilityId('UICatalog').click();12}).then(function() {13 return driver.elementByAccessibilityId('Buttons').click();14}).then(function() {15 return driver.elementByAccessibilityId('OK, Great!').click();16}).then(function() {17 return driver.elementByAccessibilityId('UICatalog').click();18}).then(function() {19 return driver.elementByAccessibilityId('Buttons').click();20}).then(function() {21 return driver.elementByAccessibilityId('OK, Great!').click();22}).then(function() {23 return driver.elementByAccessibilityId('UICatalog').click();24}).then(function() {25 return driver.elementByAccessibilityId('Buttons').click();26}).then(function() {27 return driver.elementByAccessibilityId('OK, Great!').click();28}).then(function() {29 return driver.elementByAccessibilityId('UICatalog').click();30}).then(function() {31 return driver.elementByAccessibilityId('Buttons').click();32}).then(function() {33 return driver.elementByAccessibilityId('OK, Great!').click();34}).then(function() {35 return driver.elementByAccessibilityId('UICatalog').click();36}).then(function() {37 return driver.elementByAccessibilityId('Buttons').click();38}).then(function() {39 return driver.elementByAccessibilityId('OK, Great!').click();40}).then(function() {41 return driver.elementByAccessibilityId('UICatalog').click();42}).then(function() {43 return driver.elementByAccessibilityId('Buttons').click();44}).then(function() {45 return driver.elementByAccessibilityId('OK, Great!').click();46}).then(function() {

Full Screen

Using AI Code Generation

copy

Full Screen

1driver.init(desired).then(function() {2 driver.elementByAccessibilityId('test').then(function(el) {3 driver.nativeClick(el);4 });5});6driver.init(desired).then(function() {7 driver.elementByAccessibilityId('test').then(function(el) {8 driver.touchPerform([{9 options: {10 }11 }]);12 });13});14driver.init(desired).then(function() {15 driver.elementByAccessibilityId('test').then(function(el) {16 driver.performTouchAction({17 options: {18 }19 });20 });21});

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2const chai = require('chai');3const chaiAsPromised = require('chai-as-promised');4chai.use(chaiAsPromised);5const should = chai.should();6const assert = chai.assert;7describe('Test nativeClick', function () {8 this.timeout(300000);9 let driver;10 before(async () => {11 const desiredCaps = {12 };13 await driver.init(desiredCaps);14 });15 after(async () => {16 await driver.quit();17 });18 it('should click by native click', async () => {19 const el = await driver.elementByAccessibilityId('button');20 await driver.nativeClick(el);21 });22});23[debug] [MJSONWP] Calling AppiumDriver.findElOrEls() with args: ["accessibility id","button","1",null,null,"f5aa5b5a-8d18-4c7b-9e9e-9a8c8d0f7b4c"]

Full Screen

Using AI Code Generation

copy

Full Screen

1describe('Test native click', () => {2 it('should click on the native element', async () => {3 await browser.setImplicitWaitTimeout(6000);4 await browser.saveScreenshot('screenshot.png');5 await browser.pause(3000);6 await browser.saveScreenshot('screenshot2.png');7 await browser.pause(3000);8 await browser.saveScreenshot('screenshot3.png');9 await browser.pause(3000);10 await browser.saveScreenshot('screenshot4.png');11 await browser.pause(3000);12 await browser.saveScreenshot('screenshot5.png');13 await browser.pause(3000);14 await browser.saveScreenshot('screenshot6.png');15 await browser.pause(3000);16 await browser.saveScreenshot('screenshot7.png');17 await browser.pause(3000);18 await browser.saveScreenshot('screenshot8.png');19 await browser.pause(3000);20 await browser.saveScreenshot('screenshot9.png');21 await browser.pause(3000);22 await browser.saveScreenshot('screenshot10.png');23 await browser.pause(3000);24 await browser.saveScreenshot('screenshot11.png');25 await browser.pause(3000);26 await browser.saveScreenshot('screenshot12.png');27 await browser.pause(3000);28 await browser.saveScreenshot('screenshot13.png');29 await browser.pause(3000);30 await browser.saveScreenshot('screenshot14.png');31 await browser.pause(3000);32 await browser.saveScreenshot('screenshot15.png');33 await browser.pause(3000);34 await browser.saveScreenshot('screenshot16.png');35 await browser.pause(3000);36 await browser.saveScreenshot('screenshot17.png');37 await browser.pause(3000);38 await browser.saveScreenshot('screenshot18.png');39 await browser.pause(3000);40 await browser.saveScreenshot('screenshot19.png');41 await browser.pause(3000);42 await browser.saveScreenshot('screenshot20.png');43 await browser.pause(3000);44 await browser.saveScreenshot('screenshot21.png');45 await browser.pause(3000);46 await browser.saveScreenshot('screenshot22.png');47 await browser.pause(3000);48 await browser.saveScreenshot('screenshot23.png');49 await browser.pause(3000);50 await browser.saveScreenshot('screenshot24.png');51 await browser.pause(3000

Full Screen

Using AI Code Generation

copy

Full Screen

1const wdio = require('webdriverio');2const options = {3 desiredCapabilities: {4 }5};6async function main() {7 const client = await wdio.remote(options);8 const button = await client.$('buttonName');9 await client.nativeClick(button.elementId);10 await client.deleteSession();11}12main();13{14 "scripts": {15 },16 "dependencies": {17 }18}

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium Xcuitest Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Sign up Free
_

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful