<commit_before>"use strict";

/**
  * arrive.js
  * V 0.1b
  * https://github.com/uzairfarooq/arrive
  */

(function(window, $, undefined) {

  // Class to mantain state of all registered events of a single type
  var EventsBucket = (function() {
    var EventsBucket = function() {
      // holds all the events

      this._eventsBucket    = [], 
      // function to be called while adding an event, the function should do the event initialization/registration
      this._beforeAdding    = null, 
      // function to be called while removing an event, the function should do the event destruction
      this._beforeRemoving  = null;
    };

    EventsBucket.prototype.addEvent = function(target, selector, callback) {

      // the regex would separate the selector into two parts, the first part would contain the selector for parent element while 
      // the 2nd part would contain the separator for last element
      var lastElementSelectorRegex = /(.*?)([^\s](?:[^\s\'\"\[\(]+(?:\'[^\']*\'|\[[^\]]*\]|\"[^\"]*\"|\([^\)]*\))*)*)$/, 
          subSelectors = lastElementSelectorRegex.exec(selector), 
          parentElemSelector = subSelectors[1], 
          lastElemSelector = subSelectors[2];

      var newEvent = {
        target:             target, 
        selector:           selector, 
        parentElemSelector: parentElemSelector, 
        lastElemSelector:   lastElemSelector, 
        callback:           callback, 
        firedElems:         []
      };

      if (this._beforeAdding) {
        this._beforeAdding(newEvent);
      }
      
      this._eventsBucket.push(newEvent);
      return newEvent;
    };

    EventsBucket.prototype.removeEvent = function(compareFunction) {
      for (var i=this._eventsBucket.length - 1, registeredEvent; registeredEvent = this._eventsBucket[i]; i--) {
        if (compareFunction(registeredEvent)) {
          if (this._beforeRemoving) {
              this._beforeRemoving(registeredEvent);
          }
          this._eventsBucket.splice(i, 1);
        }
      }
    };

    EventsBucket.prototype.beforeAdding = function(beforeAdding) {
      this._beforeAdding = beforeAdding;
    };

    EventsBucket.prototype.beforeRemoving = function(beforeRemoving) {
      this._beforeRemoving = beforeRemoving;
    };

    return EventsBucket;
  })();


  // General class for binding/unbinding arrive and leave events
  var MutationEvents = function(onMutation) {
    var eventsBucket  = new EventsBucket(), 
        me            = this;

    // actual event registration before adding it to bucket
      eventsBucket.beforeAdding(function(registrationData) {
        var 
          target    = registrationData.target, 
          selector  = registrationData.selector, 
          callback  = registrationData.callback, 
          observer, 
          // Configuration of the observer
          config = { 
            attributes: true, 
            attributeFilter: ["class"],
            childList: true, 
            subtree: true
          };

        // mutation observer does not work on window or document
        if (target === window.document || target === window)
          target = window.document.body;

        // Create an observer instance
        observer = new MutationObserver(function(e) {
          onMutation.call(this, e, registrationData);
        });
        
        observer.observe(target, config);

        registrationData.observer = observer;
      });

      // cleanup/unregister before removing an event
      eventsBucket.beforeRemoving(function (eventData) {
        eventData.observer.disconnect();
      });

    this.bindEvent = function(selector, callback) {
      for (var i = 0; i < this.length; i++) {
        eventsBucket.addEvent(this[i], selector, callback);
      }
    };

    this.unbindEvent = function() {
      var target = this[0];
      eventsBucket.removeEvent(function(eventObj) {
        return eventObj.target === target;
      });
    };

    this.unbindEventWithSelectorOrCallback = function(selector) {
      var target = this[0], 
          callback = selector, 
          compareFunction;

      if (typeof selector === "function") {
        compareFunction = function(eventObj) {
          return eventObj.target === target && eventObj.callback === callback;
        };
      }
      else {
        compareFunction = function(eventObj) {
          return eventObj.target === target && eventObj.selector === selector;
        };
      }
      eventsBucket.removeEvent(compareFunction);
    };

    this.unbindEventWithSelectorAndCallback = function(selector, callback) {
      var target = this[0];
      eventsBucket.removeEvent(function(eventObj) {
        return eventObj.target === target && eventObj.selector === selector && eventObj.callback === callback;
      });
    };

      return this;
  };

    function onArriveMutation(mutations, registrationData) {
    mutations.forEach(function( mutation ) {
      var newNodes    = mutation.addedNodes, 
          $targetNode = $(mutation.target);

      // If new nodes are added
      if( newNodes !== null && newNodes.length > 0 ) {
          checkRecursively(newNodes, registrationData);
      }
      else if (mutation.type === "attributes") {
          if( $targetNode.is(registrationData.selector)) {
            // make sure the arrive event is not already fired for the element
            if (registrationData.firedElems.indexOf($targetNode[0]) == -1) {
              registrationData.firedElems.push($targetNode[0]);
              registrationData.callback.call($targetNode[0]);
            }
          }
      }
    });
  }

  function checkRecursively(nodes, registrationData) {
    // check each new node if it matches the selector
    for (var i=0, node; node = nodes[i]; i++) {
        var $node = $(node);
        if ($node.is(registrationData.selector)) {
            // make sure the arrive event is not already fired for the element
            if (registrationData.firedElems.indexOf($node[0]) == -1) {
                registrationData.firedElems.push($node[0]);
                registrationData.callback.call($node[0]);
            }
        }
        if (node.childNodes.length > 0) {
            checkRecursively(node.childNodes, registrationData);
        }
    }
  }

  function onLeaveMutation(mutations, registrationData) {
    mutations.forEach(function( mutation ) {
      var removedNodes  = mutation.removedNodes, 
          $targetNode   = $(mutation.target);

      if( removedNodes !== null && removedNodes.length > 0 ) {
        if (registrationData.parentElemSelector.length == 0 || $targetNode.is(registrationData.parentElemSelector + $targetNode[0].tagName)) {
          for (var i = 0, node; node = removedNodes[i]; i++) {
            var $node = $(node);
            if ($node.is(registrationData.lastElemSelector)) {
              // make sure the leave event is not already fired for the element
              if (registrationData.firedElems.indexOf($node[0]) == -1) {
                  registrationData.firedElems.push($node[0]);
                  registrationData.callback.call($node[0]);
              }
            }
          }
        }
      }
    });
  }

    var arriveEvents = new MutationEvents(onArriveMutation), 
        leaveEvents  = new MutationEvents(onLeaveMutation);


  // to enable function overriding - By <NAME> (MIT Licensed)
    function addMethod(object, name, fn){
    var old = object[ name ];
    object[ name ] = function(){
      if ( fn.length == arguments.length )
        return fn.apply( this, arguments );
      else if ( typeof old == 'function' )
        return old.apply( this, arguments );
    };
    }


  /*** expose APIs ***/

  $.fn.arrive = arriveEvents.bindEvent;
  // expose unbindArrive function with overriding 
  addMethod($.fn, "unbindArrive", arriveEvents.unbindEvent);
  addMethod($.fn, "unbindArrive", arriveEvents.unbindEventWithSelectorOrCallback);
  addMethod($.fn, "unbindArrive", arriveEvents.unbindEventWithSelectorAndCallback);

  $.fn.leave = leaveEvents.bindEvent;
  // expose unbindLeave function with overriding 
  addMethod($.fn, "unbindLeave", leaveEvents.unbindEvent);
  addMethod($.fn, "unbindLeave", leaveEvents.unbindEventWithSelectorOrCallback);
  addMethod($.fn, "unbindLeave", leaveEvents.unbindEventWithSelectorAndCallback);

})(this, jQuery);
<commit_msg>Added copyright notice<commit_after>"use strict";

/*
 * arrive.js
 * V 0.1b
 * https://github.com/uzairfarooq/arrive
 * MIT licensed
 *
 * Copyright (c) 2014 <NAME>
 */

(function(window, $, undefined) {

  // Class to mantain state of all registered events of a single type
  var EventsBucket = (function() {
    var EventsBucket = function() {
      // holds all the events

      this._eventsBucket    = [], 
      // function to be called while adding an event, the function should do the event initialization/registration
      this._beforeAdding    = null, 
      // function to be called while removing an event, the function should do the event destruction
      this._beforeRemoving  = null;
    };

    EventsBucket.prototype.addEvent = function(target, selector, callback) {

      // the regex would separate the selector into two parts, the first part would contain the selector for parent element while 
      // the 2nd part would contain the separator for last element
      var lastElementSelectorRegex = /(.*?)([^\s](?:[^\s\'\"\[\(]+(?:\'[^\']*\'|\[[^\]]*\]|\"[^\"]*\"|\([^\)]*\))*)*)$/, 
          subSelectors = lastElementSelectorRegex.exec(selector), 
          parentElemSelector = subSelectors[1], 
          lastElemSelector = subSelectors[2];

      var newEvent = {
        target:             target, 
        selector:           selector, 
        parentElemSelector: parentElemSelector, 
        lastElemSelector:   lastElemSelector, 
        callback:           callback, 
        firedElems:         []
      };

      if (this._beforeAdding) {
        this._beforeAdding(newEvent);
      }
      
      this._eventsBucket.push(newEvent);
      return newEvent;
    };

    EventsBucket.prototype.removeEvent = function(compareFunction) {
      for (var i=this._eventsBucket.length - 1, registeredEvent; registeredEvent = this._eventsBucket[i]; i--) {
        if (compareFunction(registeredEvent)) {
          if (this._beforeRemoving) {
              this._beforeRemoving(registeredEvent);
          }
          this._eventsBucket.splice(i, 1);
        }
      }
    };

    EventsBucket.prototype.beforeAdding = function(beforeAdding) {
      this._beforeAdding = beforeAdding;
    };

    EventsBucket.prototype.beforeRemoving = function(beforeRemoving) {
      this._beforeRemoving = beforeRemoving;
    };

    return EventsBucket;
  })();


  // General class for binding/unbinding arrive and leave events
  var MutationEvents = function(onMutation) {
    var eventsBucket  = new EventsBucket(), 
        me            = this;

    // actual event registration before adding it to bucket
      eventsBucket.beforeAdding(function(registrationData) {
        var 
          target    = registrationData.target, 
          selector  = registrationData.selector, 
          callback  = registrationData.callback, 
          observer, 
          // Configuration of the observer
          config = { 
            attributes: true, 
            attributeFilter: ["class"],
            childList: true, 
            subtree: true
          };

        // mutation observer does not work on window or document
        if (target === window.document || target === window)
          target = window.document.body;

        // Create an observer instance
        observer = new MutationObserver(function(e) {
          onMutation.call(this, e, registrationData);
        });
        
        observer.observe(target, config);

        registrationData.observer = observer;
      });

      // cleanup/unregister before removing an event
      eventsBucket.beforeRemoving(function (eventData) {
        eventData.observer.disconnect();
      });

    this.bindEvent = function(selector, callback) {
      for (var i = 0; i < this.length; i++) {
        eventsBucket.addEvent(this[i], selector, callback);
      }
    };

    this.unbindEvent = function() {
      var target = this[0];
      eventsBucket.removeEvent(function(eventObj) {
        return eventObj.target === target;
      });
    };

    this.unbindEventWithSelectorOrCallback = function(selector) {
      var target = this[0], 
          callback = selector, 
          compareFunction;

      if (typeof selector === "function") {
        compareFunction = function(eventObj) {
          return eventObj.target === target && eventObj.callback === callback;
        };
      }
      else {
        compareFunction = function(eventObj) {
          return eventObj.target === target && eventObj.selector === selector;
        };
      }
      eventsBucket.removeEvent(compareFunction);
    };

    this.unbindEventWithSelectorAndCallback = function(selector, callback) {
      var target = this[0];
      eventsBucket.removeEvent(function(eventObj) {
        return eventObj.target === target && eventObj.selector === selector && eventObj.callback === callback;
      });
    };

      return this;
  };

    function onArriveMutation(mutations, registrationData) {
    mutations.forEach(function( mutation ) {
      var newNodes    = mutation.addedNodes, 
          $targetNode = $(mutation.target);

      // If new nodes are added
      if( newNodes !== null && newNodes.length > 0 ) {
          checkRecursively(newNodes, registrationData);
      }
      else if (mutation.type === "attributes") {
          if( $targetNode.is(registrationData.selector)) {
            // make sure the arrive event is not already fired for the element
            if (registrationData.firedElems.indexOf($targetNode[0]) == -1) {
              registrationData.firedElems.push($targetNode[0]);
              registrationData.callback.call($targetNode[0]);
            }
          }
      }
    });
  }

  function checkRecursively(nodes, registrationData) {
    // check each new node if it matches the selector
    for (var i=0, node; node = nodes[i]; i++) {
        var $node = $(node);
        if ($node.is(registrationData.selector)) {
            // make sure the arrive event is not already fired for the element
            if (registrationData.firedElems.indexOf($node[0]) == -1) {
                registrationData.firedElems.push($node[0]);
                registrationData.callback.call($node[0]);
            }
        }
        if (node.childNodes.length > 0) {
            checkRecursively(node.childNodes, registrationData);
        }
    }
  }

  function onLeaveMutation(mutations, registrationData) {
    mutations.forEach(function( mutation ) {
      var removedNodes  = mutation.removedNodes, 
          $targetNode   = $(mutation.target);

      if( removedNodes !== null && removedNodes.length > 0 ) {
        if (registrationData.parentElemSelector.length == 0 || $targetNode.is(registrationData.parentElemSelector + $targetNode[0].tagName)) {
          for (var i = 0, node; node = removedNodes[i]; i++) {
            var $node = $(node);
            if ($node.is(registrationData.lastElemSelector)) {
              // make sure the leave event is not already fired for the element
              if (registrationData.firedElems.indexOf($node[0]) == -1) {
                  registrationData.firedElems.push($node[0]);
                  registrationData.callback.call($node[0]);
              }
            }
          }
        }
      }
    });
  }

    var arriveEvents = new MutationEvents(onArriveMutation), 
        leaveEvents  = new MutationEvents(onLeaveMutation);


  // to enable function overriding - By <NAME> (MIT Licensed)
    function addMethod(object, name, fn){
    var old = object[ name ];
    object[ name ] = function(){
      if ( fn.length == arguments.length )
        return fn.apply( this, arguments );
      else if ( typeof old == 'function' )
        return old.apply( this, arguments );
    };
    }


  /*** expose APIs ***/

  $.fn.arrive = arriveEvents.bindEvent;
  // expose unbindArrive function with overriding 
  addMethod($.fn, "unbindArrive", arriveEvents.unbindEvent);
  addMethod($.fn, "unbindArrive", arriveEvents.unbindEventWithSelectorOrCallback);
  addMethod($.fn, "unbindArrive", arriveEvents.unbindEventWithSelectorAndCallback);

  $.fn.leave = leaveEvents.bindEvent;
  // expose unbindLeave function with overriding 
  addMethod($.fn, "unbindLeave", leaveEvents.unbindEvent);
  addMethod($.fn, "unbindLeave", leaveEvents.unbindEventWithSelectorOrCallback);
  addMethod($.fn, "unbindLeave", leaveEvents.unbindEventWithSelectorAndCallback);

})(this, jQuery);

{'fraction_non_alphanumeric': 0.11588495047141664, 'fraction_numerical': 0.0038190714882444204, 'mean_word_length': 2.3945716021875634, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 2, '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': '5165182', 'n_tokens_mistral': 4619, 'n_tokens_neox': 4366, 'n_words': 1056}