<commit_before>/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * 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.
 */
'use strict';

window.friendlyPix = window.friendlyPix || {};

/**
 * Handles the Home and Feed UI.
 */
friendlyPix.Feed = class {

  /**
   * Initializes the Friendly Pix feeds.
   * @constructor
   */
  constructor() {
    // List of all posts on the page.
    this.posts = [];
    // Map of posts that can be displayed.
    this.newPosts = {};

    // Firebase SDK.
    this.auth = firebase.app().auth();

    $(document).ready(() => {
      // Pointers to DOM elements.
      this.pageFeed = $('#page-feed');
      this.feedImageContainer = $('.fp-image-container', this.pageFeed);
      this.noPostsMessage = $('.fp-no-posts', this.pageFeed);
      this.nextPageButton = $('.fp-next-page-button button');
      this.newPostsButton = $('.fp-new-posts-button button');

      // Event bindings.
      this.newPostsButton.click(() => this.showNewPosts());
    });
  }

  /**
   * Appends the given list of `posts`.
   */
  addPosts(posts) {
    // Displays the list of posts
    let postIds = Object.keys(posts);
    for (let i = postIds.length - 1; i >= 0; i--) {
      this.noPostsMessage.hide();
      let postData = posts[postIds[i]];
      let post = friendlyPix.post.clone();
      this.posts.push(post);
      let postElement = post.fillPostData(postIds[i], postData.thumb_url || postData.url,
          postData.text, postData.author, postData.timestamp, post.full_url);
      // If a post with similar ID is already in the feed we replace it instead of appending.
      let existingPostElement = $(`.fp-post-${postIds[i]}`, this.feedImageContainer);
      if (existingPostElement.length) {
        existingPostElement.replaceWith(postElement);
      } else {
        this.feedImageContainer.append(postElement.addClass(`fp-post-${postIds[i]}`));
      }
    }
  }

  /**
   * Shows the "load next page" button and binds it the `nextPage` callback. If `nextPage` is `null`
   * then the button is hidden.
   */
  toggleNextPageButton(nextPage) {
    this.nextPageButton.unbind('click');
    if (nextPage) {
      let loadMorePosts = () => {
        this.nextPageButton.prop('disabled', true);
        console.log('Loading next page of posts.');
        nextPage().then(data => {
          this.addPosts(data.entries);
          this.toggleNextPageButton(data.nextPage);
        });
      };
      this.nextPageButton.show();
      // Enable infinite Scroll.
      friendlyPix.MaterialUtils.onEndScroll(100).then(loadMorePosts);
      this.nextPageButton.prop('disabled', false);
      this.nextPageButton.click(loadMorePosts);
    } else {
      this.nextPageButton.hide();
    }
  }

  /**
   * Prepends the list of new posts stored in `this.newPosts`. This happens when the user clicks on
   * the "Show new posts" button.
   */
  showNewPosts() {
    let newPosts = this.newPosts;
    this.newPosts = {};
    this.newPostsButton.hide();
    let postKeys = Object.keys(newPosts);

    for (let i = 0; i < postKeys.length; i++) {
      this.noPostsMessage.hide();
      let post = newPosts[postKeys[i]];
      let postElement = friendlyPix.post.clone();
      this.posts.push(postElement);
      this.feedImageContainer.prepend(postElement.fillPostData(postKeys[i], post.thumb_url ||
          post.url, post.text, post.author, post.timestamp, post.full_url));
    }
  }

  /**
   * Displays the general posts feed.
   */
  showGeneralFeed() {
    // Clear previously displayed posts if any.
    this.clear();

    // Load initial batch of posts.
    friendlyPix.firebase.getPosts().then(data => {
      // Listen for new posts.
      let latestPostId = Object.keys(data.entries)[Object.keys(data.entries).length - 1];
      friendlyPix.firebase.subscribeToGeneralFeed(
          (postId, postValue) => this.addNewPost(postId, postValue), latestPostId);

      // Adds fetched posts and next page button if necessary.
      this.addPosts(data.entries);
      this.toggleNextPageButton(data.nextPage);
    });

    // Listen for posts deletions.<commit_msg>Fixed theatre mode in the feed viewes<commit_after>/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * 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.
 */
'use strict';

window.friendlyPix = window.friendlyPix || {};

/**
 * Handles the Home and Feed UI.
 */
friendlyPix.Feed = class {

  /**
   * Initializes the Friendly Pix feeds.
   * @constructor
   */
  constructor() {
    // List of all posts on the page.
    this.posts = [];
    // Map of posts that can be displayed.
    this.newPosts = {};

    // Firebase SDK.
    this.auth = firebase.app().auth();

    $(document).ready(() => {
      // Pointers to DOM elements.
      this.pageFeed = $('#page-feed');
      this.feedImageContainer = $('.fp-image-container', this.pageFeed);
      this.noPostsMessage = $('.fp-no-posts', this.pageFeed);
      this.nextPageButton = $('.fp-next-page-button button');
      this.newPostsButton = $('.fp-new-posts-button button');

      // Event bindings.
      this.newPostsButton.click(() => this.showNewPosts());
    });
  }

  /**
   * Appends the given list of `posts`.
   */
  addPosts(posts) {
    // Displays the list of posts
    let postIds = Object.keys(posts);
    for (let i = postIds.length - 1; i >= 0; i--) {
      this.noPostsMessage.hide();
      let postData = posts[postIds[i]];
      let post = friendlyPix.post.clone();
      this.posts.push(post);
      let postElement = post.fillPostData(postIds[i], postData.thumb_url || postData.url,
          postData.text, postData.author, postData.timestamp, null, null, postData.full_url);
      // If a post with similar ID is already in the feed we replace it instead of appending.
      let existingPostElement = $(`.fp-post-${postIds[i]}`, this.feedImageContainer);
      if (existingPostElement.length) {
        existingPostElement.replaceWith(postElement);
      } else {
        this.feedImageContainer.append(postElement.addClass(`fp-post-${postIds[i]}`));
      }
    }
  }

  /**
   * Shows the "load next page" button and binds it the `nextPage` callback. If `nextPage` is `null`
   * then the button is hidden.
   */
  toggleNextPageButton(nextPage) {
    this.nextPageButton.unbind('click');
    if (nextPage) {
      let loadMorePosts = () => {
        this.nextPageButton.prop('disabled', true);
        console.log('Loading next page of posts.');
        nextPage().then(data => {
          this.addPosts(data.entries);
          this.toggleNextPageButton(data.nextPage);
        });
      };
      this.nextPageButton.show();
      // Enable infinite Scroll.
      friendlyPix.MaterialUtils.onEndScroll(100).then(loadMorePosts);
      this.nextPageButton.prop('disabled', false);
      this.nextPageButton.click(loadMorePosts);
    } else {
      this.nextPageButton.hide();
    }
  }

  /**
   * Prepends the list of new posts stored in `this.newPosts`. This happens when the user clicks on
   * the "Show new posts" button.
   */
  showNewPosts() {
    let newPosts = this.newPosts;
    this.newPosts = {};
    this.newPostsButton.hide();
    let postKeys = Object.keys(newPosts);

    for (let i = 0; i < postKeys.length; i++) {
      this.noPostsMessage.hide();
      let post = newPosts[postKeys[i]];
      let postElement = friendlyPix.post.clone();
      this.posts.push(postElement);
      this.feedImageContainer.prepend(postElement.fillPostData(postKeys[i], post.thumb_url ||
          post.url, post.text, post.author, post.timestamp, null, null, post.full_url));
    }
  }

  /**
   * Displays the general posts feed.
   */
  showGeneralFeed() {
    // Clear previously displayed posts if any.
    this.clear();

    // Load initial batch of posts.
    friendlyPix.firebase.getPosts().then(data => {
      // Listen for new posts.
      let latestPostId = Object.keys(data.entries)[Object.keys(data.entries).length - 1];
      friendlyPix.firebase.subscribeToGeneralFeed(
          (postId, postValue) => this.addNewPost(postId, postValue), latestPostId);

      // Adds fetched posts and next page button if necessary.
      this.addPosts(data.entries);
      this.toggleNextPageButton(data.nextPage);
    });

    // Listen for posts deletions.
{'fraction_non_alphanumeric': 0.13143104193687852, 'fraction_numerical': 0.00324254215304799, 'mean_word_length': 2.8, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '5482336', 'n_tokens_mistral': 2832, 'n_tokens_neox': 2691, 'n_words': 731}