<commit_before>/*
 * #%L
 * GwtMaterial
 * %%
 * Copyright (C) 2015 - 2018 GwtMaterialDesign
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package gwt.material.design.client.base.helper;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;
import gwt.material.design.client.js.ScrollOption;
import gwt.material.design.jquery.client.api.Functions;

import static gwt.material.design.jquery.client.api.JQuery.$;

/**
 * A set of scroll helper utils for GMD Widgets
 *
 * @author <EMAIL>
 */
public class ScrollHelper {

    private Element containerElement;
    private Functions.Func completeCallback;
    private String easing = "swing";
    private int duration = 400;
    private double offset;

    public ScrollHelper() {
        containerElement = $("html, body").asElement();
    }

    public Element getContainerElement() {
        return containerElement;
    }

    /**
     * The container element of which the scrolling feature will be applied.
     */
    public void setContainerElement(Element containerElement) {
        this.containerElement = containerElement;
    }

    /**
     * The container of which the scrolling feature will be applied.
     */
    public void setContainer(Widget widget) {
        this.containerElement = widget.getElement();
    }

    public Functions.Func getCompleteCallback() {
        return completeCallback;
    }

    /**
     * A function to call once the scrolling is complete, called once per matched element.
     */
    public void setCompleteCallback(Functions.Func completeCallback) {
        this.completeCallback = completeCallback;
    }

    public String getEasing() {
        return easing;
    }

    /**
     * An easing function specifies the speed at which the animation progresses at different points within the animation.
     * The only easing implementations in the jQuery library are the default, called swing, and one that progresses at a
     * constant pace, called linear.
     */
    public void setEasing(String easing) {
        this.easing = easing;
    }

    public double getOffset() {
        return offset;
    }

    public int getDuration() {
        return duration;
    }

    /**
     * Determines how long the animation will run in milliseconds.
     */
    public void setDuration(int duration) {
        this.duration = duration;
    }

    /**
     * Will scroll target widget.
     */
    public void scrollTo(Widget widget) {
        scrollTo(widget.getElement());
    }

    /**
     * Will scroll to target element.
     */
    public void scrollTo(Element target) {
        scrollTo($(target).offset().top);
    }

    /**
     * Will scroll to target offset.
     */
    public void scrollTo(double offset) {
        this.offset = offset;

        ScrollOption option = new ScrollOption();
        option.scrollTop = $(containerElement).scrollTop() - $(containerElement).offset().top + offset;

        $(containerElement).animate(option, duration, easing, () -> {
            if (completeCallback != null) {
                completeCallback.call();
            }
        });
    }

    /**
     * Will perform a detection whether the widget is in the View Port Scope or not, providing
     * the {@link #containerElement} as the wrapper or container of the target element.
     * <p/>
     * Note: By default if you didn't provide a container, the html or body element should be
     * applied as the target's container.
     *
     * @param widget The widget you are checking if it's inside the viewport scope.
     */
    public boolean isInViewPort(Widget widget) {
        return isInViewPort(widget.getElement());
    }

    /**
     * Will perform a detection whether the element is in the View Port Scope or not, providing
     * the {@link #containerElement} as the wrapper or container of the target element.
     * <p/>
     * Note: By default if you didn't provide a container, the html or body element should be
     * applied as the target's container.
     *
     * @param element The element you are checking if it's inside the viewport scope.
     */
    public boolean isInViewPort(Element element) {
        double elementTop = $(element).offset().top;
        double elementBottom = elementTop + $(element).outerHeight();

        double viewportTop = $(containerElement).offset().top;
        double viewportBottom = viewportTop + $(containerElement).height();

        return elementBottom > viewportTop && elementTop < viewportBottom;
    }
}<commit_msg>Fixed element's offset top vs window scrollTop.<commit_after>/*
 * #%L
 * GwtMaterial
 * %%
 * Copyright (C) 2015 - 2018 GwtMaterialDesign
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package gwt.material.design.client.base.helper;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;
import gwt.material.design.client.js.ScrollOption;
import gwt.material.design.jquery.client.api.Functions;

import static gwt.material.design.jquery.client.api.JQuery.$;

/**
 * A set of scroll helper utils for GMD Widgets
 *
 * @author <EMAIL>
 */
public class ScrollHelper {

    private Element containerElement;
    private Functions.Func completeCallback;
    private String easing = "swing";
    private int duration = 400;
    private double offset;

    public ScrollHelper() {
        containerElement = getDefaultContainer();
    }

    public Element getContainerElement() {
        return containerElement;
    }

    /**
     * The container element of which the scrolling feature will be applied.
     */
    public void setContainerElement(Element containerElement) {
        this.containerElement = containerElement;
    }

    /**
     * The container of which the scrolling feature will be applied.
     */
    public void setContainer(Widget widget) {
        this.containerElement = widget.getElement();
    }

    public Functions.Func getCompleteCallback() {
        return completeCallback;
    }

    /**
     * A function to call once the scrolling is complete, called once per matched element.
     */
    public void setCompleteCallback(Functions.Func completeCallback) {
        this.completeCallback = completeCallback;
    }

    public String getEasing() {
        return easing;
    }

    /**
     * An easing function specifies the speed at which the animation progresses at different points within the animation.
     * The only easing implementations in the jQuery library are the default, called swing, and one that progresses at a
     * constant pace, called linear.
     */
    public void setEasing(String easing) {
        this.easing = easing;
    }

    public double getOffset() {
        return offset;
    }

    public int getDuration() {
        return duration;
    }

    /**
     * Determines how long the animation will run in milliseconds.
     */
    public void setDuration(int duration) {
        this.duration = duration;
    }

    /**
     * Will scroll target widget.
     */
    public void scrollTo(Widget widget) {
        scrollTo(widget.getElement());
    }

    /**
     * Will scroll to target element.
     */
    public void scrollTo(Element target) {
        scrollTo($(target).offset().top);
    }

    /**
     * Will scroll to target offset.
     */
    public void scrollTo(double offset) {
        this.offset = offset;

        ScrollOption option = new ScrollOption();
        option.scrollTop = $(containerElement).scrollTop() - $(containerElement).offset().top + offset;

        $(containerElement).animate(option, duration, easing, () -> {
            if (completeCallback != null) {
                completeCallback.call();
            }
        });
    }

    /**
     * Will perform a detection whether the widget is in the View Port Scope or not, providing
     * the {@link #containerElement} as the wrapper or container of the target element.
     * <p/>
     * Note: By default if you didn't provide a container, the html or body element should be
     * applied as the target's container.
     *
     * @param widget The widget you are checking if it's inside the viewport scope.
     */
    public boolean isInViewPort(Widget widget) {
        return isInViewPort(widget.getElement());
    }

    /**
     * Will perform a detection whether the element is in the View Port Scope or not, providing
     * the {@link #containerElement} as the wrapper or container of the target element.
     * <p/>
     * Note: By default if you didn't provide a container, the html or body element should be
     * applied as the target's container.
     *
     * @param element The element you are checking if it's inside the viewport scope.
     */
    public boolean isInViewPort(Element element) {
        double elementTop = $(element).offset().top;
        double elementBottom = elementTop + $(element).outerHeight();

        double viewportTop = $(containerElement).scrollTop();
        if (containerElement != getDefaultContainer()) {
            viewportTop = $(containerElement).offset().top;
        }

        double viewportBottom = viewportTop + $(containerElement).height();

        return elementBottom > viewportTop && elementTop < viewportBottom;
    }

    protected Element getDefaultContainer() {
        return $("html, body").asElement();
    }
}
{'fraction_non_alphanumeric': 0.09124193082185182, 'fraction_numerical': 0.0028904518739762985, 'mean_word_length': 2.6913229018492175, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6003612', 'n_tokens_mistral': 2729, 'n_tokens_neox': 2608, 'n_words': 968}