{"size": 2097, "ext": "js", "max_stars_repo_path": "lib/edge.js", "max_stars_repo_name": "cameronhunter/flight-edge", "max_stars_repo_stars_event_min_datetime": "2015-07-02T15:25:14.000Z", "max_stars_repo_stars_event_max_datetime": "2015-07-02T15:25:14.000Z", "max_issues_repo_path": "lib/edge.js", "max_issues_repo_name": "cameronhunter/flight-edge", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/edge.js", "max_forks_repo_name": "cameronhunter/flight-edge", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.5921052632, "max_line_length": 99, "alphanum_fraction": 0.5698617072}
define(function (require) {

  'use strict';

  var utils = require('flight/lib/utils');
  var defineComponent = require('flight/lib/component');

  return defineComponent(edge);

  function edge() {
    this.defaultAttrs({
      left: undefined,
      right: undefined,
      top: undefined,
      bottom: undefined
    });

    this.scroll = function() {
      var left = this.$win.scrollLeft();
      var top = this.$win.scrollTop();

      this.check(left, 'left');
      this.check(left + this.viewport.width, 'right');
      this.check(top, 'top');
      this.check(top + this.viewport.height, 'bottom');
    };

    this.check = function(value, edge) {
      var trigger = this[edge + 'Trigger'];
      if (trigger == null || !between(value, trigger)) {
        if (this[edge + 'Triggered']) {
          this.trigger('leaving-edge-' + edge);
        }
        this[edge + 'Triggered'] = false;
        return;
      }

      if (!this[edge + 'Triggered']) {
        this.trigger('edge-' + edge, {value: value});
        this[edge + 'Triggered'] = true;
        this.cacheValues();
      }
    };

    this.setValue = function(edge, value) {
      if (this.attr[edge] != null) {
        this[edge + 'Trigger'] = value;
      }
    };

    this.cacheValues = function() {
      this.page = {width: this.$doc.width(), height: this.$doc.height()};
      this.viewport = {width: this.$win.width(), height: this.$win.height()};

      this.setValue('left', {start: 0, end: this.attr.left});
      this.setValue('right', {start: this.page.width - this.attr.right, end: this.page.width});
      this.setValue('top', {start: 0, end: this.attr.top});
      this.setValue('bottom', {start: this.page.height - this.attr.bottom, end: this.page.height});
    };

    this.after('initialize', function() {
      this.$win = $(window);
      this.$doc = $(document);

      this.cacheValues();

      this.on(window, 'resize', this.cacheValues);
      this.on(window, 'scroll', this.scroll);
    });
  }

  function between(value, bounds) {
    return value >= bounds.start && value <= bounds.end;
  }
});
