<commit_before>define([
  'jquery',
  'DoughBaseComponent',
  'eventsWithPromises',
  'utilities',
  'common'
], function($, DoughBaseComponent, eventsWithPromises, utilities, MAS) {
  'use strict';

  var NewsletterSticky,
    defaultConfig = {
      appearsAfterPercentage: 40,
      visibleClassName: 'news-signup-sticky--visible',
      closeClassSelector: '.news-signup-sticky__close',
      closedClassName: 'news-signup-sticky__close--closed',
      windowScrollEventName: 'scroll.newsletter',
      buttonDoneClass: '.button--done'
    },
    $newsletterStickForm,
    newsletterCloseBoxCookieURL;

  NewsletterSticky = function($el, config) {
    NewsletterSticky.baseConstructor.call(this, $el, config, defaultConfig);
  };

  /**
   * Inherit from base module, for shared methods and interface
   */
  DoughBaseComponent.extend(NewsletterSticky);

  NewsletterSticky.componentName = 'NewsletterSticky';

  /**
   * Initialize the component
   * @param {Promise} initialised
   * @return {NewsletterSticky}
   */
  NewsletterSticky.prototype.init = function(initialised) {
    this._initialisedSuccess(initialised);
    this._setVars();

    this._setListeners(true);
    this._handleScroll();

    return this;
  };

  /**
   * Sets up local variables for the sticky newsletter component
   */
  NewsletterSticky.prototype._setVars = function() {
    this.closeButton = this.$el.find(this.config.closeClassSelector);
    this.$window = $(window);

    $newsletterStickForm = this.$el.find('form');
    newsletterCloseBoxCookieURL = $newsletterStickForm.find('#close-box-cookie-url').val();
  };

  /**
   * Turn on and off the events
   * @param {Boolean} isActive true=on false=off
   */
  NewsletterSticky.prototype._setListeners = function(isActive) {
    this._setScrollListener(isActive);

    if (isActive) {
      this.closeButton.on('click', $.proxy(this._handleCloseClick, this));
      this.$el.on('click', this.config.buttonDoneClass, $.proxy(this._handleCloseClick, this));
      $newsletterStickForm.on('ajax:error', $.proxy(this._formAjaxErrorHandler, this));
    }
    else {
      this.closeButton.off('click', $.proxy(this._handleCloseClick, this));
      this.$el.off('click', this.config.buttonDoneClass, $.proxy(this._handleCloseClick, this));
      $newsletterStickForm.off('ajax:error', $.proxy(this._formAjaxErrorHandler, this));
    }
  };

  /**
   * Sets the scroll event on the window on or off
   * @param {Boolean} isActive true=on false=off
   */
  NewsletterSticky.prototype._setScrollListener = function(isActive) {
    this.$window[isActive ? 'on' : 'off'](this.config.windowScrollEventName, this._scrollDebounceMethod(isActive));
  };

  /**
   * Limit the amount of times the _handleScroll method is called
   * @param  {Boolean} isActive true=on false=off
   * @returns {Function|null}
   */
  NewsletterSticky.prototype._scrollDebounceMethod = function(isActive) {
    return isActive ? utilities.debounce($.proxy(this._handleScroll, this), 100) : null;
  };

  /**
   * TODO - write comment
   */
  NewsletterSticky.prototype._formAjaxErrorHandler = function() {

    $('.news-signup-sticky__error')
      .addClass('is-errored news-signup-sticky__error--display');

    $('.news-signup-sticky__text')
      .removeClass('news-signup-sticky__text--display');

  };

  /**
   * Make the newsletter module display if scroll is over a certain percentage
   */
  NewsletterSticky.prototype._handleScroll = function() {
    if (this._hasScrolledOverPercentage(this.config.appearsAfterPercentage)) {
      this.$el.addClass(this.config.visibleClassName);
      this._setScrollListener(false);
    }

  };

  /**
   * Works out whether the window has scrolled further than a given percentage
   * @return {Boolean}
   */
  NewsletterSticky.prototype._hasScrolledOverPercentage = function() {
    var percentScrolled;
    percentScrolled = this._pageScrolledTo().percentage;

    return (percentScrolled >= this.config.appearsAfterPercentage);
  };

  /**
   * Returns the window's scroll top number
   * @return {Number}
   */
  NewsletterSticky.prototype._windowScrollTop = function() {
    return this.$window.scrollTop();
  };

  /**
   * Works out the amount of pixels that is needed to be scrolled to before the
   * newsletter displays
   * @return {Number}
   * @deprecated This is no longer used internally, will leave this until production release.
   */<commit_msg>id: 6501 comment: Add GA Tracking when showing component.<commit_after>define([
  'jquery',
  'DoughBaseComponent',
  'eventsWithPromises',
  'utilities',
  'common'
], function($, DoughBaseComponent, eventsWithPromises, utilities, MAS) {
  'use strict';

  var NewsletterSticky,
    defaultConfig = {
      appearsAfterPercentage: 40,
      visibleClassName: 'news-signup-sticky--visible',
      closeClassSelector: '.news-signup-sticky__close',
      closedClassName: 'news-signup-sticky__close--closed',
      windowScrollEventName: 'scroll.newsletter',
      buttonDoneClass: '.button--done'
    },
    $newsletterStickForm,
    newsletterCloseBoxCookieURL;

  NewsletterSticky = function($el, config) {
    NewsletterSticky.baseConstructor.call(this, $el, config, defaultConfig);
  };

  /**
   * Inherit from base module, for shared methods and interface
   */
  DoughBaseComponent.extend(NewsletterSticky);

  NewsletterSticky.componentName = 'NewsletterSticky';

  /**
   * Initialize the component
   * @param {Promise} initialised
   * @return {NewsletterSticky}
   */
  NewsletterSticky.prototype.init = function(initialised) {
    this._initialisedSuccess(initialised);
    this._setVars();

    this._setListeners(true);
    this._handleScroll();

    return this;
  };

  /**
   * Sets up local variables for the sticky newsletter component
   */
  NewsletterSticky.prototype._setVars = function() {
    this.closeButton = this.$el.find(this.config.closeClassSelector);
    this.$window = $(window);

    $newsletterStickForm = this.$el.find('form');
    newsletterCloseBoxCookieURL = $newsletterStickForm.find('#close-box-cookie-url').val();
  };

  /**
   * Turn on and off the events
   * @param {Boolean} isActive true=on false=off
   */
  NewsletterSticky.prototype._setListeners = function(isActive) {
    this._setScrollListener(isActive);

    if (isActive) {
      this.closeButton.on('click', $.proxy(this._handleCloseClick, this));
      this.$el.on('click', this.config.buttonDoneClass, $.proxy(this._handleCloseClick, this));
      $newsletterStickForm.on('ajax:error', $.proxy(this._formAjaxErrorHandler, this));
    }
    else {
      this.closeButton.off('click', $.proxy(this._handleCloseClick, this));
      this.$el.off('click', this.config.buttonDoneClass, $.proxy(this._handleCloseClick, this));
      $newsletterStickForm.off('ajax:error', $.proxy(this._formAjaxErrorHandler, this));
    }
  };

  /**
   * Sets the scroll event on the window on or off
   * @param {Boolean} isActive true=on false=off
   */
  NewsletterSticky.prototype._setScrollListener = function(isActive) {
    this.$window[isActive ? 'on' : 'off'](this.config.windowScrollEventName, this._scrollDebounceMethod(isActive));
  };

  /**
   * Limit the amount of times the _handleScroll method is called
   * @param  {Boolean} isActive true=on false=off
   * @returns {Function|null}
   */
  NewsletterSticky.prototype._scrollDebounceMethod = function(isActive) {
    return isActive ? utilities.debounce($.proxy(this._handleScroll, this), 100) : null;
  };

  /**
   * TODO - write comment
   */
  NewsletterSticky.prototype._formAjaxErrorHandler = function() {

    $('.news-signup-sticky__error')
      .addClass('is-errored news-signup-sticky__error--display');

    $('.news-signup-sticky__text')
      .removeClass('news-signup-sticky__text--display');

  };

  /**
   * Make the newsletter module display if scroll is over a certain percentage
   */
  NewsletterSticky.prototype._handleScroll = function() {
    if (this._hasScrolledOverPercentage(this.config.appearsAfterPercentage)) {
      this.$el.addClass(this.config.visibleClassName);
      this._setScrollListener(false);

      MAS.publish('analytics:trigger', {
        event: 'gaEvent',
        gaEventCat: 'Newsletter SignUp Sticky',
        gaEventAct: 'Visible',
        gaEventLab: 'Component'
      });

    }

  };

  /**
   * Works out whether the window has scrolled further than a given percentage
   * @return {Boolean}
   */
  NewsletterSticky.prototype._hasScrolledOverPercentage = function() {
    var percentScrolled;
    percentScrolled = this._pageScrolledTo().percentage;

    return (percentScrolled >= this.config.appearsAfterPercentage);
  };

  /**
   * Returns the window's scroll top number
   * @return {Number}
   */
  NewsletterSticky.prototype._windowScrollTop = function() {
    return this.$window.scrollTop();
  };

  /**
   * Works out the amount of pixels that is needed to be scrolled to before the
   * newsletter displays
   * @return {Number}
   * @deprecated This is no longer used internally, will leave this until production release.
   */
{'fraction_non_alphanumeric': 0.1274812527569475, 'fraction_numerical': 0.0015438906043228936, 'mean_word_length': 3.7681388012618298, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6811245', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2688, 'n_words': 543}