<commit_before>import elrUI from 'elr-ui';
const $ = require('jquery');

let ui = elrUI();

const elrSimpleSlider = function({
    sliderClass = 'elr-simple-slider',
    slideClass = 'elr-simple-slide',
    slideHolderClass = 'elr-simple-slide-holder',
    effect = 'fade',
    navClass = 'elr-simple-slider-nav',
    slideListClass = 'elr-simple-slider-list',
    speed = 500,
    interval = 10000,
    auto = true
} = {}) {
    const $slider = $(`.${sliderClass}`);
    let isAnimating = false;
    const self = {
        createSlideList($slides) {
            let li = '';

            $.each($slides, function(index) {
                if (index === 0) {
                    li += `<li><button class="active" data-item-num="${index}"></button></li>`;
                } else {
                    li += `<li><button data-item-num="${index}"></button></li>`;
                }
            });

            return ui.createElement('ul', {
                'class': slideListClass,
                'html': li
            });
        },
        getCurrent($slideHolder) {
            const $slides = $slideHolder.find(`.${slideClass}`);

            if (effect === 'fade') {
                return $slides.not(':hidden').index();
            } else if (effect === 'slide-left') {
                return (Math.abs($slideHolder.position().left) / parseInt($slides.first().width(), 10));
            }
        },
        goToSlide(current, slideNum, $slideHolder) {
            const $slides = $slideHolder.find(`.${slideClass}`);

            if (effect === 'fade') {
                $slides.eq(current).fadeOut(speed);
                $slides.eq(slideNum).fadeIn(speed);
            } else if (effect === 'slide-left') {
                const slideWidth = parseInt($slides.first().width(), 10);
                const pos = $slideHolder.position().left;
                let newPos;

                if (current < slideNum) {
                    const slideDiff = current - slideNum;
                    newPos = pos + (slideWidth * slideDiff);
                } else if (current > slideNum) {
                    const slideDiff = -(current - slideNum);
                    newPos = pos - (slideWidth * slideDiff);
                }

                $slideHolder.stop().animate({
                    left: newPos
                }, speed);
            }
        },
        fadeSlide(current, dir, $slides) {
            const lastSlide = $slides.length - 1;

            $slides.eq(current).fadeOut(speed);

            if (dir === 'next' && current === lastSlide) {
                $slides.first().fadeIn(speed);

                return 0;
            } else if (dir === 'next') {
                $slides.eq(current + 1).fadeIn(speed);

                return current + 1;
            } else if (dir === 'prev' && current === 0) {
                $slides.eq(lastSlide).fadeIn(speed);

                return lastSlide;
            } else {
                $slides.eq(current - 1).fadeIn(speed);

                return current - 1;
            }
        },
        slideLeft(current, dir, $slides, $slideHolder) {
            // TODO: fix bug where paging doesn't work properly when paging from first to last slide

            const lastSlide = $slides.length - 1;
            const slideWidth = parseInt($slides.first().width(), 10);
            const pos = $slideHolder.position().left;

            isAnimating = true;

            if (dir === 'next' && current === lastSlide) {
                let $oldSlides = $slides;
                let $newSlides = $slides.clone();
                let numSlides = $slides.length;

                $slideHolder.css('width', slideWidth * (numSlides * 2));
                $slideHolder.append($newSlides);
                let newPos = pos - slideWidth;

                $slideHolder.stop().animate({
                    left: newPos
                }, speed, 'linear', function() {
                    $slideHolder.css({
                        'width': slideWidth * numSlides,
                        'left': 0
                    });
                    $oldSlides.remove();
                    isAnimating = false;
                });

                return 0;
            } else if (dir === 'next') {
                let newPos = pos - slideWidth;

                $slideHolder.stop().animate({
                    left: newPos
                }, speed, 'linear', function() {
                    isAnimating = false;
                });

                return (Math.abs(newPos) / slideWidth);
            } else if (dir === 'prev' && current === 0) {
                let $oldSlides = $slides;
                let $newSlides = $slides.clone();
                let numSlides = $slides.length;
                let width = slideWidth * numSlides;

                $slideHolder.css('width', width * 2);
                $slideHolder.prepend($newSlides);
                $slideHolder.css('left', -width);
                let newPos = -width + slideWidth;

                $slideHolder.stop().animate({
                    left: newPos
                }, speed, 'linear', function() {
                    $slideHolder.css({
                        'width': width,
                        'left': -(width - slideWidth)
                    });
                    $oldSlides.remove();
                    isAnimating = false;
                });

                return numSlides - 1;
            } else {
                let newPos = pos + slideWidth;

                $slideHolder.stop().animate({
                    left: newPos
                }, speed, 'linear', function() {
                    isAnimating = false;
                });

                return (Math.abs(newPos) / slideWidth);
            }
        },
        advanceSlide(current, dir, $slideHolder) {
            const $slides = $slideHolder.find(`.${slideClass}`);

            if (effect === 'fade') {
                return self.fadeSlide(current, dir, $slides);
            } else if (effect === 'slide-left') {
                return self.slideLeft(current, dir, $slides, $slideHolder);
            }
        },
        pageSlide(e, $slideHolder) {
            const current = self.getCurrent($slideHolder);
            let dir;

            if (e.which === 37) {
                dir = 'prev';
            } else if (e.which === 39) {
                dir = 'next';
            } else {
                return;
            }

            return self.advanceSlide(current, dir, $slideHolder);
        },
        startShow(interval, $slideHolder, $slideList) {
            return setInterval(function() {
                // $nextControl.trigger('click');
                const current = self.getCurrent($slideHolder);
                const nextSlide = self.advanceSlide(current, 'next', $slideHolder);<commit_msg>refactor slide effect function<commit_after>import elrUI from 'elr-ui';
const $ = require('jquery');

let ui = elrUI();

const elrSimpleSlider = function({
    sliderClass = 'elr-simple-slider',
    slideClass = 'elr-simple-slide',
    slideHolderClass = 'elr-simple-slide-holder',
    effect = 'fade',
    navClass = 'elr-simple-slider-nav',
    slideListClass = 'elr-simple-slider-list',
    speed = 500,
    interval = 10000,
    auto = true
} = {}) {
    const $slider = $(`.${sliderClass}`);
    let isAnimating = false;
    const self = {
        createSlideList($slides) {
            let li = '';

            $.each($slides, function(index) {
                if (index === 0) {
                    li += `<li><button class="active" data-item-num="${index}"></button></li>`;
                } else {
                    li += `<li><button data-item-num="${index}"></button></li>`;
                }
            });

            return ui.createElement('ul', {
                'class': slideListClass,
                'html': li
            });
        },
        getCurrent($slideHolder) {
            const $slides = $slideHolder.find(`.${slideClass}`);

            if (effect === 'fade') {
                return $slides.not(':hidden').index();
            } else if (effect === 'slide-left') {
                return (Math.abs($slideHolder.position().left) / parseInt($slides.first().width(), 10));
            }
        },
        goToSlide(current, slideNum, $slideHolder) {
            const $slides = $slideHolder.find(`.${slideClass}`);

            if (effect === 'fade') {
                $slides.eq(current).fadeOut(speed);
                $slides.eq(slideNum).fadeIn(speed);
            } else if (effect === 'slide-left') {
                const slideWidth = parseInt($slides.first().width(), 10);
                const pos = $slideHolder.position().left;
                let newPos;

                if (current < slideNum) {
                    const slideDiff = current - slideNum;
                    newPos = pos + (slideWidth * slideDiff);
                } else if (current > slideNum) {
                    const slideDiff = -(current - slideNum);
                    newPos = pos - (slideWidth * slideDiff);
                }

                $slideHolder.stop().animate({
                    left: newPos
                }, speed);
            }
        },
        fadeSlide(current, dir, $slides) {
            const lastSlide = $slides.length - 1;

            $slides.eq(current).fadeOut(speed);

            if (dir === 'next' && current === lastSlide) {
                $slides.first().fadeIn(speed);

                return 0;
            } else if (dir === 'next') {
                $slides.eq(current + 1).fadeIn(speed);

                return current + 1;
            } else if (dir === 'prev' && current === 0) {
                $slides.eq(lastSlide).fadeIn(speed);

                return lastSlide;
            } else {
                $slides.eq(current - 1).fadeIn(speed);

                return current - 1;
            }
        },
        makeNewSlides(current, dir, $slides, $slideHolder) {
            const slideWidth = parseInt($slides.first().width(), 10);
            let $oldSlides = $slides;
            let $newSlides = $slides.clone();
            let numSlides = $slides.length;
            let width = slideWidth * numSlides;
            const pos = $slideHolder.position().left;
            let newPos;
            let left;

            isAnimating = true;

            if (dir === 'next') {
                $slideHolder.css('width', slideWidth * (numSlides * 2));
                $slideHolder.append($newSlides);
                newPos = pos - slideWidth;
                left = 0;
            } else {
                $slideHolder.css('width', width * 2);
                $slideHolder.prepend($newSlides);
                $slideHolder.css('left', -width);
                newPos = -width + slideWidth;
                left = -(width - slideWidth);
            }

            $slideHolder.stop().animate({
                left: newPos
            }, speed, 'linear', function() {
                $slideHolder.css({
                    'width': width,
                    'left': left
                });
                $oldSlides.remove();
                isAnimating = false;
            });

            return (dir === 'next') ? 0 : numSlides - 1;
        },
        moveSlide($slideHolder, $slides) {
            const numSlides = $slides.length;
            const slideWidth = parseInt($slides.first().width(), 10);
            const pos = $slideHolder.position().left;
            const newPos = (dir = 'next') ? pos - slideWidth : pos + slideWidth;

            isAnimating = true;

            $slideHolder.stop().animate({
                left: newPos
            }, speed, 'linear', function() {
                isAnimating = false;
            });

            return (Math.abs(newPos) / slideWidth);
        },
        slideLeft(current, dir, $slides, $slideHolder) {
            // TODO: fix bug where paging doesn't work properly when paging from first to last slide
            const lastSlide = $slides.length - 1;

            if (dir === 'next' && current === lastSlide) {
                return self.makeNewSlides(lastSlide, 'next', $slides, $slideHolder);
            } else if (dir === 'next') {
                return self.moveSlide($slideHolder, $slides);
            } else if (dir === 'prev' && current === 0) {
                return self.makeNewSlides(0, 'prev', $slides, $slideHolder);
            }

            return self.moveSlide($slideHolder, $slides);
        },
        advanceSlide(current, dir, $slideHolder) {
            const $slides = $slideHolder.find(`.${slideClass}`);

            if (effect === 'slide-left') {
                return self.slideLeft(current, dir, $slides, $slideHolder);
            }

            return self.fadeSlide(current, dir, $slides);
        },
        pageSlide(e, $slideHolder) {
            const current = self.getCurrent($slideHolder);
            let dir;

            if (e.which === 37) {
                dir = 'prev';
            } else if (e.which === 39) {
                dir = 'next';
            } else {
                return;
            }

            return self.advanceSlide(current, dir, $slideHolder);
        },
        startShow(interval, $slideHolder, $slideList) {
            return setInterval(function() {
                // $nextControl.trigger('click');
                const current = self.getCurrent($slideHolder);
                const nextSlide = self.advanceSlide(current, 'next', $slideHolder);
{'fraction_non_alphanumeric': 0.13798725359419, 'fraction_numerical': 0.005113383726100489, 'mean_word_length': 1.3927304964539007, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4955798', 'n_tokens_mistral': 3808, 'n_tokens_neox': 3518, 'n_words': 771}