How to use isUndef method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

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

jquery.quiz.js

Source: jquery.quiz.js Github

copy
1
2(function( $ ) {
3
4function isundef(x, y) {
5    return (x == undefined) ? y : x;
6}
7
8QuizSingleHandler = function (question, idQuestion) {
9    this.question = question;
10    this.idQuestion = idQuestion;
11    this.hintsShown = 0;
12    this.attempts = 0;
13}
14
15QuizSingleHandler.prototype = {
16      
17    makeCorrection: function(self) {
18        // when used as a callback, self will be passed as parameter,
19        // since 'this' will be overridden by jquery
20        var self = isundef(self, this);
21        self.question.data('submitted', true);
22        self.attempts += 1;
23        console.log(self.attempts);
24        
25        $('.quiz-radio:checked', self.question).each(function() {
26            var $radio = $(this);
27            var $option = $radio.parent();
28            
29            if ( $option.hasClass('quiz-answer') ) {
30                $option.addClass('quiz-correct');
31                $option.parents('div.quiz').data('attempts', self.attempts);
32                $option.parents('div.quiz').data('status', 'correct');
33                self.explanationVisible(true);  
34            } else {
35                $option.addClass('quiz-wrong');
36                $option.parents('div.quiz').data('attempts', self.attempts);
37                $option.parents('div.quiz').data('status', 'wrong');
38            };
39        });
40    },
41    
42    clear: function(self) {    
43        var self = isundef(self, this);
44        $('.quiz-radio', self.question).removeAttr('checked');
45        self.clearCorrection();
46    },
47    
48    clearCorrection: function(self) {
49        var self = isundef(self, this);
50        
51        if ( self.question.data('submitted') == true ) {
52            $('.quiz-option', self.question).removeClass('quiz-correct quiz-wrong');
53
54            self.question.data('submitted', false);
55        }      
56        
57        self.explanationVisible(false); 
58        self.hintVisible(false);
59    },
60    
61    showAnswer: function(self) {
62        var self = isundef(self, this);
63        self.clear();
64        
65        $('.quiz-answer', self.question).each(function() {
66            $(this).addClass('quiz-correct');
67            $(this).find('.quiz-radio').attr('checked', 'checked');
68        });
69        
70        self.question.data('submitted', true);
71        self.explanationVisible(true);
72    },
73    
74    hintVisible: function(mode, self) {
75        var self = isundef(self, this);
76        mode = (mode) ? 'block' : 'none';
77        $('.quiz-hint', self.question).each(function() {
78          $(this).css('display', mode);
79          // $(this).find('li:lt(' + self.hintsShown + 1, ')').show();
80          // console.log(self.hintsShown);
81          // self.hintsShown++;
82        });
83    },
84    
85    toggleHint: function(self) {
86        var self = isundef(self, this);
87        
88        $('.quiz-hint', self.question).each(function() {
89            if ( $(this).css('display') == 'none' )
90              self.hintVisible(true);
91            else
92              self.hintVisible(false);
93        });
94    },
95    
96    explanationVisible: function(mode, self) {
97        var self = isundef(self, this);
98        
99        mode = (mode) ? 'block' : 'none';
100        
101        $('.quiz-explanation', self.question).each(function() {
102            // commented out to display answer as modal
103            // $(this).css('display', mode); 
104            $(this).css('display', 'none');
105        });
106    },
107    
108    init: function() {   
109        var self = this;
110        
111        // added data-numOption to the template
112        var template = Mustache.compile('<input type="radio" class="quiz-radio" ' +
113        'name="quiz-question-{{idQuestion}}-options" value="{{label}}"' +
114        'data-numOption="{{numOption}}"' +
115        'id="quiz-question-{{idQuestion}}-option-{{numOption}}"/>' +
116        '<label for="quiz-question-{{idQuestion}}-option-{{numOption}}">{{label}}</label>' 
117        );
118        
119        /*
120        var template = Mustache.compile('<label for="quiz-question-{{idQuestion}}-option-{{numOption}}" class="radio">' +
121          '<input type="radio" class="quiz-radio" ' +
122          'name="quiz-question-{{idQuestion}}-options" value="{{label}}"' +
123          'data-numOption="{{numOption}}"' +
124          'id="quiz-question-{{idQuestion}}-option-{{numOption}}"/>' +
125          '{{label}}</label>' 
126        );
127        */
128        
129        // creates radio buttons for the quiz
130
131        var numOption = 0;
132        $('.quiz-answer, .quiz-option', self.question).each(function() {
133            var $this = $(this);
134            $this.addClass('quiz-option');
135            $this.html(template({
136                idQuestion: self.idQuestion, 
137                numOption: ++numOption, 
138                label: $this.text() 
139            }));
140        });
141        
142        // bind clear to the quiz-clear elements, if any
143        $('.quiz-clear', self.question).each(function() {
144            $(this).bind('click.clear', function() { self.clear(self) });
145        });
146       
147        // clear correction if there are correct/wrong classes but the options checked changed
148        $('.quiz-radio', self.question).each(function() {
149            $(this).bind('click.clearCorrection', function() { self.clearCorrection(self) });
150        });
151
152        // bind makeCorrection to the quiz-submit elements, if any
153        $('.quiz-submit', self.question).each(function() {
154            $(this).bind('click.makeCorrection', function() { self.makeCorrection(self) });
155        });
156        
157        // bind makeCorrection to the quiz-submit elements, if any
158        $('.quiz-show-answer', self.question).each(function() {
159            $(this).bind('click.showAnswer', function() { self.showAnswer(self) });
160        });
161        
162        $('.quiz-toggle-hint', self.question).each(function() {
163            $(this).bind('click.toggleHint', function() { self.toggleHint(self) });
164        });
165
166        self.hintVisible(false);
167        self.explanationVisible(false);
168    }
169}
170
171QuizMultipleHandler = function (question, idQuestion) {
172    this.question = question;
173    this.idQuestion = idQuestion;
174}
175
176QuizMultipleHandler.prototype = {
177
178    _individual: function(self) {
179        var self = isundef(self, this);
180        self.question.data('submitted', true);
181        
182        var isCorrect = true;
183
184        $('.quiz-checkbox', self.question).each(function() {
185            var $checkbox = $(this);
186            var $option = $checkbox.parent();
187            
188            isCorrect = isCorrect && 
189                ($option.hasClass('quiz-answer') == $checkbox.is(':checked'));
190        });
191        
192        $('.quiz-checkbox:checked', self.question).each(function() {
193            var $checkbox = $(this);
194            var $option = $checkbox.parent();
195            
196            if ( $option.hasClass('quiz-answer') == $checkbox.is(':checked') )
197                $option.addClass('quiz-correct');
198            else
199                $option.addClass('quiz-wrong');
200        });
201        
202        if ( isCorrect ) 
203            self.explanationVisible(true);
204    },
205    
206    _whole: function(self) {
207        var self = isundef(self, this);
208        self.question.data('submitted', true);
209        
210        var isCorrect = true;
211
212        $('.quiz-checkbox', self.question).each(function() {
213            var $checkbox = $(this);
214            var $option = $checkbox.parent();
215            
216            isCorrect = isCorrect && 
217                ($option.hasClass('quiz-answer') == $checkbox.is(':checked'));
218        });
219        
220        $('.quiz-checkbox:checked', self.question).each(function() {
221            var $checkbox = $(this);
222            var $option = $checkbox.parent();
223            
224            if ( isCorrect )
225                $option.addClass('quiz-correct');    
226            else
227                $option.addClass('quiz-wrong');
228        });
229        
230        if ( isCorrect ) 
231            self.explanationVisible(true);
232    },
233    
234    clear: function(self) {    
235        var self = isundef(self, this);
236        $('.quiz-checkbox', self.question).removeAttr('checked');
237        self.clearCorrection();
238    },
239    
240    clearCorrection: function(self) {
241        var self = isundef(self, this);
242        
243        if ( self.question.data('submitted') == true ) {
244            $('.quiz-option', self.question).removeClass('quiz-correct quiz-wrong');
245
246            self.question.data('submitted', false);
247        }   
248        
249        self.explanationVisible(false);             
250    },
251      
252    showAnswer: function(self) {
253        var self = isundef(self, this);
254        self.clear();
255        
256        $('.quiz-answer', self.question).each(function() {
257            var $this = $(this);
258            $this.addClass('quiz-correct');
259            
260            $('.quiz-checkbox', $this).each(function() {
261                $(this).attr('checked', 'checked');
262            });
263        });
264        
265        self.question.data('submitted', true);
266        self.explanationVisible(true);
267    },
268    
269    hintVisible: function(mode, self) {
270        var self = isundef(self, this);
271        mode = (mode) ? 'block' : 'none';
272        $('.quiz-hint', self.question).each(function() {
273            $(this).css('display', mode);
274        });
275    },
276    
277    toggleHint: function(self) {
278        var self = isundef(self, this);
279        
280        $('.quiz-hint', self.question).each(function() {
281            if ( $(this).css('display') == 'none' )
282                self.hintVisible(true);
283            else
284                self.hintVisible(false);
285        });
286    },
287    
288    explanationVisible: function(mode, self) {
289        var self = isundef(self, this);
290        
291        mode = (mode) ? 'block' : 'none';
292        
293        $('.quiz-explanation', self.question).each(function() {
294          $(this).css('display', 'none');
295          // $(this).css('display', mode);
296        });
297    },
298      
299    init: function() {   
300        var self = this;
301        
302        var template = Mustache.compile('<input type="checkbox" class="quiz-checkbox" ' +
303        'id="quiz-question-{{idQuestion}}-option-{{numOption}}"/>' +
304        '<label for="quiz-question-{{idQuestion}}-option-{{numOption}}">{{label}}</label>');
305        
306        var numOption = 0;
307
308        $('.quiz-answer, .quiz-option', self.question).each(function() {
309            var $this = $(this);
310            $this.addClass('quiz-option');
311            $this.html(template({
312                idQuestion: self.idQuestion, 
313                numOption: ++numOption, 
314                label: $this.html() 
315            }));
316        });
317        
318        if ( self.question.attr('data-individual') != undefined ) {
319            self.makeCorrection = self._individual;
320        } else {
321            self.makeCorrection = self._whole;
322        }
323        
324        $('.quiz-clear', self.question).each(function() {
325            $(this).bind('click.clear', function() { self.clear(self) });
326        });
327       
328        $('.quiz-checkbox', self.question).each(function() {
329            $(this).bind('click.clearCorrection', function() { self.clearCorrection(self) });
330        });
331        
332        $('.quiz-submit', self.question).each(function() {
333            $(this).bind('click.makeCorrection', function() { self.makeCorrection(self) });
334        });
335        
336        $('.quiz-show-answer', self.question).each(function() {
337            $(this).bind('click.showAnswer', function() { self.showAnswer(self) });
338        });
339        
340        $('.quiz-toggle-hint', self.question).each(function() {
341            $(this).bind('click.toggleHint', function() { self.toggleHint(self) });
342        });
343
344        self.hintVisible(false);
345        self.explanationVisible(false);
346    }
347}
348
349QuizTextHandler = function(question, idQuestion) { 
350    this.question = question;
351    this.idQuestion = idQuestion;
352}
353
354QuizTextHandler.prototype = {
355  
356    makeCorrection: function(self){
357      var self = isundef(self, this);
358      self.clearCorrection();
359      self.question.data('submitted', true);
360      
361      $(".quiz-answerbox", self.question).each(function(){
362        var userAnswer = parseFloat($(this).val());
363        self.answer = $(this).data("answer");
364        if (Math.abs(userAnswer - self.answer)/self.answer < 0.02){
365          self.explanationVisible(true);
366          $(this).addClass('quiz-correct')
367        } else {
368          $(this).addClass('quiz-wrong')
369        }
370      });
371    },
372    
373    makeCorrection2: function(self) {
374        var self = isundef(self, this);
375        self.clearCorrection();
376        self.question.data('submitted', true);
377
378        var userAnswer = '';
379        $('.quiz-answerbox', self.question).each(function() {
380            var $answerbox = $(this);
381            userAnswer = $answerbox.val();
382            self.answer = $(this).attr("data-answer");
383        });
384        
385        if ( !self.caseSensitive ) {
386            userAnswer = userAnswer.toLowerCase();
387            if ( self.answer )
388                self.answer = self.answer.toLowerCase();
389        }
390        
391        if ( !self.notrim ) {
392            userAnswer = $.trim(userAnswer);
393            self.answer = $.trim(self.answer);
394            if ( self.answer )
395                self.answer = self.answer.toLowerCase();
396        }
397        
398        if ( userAnswer == self.answer || self.validator(userAnswer) ) {
399            self.explanationVisible(true);
400            $('.quiz-answerbox', self.question).each(function() {
401                $(this).addClass('quiz-correct');
402            });
403        } else 
404            $('.quiz-answerbox', self.question).each(function() {
405                $(this).addClass('quiz-wrong');
406            });
407    },
408    
409    hintVisible: function(mode, self) {
410        var self = isundef(self, this);
411        mode = (mode) ? 'block' : 'none';
412        $('.quiz-hint', self.question).each(function() {
413            $(this).css('display', mode);
414        });
415    },
416    
417    toggleHint: function(self) {
418        var self = isundef(self, this);
419        
420        $('.quiz-hint', self.question).each(function() {
421            if ( $(this).css('display') == 'none' )
422                self.hintVisible(true);
423            else
424                self.hintVisible(false);
425        });
426    },
427    
428    clear: function(self) {    
429        var self = isundef(self, this);
430        $('.quiz-answerbox', self.question).val('');
431        self.clearCorrection();
432    },
433    
434    clearCorrection: function(self) {
435        var self = isundef(self, this); 
436        
437        if ( self.question.data('submitted') == true ) {
438            $('.quiz-answerbox', self.question).removeClass('quiz-correct quiz-wrong');
439            self.question.data('submitted', false);
440        }   
441        
442        self.explanationVisible(false);             
443    },
444    
445    showAnswer: function(self) {
446        // if ( self.answer == undefined ) 
447        //   return;
448    
449        var self = isundef(self, this);
450        self.clear();
451        
452        $('.quiz-answerbox', self.question).each(function() {
453            var $this = $(this);
454            self.answer = $this.attr("data-answer");
455            $this.val(self.answer);
456            $this.addClass('quiz-correct');
457        });
458        
459        self.question.data('submitted', true);
460        self.explanationVisible(true);
461    },
462    
463    explanationVisible: function(mode, self) {
464        var self = isundef(self, this);
465        
466        mode = (mode) ? 'block' : 'none';
467      
468        $('.quiz-explanation', self.question).each(function() {
469            $(this).css('display', 'none')
470            // $(this).css('display', mode);
471        });
472        
473    },
474    
475    init: function() {
476        var self = this;
477        
478        self.answer = self.question.attr('data-answer');
479        
480        if ( self.question.attr('data-validator') != undefined )
481            self.validator = eval("(" + self.question.attr('data-validator') + ")"); 
482           
483        self.validator = isundef(self.validator, function(x) { return false; });
484            
485        self.caseSensitive = self.question.attr('data-sensitive') != undefined;
486        self.notrim = self.question.attr('data-notrim') != undefined; 
487        
488        $('.quiz-clear', self.question).each(function() {
489            $(this).bind('click.clear', function() { self.clear(self) });
490        });
491        
492        $('.quiz-submit', self.question).each(function() {
493            $(this).bind('click.makeCorrection', function() { self.makeCorrection(self) });
494        });
495        
496        $('.quiz-answerbox', self.question).each(function() {
497            $(this).bind('change.clearCorrection', function() { self.clearCorrection(self) });
498        });
499        
500        $('.quiz-show-answer', self.question).each(function() {
501            $(this).bind('click.showAnswer', function() { self.showAnswer(self) });
502        });
503        
504        $('.quiz-toggle-hint', self.question).each(function() {
505            $(this).bind('click.toggleHint', function() { self.toggleHint(self) });
506        }); 
507
508        self.hintVisible(false);
509        self.explanationVisible(false);
510    }
511}
512
513$.quiz = function($context) {
514    var self = $.quiz;
515    // if no context is given, set it as document  
516    $context = ($context == undefined) ? $(document) : $context;
517    var handler = self.getHandler($context);
518    
519    // if it doesn't have a handler, check all children for handlers
520    if ( handler == undefined ) {
521        for (handler in self.handlers)
522            $('.' + handler, $context).each(function() {
523                var $this = $(this);
524                var newQuiz = new self.handlers[handler]($this, self.getId($this));
525                self.quizzes.push(newQuiz);
526                newQuiz.init();
527            });
528    } // if it has, make it a quiz
529    else {
530        var newQuiz = new self.handlers[handler]($context, self.getId($context));
531        self.quizzes.push(newQuiz);
532        newQuiz.init();
533    }
534}
535
536$.quiz.getId = function($element) {
537    self.counter = (self.counter == undefined) ? 0 : self.counter;
538    if ( $element.attr('id') != undefined )
539        return $element.attr('id');
540    else
541        return ++self.counter;
542}
543
544$.quiz.getHandler = function($context) {
545    for (var handler in $.quiz.handlers)
546        if ( $context.hasClass(handler) ) 
547            return handler;
548    
549    return undefined;
550}
551
552$.quiz.handlers = {
553    'quiz-single': QuizSingleHandler,
554    'quiz-multiple': QuizMultipleHandler,
555    'quiz-text': QuizTextHandler
556};
557
558$.quiz.quizzes = [];
559
560$.fn.quiz = function() {
561    $.quiz(this);
562    return this;
563}
564
565})(jQuery);
566
Full Screen

build.js

Source: build.js Github

copy
1(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
2'use strict';
3
4var snabbdom = require('../../snabbdom.js');
5var patch = snabbdom.init([require('../../modules/class'), require('../../modules/props'), require('../../modules/style'), require('../../modules/eventlisteners')]);
6var h = require('../../h.js');
7
8var vnode;
9
10var nextKey = 11;
11var margin = 8;
12var sortBy = 'rank';
13var totalHeight = 0;
14var originalData = [{ rank: 1, title: 'The Shawshank Redemption', desc: 'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.', elmHeight: 0 }, { rank: 2, title: 'The Godfather', desc: 'The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.', elmHeight: 0 }, { rank: 3, title: 'The Godfather: Part II', desc: 'The early life and career of Vito Corleone in 1920s New York is portrayed while his son, Michael, expands and tightens his grip on his crime syndicate stretching from Lake Tahoe, Nevada to pre-revolution 1958 Cuba.', elmHeight: 0 }, { rank: 4, title: 'The Dark Knight', desc: 'When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, the caped crusader must come to terms with one of the greatest psychological tests of his ability to fight injustice.', elmHeight: 0 }, { rank: 5, title: 'Pulp Fiction', desc: 'The lives of two mob hit men, a boxer, a gangster\'s wife, and a pair of diner bandits intertwine in four tales of violence and redemption.', elmHeight: 0 }, { rank: 6, title: 'Schindler\'s List', desc: 'In Poland during World War II, Oskar Schindler gradually becomes concerned for his Jewish workforce after witnessing their persecution by the Nazis.', elmHeight: 0 }, { rank: 7, title: '12 Angry Men', desc: 'A dissenting juror in a murder trial slowly manages to convince the others that the case is not as obviously clear as it seemed in court.', elmHeight: 0 }, { rank: 8, title: 'The Good, the Bad and the Ugly', desc: 'A bounty hunting scam joins two men in an uneasy alliance against a third in a race to find a fortune in gold buried in a remote cemetery.', elmHeight: 0 }, { rank: 9, title: 'The Lord of the Rings: The Return of the King', desc: 'Gandalf and Aragorn lead the World of Men against Sauron\'s army to draw his gaze from Frodo and Sam as they approach Mount Doom with the One Ring.', elmHeight: 0 }, { rank: 10, title: 'Fight Club', desc: 'An insomniac office worker looking for a way to change his life crosses paths with a devil-may-care soap maker and they form an underground fight club that evolves into something much, much more...', elmHeight: 0 }];
15var data = [originalData[0], originalData[1], originalData[2], originalData[3], originalData[4], originalData[5], originalData[6], originalData[7], originalData[8], originalData[9]];
16
17function changeSort(prop) {
18  sortBy = prop;
19  data.sort(function (a, b) {
20    if (a[prop] > b[prop]) {
21      return 1;
22    }
23    if (a[prop] < b[prop]) {
24      return -1;
25    }
26    return 0;
27  });
28  render();
29}
30
31function add() {
32  var n = originalData[Math.floor(Math.random() * 10)];
33  data = [{ rank: nextKey++, title: n.title, desc: n.desc, elmHeight: 0 }].concat(data);
34  render();
35  render();
36}
37
38function remove(movie) {
39  data = data.filter(function (m) {
40    return m !== movie;
41  });
42  render();
43}
44
45function movieView(movie) {
46  return h('div.row', {
47    key: movie.rank,
48    style: { opacity: '0', transform: 'translate(-200px)',
49      delayed: { transform: 'translateY(' + movie.offset + 'px)', opacity: '1' },
50      remove: { opacity: '0', transform: 'translateY(' + movie.offset + 'px) translateX(200px)' } },
51    hook: { insert: function insert(vnode) {
52        movie.elmHeight = vnode.elm.offsetHeight;
53      } } }, [h('div', { style: { fontWeight: 'bold' } }, movie.rank), h('div', movie.title), h('div', movie.desc), h('div.btn.rm-btn', { on: { click: [remove, movie] } }, 'x')]);
54}
55
56function render() {
57  data = data.reduce(function (acc, m) {
58    var last = acc[acc.length - 1];
59    m.offset = last ? last.offset + last.elmHeight + margin : margin;
60    return acc.concat(m);
61  }, []);
62  totalHeight = data[data.length - 1].offset + data[data.length - 1].elmHeight;
63  vnode = patch(vnode, view(data));
64}
65
66function view(data) {
67  return h('div', [h('h1', 'Top 10 movies'), h('div', [h('a.btn.add', { on: { click: add } }, 'Add'), 'Sort by: ', h('span.btn-group', [h('a.btn.rank', { 'class': { active: sortBy === 'rank' }, on: { click: [changeSort, 'rank'] } }, 'Rank'), h('a.btn.title', { 'class': { active: sortBy === 'title' }, on: { click: [changeSort, 'title'] } }, 'Title'), h('a.btn.desc', { 'class': { active: sortBy === 'desc' }, on: { click: [changeSort, 'desc'] } }, 'Description')])]), h('div.list', { style: { height: totalHeight + 'px' } }, data.map(movieView))]);
68}
69
70window.addEventListener('DOMContentLoaded', function () {
71  var container = document.getElementById('container');
72  vnode = patch(container, view(data));
73  render();
74});
75
76},{"../../h.js":2,"../../modules/class":4,"../../modules/eventlisteners":5,"../../modules/props":6,"../../modules/style":7,"../../snabbdom.js":8}],2:[function(require,module,exports){
77'use strict';
78
79var VNode = require('./vnode');
80var is = require('./is');
81
82module.exports = function h(sel, b, c) {
83  var data = {},
84      children,
85      text,
86      i;
87  if (arguments.length === 3) {
88    data = b;
89    if (is.array(c)) {
90      children = c;
91    } else if (is.primitive(c)) {
92      text = c;
93    }
94  } else if (arguments.length === 2) {
95    if (is.array(b)) {
96      children = b;
97    } else if (is.primitive(b)) {
98      text = b;
99    } else {
100      data = b;
101    }
102  }
103  if (is.array(children)) {
104    for (i = 0; i < children.length; ++i) {
105      if (is.primitive(children[i])) children[i] = VNode(undefined, undefined, undefined, children[i]);
106    }
107  }
108  return VNode(sel, data, children, text, undefined);
109};
110
111},{"./is":3,"./vnode":9}],3:[function(require,module,exports){
112'use strict';
113
114module.exports = {
115  array: Array.isArray,
116  primitive: function primitive(s) {
117    return typeof s === 'string' || typeof s === 'number';
118  } };
119
120},{}],4:[function(require,module,exports){
121'use strict';
122
123function updateClass(oldVnode, vnode) {
124  var cur,
125      name,
126      elm = vnode.elm,
127      oldClass = oldVnode.data['class'] || {},
128      klass = vnode.data['class'] || {};
129  for (name in klass) {
130    cur = klass[name];
131    if (cur !== oldClass[name]) {
132      elm.classList[cur ? 'add' : 'remove'](name);
133    }
134  }
135}
136
137module.exports = { create: updateClass, update: updateClass };
138
139},{}],5:[function(require,module,exports){
140'use strict';
141
142var is = require('../is');
143
144function arrInvoker(arr) {
145  return function () {
146    arr[0](arr[1]);
147  };
148}
149
150function updateEventListeners(oldVnode, vnode) {
151  var name,
152      cur,
153      old,
154      elm = vnode.elm,
155      oldOn = oldVnode.data.on || {},
156      on = vnode.data.on;
157  if (!on) return;
158  for (name in on) {
159    cur = on[name];
160    old = oldOn[name];
161    if (old === undefined) {
162      elm.addEventListener(name, is.array(cur) ? arrInvoker(cur) : cur);
163    } else if (is.array(old)) {
164      old[0] = cur[0]; // Deliberately modify old array since it's
165      old[1] = cur[1]; // captured in closure created with `arrInvoker`
166    }
167  }
168}
169
170module.exports = { create: updateEventListeners, update: updateEventListeners };
171
172},{"../is":3}],6:[function(require,module,exports){
173"use strict";
174
175function updateProps(oldVnode, vnode) {
176  var key,
177      cur,
178      old,
179      elm = vnode.elm,
180      oldProps = oldVnode.data.props || {},
181      props = vnode.data.props || {};
182  for (key in props) {
183    cur = props[key];
184    old = oldProps[key];
185    if (old !== cur) {
186      elm[key] = cur;
187    }
188  }
189}
190
191module.exports = { create: updateProps, update: updateProps };
192
193},{}],7:[function(require,module,exports){
194'use strict';
195
196var raf = requestAnimationFrame || setTimeout;
197var nextFrame = function nextFrame(fn) {
198  raf(function () {
199    raf(fn);
200  });
201};
202
203function setNextFrame(obj, prop, val) {
204  nextFrame(function () {
205    obj[prop] = val;
206  });
207}
208
209function updateStyle(oldVnode, vnode) {
210  var cur,
211      name,
212      elm = vnode.elm,
213      oldStyle = oldVnode.data.style || {},
214      style = vnode.data.style || {},
215      oldHasDel = ('delayed' in oldStyle);
216  for (name in style) {
217    cur = style[name];
218    if (name === 'delayed') {
219      for (name in style.delayed) {
220        cur = style.delayed[name];
221        if (!oldHasDel || cur !== oldStyle.delayed[name]) {
222          setNextFrame(elm.style, name, cur);
223        }
224      }
225    } else if (name !== 'remove' && cur !== oldStyle[name]) {
226      elm.style[name] = cur;
227    }
228  }
229}
230
231function applyDestroyStyle(vnode) {
232  var style,
233      name,
234      elm = vnode.elm,
235      s = vnode.data.style;
236  if (!s || !(style = s.destroy)) return;
237  for (name in style) {
238    elm.style[name] = style[name];
239  }
240}
241
242function applyRemoveStyle(vnode, rm) {
243  var s = vnode.data.style;
244  if (!s || !s.remove) {
245    rm();
246    return;
247  }
248  var name,
249      elm = vnode.elm,
250      idx,
251      i = 0,
252      maxDur = 0,
253      compStyle,
254      style = s.remove,
255      amount = 0;
256  var applied = [];
257  for (name in style) {
258    applied.push(name);
259    elm.style[name] = style[name];
260  }
261  compStyle = getComputedStyle(elm);
262  var props = compStyle['transition-property'].split(', ');
263  for (; i < props.length; ++i) {
264    if (applied.indexOf(props[i]) !== -1) amount++;
265  }
266  elm.addEventListener('transitionend', function (ev) {
267    if (ev.target === elm) --amount;
268    if (amount === 0) rm();
269  });
270}
271
272module.exports = { create: updateStyle, update: updateStyle, destroy: applyDestroyStyle, remove: applyRemoveStyle };
273
274},{}],8:[function(require,module,exports){
275// jshint newcap: false
276'use strict';
277
278var VNode = require('./vnode');
279var is = require('./is');
280
281function isUndef(s) {
282  return s === undefined;
283}
284
285function emptyNodeAt(elm) {
286  return VNode(elm.tagName, {}, [], undefined, elm);
287}
288
289var emptyNode = VNode('', {}, [], undefined, undefined);
290
291var insertedVnodeQueue;
292
293function sameVnode(vnode1, vnode2) {
294  return vnode1.key === vnode2.key && vnode1.sel === vnode2.sel;
295}
296
297function createKeyToOldIdx(children, beginIdx, endIdx) {
298  var i,
299      map = {},
300      key;
301  for (i = beginIdx; i <= endIdx; ++i) {
302    key = children[i].key;
303    if (!isUndef(key)) map[key] = i;
304  }
305  return map;
306}
307
308function createRmCb(parentElm, childElm, listeners) {
309  return function () {
310    if (--listeners === 0) parentElm.removeChild(childElm);
311  };
312}
313
314var hooks = ['create', 'update', 'remove', 'destroy', 'pre', 'post'];
315
316function init(modules) {
317  var i,
318      j,
319      cbs = {};
320  for (i = 0; i < hooks.length; ++i) {
321    cbs[hooks[i]] = [];
322    for (j = 0; j < modules.length; ++j) {
323      if (modules[j][hooks[i]] !== undefined) cbs[hooks[i]].push(modules[j][hooks[i]]);
324    }
325  }
326
327  function createElm(vnode) {
328    var i;
329    if (!isUndef(i = vnode.data) && !isUndef(i = i.hook) && !isUndef(i = i.init)) {
330      i(vnode);
331    }
332    if (!isUndef(i = vnode.data) && !isUndef(i = i.vnode)) vnode = i;
333    var elm,
334        children = vnode.children,
335        sel = vnode.sel;
336    if (!isUndef(sel)) {
337      // Parse selector
338      var hashIdx = sel.indexOf('#');
339      var dotIdx = sel.indexOf('.', hashIdx);
340      var hash = hashIdx > 0 ? hashIdx : sel.length;
341      var dot = dotIdx > 0 ? dotIdx : sel.length;
342      var tag = hashIdx !== -1 || dotIdx !== -1 ? sel.slice(0, Math.min(hash, dot)) : sel;
343      elm = vnode.elm = document.createElement(tag);
344      if (hash < dot) elm.id = sel.slice(hash + 1, dot);
345      if (dotIdx > 0) elm.className = sel.slice(dot + 1).replace(/\./g, ' ');
346      if (is.array(children)) {
347        for (i = 0; i < children.length; ++i) {
348          elm.appendChild(createElm(children[i]));
349        }
350      } else if (is.primitive(vnode.text)) {
351        elm.appendChild(document.createTextNode(vnode.text));
352      }
353      for (i = 0; i < cbs.create.length; ++i) cbs.create[i](emptyNode, vnode);
354      i = vnode.data.hook; // Reuse variable
355      if (!isUndef(i)) {
356        if (i.create) i.create(vnode);
357        if (i.insert) insertedVnodeQueue.push(vnode);
358      }
359    } else {
360      elm = vnode.elm = document.createTextNode(vnode.text);
361    }
362    return elm;
363  }
364
365  function addVnodes(parentElm, before, vnodes, startIdx, endIdx) {
366    if (isUndef(before)) {
367      for (; startIdx <= endIdx; ++startIdx) {
368        parentElm.appendChild(createElm(vnodes[startIdx]));
369      }
370    } else {
371      var elm = before.elm;
372      for (; startIdx <= endIdx; ++startIdx) {
373        parentElm.insertBefore(createElm(vnodes[startIdx]), elm);
374      }
375    }
376  }
377
378  function invokeDestroyHook(vnode) {
379    var i = vnode.data.hook,
380        j;
381    if (!isUndef(i) && !isUndef(j = i.destroy)) j(vnode);
382    for (i = 0; i < cbs.destroy.length; ++i) cbs.destroy[i](vnode);
383    if (!isUndef(vnode.children)) {
384      for (j = 0; j < vnode.children.length; ++j) {
385        invokeDestroyHook(vnode.children[j]);
386      }
387    }
388  }
389
390  function removeVnodes(parentElm, vnodes, startIdx, endIdx) {
391    for (; startIdx <= endIdx; ++startIdx) {
392      var i,
393          listeners,
394          rm,
395          ch = vnodes[startIdx];
396      if (!isUndef(ch)) {
397        listeners = cbs.remove.length + 1;
398        rm = createRmCb(parentElm, ch.elm, listeners);
399        for (i = 0; i < cbs.remove.length; ++i) cbs.remove[i](ch, rm);
400        invokeDestroyHook(ch);
401        if (ch.data.hook && ch.data.hook.remove) {
402          ch.data.hook.remove(ch, rm);
403        } else {
404          rm();
405        }
406      }
407    }
408  }
409
410  function updateChildren(parentElm, oldCh, newCh) {
411    var oldStartIdx = 0,
412        newStartIdx = 0;
413    var oldEndIdx = oldCh.length - 1;
414    var oldStartVnode = oldCh[0];
415    var oldEndVnode = oldCh[oldEndIdx];
416    var newEndIdx = newCh.length - 1;
417    var newStartVnode = newCh[0];
418    var newEndVnode = newCh[newEndIdx];
419    var oldKeyToIdx, idxInOld, elmToMove;
420
421    while (oldStartIdx <= oldEndIdx && newStartIdx <= newEndIdx) {
422      if (isUndef(oldStartVnode)) {
423        oldStartVnode = oldCh[++oldStartIdx]; // Vnode has been moved left
424      } else if (isUndef(oldEndVnode)) {
425        oldEndVnode = oldCh[--oldEndIdx];
426      } else if (sameVnode(oldStartVnode, newStartVnode)) {
427        patchVnode(oldStartVnode, newStartVnode);
428        oldStartVnode = oldCh[++oldStartIdx];
429        newStartVnode = newCh[++newStartIdx];
430      } else if (sameVnode(oldEndVnode, newEndVnode)) {
431        patchVnode(oldEndVnode, newEndVnode);
432        oldEndVnode = oldCh[--oldEndIdx];
433        newEndVnode = newCh[--newEndIdx];
434      } else if (sameVnode(oldStartVnode, newEndVnode)) {
435        // Vnode moved right
436        patchVnode(oldStartVnode, newEndVnode);
437        parentElm.insertBefore(oldStartVnode.elm, oldEndVnode.elm.nextSibling);
438        oldStartVnode = oldCh[++oldStartIdx];
439        newEndVnode = newCh[--newEndIdx];
440      } else if (sameVnode(oldEndVnode, newStartVnode)) {
441        // Vnode moved left
442        patchVnode(oldEndVnode, newStartVnode);
443        parentElm.insertBefore(oldEndVnode.elm, oldStartVnode.elm);
444        oldEndVnode = oldCh[--oldEndIdx];
445        newStartVnode = newCh[++newStartIdx];
446      } else {
447        if (isUndef(oldKeyToIdx)) oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);
448        idxInOld = oldKeyToIdx[newStartVnode.key];
449        if (isUndef(idxInOld)) {
450          // New element
451          parentElm.insertBefore(createElm(newStartVnode), oldStartVnode.elm);
452          newStartVnode = newCh[++newStartIdx];
453        } else {
454          elmToMove = oldCh[idxInOld];
455          patchVnode(elmToMove, newStartVnode);
456          oldCh[idxInOld] = undefined;
457          parentElm.insertBefore(elmToMove.elm, oldStartVnode.elm);
458          newStartVnode = newCh[++newStartIdx];
459        }
460      }
461    }
462    if (oldStartIdx > oldEndIdx) addVnodes(parentElm, oldStartVnode, newCh, newStartIdx, newEndIdx);else if (newStartIdx > newEndIdx) removeVnodes(parentElm, oldCh, oldStartIdx, oldEndIdx);
463  }
464
465  function patchVnode(oldVnode, vnode) {
466    var i;
467    if (!isUndef(i = vnode.data) && !isUndef(i = i.hook) && !isUndef(i = i.patch)) {
468      i = i(oldVnode, vnode);
469    }
470    if (!isUndef(i = oldVnode.data) && !isUndef(i = i.vnode)) oldVnode = i;
471    if (!isUndef(i = vnode.data) && !isUndef(i = i.vnode)) vnode = i;
472    var elm = vnode.elm = oldVnode.elm,
473        oldCh = oldVnode.children,
474        ch = vnode.children;
475    if (oldVnode === vnode) return;
476    if (!isUndef(vnode.data)) {
477      for (i = 0; i < cbs.update.length; ++i) cbs.update[i](oldVnode, vnode);
478      i = vnode.data.hook;
479      if (!isUndef(i) && !isUndef(i = i.update)) i(vnode);
480    }
481    if (isUndef(vnode.text)) {
482      if (!isUndef(oldCh) && !isUndef(ch)) {
483        if (oldCh !== ch) updateChildren(elm, oldCh, ch);
484      } else if (!isUndef(ch)) {
485        addVnodes(elm, undefined, ch, 0, ch.length - 1);
486      } else if (!isUndef(oldCh)) {
487        removeVnodes(elm, oldCh, 0, oldCh.length - 1);
488      }
489    } else if (oldVnode.text !== vnode.text) {
490      elm.childNodes[0].nodeValue = vnode.text;
491    }
492    return vnode;
493  }
494
495  return function (oldVnode, vnode) {
496    var i;
497    insertedVnodeQueue = [];
498    if (oldVnode instanceof Element) {
499      oldVnode = emptyNodeAt(oldVnode);
500    }
501    for (i = 0; i < cbs.pre.length; ++i) cbs.pre[i]();
502    patchVnode(oldVnode, vnode);
503    for (i = 0; i < insertedVnodeQueue.length; ++i) {
504      insertedVnodeQueue[i].data.hook.insert(insertedVnodeQueue[i]);
505    }
506    insertedVnodeQueue = undefined;
507    for (i = 0; i < cbs.post.length; ++i) cbs.post[i]();
508    return vnode;
509  };
510}
511
512module.exports = { init: init };
513
514},{"./is":3,"./vnode":9}],9:[function(require,module,exports){
515"use strict";
516
517module.exports = function (sel, data, children, text, elm) {
518  var key = data === undefined ? undefined : data.key;
519  return { sel: sel, data: data, children: children,
520    text: text, elm: elm, key: key };
521};
522
523},{}]},{},[1]);
524
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)