Best JavaScript code snippet using wpt
sweetalert.js
Source:sweetalert.js
1'use strict';2var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };3Object.defineProperty(exports, '__esModule', {4 value: true5});6// SweetAlert7// 2014-2015 (c) - Tristan Edwards8// github.com/t4t5/sweetalert9/*10 * jQuery-like functions for manipulating the DOM11 */12var _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation = require('./modules/handle-dom');13/*14 * Handy utilities15 */16var _extend$hexToRgb$isIE8$logStr$colorLuminance = require('./modules/utils');17/*18 * Handle sweetAlert's DOM elements19 */20var _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition = require('./modules/handle-swal-dom');21// Handle button events and keyboard events22var _handleButton$handleConfirm$handleCancel = require('./modules/handle-click');23var _handleKeyDown = require('./modules/handle-key');24var _handleKeyDown2 = _interopRequireWildcard(_handleKeyDown);25// Default values26var _defaultParams = require('./modules/default-params');27var _defaultParams2 = _interopRequireWildcard(_defaultParams);28var _setParameters = require('./modules/set-params');29var _setParameters2 = _interopRequireWildcard(_setParameters);30/*31 * Remember state in cases where opening and handling a modal will fiddle with it.32 * (We also use window.previousActiveElement as a global variable)33 */34var previousWindowKeyDown;35var lastFocusedButton;36/*37 * Global sweetAlert function38 * (this is what the user calls)39 */40var sweetAlert, swal;41exports['default'] = sweetAlert = swal = function () {42 var customizations = arguments[0];43 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.addClass(document.body, 'stop-scrolling');44 _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.resetInput();45 /*46 * Use argument if defined or default value from params object otherwise.47 * Supports the case where a default value is boolean true and should be48 * overridden by a corresponding explicit argument which is boolean false.49 */50 function argumentOrDefault(key) {51 var args = customizations;52 return args[key] === undefined ? _defaultParams2['default'][key] : args[key];53 }54 if (customizations === undefined) {55 _extend$hexToRgb$isIE8$logStr$colorLuminance.logStr('SweetAlert expects at least 1 attribute!');56 return false;57 }58 var params = _extend$hexToRgb$isIE8$logStr$colorLuminance.extend({}, _defaultParams2['default']);59 switch (typeof customizations) {60 // Ex: swal("Hello", "Just testing", "info");61 case 'string':62 params.title = customizations;63 params.text = arguments[1] || '';64 params.type = arguments[2] || '';65 break;66 // Ex: swal({ title:"Hello", text: "Just testing", type: "info" });67 case 'object':68 if (customizations.title === undefined) {69 _extend$hexToRgb$isIE8$logStr$colorLuminance.logStr('Missing "title" argument!');70 return false;71 }72 params.title = customizations.title;73 for (var customName in _defaultParams2['default']) {74 params[customName] = argumentOrDefault(customName);75 }76 // Show "Confirm" instead of "OK" if cancel button is visible77 params.confirmButtonText = params.showCancelButton ? 'Confirm' : _defaultParams2['default'].confirmButtonText;78 params.confirmButtonText = argumentOrDefault('confirmButtonText');79 // Callback function when clicking on "OK"/"Cancel"80 params.doneFunction = arguments[1] || null;81 break;82 default:83 _extend$hexToRgb$isIE8$logStr$colorLuminance.logStr('Unexpected type of argument! Expected "string" or "object", got ' + typeof customizations);84 return false;85 }86 _setParameters2['default'](params);87 _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.fixVerticalPosition();88 _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.openModal(arguments[1]);89 // Modal interactions90 var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();91 /*92 * Make sure all modal buttons respond to all events93 */94 var $buttons = modal.querySelectorAll('button');95 var buttonEvents = ['onclick', 'onmouseover', 'onmouseout', 'onmousedown', 'onmouseup', 'onfocus'];96 var onButtonEvent = function onButtonEvent(e) {97 return _handleButton$handleConfirm$handleCancel.handleButton(e, params, modal);98 };99 for (var btnIndex = 0; btnIndex < $buttons.length; btnIndex++) {100 for (var evtIndex = 0; evtIndex < buttonEvents.length; evtIndex++) {101 var btnEvt = buttonEvents[evtIndex];102 $buttons[btnIndex][btnEvt] = onButtonEvent;103 }104 }105 // Clicking outside the modal dismisses it (if allowed by user)106 _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getOverlay().onclick = onButtonEvent;107 previousWindowKeyDown = window.onkeydown;108 var onKeyEvent = function onKeyEvent(e) {109 return _handleKeyDown2['default'](e, params, modal);110 };111 window.onkeydown = onKeyEvent;112 window.onfocus = function () {113 // When the user has focused away and focused back from the whole window.114 setTimeout(function () {115 // Put in a timeout to jump out of the event sequence.116 // Calling focus() in the event sequence confuses things.117 if (lastFocusedButton !== undefined) {118 lastFocusedButton.focus();119 lastFocusedButton = undefined;120 }121 }, 0);122 };123 // Show alert with enabled buttons always124 swal.enableButtons();125};126/*127 * Set default params for each popup128 * @param {Object} userParams129 */130sweetAlert.setDefaults = swal.setDefaults = function (userParams) {131 if (!userParams) {132 throw new Error('userParams is required');133 }134 if (typeof userParams !== 'object') {135 throw new Error('userParams has to be a object');136 }137 _extend$hexToRgb$isIE8$logStr$colorLuminance.extend(_defaultParams2['default'], userParams);138};139/*140 * Animation when closing modal141 */142sweetAlert.close = swal.close = function () {143 var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();144 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.fadeOut(_sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getOverlay(), 5);145 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.fadeOut(modal, 5);146 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass(modal, 'showSweetAlert');147 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.addClass(modal, 'hideSweetAlert');148 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass(modal, 'visible');149 /*150 * Reset icon animations151 */152 var $successIcon = modal.querySelector('.sa-icon.sa-success');153 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($successIcon, 'animate');154 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($successIcon.querySelector('.sa-tip'), 'animateSuccessTip');155 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($successIcon.querySelector('.sa-long'), 'animateSuccessLong');156 var $errorIcon = modal.querySelector('.sa-icon.sa-error');157 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($errorIcon, 'animateErrorIcon');158 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($errorIcon.querySelector('.sa-x-mark'), 'animateXMark');159 var $warningIcon = modal.querySelector('.sa-icon.sa-warning');160 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($warningIcon, 'pulseWarning');161 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($warningIcon.querySelector('.sa-body'), 'pulseWarningIns');162 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($warningIcon.querySelector('.sa-dot'), 'pulseWarningIns');163 // Reset custom class (delay so that UI changes aren't visible)164 setTimeout(function () {165 var customClass = modal.getAttribute('data-custom-class');166 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass(modal, customClass);167 }, 300);168 // Make page scrollable again169 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass(document.body, 'stop-scrolling');170 // Reset the page to its previous state171 window.onkeydown = previousWindowKeyDown;172 if (window.previousActiveElement) {173 window.previousActiveElement.focus();174 }175 lastFocusedButton = undefined;176 clearTimeout(modal.timeout);177 return true;178};179/*180 * Validation of the input field is done by user181 * If something is wrong => call showInputError with errorMessage182 */183sweetAlert.showInputError = swal.showInputError = function (errorMessage) {184 var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();185 var $errorIcon = modal.querySelector('.sa-input-error');186 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.addClass($errorIcon, 'show');187 var $errorContainer = modal.querySelector('.sa-error-container');188 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.addClass($errorContainer, 'show');189 $errorContainer.querySelector('p').innerHTML = errorMessage;190 setTimeout(function () {191 sweetAlert.enableButtons();192 }, 1);193 modal.querySelector('input').focus();194};195/*196 * Reset input error DOM elements197 */198sweetAlert.resetInputError = swal.resetInputError = function (event) {199 // If press enter => ignore200 if (event && event.keyCode === 13) {201 return false;202 }203 var $modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();204 var $errorIcon = $modal.querySelector('.sa-input-error');205 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($errorIcon, 'show');206 var $errorContainer = $modal.querySelector('.sa-error-container');207 _hasClass$addClass$removeClass$escapeHtml$_show$show$_hide$hide$isDescendant$getTopMargin$fadeIn$fadeOut$fireClick$stopEventPropagation.removeClass($errorContainer, 'show');208};209/*210 * Disable confirm and cancel buttons211 */212sweetAlert.disableButtons = swal.disableButtons = function (event) {213 var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();214 var $confirmButton = modal.querySelector('button.confirm');215 var $cancelButton = modal.querySelector('button.cancel');216 $confirmButton.disabled = true;217 $cancelButton.disabled = true;218};219/*220 * Enable confirm and cancel buttons221 */222sweetAlert.enableButtons = swal.enableButtons = function (event) {223 var modal = _sweetAlertInitialize$getModal$getOverlay$getInput$setFocusStyle$openModal$resetInput$fixVerticalPosition.getModal();224 var $confirmButton = modal.querySelector('button.confirm');225 var $cancelButton = modal.querySelector('button.cancel');226 $confirmButton.disabled = false;227 $cancelButton.disabled = false;228};229if (typeof window !== 'undefined') {230 // The 'handle-click' module requires231 // that 'sweetAlert' was set as global.232 window.sweetAlert = window.swal = sweetAlert;233} else {234 _extend$hexToRgb$isIE8$logStr$colorLuminance.logStr('SweetAlert is a frontend module!');235}...
commonAncestor.js
Source:commonAncestor.js
1const Tree = function () {2 this.children = [];3};4Tree.prototype.addChild = function (child) {5 if (!this.isDescendant(child)) {6 this.children.push(child);7 } else {8 throw new Error("That child is already a child of this tree");9 }10 return this;11};12Tree.prototype.getClosestCommonAncestor = function (item1, item2) {13 if (!this.isDescendant(item1) || !this.isDescendant(item2)) {14 return null;15 }16 if (this.children.indexOf(item1) !== -1 || this.children.indexOf(item2) !== -1) {17 return this;18 }19 for (let i = 0; i < this.children.length; i++) {20 if (this.children[i].isDescendant(item1) || this.children[i].isDescendant(item2)) {21 if (this.children[i].isDescendant(item1) && this.children[i].isDescendant(item2)) {22 return this.children[i].getClosestCommonAncestor(item1, item2);23 } else {24 return this;25 }26 }27 }28};29Tree.prototype.getAncestorPath = function (item) {30 if (!this.isDescendant(item)) {31 return null;32 }33 const path = [this];34 for (let i = 0; i < this.children.length; i++) {35 if (this.children[i] === item) {36 path.push(item);37 return path;38 }39 if (this.children[i].isDescendant(item)) {40 path.push(...this.children[i].getAncestorPath(item));41 return path;42 }43 }44};45Tree.prototype.isDescendant = function (child) {46 if (this.children.indexOf(child) !== -1) {47 return true;48 } else {49 for (let i = 0; i < this.children.length; i++) {50 if (this.children[i].isDescendant(child)) {51 return true;52 }53 }54 return false;55 }56};57Tree.prototype.removeChild = function (child) {58 const index = this.children.indexOf(child);59 if (index !== -1) {60 this.children.splice(index, 1);61 } else {62 throw new Error("That node is not an imediate child of this tree");63 }64};65const grandma = new Tree();66const mom = new Tree();67const me = new Tree();68const uncle = new Tree();69const sister = new Tree();70const superman = new Tree();71mom.addChild(me);72console.assert(mom.isDescendant(me) === true, "should return true when a descendant");73console.assert(mom.isDescendant(superman) === false, "should return false when not a descendant");74console.assert(75 JSON.stringify(mom.getAncestorPath(me)) === JSON.stringify([mom, me]),76 "returns a correct descendant path"77);78grandma.addChild(mom);79console.assert(grandma.isDescendant(mom) === true);80console.assert(grandma.isDescendant(me) === true, "should find descendant for more and just immediate children");81console.assert(82 JSON.stringify(grandma.getAncestorPath(me)) === JSON.stringify([grandma, mom, me]),83 "should find path for more than immediate children"84);85console.assert(grandma.getAncestorPath(superman) === null, "should return null when there is no path");86mom.addChild(sister);87console.assert(88 mom.getClosestCommonAncestor(me, sister) === mom,89 "should return root when the root is the closest common ancestor"90);91console.assert(grandma.getClosestCommonAncestor(me, sister) === mom, "should return the closest common ancestor");92grandma.addChild(uncle);93console.assert(94 grandma.getClosestCommonAncestor(me, uncle) === grandma,...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var isDescendant = wptoolkit.isDescendant;3var isDescendant = require('wptoolkit').isDescendant;4var wptoolkit = require('wptoolkit');5var isDescendant = wptoolkit.isDescendant;6var parent = document.getElementById('parent');7var child = document.getElementById('child');8var notChild = document.getElementById('not-child');
Using AI Code Generation
1var wptools = require('wptools');2var isDescendant = wptools.isDescendant;3var result = isDescendant('en.wikipedia.org', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War', 'en.wikipedia.org/wiki/Revolutionary_War');4var result = isDescendant('en.wikipedia.org', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War', 'en.wikipedia.org/wiki/History_of_the_American_Civil_War');5var result = isDescendant('en.wikipedia.org', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War', 'en.wikipedia.org');6var result = isDescendant('en.wikipedia.org', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War');7var result = isDescendant('en.wikipedia.org', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War#cite_note-1');8var result = isDescendant('en.wikipedia.org', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War#cite_note-1');9var result = isDescendant('en.wikipedia.org', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War#cite_note-1');10var result = isDescendant('en.wikipedia.org', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War#cite_note-1');11var result = isDescendant('en.wikipedia.org', 'en.wikipedia.org/wiki/History_of_the_American_Revolutionary_War', 'en.wikipedia.org/wiki/History_of_the_American_Revolution
Using AI Code Generation
1var isDescendant = wptc.isDescendant;2var isDescendant = wptc.isDescendant(element, parentElement);3var isDescendant = wptc.isDescendant;4var isDescendant = wptc.isDescendant(element, parentElement);5var isDescendant = wptc.isDescendant;6var isDescendant = wptc.isDescendant(element, parentElement);7var isDescendant = wptc.isDescendant;8var isDescendant = wptc.isDescendant(element, parentElement);9var isDescendant = wptc.isDescendant;10var isDescendant = wptc.isDescendant(element, parentElement);11var isDescendant = wptc.isDescendant;12var isDescendant = wptc.isDescendant(element, parentElement);13var isDescendant = wptc.isDescendant;14var isDescendant = wptc.isDescendant(element, parentElement);15var isDescendant = wptc.isDescendant;16var isDescendant = wptc.isDescendant(element, parentElement);17var isDescendant = wptc.isDescendant;
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!