Best JavaScript code snippet using playwright-internal
app.js
Source: app.js
1var types = [ //è£å種é¡é¸é
2 {val:'wp01' ,name:'å'},3 {val:'wp02' ,name:'æ§'},4 {val:'wp03' ,name:'æ§'},5 {val:'wp04' ,name:'å¼'},6 {val:'wp05' ,name:'æ'},7 {val:'wp06' ,name:'å°å'},8 {val:'wp07' ,name:'ååé¢'},9 {val:'wp08' ,name:'ç¸å½'},10 {val:'sh01' ,name:'é ç'},11 {val:'sh02' ,name:'帽å'},12 {val:'sh03' ,name:'é§ç²'},13 {val:'sh04' ,name:'è¡£æ'},14 {val:'sh05' ,name:'è¢'},15 {val:'sh06' ,name:'é´'},16 {val:'sh07' ,name:'éå'},17 {val:'sh08' ,name:'ç¾'},18 {val:'md01' ,name:'è¥æ°´'},19 {val:'fd01' ,name:'æç'},20 {val:'sp01' ,name:'飾å'},21 {val:'pa01' ,name:'寵ç©æè£'},22 {val:'pa02' ,name:'寵ç©è£ç²'},23 {val:'pa03' ,name:'寵ç©é
å'},24 {val:'pa04' ,name:'寵ç©æ¶ç³'},25 {val:'pa05' ,name:'寵ç©é£¾å'}26];27var product;28var useMts = [];29var readyToSave = false,30 orderItem;31$(function(){32 $('#prd_match').addOption('all', 'ææè£å');33 for(var i=0;i<types.length;i++){ //ç¢çè£å種é¡é¸é
æ¸
å®34 $('#type,#prd_match').addOption(types[i].val, types[i].name);35 }36 $('#prd_match').val('all');37 var mt_match = $('#mt_match');38 var option;39 for(var i=0;i<materials.length;i++){40 mt_match.addOption(materials[i].code, materials[i].name);41 option = mt_match.find('option:last');42 if(/w[0-9]/.test(materials[i].code))43 option.addClass('wood');44 if(/m[0-9]/.test(materials[i].code))45 option.addClass('mineral');46 if(/c[0-9]/.test(materials[i].code))47 option.addClass('cloth');48 if(/f[0-9]/.test(materials[i].code))49 option.addClass('flower');50 if(/g[0-9]/.test(materials[i].code))51 option.addClass('food');52 if(/s[0-9]/.test(materials[i].code))53 option.addClass('special');54 }55 mt_match.val('m101');56 57 $('#mt_match').val('m101');58 $('.plus_list').data('mt_list', []);59 $('.minus_list').data('mt_list', []);60 $('#type')61 .val('wp01')62 .find('option')63 .slice(0,8)64 .css('background-color', '#708090')65 .end()66 .slice(8,16)67 .css('background-color', '#80A0C0')68 .end()69 .slice(16,19)70 .css('background-color', '#90C0F0');71 genEqpList($('#type').val()); //ç¢çè£åé
ç®æ¸
å®72 noticeBoard.init(); //030873 74 $('#type').change(function(){ //ç±è£å種é¡é¸é
ç¢çè£åé
ç®æ¸
å®75 //var index = $(this)[0].selectedIndex;76 var index = $(this).selectedOptions().first().index();77 var tc;78 clearTable();79 useMts = [];80 81 tc = (function(){82 if(index >= 8)83 return (index >= 16)? '#90C0F0' : '80A0C0';84 else85 return '#708090';86 }());87 88 $(this).add('#product').css('background-color', tc);89 genEqpList($(this).val());90 });91 92 $('#product').change(function(){ //ç±ä½¿ç¨è
é¸æçè£å顯示åºæéææ93 clearTable();94 setProduct();95 });96 97 $('#slots').keydown(function(event){ //輸å
¥å®æ¸éå¾ï¼æä¸Enterè¦çºæä¸è¨ç®æé98 if(event.which == 13) //Enter99 $('#calc').click();100 });101 $('.sbtn').click(function(){102 if(readyToSave)103 noticeBoard.save(orderItem);104 });105 $('.plus_btn').click(function(){ // æ£å表åå å
¥æ¸
å®106 var mt = findMaterial($('#mt_match').val());107 var mt_plist = $('.plus_list').data('mt_list');108 var mt_mlist = $('.minus_list').data('mt_list');109 var dunplicate = false;110 for(var i=0;i<mt_plist.length;i++){111 if(mt_plist[i].code == mt.code)112 dunplicate = true;113 }114 for(var i=0;i<mt_mlist.length;i++){115 if(mt_mlist[i].code == mt.code)116 dunplicate = true;117 }118 if(!dunplicate && (mt_plist.length < 5)){119 mt_plist.push(mt);120 $('.plus_list').data('mt_list', mt_plist);121 $('.plus_list').append('<span class="p_list_item">' + mt.name + '</sapn>');122 }123 });124 $('.minus_btn').click(function(){ // è² å表åå å
¥æ¸
å®125 var mt = findMaterial($('#mt_match').val());126 var mt_plist = $('.plus_list').data('mt_list');127 var mt_mlist = $('.minus_list').data('mt_list');128 var dunplicate = false;129 for(var i=0;i<mt_plist.length;i++){130 if(mt_plist[i].code == mt.code)131 dunplicate = true;132 }133 for(var i=0;i<mt_mlist.length;i++){134 if(mt_mlist[i].code == mt.code)135 dunplicate = true;136 }137 if(!dunplicate && (mt_mlist.length < 5)){138 mt_mlist.push(mt);139 $('.minus_list').data('mt_list', mt_mlist);140 $('.minus_list').append('<span class="m_list_item">' + mt.name + '</sapn>');141 }142 });143 $('.plus_list').on('click', 'span', function(){144 var mt_plist = $('.plus_list').data('mt_list');145 var selected_mt = $(this).text();146 var new_list = [];147 for(var i=0;i<mt_plist.length;i++){148 if(mt_plist[i].name != selected_mt)149 new_list.push(mt_plist[i]);150 }151 $('.plus_list').data('mt_list', new_list);152 $(this).remove();153 });154 $('.minus_list').on('click', 'span', function(){155 var mt_mlist = $('.minus_list').data('mt_list');156 var selected_mt = $(this).text();157 var new_list = [];158 for(var i=0;i<mt_mlist.length;i++){159 if(mt_mlist[i].name != selected_mt)160 new_list.push(mt_mlist[i]);161 }162 $('.minus_list').data('mt_list', new_list);163 $(this).remove();164 });165 $('.mbtn').click(function(){166 // 167 var type = $('#prd_match').val();168 var mt = $('#mt_match').val();169 var mt_plist = $('.plus_list').data('mt_list');170 var mt_mlist = $('.minus_list').data('mt_list');171 var count = 0;172 var first_val = '';173 var plus_compare = true;174 var minus_compare = false;175 var prd, _type, compare_result;176 $('.match, .nomatch').hide();177 $('#match_list').removeOption(/./); //æ¸
é¤è£åé
ç®æ¸
å®178 if(type == 'all'){179 for(var i=0;i<types.length;i++){180 _type = types[i].val;181 for(var j=0;j<recipe[_type].length;j++){182 prd = recipe[_type][j];183 plus_compare = true;184 minus_compare = false;185 for(var pl=0; pl<mt_plist.length; pl++){186 plus_compare &= containsMaterial(prd, mt_plist[pl].code);187 }188 for(var ml=0;ml<mt_mlist.length;ml++){189 minus_compare |= containsMaterial(prd, mt_mlist[ml].code);190 }191 compare_result = (!minus_compare) && plus_compare;192 if(compare_result){193 $('#match_list').addOption((_type+'_'+j), ('Lv.' + prd.level + ' ' + prd.name));194 if(first_val == '')195 first_val = (_type+'_'+j);196 count++;197 }198 }199 }200 }201 else{202 for(var j=0;j<recipe[type].length;j++){203 prd = recipe[type][j];204 plus_compare = true;205 minus_compare = false;206 for(var pl=0; pl<mt_plist.length; pl++){207 plus_compare &= containsMaterial(prd, mt_plist[pl].code);208 }209 for(var ml=0;ml<mt_mlist.length;ml++){210 minus_compare |= containsMaterial(prd, mt_mlist[ml].code);211 }212 compare_result = (!minus_compare) && plus_compare;213 if(compare_result){214 $('#match_list').addOption((type+'_'+j), ('Lv.' + prd.level + ' ' + prd.name));215 if(first_val == '')216 first_val = (type+'_'+j);217 count++;218 }219 }220 }221 if(count > 0){222 $('.match .count').html('總å
±æ¾å° ' + count + ' 種è£åã')223 $('.match').show();224 $('#match_list').val(first_val).change();225 }226 else227 $('.nomatch').html('æ¾ä¸å°åé©çè£åï¼ï¼').show();228 });229 $('#match_list').change(function(){230 var type = $(this).val().split('_')[0];231 var index = $(this).val().split('_')[1];232 $('#type').val(type);233 genEqpList(type);234 $('#product').val(index).change();235 });236 $('#match_list').click(function(){237 $(this).change();238 });239 $('.close').on('click', function(){240 var noticeId = parseInt($(this).data('nid'), 10)241 div = $(this).closest('div');242 noticeBoard.delete(noticeId, div);243 });244 $('#noticeBoard').on('click', '.mt-item', function(){245 noticeBoard.mark(this);246 });247 248 $('#calc').click(function(){249 var useSlots = 0; //總å
±æéç©ºæ ¼æ¸é250 var result = '';251 var j;252 var str, idstr;253 var dt = new Date();254 var items; //å®ä¸æææ¸é255 var type = $('#type').val();256 var method = $('input[name=method]:checked').val(); //è®å使ç¨è
æéä¹è¨ç®æ¹å¼257 // method1 : ä¾ä½¿ç¨è
å¡«å
¥ä¹ç©ºæ ¼æ¸éè¨ç®åºå¯çç¢ä¹ç©åæ¸é258 // method2 : ä¾ä½¿ç¨è
å¡«å
¥ä¹çç¢ç©åæ¸éè¨ç®åºæéä¹ç©ºæ ¼æ¸é259 orderItem = {260 id: noticeBoard.orderItems.idSeed++,261 qty: 0,262 product: product.name, //0308 è¨éç¢åå稱263 mts: [],264 code: product.code, //0825 è¨éç¢å代碼265 type: type //0825 è¨éç¢å種é¡266 };267 readyToSave = false;268 $('.sbtn').hide();269 270 if(method == 'method1'){271 var count = 0; //å¯çç¢ç©åæ¸é272 var slots = parseInt($('#slots').val()); //ç©ºæ ¼æ¸é273 if(useMts.length <= slots){274 while(useSlots <= slots){275 count++;276 useSlots = 0;277 for(var i=0;i<useMts.length;i++){278 useSlots += Math.ceil((product['qty'+(i+1)]*count) / useMts[i].set);279 }280 }281 count--;282 useSlots = 0;283 284 for(var i=0;i<5;i++){285 j = i + 1;286 if(i < useMts.length){287 items = product['qty'+j] * count;288 str = new String(Math.floor(items / useMts[i].set)).fontcolor('blue') + ' ' + useMts[i].unit;289 if((items % useMts[i].set) != 0){290 str += 'å ' + new String(items % useMts[i].set).fontcolor('blue') + ' å';291 }292 useSlots += Math.ceil(items / useMts[i].set);293 294 $('#slt'+j).html(str); //0828 顯示æ¬ä½ç°å295 orderItem.mts.push({296 code: useMts[i].code,297 text: useMts[i].name + ' ' + str298 }); //0825 è¨é使ç¨ææèçµæ¸299 }300 else{301 $('#slt'+j).html(' '); //0828 顯示æ¬ä½ç°å302 orderItem.mts.push({303 code: '',304 text: ' '305 }); //0825 è¨éempty306 }307 }308 if(type == 'wp08'){ // ç¸å½309 str = new String(count).fontcolor('red') + ' 次 / å
± ';310 str += new String(count * product.pcs).fontcolor('blue') + ' å ';311 orderItem.qty = (count * product.pcs); //0308 è¨éçç¢æ¸é(ç¸å½)312 }313 else{314 str = new String(count).fontcolor('red') + ' å ';315 orderItem.qty = count; //0308 è¨éçç¢æ¸é316 }317 318 $('#pqty').html('å¯çç¢ ' + product.name.fontcolor('blue') + ' ' + new String(count).fontcolor('red') + ' å');319 //0828 顯示æ¬ä½ç°å320 $('#uslt').html('å
±éç©åæ¬ ' + new String(useSlots).fontcolor('red') + ' æ ¼'); //0828 顯示æ¬ä½ç°å321 readyToSave = true;322 $('.sbtn').show();323 $('#errMessage').hide();324 }325 else{326 $('#errMessage').html('å¯ä½¿ç¨æ ¼æ¸ä¸è¶³ï¼ï¼').show();327 $('#pqty,#uslt').html(' ');328 }329 }330 else{331 var qty = parseInt($('#slots').val());332 if(type == 'wp08'){333 qty = Math.ceil(qty / product.pcs);334 }335 orderItem.qty = qty; //0308 è¨éçç¢æ¸é336 for(var i=0;i<5;i++){337 j = i+1;338 if(i < useMts.length){339 items = product['qty'+j] * qty;340 useSlots += Math.ceil(items / useMts[i].set);341 342 str = new String(Math.floor(items / useMts[i].set)).fontcolor('blue') + ' ' + useMts[i].unit;343 if((items % useMts[i].set) != 0)344 str += 'å ' + new String(items % useMts[i].set).fontcolor('blue') + ' å';345 346 $('#slt'+j).html(str); //0828 顯示æ¬ä½ç°å347 orderItem.mts.push({348 code: useMts[i].code,349 text: useMts[i].name + ' ' + str350 }); //0825 è¨é使ç¨ææèçµæ¸351 }352 else{353 $('#slt'+j).html(' '); //0828 顯示æ¬ä½ç°å354 orderItem.mts.push({355 code: '',356 text: ' '357 }); //0825 è¨éempty358 }359 }360 361 readyToSave = (qty != 0);362 if(readyToSave){363 $('#pqty').html('å¯çç¢ ' + product.name.fontcolor('blue') + ' ' + new String(qty).fontcolor('red') + ' å');364 //0828 顯示æ¬ä½ç°å365 $('#uslt').html('å
±éç©åæ¬ ' + new String(useSlots).fontcolor('red') + ' æ ¼'); //0828 顯示æ¬ä½ç°å366 $('.sbtn').show();367 $('#errMessage').hide();368 }369 else{370 $('#errMessage').html('çç¢æ¸éçº 0ï¼ï¼').show();371 $('#pqty,#uslt').html(' ');372 }373 }374 });375 376});377function findMaterial(code){ //ç±ææç·¨èæ¥è©¢ææå
§å®¹378 var m;379 for(var i=0;i<materials.length;i++)380 if(materials[i].code == code){381 m = materials[i];382 break;383 }384 return m;385}386function clearTable(){ //æ¸
é¤æ顯示ä¹ææ表387 for(var i=1;i<=5;i++){388 $('#slt'+i).html(' ');389 }390 $('#pqty,#uslt,#errMessage').html(' ');391}392function fillMaterial(product){ //顯示è£åç©åä¹ææ表ï¼ä¸¦è¨éæéä¹ææ393 var mt, tpl;394 395 for(var i=1;i<=5;i++){396 mt = findMaterial(product['mt'+i]); //ç±ææç·¨èæ¥è©¢å°æçææ397 tpl = $('#n_mt'+i);398 if(mt){399 tpl.find('img')400 .attr('src','images/pic/' + mt.code + '.gif')401 .data('code', mt.code)402 .mousedown(function(e){403 $('#mt_match').val($(this).data('code'));404 if(e.which == 1)405 $('.plus_btn').click();406 if(e.which == 3)407 $('.minus_btn').click();408 })409 .contextmenu(function(){return false;});410 tpl.find('.qty').html(product['qty'+i]);411 tpl.find('.name').html('(' + mt.set + ')<br>' + mt.name);412 useMts.push(mt);413 }414 else{415 tpl.find('img').attr('src','images/pic/blank.gif');416 tpl.find('.qty').html(' ');417 tpl.find('.name').html(' ');418 }419 }420}421function containsMaterial(product, mt_code){422 for(var i=1;i<=5;i++){423 if(product['mt'+i] == mt_code)424 return true;425 }426 return false;427}428function setProduct(){429 var type = $('#type').val();430 useMts = [];431 product = recipe[type][$('#product').val()];432 var src ='images/pic/' + type + '/' + product.code + '.gif';433 $('#n_prd').find('img').attr('src', src);434 $('#n_prd').find('.name').html(product.name);435 if(type == 'wp08')436 $('#n_prd').find('.qty').html(product.pcs);437 fillMaterial(product);438}439function genEqpList(type){440 var products = recipe[type];441 442 $('#product').removeOption(/./); //æ¸
é¤è£åé
ç®æ¸
å®443 for(var i=0;i<products.length;i++){ //ç¢çè£åé
ç®æ¸
å®444 $('#product').addOption(i, ('Lv.' + products[i].level + ' ' + products[i].name));445 }446 $('#n_prd').find('.qty').html(' ');447 $('#product')448 .val(0)449 .find('option')450 .each(function(){451 var lv = 0;452 var txt = $.trim($(this).text().substring(3, 5));453 lv = parseInt(txt);454 if((lv % 2) != 0){455 $(this).css({456 'background-color':'#333',457 'color':'#FFFF33'458 });459 }460 })461 .css('border', 'dashed 1px #808080');462 setProduct();...
notice1.mm.full.js
Source: notice1.mm.full.js
1 var types = [ //è£å種é¡é¸é
2 {val:'wp01' ,name:'å'},3 {val:'wp02' ,name:'æ§'},4 {val:'wp03' ,name:'æ§'},5 {val:'wp04' ,name:'å¼'},6 {val:'wp05' ,name:'æ'},7 {val:'wp06' ,name:'å°å'},8 {val:'wp07' ,name:'ååé¢'},9 {val:'wp08' ,name:'ç¸å½'},10 {val:'sh01' ,name:'é ç'},11 {val:'sh02' ,name:'帽å'},12 {val:'sh03' ,name:'é§ç²'},13 {val:'sh04' ,name:'è¡£æ'},14 {val:'sh05' ,name:'è¢'},15 {val:'sh06' ,name:'é´'},16 {val:'sh07' ,name:'éå'},17 {val:'sh08' ,name:'ç¾'},18 {val:'md01' ,name:'è¥æ°´'},19 {val:'fd01' ,name:'æç'}20 ];21 var product;22 var useMts = [];23 var readyToSave = false,24 orderItem;25 26 $(function(){27 for(var i=0;i<types.length;i++) //ç¢çè£å種é¡é¸é
æ¸
å®28 $('#type').addOption(types[i].val, types[i].name);29 30 $('#resultTable1')31 .add('#resultTable2')32 .find('div')33 .css('font-family', 'Verdana');34 35 $('#type')36 .val('wp01')37 .find('option')38 .slice(0,8)39 .css('background-color', '#708090')40 .end()41 .slice(8,16)42 .css('background-color', '#80A0C0')43 .end()44 .slice(16,18)45 .css('background-color', '#90C0F0');46 genEqpList($('#type').val()); //ç¢çè£åé
ç®æ¸
å®47 noticeBoard.init(); //030848 49 $('#type').change(function(){ //ç±è£å種é¡é¸é
ç¢çè£åé
ç®æ¸
å®50 //var index = $(this)[0].selectedIndex;51 var index = $(this).selectedOptions().first().index();52 var tc;53 clearTable();54 useMts = [];55 56 tc = (function(){57 if(index >= 8)58 return (index >= 16)? '#90C0F0' : '80A0C0';59 else60 return '#708090';61 }());62 63 $(this).add('#product').css('background-color', tc);64 $('#result').html('');65 genEqpList($(this).val());66 });67 68 $('#product').change(function(){ //ç±ä½¿ç¨è
é¸æçè£å顯示åºæéææ69 clearTable();70 setProduct();71 $('#resultTable1') //æ¸
é¤å次æ顯示çè¨ç®çµæ72 .add('#resultTable2')73 .add('#errMessage')74 .hide();75 });76 77 $('#slots').keydown(function(event){ //輸å
¥å®æ¸éå¾ï¼æä¸Enterè¦çºæä¸è¨ç®æé78 if(event.which == 13) //Enter79 $('#calc').click();80 });81 $('.sbtn').click(function(){82 if(readyToSave){83 noticeBoard.save(orderItem);84 }85 else86 console.log('Not Ready To Save...');87 })88 $('.close').on('click', function(){89 var noticeId = parseInt($(this).data('nid'), 10)90 div = $(this).closest('div');91 noticeBoard.delete(noticeId, div);92 });93 $('#noticeBoard').on('click', '.mt-item', function(){94 noticeBoard.mark(this);95 });96 97 $('#calc').click(function(){98 var useSlots = 0; //總å
±æéç©ºæ ¼æ¸é99 var result = '';100 var j;101 var str, idstr;102 var dt = new Date();103 var items; //å®ä¸æææ¸é104 var type = $('#type').val();105 var method = $('input[name=method]:checked').val(); //è®å使ç¨è
æéä¹è¨ç®æ¹å¼106 // method1 : ä¾ä½¿ç¨è
å¡«å
¥ä¹ç©ºæ ¼æ¸éè¨ç®åºå¯çç¢ä¹ç©åæ¸é107 // method2 : ä¾ä½¿ç¨è
å¡«å
¥ä¹çç¢ç©åæ¸éè¨ç®åºæéä¹ç©ºæ ¼æ¸é108 $('#resultTable1, #resultTable2, #errMessage').hide(); //æ¸
é¤å次æ顯示çè¨ç®çµæ109 orderItem = {110 id: noticeBoard.orderItems.idSeed++,111 qty: 0,112 product: product.name, //0308 è¨éç¢åå稱113 mts:[]114 };115 readyToSave = false;116 117 if(method == 'method1'){118 var count = 0; //å¯çç¢ç©åæ¸é119 var slots = parseInt($('#slots').val()); //ç©ºæ ¼æ¸é120 if(useMts.length <= slots){121 while(useSlots <= slots){122 count++;123 useSlots = 0;124 for(var i=0;i<useMts.length;i++){125 useSlots += Math.ceil((product['qty'+(i+1)]*count) / useMts[i].set);126 }127 }128 count--;129 130 $('#pname1').html(product.name);131 132 for(var i=0;i<5;i++){133 j = i + 1;134 if(i < useMts.length){135 items = product['qty'+j] * count;136 str = new String(Math.floor(items / useMts[i].set)).fontcolor('blue') + ' çµ';137 if((items % useMts[i].set) != 0)138 str += 'å ' + new String(items % useMts[i].set).fontcolor('blue') + ' å';139 140 $('#p1mt'+j).html(useMts[i].name);141 $('#p1slt'+j).html(str);142 orderItem.mts.push(useMts[i].name + ' ' + str); //0308 è¨é使ç¨ææèçµæ¸143 }144 else{145 $('#p1mt'+j).html(' ');146 $('#p1slt'+j).html(' ');147 orderItem.mts.push(' '); //0308 empty148 }149 }150 if(type == 'wp08'){ // ç¸å½151 str = new String(count).fontcolor('red') + ' 次 / å
± ';152 str += new String(count * product.pcs).fontcolor('blue') + ' å ';153 orderItem.qty = (count * product.pcs); //0308 è¨éçç¢æ¸é(ç¸å½)154 }155 else{156 str = new String(count).fontcolor('red') + ' å ';157 orderItem.qty = count; //0308 è¨éçç¢æ¸é158 }159 160 $('#pqty1').html(str);161 $('#resultTable1').show();162 readyToSave = true;163 }164 else{165 $('#errMessage').html('å¯ä½¿ç¨æ ¼æ¸ä¸è¶³ï¼ï¼').show();166 }167 }168 else{169 var qty = parseInt($('#slots').val());170 if(type == 'wp08'){171 qty = Math.floor(qty / product.pcs);172 }173 orderItem.qty = qty; //0308 è¨éçç¢æ¸é174 $('#pname2').html(product.name);175 $('#pqty2').html(new String(qty).fontcolor('red'));176 177 for(var i=0;i<5;i++){178 j = i+1;179 if(i < useMts.length){180 items = product['qty'+j] * qty;181 useSlots += Math.ceil(items / useMts[i].set);182 183 $('#p2mt'+j).html(useMts[i].name);184 185 str = new String(Math.floor(items / useMts[i].set)).fontcolor('blue') + ' çµ';186 if((items % useMts[i].set) != 0)187 str += 'å ' + new String(items % useMts[i].set).fontcolor('blue') + ' å';188 189 $('#p2slt'+j).html(str);190 orderItem.mts.push(useMts[i].name + ' ' + str); //0308 è¨é使ç¨ææèçµæ¸191 }192 else{193 $('#p2mt'+j).html(' ');194 $('#p2slt'+j).html(' ');195 orderItem.mts.push(' '); //0308 empty196 }197 }198 $('#p2uslt').html(new String(useSlots).fontcolor('red') + ' æ ¼');199 $('#resultTable2').show();200 readyToSave = (qty != 0);201 }202 });203 204 });205 206 function findMaterial(code){ //ç±ææç·¨èæ¥è©¢ææå
§å®¹207 var m;208 for(var i=0;i<materials.length;i++)209 if(materials[i].code == code){210 m = materials[i];211 break;212 }213 return m;214 }215 216 function clearTable(){ //æ¸
é¤æ顯示ä¹ææ表217 for(var i=1;i<=5;i++){218 $('#mt'+i).html(' ');219 $('#set'+i).html(' ');220 }221 }222 function fillMaterial(product){ //顯示è£åç©åä¹ææ表ï¼ä¸¦è¨éæéä¹ææ223 var mt;224 225 for(var i=1;i<=5;i++){226 mt = findMaterial(product['mt'+i]);227 if(mt){228 $('#mt'+i).html(mt.name + ' x ' + product['qty'+i]);229 $('#set'+i).html(mt.set + ' pcs/çµ');230 useMts.push(mt);231 }232 }233 }234 235 function setProduct(){236 useMts = [];237 product = recipe[$('#type').val()][$('#product').val()];238 fillMaterial(product);239 }240 241 function genEqpList(type){242 var products = recipe[type];243 244 $('#product').removeOption(/./); //æ¸
é¤è£åé
ç®æ¸
å®245 for(var i=0;i<products.length;i++) //ç¢çè£åé
ç®æ¸
å®246 $('#product').addOption(i, ('Lv.' + products[i].level + ' ' + products[i].name));247 $('#product')248 .val(0)249 .find('option')250 .each(function(){251 var lv = 0;252 var txt = $.trim($(this).text().substring(3, 5));253 lv = parseInt(txt);254 if((lv % 2) != 0){255 $(this).css({256 'background-color':'#333',257 'color':'#FFFF33'258 });259 }260 })261 .css('border', 'dashed 1px #808080');262 setProduct();263 }264 var noticeBoard = {265 orderItems : {},266 init : function(){267 var result = localStorage.getItem('orderItems');268 if(result !== null){269 noticeBoard.orderItems = JSON.parse(result);270 noticeBoard.show();271 }272 else{273 noticeBoard.orderItems.idSeed = 0;274 noticeBoard.orderItems.items = [];275 } 276 },277 save : function(item){278 var ois = noticeBoard.orderItems,279 marked = ['u','u','u','u','u'];280 281 item.m = marked;282 ois.items.push(item);283 noticeBoard.update(ois);284 noticeBoard.showNotice(item, 0);285 },286 delete : function(id, div){287 var orderItems = noticeBoard.orderItems,288 items = orderItems.items,289 index = noticeBoard.getIndex(id);290 if(index != -1){291 items.splice(index,1);292 div.fadeOut(1000, function() {293 this.remove();294 });295 noticeBoard.update(orderItems);296 }297 },298 update : function(ois){299 localStorage.setItem('orderItems', JSON.stringify(ois));300 },301 mark : function(div){302 var panel = $(div),303 m_index = panel.data('m'),304 notice = panel.closest('.notice'),305 noticeId = parseInt(notice.find('.close').data('nid'), 10),306 orderItems = noticeBoard.orderItems,307 items = orderItems.items,308 item_index = noticeBoard.getIndex(noticeId),309 marked;310 if(item_index != -1){311 marked = items[item_index].m[m_index];312 if(marked==='m'){313 marked = 'u';314 panel.css('text-decoration', 'none');315 }316 else{317 marked = 'm';318 panel.css('text-decoration', 'line-through');319 }320 items[item_index].m[m_index] = marked;321 noticeBoard.update(orderItems);322 // localStorage.setItem('orderItems', JSON.stringify(orderItems));323 };324 },325 getIndex : function(id){326 var orderItems = noticeBoard.orderItems,327 items = orderItems.items,328 index = -1;329 for(var i=0;i<items.length;i++){330 if(id == items[i].id)331 index = i;332 }333 return index;334 },335 showNotice : function(item, i){336 var notice = $('#noticeTemplate').clone(true),337 delay = 600,338 panels = [];339 notice340 .removeClass('template')341 .removeAttr('id')342 .find('.close')343 .data('nid', item.id)344 .end()345 .find('h3')346 .html(item.product + (' X ' + item.qty).fontcolor('#822'))347 .end()348 .hide()349 .appendTo('#noticeBoard')350 .delay(delay*i)351 .fadeIn(delay);352 panels = notice.find('.mt-item');353 $.each(panels, function(index,panel){354 $(panel)355 .html(item.mts[index])356 .data('m', index);357 if(item.m[index] == 'm')358 $(panel).addClass('marked');359 });360 },361 show : function(){362 var result = localStorage.getItem('orderItems'),363 orderItems = JSON.parse(result),364 items = orderItems.items;365 $('#noticeBoard').html('');366 for(var i=0;i<items.length;i++){367 noticeBoard.showNotice(items[i], i);368 }369 }...
notice.mm.full.js
Source: notice.mm.full.js
1 var types = [ //è£å種é¡é¸é
2 {val:'wp01' ,name:'å'},3 {val:'wp02' ,name:'æ§'},4 {val:'wp03' ,name:'æ§'},5 {val:'wp04' ,name:'å¼'},6 {val:'wp05' ,name:'æ'},7 {val:'wp06' ,name:'å°å'},8 {val:'wp07' ,name:'ååé¢'},9 {val:'wp08' ,name:'ç¸å½'},10 {val:'sh01' ,name:'é ç'},11 {val:'sh02' ,name:'帽å'},12 {val:'sh03' ,name:'é§ç²'},13 {val:'sh04' ,name:'è¡£æ'},14 {val:'sh05' ,name:'è¢'},15 {val:'sh06' ,name:'é´'},16 {val:'sh07' ,name:'éå'},17 {val:'sh08' ,name:'ç¾'},18 {val:'md01' ,name:'è¥æ°´'},19 {val:'fd01' ,name:'æç'}20 ];21 var product;22 var useMts = [];23 var readyToSave = false,24 orderItem;25 26 $(function(){27 for(var i=0;i<types.length;i++) //ç¢çè£å種é¡é¸é
æ¸
å®28 $('#type').addOption(types[i].val, types[i].name);29 30 $('#resultTable1')31 .add('#resultTable2')32 .find('div')33 .css('font-family', 'Verdana');34 35 $('#type')36 .val('wp01')37 .find('option')38 .slice(0,8)39 .css('background-color', '#708090')40 .end()41 .slice(8,16)42 .css('background-color', '#80A0C0')43 .end()44 .slice(16,18)45 .css('background-color', '#90C0F0');46 genEqpList($('#type').val()); //ç¢çè£åé
ç®æ¸
å®47 noticeBoard.init(); //030848 49 $('#type').change(function(){ //ç±è£å種é¡é¸é
ç¢çè£åé
ç®æ¸
å®50 //var index = $(this)[0].selectedIndex;51 var index = $(this).selectedOptions().first().index();52 var tc;53 clearTable();54 useMts = [];55 56 tc = (function(){57 if(index >= 8)58 return (index >= 16)? '#90C0F0' : '80A0C0';59 else60 return '#708090';61 }());62 63 $(this).add('#product').css('background-color', tc);64 $('#result').html('');65 genEqpList($(this).val());66 });67 68 $('#product').change(function(){ //ç±ä½¿ç¨è
é¸æçè£å顯示åºæéææ69 clearTable();70 setProduct();71 $('#resultTable1') //æ¸
é¤å次æ顯示çè¨ç®çµæ72 .add('#resultTable2')73 .add('#errMessage')74 .hide();75 });76 77 $('#slots').keydown(function(event){ //輸å
¥å®æ¸éå¾ï¼æä¸Enterè¦çºæä¸è¨ç®æé78 if(event.which == 13) //Enter79 $('#calc').click();80 });81 $('.sbtn').click(function(){82 if(readyToSave){83 noticeBoard.save(orderItem);84 }85 else86 console.log('Not Ready To Save...');87 })88 $('.close').on('click', function(){89 var noticeId = parseInt($(this).data('nid'), 10);90 $(this).closest('div').remove();91 noticeBoard.delete(noticeId);92 });93 $('#noticeBoard').on('click', '.mt-item', function(){94 // $(this).toggleClass('marked', 'unmark');95 noticeBoard.mark(this);96 });97 98 $('#calc').click(function(){99 var useSlots = 0; //總å
±æéç©ºæ ¼æ¸é100 var result = '';101 var j;102 var str, idstr;103 var dt = new Date();104 var items; //å®ä¸æææ¸é105 var type = $('#type').val();106 var method = $('input[name=method]:checked').val(); //è®å使ç¨è
æéä¹è¨ç®æ¹å¼107 // method1 : ä¾ä½¿ç¨è
å¡«å
¥ä¹ç©ºæ ¼æ¸éè¨ç®åºå¯çç¢ä¹ç©åæ¸é108 // method2 : ä¾ä½¿ç¨è
å¡«å
¥ä¹çç¢ç©åæ¸éè¨ç®åºæéä¹ç©ºæ ¼æ¸é109 $('#resultTable1, #resultTable2, #errMessage').hide(); //æ¸
é¤å次æ顯示çè¨ç®çµæ110 orderItem = {111 id: noticeBoard.orderItems.idSeed++,112 qty: 0,113 product: product.name, //0308 è¨éç¢åå稱114 mts:[]115 };116 readyToSave = false;117 118 if(method == 'method1'){119 var count = 0; //å¯çç¢ç©åæ¸é120 var slots = parseInt($('#slots').val()); //ç©ºæ ¼æ¸é121 if(useMts.length <= slots){122 while(useSlots <= slots){123 count++;124 useSlots = 0;125 for(var i=0;i<useMts.length;i++){126 useSlots += Math.ceil((product['qty'+(i+1)]*count) / useMts[i].set);127 }128 }129 count--;130 131 $('#pname1').html(product.name);132 133 for(var i=0;i<5;i++){134 j = i + 1;135 if(i < useMts.length){136 items = product['qty'+j] * count;137 str = new String(Math.floor(items / useMts[i].set)).fontcolor('blue') + ' çµ';138 if((items % useMts[i].set) != 0)139 str += 'å ' + new String(items % useMts[i].set).fontcolor('blue') + ' å';140 141 $('#p1mt'+j).html(useMts[i].name);142 $('#p1slt'+j).html(str);143 orderItem.mts.push(useMts[i].name + ' ' + str); //0308 è¨é使ç¨ææèçµæ¸144 }145 else{146 $('#p1mt'+j).html(' ');147 $('#p1slt'+j).html(' ');148 orderItem.mts.push(' '); //0308 empty149 }150 }151 if(type == 'wp08'){ // ç¸å½152 str = new String(count).fontcolor('red') + ' 次 / å
± ';153 str += new String(count * product.pcs).fontcolor('blue') + ' å ';154 orderItem.qty = (count * product.pcs); //0308 è¨éçç¢æ¸é(ç¸å½)155 }156 else{157 str = new String(count).fontcolor('red') + ' å ';158 orderItem.qty = count; //0308 è¨éçç¢æ¸é159 }160 161 $('#pqty1').html(str);162 $('#resultTable1').show();163 readyToSave = true;164 }165 else{166 $('#errMessage').html('å¯ä½¿ç¨æ ¼æ¸ä¸è¶³ï¼ï¼').show();167 }168 }169 else{170 var qty = parseInt($('#slots').val());171 if(type == 'wp08'){172 qty = Math.floor(qty / product.pcs);173 }174 orderItem.qty = qty; //0308 è¨éçç¢æ¸é175 $('#pname2').html(product.name);176 $('#pqty2').html(new String(qty).fontcolor('red'));177 178 for(var i=0;i<5;i++){179 j = i+1;180 if(i < useMts.length){181 items = product['qty'+j] * qty;182 useSlots += Math.ceil(items / useMts[i].set);183 184 $('#p2mt'+j).html(useMts[i].name);185 186 str = new String(Math.floor(items / useMts[i].set)).fontcolor('blue') + ' çµ';187 if((items % useMts[i].set) != 0)188 str += 'å ' + new String(items % useMts[i].set).fontcolor('blue') + ' å';189 190 $('#p2slt'+j).html(str);191 orderItem.mts.push(useMts[i].name + ' ' + str); //0308 è¨é使ç¨ææèçµæ¸192 }193 else{194 $('#p2mt'+j).html(' ');195 $('#p2slt'+j).html(' ');196 orderItem.mts.push(' '); //0308 empty197 }198 }199 $('#p2uslt').html(new String(useSlots).fontcolor('red') + ' æ ¼');200 $('#resultTable2').show();201 readyToSave = (qty != 0);202 }203 });204 205 });206 207 function findMaterial(code){ //ç±ææç·¨èæ¥è©¢ææå
§å®¹208 var m;209 for(var i=0;i<materials.length;i++)210 if(materials[i].code == code){211 m = materials[i];212 break;213 }214 return m;215 }216 217 function clearTable(){ //æ¸
é¤æ顯示ä¹ææ表218 for(var i=1;i<=5;i++){219 $('#mt'+i).html(' ');220 $('#set'+i).html(' ');221 }222 }223 function fillMaterial(product){ //顯示è£åç©åä¹ææ表ï¼ä¸¦è¨éæéä¹ææ224 var mt;225 226 for(var i=1;i<=5;i++){227 mt = findMaterial(product['mt'+i]);228 if(mt){229 $('#mt'+i).html(mt.name + ' x ' + product['qty'+i]);230 $('#set'+i).html(mt.set + ' pcs/çµ');231 useMts.push(mt);232 }233 }234 }235 236 function setProduct(){237 useMts = [];238 product = recipe[$('#type').val()][$('#product').val()];239 fillMaterial(product);240 }241 242 function genEqpList(type){243 var products = recipe[type];244 245 $('#product').removeOption(/./); //æ¸
é¤è£åé
ç®æ¸
å®246 for(var i=0;i<products.length;i++) //ç¢çè£åé
ç®æ¸
å®247 $('#product').addOption(i, ('Lv.' + products[i].level + ' ' + products[i].name));248 $('#product')249 .val(0)250 .find('option')251 .each(function(){252 var lv = 0;253 var txt = $.trim($(this).text().substring(3, 5));254 lv = parseInt(txt);255 if((lv % 2) != 0){256 $(this).css({257 'background-color':'#333',258 'color':'#FFFF33'259 });260 }261 })262 .css('border', 'dashed 1px #808080');263 setProduct();264 }265 var noticeBoard = {266 orderItems : {},267 init : function(){268 var result = localStorage.getItem('orderItems');269 if(result !== null){270 noticeBoard.orderItems = JSON.parse(result);271 noticeBoard.show();272 }273 else{274 noticeBoard.orderItems.idSeed = 0;275 noticeBoard.orderItems.items = [];276 } 277 },278 save : function(item){279 var ois = noticeBoard.orderItems,280 marked = ['u','u','u','u','u'];281 item.m = marked;282 ois.items.push(item);283 noticeBoard.update(ois);284 noticeBoard.show();285 },286 delete : function(id){287 var orderItems = noticeBoard.orderItems,288 items = orderItems.items,289 index = noticeBoard.getIndex(id);290 if(index != -1){291 items.splice(index,1);292 noticeBoard.update(orderItems);293 }294 noticeBoard.show();295 },296 update : function(ois){297 localStorage.setItem('orderItems', JSON.stringify(ois));298 },299 mark : function(div){300 var panel = $(div),301 m_index = panel.data('m'),302 notice = panel.closest('.notice'),303 noticeId = parseInt(notice.find('.close').data('nid'), 10),304 orderItems = noticeBoard.orderItems,305 items = orderItems.items,306 item_index = noticeBoard.getIndex(noticeId),307 marked;308 if(item_index != -1){309 marked = items[item_index].m[m_index];310 if(marked==='m'){311 marked = 'u';312 panel.css('text-decoration', 'none');313 }314 else{315 marked = 'm';316 panel.css('text-decoration', 'line-through');317 }318 items[item_index].m[m_index] = marked;319 noticeBoard.update(orderItems);320 };321 },322 getIndex : function(id){323 var orderItems = noticeBoard.orderItems,324 items = orderItems.items,325 index = -1;326 for(var i=0;i<items.length;i++){327 if(id == items[i].id)328 index = i;329 }330 return index;331 },332 show : function(){333 var result = localStorage.getItem('orderItems'),334 orderItems = JSON.parse(result),335 items = orderItems.items,336 item, i, panels,337 notice;338 $('#noticeBoard').html('');339 for(var i=0;i<items.length;i++){340 item = items[i];341 notice = $('#noticeTemplate').clone(true);342 notice343 .removeClass('template')344 .removeAttr('id')345 .find('.close')346 .data('nid', item.id)347 .end()348 .find('h3')349 .html(item.product + (' X ' + item.qty).fontcolor('#822'))350 .end()351 .appendTo('#noticeBoard');352 panels = notice.find('.mt-item');353 $.each(panels, function(index,panel){354 $(panel)355 .html(item.mts[index])356 .data('m', index);357 if(item.m[index] == 'm')358 $(panel).addClass('marked');359 });360 }361 }...
index.js
Source: index.js
1import baseComponent from '../helpers/baseComponent'2import classNames from '../helpers/classNames'3import { getTouchPoints, getPointsNumber, getSwipeDirection } from '../helpers/gestures'4baseComponent({5 relations: {6 '../swipe-action-group/index': {7 type: 'ancestor',8 },9 },10 properties: {11 prefixCls: {12 type: String,13 value: 'wux-swipe',14 },15 autoClose: {16 type: Boolean,17 value: false,18 },19 disabled: {20 type: Boolean,21 value: false,22 },23 left: {24 type: Array,25 value: [],26 observer: 'updateBtns',27 },28 right: {29 type: Array,30 value: [],31 observer: 'updateBtns',32 },33 useSlots: {34 type: Boolean,35 value: false,36 },37 data: {38 type: null,39 value: null,40 },41 },42 data: {43 index: 0,44 swiping: false,45 showCover: false,46 offsetStyle: '',47 },48 computed: {49 classes: ['prefixCls, swiping', function(prefixCls, swiping) {50 const wrap = classNames(prefixCls, {51 [`${prefixCls}--swiping`]: swiping,52 })53 const cover = `${prefixCls}__cover`54 const left = classNames(`${prefixCls}__actions`, {55 [`${prefixCls}__actions--left`]: true,56 })57 const right = classNames(`${prefixCls}__actions`, {58 [`${prefixCls}__actions--right`]: true,59 })60 const action = `${prefixCls}__action`61 const text = `${prefixCls}__text`62 const content = `${prefixCls}__content`63 return {64 wrap,65 cover,66 left,67 right,68 action,69 text,70 content,71 }72 }],73 },74 methods: {75 updated(index) {76 if (this.data.index !== index) {77 this.setData({ index })78 }79 },80 onCloseSwipe() {81 const parent = this.getRelationNodes('../swipe-action-group/index')[0]82 if (parent) {83 parent.onCloseSwipe(this.data.index)84 }85 },86 getContentEasing(value, limit) {87 // limit content style left when value > actions width88 const delta = Math.abs(value) - Math.abs(limit)89 const isOverflow = delta > 090 const factor = limit > 0 ? 1 : -191 if (isOverflow) {92 value = limit + Math.pow(delta, 0.85) * factor93 return Math.abs(value) > Math.abs(limit) ? limit : value94 }95 return value96 },97 setStyle(value) {98 const limit = value > 0 ? this.btnsLeftWidth : -this.btnsRightWidth99 const left = this.getContentEasing(value, limit)100 const offsetStyle = `left: ${left}px`101 const showCover = Math.abs(value) > 0102 if (this.data.offsetStyle !== offsetStyle || this.data.showCover !== showCover) {103 this.setData({ offsetStyle, showCover })104 }105 },106 updateBtns() {107 const { prefixCls } = this.data108 const query = wx.createSelectorQuery().in(this)109 query.select(`.${prefixCls}__actions--left`).boundingClientRect()110 query.select(`.${prefixCls}__actions--right`).boundingClientRect()111 query.exec((rects) => {112 const [left, right] = rects113 this.btnsLeftWidth = left ? left.width : 0114 this.btnsRightWidth = right ? right.width : 0115 })116 },117 onTap(e) {118 const { type } = e.currentTarget.dataset119 const params = {120 ...e.currentTarget.dataset,121 buttons: this.data[type],122 data: this.data.data,123 }124 if (this.data.autoClose) {125 this.onClose()126 }127 this.triggerEvent('click', params)128 },129 onAcitons() {130 if (this.data.autoClose) {131 this.onClose()132 }133 },134 onOpen(value, openedLeft, openedRight) {135 if (!this.openedLeft && !this.openedRight) {136 this.triggerEvent('open')137 }138 this.openedLeft = openedLeft139 this.openedRight = openedRight140 this.setStyle(value)141 },142 onClose() {143 if (this.openedLeft || this.openedRight) {144 this.triggerEvent('close')145 }146 this.openedLeft = false147 this.openedRight = false148 this.setStyle(0)149 },150 onOpenLeft() {151 this.onOpen(this.btnsLeftWidth, true, false)152 },153 onOpenRight() {154 this.onOpen(-this.btnsRightWidth, true, false)155 },156 onTouchStart(e) {157 if (this.data.disabled || getPointsNumber(e) > 1) return158 this.start = getTouchPoints(e)159 this.onCloseSwipe()160 },161 onTouchMove(e) {162 if (this.data.disabled || getPointsNumber(e) > 1) return163 this.move = getTouchPoints(e)164 const deltaX = this.move.x - this.start.x165 const direction = getSwipeDirection(this.start.x, this.move.x, this.start.y, this.move.y)166 const isLeft = direction === 'Left'167 const isRight = direction === 'Right'168 if (!isLeft && !isRight) return169 const { left, right, useSlots } = this.data170 this.needShowRight = isLeft && (useSlots || right.length > 0)171 this.needShowLeft = isRight && (useSlots || left.length > 0)172 if (this.needShowLeft || this.needShowRight) {173 this.swiping = true174 this.setData({ swiping: true })175 this.setStyle(deltaX)176 }177 },178 onTouchEnd(e) {179 if (this.data.disabled || getPointsNumber(e) > 1 || !this.swiping) return180 this.end = getTouchPoints(e)181 const deltaX = this.end.x - this.start.x182 const needOpenRight = this.needShowRight && Math.abs(deltaX) > this.btnsRightWidth / 2183 const needOpenLeft = this.needShowLeft && Math.abs(deltaX) > this.btnsLeftWidth / 2184 if (needOpenRight) {185 this.onOpenRight()186 } else if (needOpenLeft) {187 this.onOpenLeft()188 } else {189 this.onClose()190 }191 this.swiping = false192 this.setData({ swiping: false })193 this.needShowLeft = false194 this.needShowRight = false195 },196 noop() {},197 },198 created() {199 this.btnsLeftWidth = 0200 this.btnsRightWidth = 0201 this.openedLeft = false202 this.openedRight = false203 this.needShowLeft = false204 this.needShowRight = false205 },206 ready() {207 this.updateBtns()208 },...
3-Swipe.stories.js
Source: 3-Swipe.stories.js
1import React, { useState } from "react";2import { defineSlots, useLayout, useSlots } from "./src";3import { layoutGrid, leftBox, rightBox } from "./Mirrored.module.scss";4const First = () => {5 const { Left, Right } = useSlots("Left", "Right");6 return (7 <div className={layoutGrid}>8 <div className={rightBox}>{Right}</div>9 <div className={leftBox}>{Left}</div>10 </div>11 );12};13const Second = () => {14 const { Left, Right } = useSlots("Left", "Right");15 return (16 <div className={layoutGrid}>17 <div className={leftBox}>{Left}</div>18 <div className={rightBox}>{Right}</div>19 </div>20 );21};22export const SwipeStory = () => {23 const [isSwipped, setSwipped] = useState(false);24 const Layout = useLayout(isSwipped ? First : Second);25 const { Left, Right } = defineSlots("Left", "Right");26 return (27 <>28 <Layout>...
2-Nested.stories.js
Source: 2-Nested.stories.js
1import React from "react";2import { defineSlots, useLayout, useSlots } from "./src";3import { innerBox, outerBox } from "./Nested.module.scss";4const Inner = () => {5 const { Content } = useSlots("Content");6 return (7 <div className={innerBox}>8 Inner box9 <br />10 {Content}11 </div>12 );13};14const Outer = () => {15 const { Content } = useSlots("Content");16 return (17 <div className={outerBox}>18 Outer box19 <br />20 {Content}21 </div>22 );23};24export const NestedStory = () => {25 const OuterLayout = useLayout(Outer);26 const InnerLayout = useLayout(Inner);27 const { Content } = defineSlots("Content");28 return (29 <OuterLayout>...
code-sample.js
Source: code-sample.js
2import React from "react"3import { Code } from "@elements"4import { useSlots } from "@hooks"5export const CodeSample = ({ children, className, ...props }) => {6 const Slots = useSlots(children, ["jsx", "code"])7 console.log(Slots)8 return (9 <div className="relative overflow-hidden mb-8">10 <div11 className={`bg-white rounded-t-lg overflow-hidden border-t border-l border-r border-gray-400 p-4 ${className}`}12 {...props}13 >14 {Slots.jsx}15 </div>16 <Code className="rounded-b-lg bg-gray-800">{Slots.code}</Code>17 </div>18 )19}20CodeSample.propTypes = {...
use-slots.test.js
Source: use-slots.test.js
1import React from "react";2import { render } from "@testing-library/react";3import { useSlots } from "./env-src";4test("useSlots should return an object", () => {5 expect(useSlots()).toEqual({});6});7test("useSlots should accept string argument and return object with same key", () => {8 expect(useSlots("A")).toHaveProperty("A");9});10test("useSlots should accept array of strings argument and return object with same keys", () => {11 expect(useSlots(["A"])).toHaveProperty("A");...
Using AI Code Generation
1const { useSlots } = require('playwright');2const { chromium } = require('playwright-chromium');3const browser = await chromium.launch();4const context = await browser.newContext();5const page = await context.newPage();6await useSlots(page, async (page) => {7});8await browser.close();9const { chromium } = require('playwright-chromium');10const browser = await chromium.launch();11const context = await browser.newContext();12const page = await context.newPage();13await page.useSlots(async (page) => {14});15await browser.close();
Using AI Code Generation
1const { useSlots } = require('playwright');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 const context = await browser.newContext();6 const [slot1, slot2] = await useSlots(context, 2);7 const page1 = await slot1.newPage();8 const page2 = await slot2.newPage();9})();10const { useSlots } = require('playwright');11const { chromium } = require('playwright');12(async () => {13 const browser = await chromium.launch();14 const context = await browser.newContext();15 const [slot1, slot2] = await useSlots(context, 2);16 const page1 = await slot1.newPage();17 const page2 = await slot2.newPage();18})();
Using AI Code Generation
1const { useSlots } = require('playwright');2const { chromium } = require('playwright-chromium');3const browser = await chromium.launch();4const context = await browser.newContext();5const page = await context.newPage();6await useSlots(page, async (page) => {7});8await browser.close();9const { chromium } = require('playwright-chromium');10const browser = await chromium.launch();11const context = await browser.newContext();12const page = await context.newPage();13await page.useSlots(async (page) => {14});15await browser.close();
Using AI Code Generation
1const { useSlots } = require('playwright/lib/server/supplements/recorder/recorderSupplement');2const { Page } = require('playwright/lib/server/page');3const { Frame } = require('playwright/lib/server/frames');4const page = await browser.newPage();5const frame = await page.addFrame();6useSlots(page, { page: page });7useSlots(frame, { page: page, frame: frame });8await page.selectOption('select#state', 'CA');9await frame.selectOption('select#state', 'CA');10const { test, expect } = require('@playwright/test');11test('test', async ({ page }) => {12 await page.selectOption('select#state', 'CA');13});14const { test, expect } = require('@playwright/test');15test.describe('test', () => {16 test('test1', async ({ page }) => {17 await page.selectOption('select#state', 'CA');18 });19 test('test2', async ({ page }) => {20 await page.selectOption('select#state', 'CA');21 });22});23const { test, expect } = require('@playwright/test');24test.describe('test', () => {25 test.describe('test1', () => {26 test('test1', async ({ page }) => {27 await page.selectOption('select#state', 'CA');28 });29 test('test2', async ({ page }) => {30 await page.selectOption('select#state', 'CA');31 });32 });33 test.describe('test2', () => {34 test('test1', async ({ page }) => {35 await page.selectOption('select#state', 'CA');36 });37 test('test2', async ({ page }) => {38 await page.selectOption('select#state', 'CA');39 });
Using AI Code Generation
1const { useSlots } = require('playwright/lib/server/supplements/recorder/recorderSupplement');2const { Page } = require('playwright/lib/server/page');3const { Frame } = require('playwright/lib/server/frames');4const page = await browser.newPage();5const frame = await page.addFrame();6useSlots(page, { page: page });7useSlots(frame, { page: page, frame: frame });8await page.selectOption('select#state', 'CA');9await frame.selectOption('select#state', 'CA');10const { test, expect } = require('@playwright/test');11test('test', async ({ page }) => {12 await page.selectOption('select#state', 'CA');13});14const { test, expect } = require('@playwright/test');15test.describe('test', () => {16 test('test1', async ({ page }) => {17 await page.selectOption('select#state', 'CA');18 });19 test('test2', async ({ page }) => {20 await page.selectOption('select#state', 'CA');21 });22});23const { test, expect } = require('@playwright/test');24test.describe('test', () => {25 test.describe('test1', () => {26 test('test1', async ({ page }) => {27 await page.selectOption('select#state', 'CA');28 });29 test('test2', async ({ page }) => {30 await page.selectOption('select#state', 'CA');31 });32 });33 test.describe('test2', () => {34 test('test1', async ({ page }) => {35 await page.selectOption('select#state', 'CA');36 });37 test('test2', async ({ page }) => {38 await page.selectOption('select#state', 'CA');39 });
Using AI Code Generation
1const { useSlots } = require('@playwright/test/lib/internal/api');2const { test, expect } = require('@playwright/test');3test.use(useSlots(2));4test('test 1', async ({ page }) => {5 expect(await page.title()).toBe('Playwright');6});7test('test 2', async ({ page }) => {8 expect(await page.title()).toBe('Playwright');9});10test('test 3', async ({ page }) => {11 expect(await page.title()).toBe('Playwright');
Using AI Code Generation
1})code to use useSlots method of Playwright Internal API2const { useSlots } = require('playwright/lib/server/slotPool');3useSlots(new BigUint;4test('test 4', async ({ page }) => {5 expect(await page.title()).toBe('Playwright');6});7test('test 5', async ({ page }) => {8 expect(await page.title()).toBe('Playwright');9});10test('test 6', async ({ page }) => {11 expect(await page.title()).toBe('Playwright');12});
Using AI Code Generation
1const { useSlots } = require('@playwright/test/lib/api/test');2const { test } = useSlots();3test('test', async ({ page }) => {4});5test('test 7', async ({ page }) => {6 expect(await page.title()).toBe('Playwright');7});
Using AI Code Generation
1const playwright = require('playwright');2const browser = await playwright['chromium'].launch();3const context = await browser.newContext();4const page = await context.newPage();5await page._useSlots([{6}]);7await browser.close();8test('test 8', async ({ page }) => {9 expect(await page.title()).toBe('Playwright');10});11test('test 9', async ({ page }) => {12 expect(await page.title()).toBe('Playwright');13});14test('test 10', async ({ page }) => {15 expect(await page.title()).toBe('Playwright');16});17test('test 11', async ({ page }) => {18 expect(await page.title()).toBe('Playwright');19});20test('test 12', async ({ page }) => {21 expect(await page.title()).toBe('Playwright');22});23test('test 13', async ({ page }) => {24 expect(await page.title()).toBe('Playwright');25});26test('test 14', async
Using AI Code Generation
1const { useSlots } = require('playwright/lib/server/chromium/crPage');2useSlots(page, true);3test('test', async ({ page }) => {4 const { useSlots } = require('playwright/lib/server/chromium/crPage');5 useSlots(page, true);6});7const { useSlots } = require('playwright/lib/server/chromium/crPage');8module.exports = async ({ page }) => {9 useSlots(page, true);10};
Using AI Code Generation
1const playwright = require('playwright');2const browser = await playwright['chromium'].launch();3const context = await browser.newContext();4const page = await context.newPage();5await page._useSlots([{6}]);7await browser.close();
Jest + Playwright - Test callbacks of event-based DOM library
firefox browser does not start in playwright
Is it possible to get the selector from a locator object in playwright?
How to run a list of test suites in a single file concurrently in jest?
Running Playwright in Azure Function
firefox browser does not start in playwright
This question is quite close to a "need more focus" question. But let's try to give it some focus:
Does Playwright has access to the cPicker object on the page? Does it has access to the window object?
Yes, you can access both cPicker and the window object inside an evaluate call.
Should I trigger the events from the HTML file itself, and in the callbacks, print in the DOM the result, in some dummy-element, and then infer from that dummy element text that the callbacks fired?
Exactly, or you can assign values to a javascript variable:
const cPicker = new ColorPicker({
onClickOutside(e){
},
onInput(color){
window['color'] = color;
},
onChange(color){
window['result'] = color;
}
})
And then
it('Should call all callbacks with correct arguments', async() => {
await page.goto(`http://localhost:5000/tests/visual/basic.html`, {waitUntil:'load'})
// Wait until the next frame
await page.evaluate(() => new Promise(requestAnimationFrame))
// Act
// Assert
const result = await page.evaluate(() => window['color']);
// Check the value
})
Check out the latest blogs from LambdaTest on this topic:
Native apps are developed specifically for one platform. Hence they are fast and deliver superior performance. They can be downloaded from various app stores and are not accessible through browsers.
One of the essential parts when performing automated UI testing, whether using Selenium or another framework, is identifying the correct web elements the tests will interact with. However, if the web elements are not located correctly, you might get NoSuchElementException in Selenium. This would cause a false negative result because we won’t get to the actual functionality check. Instead, our test will fail simply because it failed to interact with the correct element.
Smartphones have changed the way humans interact with technology. Be it travel, fitness, lifestyle, video games, or even services, it’s all just a few touches away (quite literally so). We only need to look at the growing throngs of smartphone or tablet users vs. desktop users to grasp this reality.
As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!