<commit_before>/**
 * Copyright 2017 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';

FriendlyEats.prototype.initTemplates = function() {
  this.templates = {};
  document.querySelectorAll('.template').forEach(el => {
    this.templates[el.getAttribute('id')] = el;
  });
};

FriendlyEats.prototype.viewHome = function() {
  this.getAllRestaurants();
};

FriendlyEats.prototype.viewList = function(filters, filter_description) {
  if (!filter_description) {
    filter_description = 'any type of food with any price in any city.';
  }

  const mainEl = this.renderTemplate('main-adjusted');
  const headerEl = this.renderTemplate('header-base', {
    hasSectionHeader: true
  });

  this.replaceElement(
      headerEl.querySelector('#section-header'),
      this.renderTemplate('filter-display', { filter_description })
  );

  this.replaceElement(document.querySelector('.header'), headerEl);
  this.replaceElement(document.querySelector('main'), mainEl);

  headerEl.querySelector('#show-filters').addEventListener('click', () => {
    this.dialogs.filter.show();
  });

  const renderResults = doc => {
    if (!doc) {
      const headerEl = this.renderTemplate('header-base', {
        hasSectionHeader: true
      });
    
      const noResultsEl = this.renderTemplate('no-results');

      this.replaceElement(
          headerEl.querySelector('#section-header'),
          this.renderTemplate('filter-display', { filter_description })
      );

      headerEl.querySelector('#show-filters').addEventListener('click', () => {
        this.dialogs.filter.show();
      });

      this.replaceElement(document.querySelector('.header'), headerEl);
      this.replaceElement(document.querySelector('main'), noResultsEl);
      return;
    }
    const data = doc.data();
    data['.id'] = doc.id;
    data['go_to_restaurant'] = () => {
      this.router.navigate(`/restaurants/${doc.id}`);
    };

    const el = this.renderTemplate('restaurant-card', data);
    el.querySelector('.rating').append(this.renderRating(data.avgRating));
    el.querySelector('.price').append(this.renderPrice(data.price));

    mainEl.querySelector('#cards').append(el);
  };

  if (filters.city || filters.category || filters.price || filters.sort !== 'Rating' ) {
    this.getFilteredRestaurants({
     city: filters.city || 'Any',
     category: filters.category || 'Any',
     price: filters.price || 'Any',
     sort: filters.sort 
    }, renderResults);
  } else {
    this.getAllRestaurants(renderResults);
  }

  const toolbar = mdc.toolbar.MDCToolbar.attachTo(document.querySelector('.mdc-toolbar'));
  toolbar.fixedAdjustElement = document.querySelector('.mdc-toolbar-fixed-adjust');

  mdc.autoInit();
};

FriendlyEats.prototype.viewSetup = function() {
  const headerEl = this.renderTemplate('header-base', {
    hasSectionHeader: false
  });

  const config = this.getFirebaseConfig();
  const noRestaurantsEl = this.renderTemplate('no-restaurants', config);

  const button = noRestaurantsEl.querySelector('#add_mock_data');
  let addingMockData = false;

  button.addEventListener('click', () => {
    if (addingMockData) return;
    addingMockData = true;

    this.style.opacity = '0.4';
    this.innerText = 'Please wait...';

    this.addMockRestaurants().then(() => {
      this.rerender();
    });
  });

  this.replaceElement(document.querySelector('.header'), headerEl);
  this.replaceElement(document.querySelector('main'), noRestaurantsEl);

  firebase
  .firestore()
  .collection('restaurants')
  .limit(1)
  .onSnapshot(snapshot => {
    if (snapshot.size && !addingMockData) {
      this.router.navigate('/');
    }
  });
};

FriendlyEats.prototype.initReviewDialog = function() {
  const dialog = document.querySelector('#dialog-add-review');
  this.dialogs.add_review = new mdc.dialog.MDCDialog(dialog);

  this.dialogs.add_review.listen('MDCDialog:accept', () => {
    let pathname = this.getCleanPath(document.location.pathname);
    let id = pathname.split('/')[2];

    this.addRating(id, {<commit_msg>Bugfix?<commit_after>/**
 * Copyright 2017 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';

FriendlyEats.prototype.initTemplates = function() {
  this.templates = {};
  document.querySelectorAll('.template').forEach(el => {
    this.templates[el.getAttribute('id')] = el;
  });
};

FriendlyEats.prototype.viewHome = function() {
  this.getAllRestaurants();
};

FriendlyEats.prototype.viewList = function(filters, filter_description) {
  if (!filter_description) {
    filter_description = 'any type of food with any price in any city.';
  }

  const mainEl = this.renderTemplate('main-adjusted');
  const headerEl = this.renderTemplate('header-base', {
    hasSectionHeader: true
  });

  this.replaceElement(
      headerEl.querySelector('#section-header'),
      this.renderTemplate('filter-display', { filter_description })
  );

  this.replaceElement(document.querySelector('.header'), headerEl);
  this.replaceElement(document.querySelector('main'), mainEl);

  headerEl.querySelector('#show-filters').addEventListener('click', () => {
    this.dialogs.filter.show();
  });

  const renderResults = doc => {
    if (!doc) {
      const headerEl = this.renderTemplate('header-base', {
        hasSectionHeader: true
      });
    
      const noResultsEl = this.renderTemplate('no-results');

      this.replaceElement(
          headerEl.querySelector('#section-header'),
          this.renderTemplate('filter-display', { filter_description })
      );

      headerEl.querySelector('#show-filters').addEventListener('click', () => {
        this.dialogs.filter.show();
      });

      this.replaceElement(document.querySelector('.header'), headerEl);
      this.replaceElement(document.querySelector('main'), noResultsEl);
      return;
    }
    const data = doc.data();
    data['.id'] = doc.id;
    data['go_to_restaurant'] = () => {
      this.router.navigate(`/restaurants/${doc.id}`);
    };

    const el = this.renderTemplate('restaurant-card', data);
    el.querySelector('.rating').append(this.renderRating(data.avgRating));
    el.querySelector('.price').append(this.renderPrice(data.price));

    mainEl.querySelector('#cards').append(el);
  };

  if (filters.city || filters.category || filters.price || filters.sort !== 'Rating' ) {
    this.getFilteredRestaurants({
     city: filters.city || 'Any',
     category: filters.category || 'Any',
     price: filters.price || 'Any',
     sort: filters.sort 
    }, renderResults);
  } else {
    this.getAllRestaurants(renderResults);
  }

  const toolbar = mdc.toolbar.MDCToolbar.attachTo(document.querySelector('.mdc-toolbar'));
  toolbar.fixedAdjustElement = document.querySelector('.mdc-toolbar-fixed-adjust');

  mdc.autoInit();
};

FriendlyEats.prototype.viewSetup = function() {
  const headerEl = this.renderTemplate('header-base', {
    hasSectionHeader: false
  });

  const config = this.getFirebaseConfig();
  const noRestaurantsEl = this.renderTemplate('no-restaurants', config);

  const button = noRestaurantsEl.querySelector('#add_mock_data');
  let addingMockData = false;

  button.addEventListener('click', event => {
    if (addingMockData) return;
    addingMockData = true;

    event.target.style.opacity = '0.4';
    event.target.innerText = 'Please wait...';

    this.addMockRestaurants().then(() => {
      this.rerender();
    });
  });

  this.replaceElement(document.querySelector('.header'), headerEl);
  this.replaceElement(document.querySelector('main'), noRestaurantsEl);

  firebase
  .firestore()
  .collection('restaurants')
  .limit(1)
  .onSnapshot(snapshot => {
    if (snapshot.size && !addingMockData) {
      this.router.navigate('/');
    }
  });
};

FriendlyEats.prototype.initReviewDialog = function() {
  const dialog = document.querySelector('#dialog-add-review');
  this.dialogs.add_review = new mdc.dialog.MDCDialog(dialog);

  this.dialogs.add_review.listen('MDCDialog:accept', () => {
    let pathname = this.getCleanPath(document.location.pathname);
    let id = pathname.split('/')[2];

    this.addRating(id, {
{'fraction_non_alphanumeric': 0.1492098092643052, 'fraction_numerical': 0.0026158038147138965, 'mean_word_length': 3.8782562466772994, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '3074961', 'n_tokens_mistral': 2686, 'n_tokens_neox': 2656, 'n_words': 517}