<commit_before>import isElementInScrollableDiv from "../lib/is-element-in-scrollable-div";

export default Ember.Component.extend({

  isElementInScrollableDiv: isElementInScrollableDiv,
  layoutName: Ember.computed(function() { return 'components/' + this.get('template'); }),

  loading: Ember.computed.empty('topic'),
  unreadCount: Ember.computed('topic', function() {
    const topic = this.get('topic');
    if (topic) { return topic.highest_post_number - topic.last_read_post_number; }
    else { return 0; }
  }),

  fetchOrSetTopic: function() {
    if (Discourse.Babble == null) {
      var _this = this
      Discourse.ajax('/babble/topic.json').then(function(topic) {
        var topic = Discourse.Topic.create(topic)
        var postStream = Discourse.PostStream.create(topic.post_stream)
        postStream.posts = topic.post_stream.posts
        postStream.topic = topic
        Discourse.Babble = { topic: topic, postStream: postStream }
        _this.setupTopic()
      })
    } else { this.setupTopic() }
  }.on('init'),

  setupTopic: function() {
    this.set('topic',            Discourse.Babble.topic)
    this.set('topic.postStream', Discourse.Babble.postStream)
    this.setupMessageBus()
  },

  setupMessageBus: function() {
    const _this = this
    const messageBus = Discourse.__container__.lookup('message-bus:main')
    messageBus.subscribe('/babble', function(data) {
      var post = Discourse.Post.create(data)
      post.set('topic', _this.get('topic'))
      _this.get('topic.postStream').appendPost(post)
    })
  },

  // Sorry Discourse.scrolling, you force me to use document and window
  // for my scrolly elements and that's no good here.
  _inserted: function() {
    var self = this
    var containerElement = $(self.element).find('ul.babble-posts')
    var lastSeenPost = function() {
      var lastReadPostNumber = _.max(_.map(containerElement.find('.babble-post-container'), function(post) {
        var postElement = $(post)
        if (self.isElementInScrollableDiv(postElement.parent(), containerElement)) { return postElement.data('post-number') }
      }))
      if (lastReadPostNumber > self.get('topic.last_read_post_number')) {
        console.log(lastReadPostNumber)
      }
    }
    lastSeenPost = Discourse.debounce(lastSeenPost, 500)
    containerElement.on('scroll', lastSeenPost)
    lastSeenPost()
  }.on('didInsertElement')
});<commit_msg>Get initial scroll working<commit_after>import isElementInScrollableDiv from "../lib/is-element-in-scrollable-div";

export default Ember.Component.extend({

  isElementInScrollableDiv: isElementInScrollableDiv,
  layoutName: Ember.computed(function() { return 'components/' + this.get('template'); }),

  loading: Ember.computed.empty('topic'),
  unreadCount: Ember.computed('topic', function() {
    const topic = this.get('topic');
    if (topic) { return topic.highest_post_number - topic.last_read_post_number; }
    else { return 0; }
  }),

  fetchOrSetTopic: function() {
    if (Discourse.Babble == null) {
      var _this = this
      Discourse.ajax('/babble/topic.json').then(function(topic) {
        var topic = Discourse.Topic.create(topic)
        var postStream = Discourse.PostStream.create(topic.post_stream)
        postStream.posts = topic.post_stream.posts
        postStream.topic = topic
        Discourse.Babble = { topic: topic, postStream: postStream }
        _this.setupTopic()
      })
    } else { this.setupTopic() }
  }.on('init'),

  setupTopic: function() {
    this.set('topic',            Discourse.Babble.topic)
    this.set('topic.postStream', Discourse.Babble.postStream)
    this.setupMessageBus()
  },

  setupMessageBus: function() {
    const _this = this
    const messageBus = Discourse.__container__.lookup('message-bus:main')
    messageBus.subscribe('/babble', function(data) {
      var post = Discourse.Post.create(data)
      post.set('topic', _this.get('topic'))
      _this.get('topic.postStream').appendPost(post)
    })
  },

  _inserted: function() {
    var self = this
    var scrollContainer = $(this.element).find('ul.babble-posts')
    if (!scrollContainer.length) { return }

    var readVisiblePosts = function() {
      var lastVisiblePostNumber = self.getLastVisiblePostNumber(scrollContainer)
      if (lastVisiblePostNumber > self.get('topic.last_read_post_number')) {
        console.log(lastVisiblePostNumber)
      }
    }
    scrollContainer.on('scroll', Discourse.debounce(readVisiblePosts, 500))
    Ember.run.next(this, this._rendered)
  }.on('didInsertElement'),

  _rendered: function() {
    var scrollContainer = $(this.element).find('ul.babble-posts')
    scrollContainer.animate({ scrollTop: this.getLastReadLinePosition(scrollContainer) })
  },

  getLastReadLinePosition: function(scrollContainer) {
    var lastReadLine = scrollContainer.find('.babble-last-read-post-line')
    if (lastReadLine.length) {
      return lastReadLine.position().top + 45
    } else {
      return scrollContainer.height()
    }
  },

  getLastVisiblePostNumber: function(container) {
    var self = this
    return _.max(_.map(container.find('.babble-post-container'), function(post) {
      var postElement = $(post)
      if (self.isElementInScrollableDiv(postElement.parent(), container)) { return postElement.data('post-number') }
    }))
  }
});
{'fraction_non_alphanumeric': 0.13654316343802897, 'fraction_numerical': 0.001880759826970096, 'mean_word_length': 3.6163194444444446, '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': '3519626', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1593, 'n_words': 282}