<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';

// Creates Google maps: <-- TO-DO: Reset the Google Map upon reload.

// This example requires the Places library. Include the libraries=places
  // parameter when you first load the API. For example:
  // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

  function initAutocomplete() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: -33.8688, lng: 151.2195},
      zoom: 13,
      mapTypeId: 'roadmap'
    });

    // Create the search box and link it to the UI element.
    var input = document.getElementById('pac-input');
    var searchBox = new google.maps.places.SearchBox(input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    // Bias the SearchBox results towards current map's viewport.
    map.addListener('bounds_changed', function() {
      searchBox.setBounds(map.getBounds());
    });

    var markers = [];
    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed', function() {
      var places = searchBox.getPlaces();

      if (places.length == 0) {
        return;
      }

      // Clear out the old markers.
      markers.forEach(function(marker) {
        marker.setMap(null);
      });
      markers = [];

      // For each place, get the icon, name and location.
      var bounds = new google.maps.LatLngBounds();
      places.forEach(function(place) {
        if (!place.geometry) {
          console.log("Returned place contains no geometry");
          return;
        }
        var icon = {
          url: place.icon,
          size: new google.maps.Size(71, 71),
          origin: new google.maps.Point(0, 0),
          anchor: new google.maps.Point(17, 34),
          scaledSize: new google.maps.Size(25, 25)
        };

        // Create a marker for each place.
        markers.push(new google.maps.Marker({
          map: map,
          icon: icon,
          title: place.name,
          position: place.geometry.location
        }));

        if (place.geometry.viewport) {
          // Only geocodes have viewport.
          bounds.union(place.geometry.viewport);
        } else {
          bounds.extend(place.geometry.location);
        }
      });
      map.fitBounds(bounds);
    });
  }

// Initializes FriendlyChat.
function FriendlyChat() {
  this.checkSetup();

  // Shortcuts to DOM Elements.
  this.messageList = document.getElementById('messages');
  this.messageForm = document.getElementById('message-form');
  this.messageInput = document.getElementById('message');
  this.submitButton = document.getElementById('submit');
  this.submitImageButton = document.getElementById('submitImage');
  this.imageForm = document.getElementById('image-form');
  this.mediaCapture = document.getElementById('mediaCapture');
  this.userPic = document.getElementById('user-pic');
  this.userName = document.getElementById('user-name');
  this.signInButton = document.getElementById('sign-in');
  this.signOutButton = document.getElementById('sign-out');
  this.signInSnackbar = document.getElementById('must-signin-snackbar');

  // DOM elements for the new chatroom form
  this.newChatForm = document.getElementById('new-chat-form')
  this.newChatInputTitle = document.getElementById('new-chat-input-title')
  this.newChatInputWhat = document.getElementById('new-chat-input-what')
  this.newChatInputWho = document.getElementById('new-chat-input-who')
  this.newChatWhenIcon = document.getElementById('when-icon-div')
  this.newChatWhenBounds = document.getElementById('when-column-bounds')
  this.newChatInputWhenDate = document.getElementById('new-chat-input-when-date')
  this.newChatInputWhenTime = document.getElementById('new-chat-input-when-time')
  this.newChatInputWhere = document.getElementById('pac-input')
  this.newChatButton = document.getElementById('new-chat-button')
  this.newChatPopup = document.getElementById('new-chat-popup')
  this.chatList = document.getElementById('chat-list')
  this.chatInputMap = document.getElementById('map')

  // Load chat data:
  this.chatItemData = document.getElementById('show-chat-data');
  this.chatItemData.addEventListener('click', this.loadMessages.bind(this)); // <-- Developer: return to this.

  // Save chats on chatroom form submit:
  this.newChatForm.addEventListener('submit', this.saveChat.bind(this));

  // Reveal when and where upon form section click:
  this.newChatWhenIcon.addEventListener('click', this.showDateTimeInputs.bind(this));

  // Save message on form submit:
  this.messageForm.addEventListener('submit', this.saveMessage.bind(this));
  this.signOutButton.addEventListener('click', this.signOut.bind(this));
  this.signInButton.addEventListener('click', this.signIn.bind(this));

  // Toggle for the button:
  var buttonTogglingHandler = this.toggleButton.bind(this);
  this.messageInput.addEventListener('keyup', buttonTogglingHandler);
  this.messageInput.addEventListener('change', buttonTogglingHandler);

  // Events for image upload:
  this.submitImageButton.addEventListener('click', function(e) {
    e.preventDefault();
    this.mediaCapture.click();
  }.bind(this));
  this.mediaCapture.addEventListener('change', this.saveImageMessage.bind(this));
  this.initFirebase();
}

// Sets up shortcuts to Firebase features and initiate firebase auth:
FriendlyChat.prototype.initFirebase = function() {
  this.auth = firebase.auth();
  this.database = firebase.database();
  this.storage = firebase.storage();
  // Initiates Firebase auth and listen to auth state changes.
  this.auth.onAuthStateChanged(this.onAuthStateChanged.bind(this));
};

// Add dynamic 'When' form:
FriendlyChat.prototype.showDateTimeInputs = function () {
  this.newChatWhenBounds.removeAttribute('hidden');
}

FriendlyChat.prototype.removeChats = function() {
  this.chatList.innerHTML = "";
  this.messageList.innerHTML = "";
  this.chatItemData.innerHTML = "Your DoWhop details will appear here!";
}

FriendlyChat.prototype.loadChats = function() {
  // First, make sure the view element is chosen:
  var myView = this.chatList;
  var myViewMessageList = this.messageList;
  // Second, make sure we have reference to the current user's data:
  var me = this.auth.currentUser;
  var myRef = this.database.ref().child('chats/');
  var myChatData = this.chatItemData;

  myRef.on('child_added', snap => {
    // Creating the buttons to further load chat data:
      var container = document.createElement('div');
      container.innerHTML = FriendlyChat.CHAT_TEMPLATE;
      let button = container.firstChild;
      button.setAttribute('id', snap.key);
      button.innerHTML = snap.val().title;

      // Setting the events for when chat-thread button is clicked.
      button.addEventListener('click', function(){
        myViewMessageList.innerText = ''; // <-- Resetting the form;
        myChatData.innerText = snap.val().title;
        myChatData.innerHTML = "<h3 id='" + snap.key + "'>" + snap.val().title + '</h3>' +
                "<p>Click  to load messages.</p>" +
                "<h5>When?</h5>" +
                "<p>" + snap.val().whenDate + ' at ' + snap.val().whenTime + '</p>' +
                "<h5>Who?</h5>" +
                "<p>" + snap.val().who + '</p>' +
                "<h5>Where?</h5>" +
                "<p>" + snap.val().where + '</p>'
      });
      myView.appendChild(button);
  });
};

// Loads messages history and listens for upcoming ones:
FriendlyChat.prototype.loadMessages = function() {
  let user = this.auth.currentUser.uid;
  var chatIdCurrent = this.chatItemData.firstChild.id // <-- Refactor
  this.messagesRef = this.database.ref().child('messages/' + chatIdCurrent);

  // Make sure we remove all previous listeners.
  this.messagesRef.off();

  // Loads the last x number of messages and listen for new ones:
  var setMessage = function(data) {
    var val = data.val();
    this.displayMessage(data.key, val.name, val.text, val.photoUrl, val.imageUrl);
  }.bind(this);
  this.messagesRef.orderByKey().limitToLast(12).on('child_added', setMessage);
  this.messagesRef.orderByKey().limitToLast(12).on('child_changed', setMessage);<commit_msg>Update views<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';

// Creates Google maps: <-- TO-DO: Reset the Google Map upon reload.

// This example requires the Places library. Include the libraries=places
  // parameter when you first load the API. For example:
  // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

  function initAutocomplete() {
    var map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: -33.8688, lng: 151.2195},
      zoom: 13,
      mapTypeId: 'roadmap'
    });

    // Create the search box and link it to the UI element.
    var input = document.getElementById('pac-input');
    var searchBox = new google.maps.places.SearchBox(input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    // Bias the SearchBox results towards current map's viewport.
    map.addListener('bounds_changed', function() {
      searchBox.setBounds(map.getBounds());
    });

    var markers = [];
    // Listen for the event fired when the user selects a prediction and retrieve
    // more details for that place.
    searchBox.addListener('places_changed', function() {
      var places = searchBox.getPlaces();

      if (places.length == 0) {
        return;
      }

      // Clear out the old markers.
      markers.forEach(function(marker) {
        marker.setMap(null);
      });
      markers = [];

      // For each place, get the icon, name and location.
      var bounds = new google.maps.LatLngBounds();
      places.forEach(function(place) {
        if (!place.geometry) {
          console.log("Returned place contains no geometry");
          return;
        }
        var icon = {
          url: place.icon,
          size: new google.maps.Size(71, 71),
          origin: new google.maps.Point(0, 0),
          anchor: new google.maps.Point(17, 34),
          scaledSize: new google.maps.Size(25, 25)
        };

        // Create a marker for each place.
        markers.push(new google.maps.Marker({
          map: map,
          icon: icon,
          title: place.name,
          position: place.geometry.location
        }));

        if (place.geometry.viewport) {
          // Only geocodes have viewport.
          bounds.union(place.geometry.viewport);
        } else {
          bounds.extend(place.geometry.location);
        }
      });
      map.fitBounds(bounds);
    });
  }

// Initializes FriendlyChat.
function FriendlyChat() {
  this.checkSetup();

  // Shortcuts to DOM Elements.
  this.messageList = document.getElementById('messages');
  this.messageForm = document.getElementById('message-form');
  this.messageInput = document.getElementById('message');
  this.submitButton = document.getElementById('submit');
  this.submitImageButton = document.getElementById('submitImage');
  this.imageForm = document.getElementById('image-form');
  this.mediaCapture = document.getElementById('mediaCapture');
  this.userPic = document.getElementById('user-pic');
  this.userName = document.getElementById('user-name');
  this.signInButton = document.getElementById('sign-in');
  this.signOutButton = document.getElementById('sign-out');
  this.signInSnackbar = document.getElementById('must-signin-snackbar');

  // DOM elements for the new chatroom form
  this.newChatForm = document.getElementById('new-chat-form')
  this.newChatInputTitle = document.getElementById('new-chat-input-title')
  this.newChatInputWhat = document.getElementById('new-chat-input-what')
  this.newChatInputWho = document.getElementById('new-chat-input-who')
  this.newChatWhenIcon = document.getElementById('when-icon-div')
  this.newChatWhenBounds = document.getElementById('when-column-bounds')
  this.newChatInputWhenDate = document.getElementById('new-chat-input-when-date')
  this.newChatInputWhenTime = document.getElementById('new-chat-input-when-time')
  this.newChatInputWhere = document.getElementById('pac-input')
  this.newChatButton = document.getElementById('new-chat-button')
  this.newChatPopup = document.getElementById('new-chat-popup')
  this.chatList = document.getElementById('chat-list')
  this.chatInputMap = document.getElementById('map')

  // Load chat data:
  this.chatItemData = document.getElementById('show-chat-data');
  this.chatItemData.addEventListener('click', this.loadMessages.bind(this)); // <-- Developer: return to this.

  // Save chats on chatroom form submit:
  this.newChatForm.addEventListener('submit', this.saveChat.bind(this));

  // Reveal when and where upon form section click:
  this.newChatWhenIcon.addEventListener('click', this.showDateTimeInputs.bind(this));

  // Save message on form submit:
  this.messageForm.addEventListener('submit', this.saveMessage.bind(this));
  this.signOutButton.addEventListener('click', this.signOut.bind(this));
  this.signInButton.addEventListener('click', this.signIn.bind(this));

  // Toggle for the button:
  var buttonTogglingHandler = this.toggleButton.bind(this);
  this.messageInput.addEventListener('keyup', buttonTogglingHandler);
  this.messageInput.addEventListener('change', buttonTogglingHandler);

  // Events for image upload:
  this.submitImageButton.addEventListener('click', function(e) {
    e.preventDefault();
    this.mediaCapture.click();
  }.bind(this));
  this.mediaCapture.addEventListener('change', this.saveImageMessage.bind(this));
  this.initFirebase();
}

// Sets up shortcuts to Firebase features and initiate firebase auth:
FriendlyChat.prototype.initFirebase = function() {
  this.auth = firebase.auth();
  this.database = firebase.database();
  this.storage = firebase.storage();
  // Initiates Firebase auth and listen to auth state changes.
  this.auth.onAuthStateChanged(this.onAuthStateChanged.bind(this));
};

// Add dynamic 'When' form:
FriendlyChat.prototype.showDateTimeInputs = function () {
  this.newChatWhenBounds.removeAttribute('hidden');
}

FriendlyChat.prototype.removeChats = function() {
  this.chatList.innerHTML = "";
  this.messageList.innerHTML = "";
  this.chatItemData.innerHTML = "Your DoWhop details will appear here!";
}

FriendlyChat.prototype.loadChats = function() {
  // First, make sure the view element is chosen:
  var myView = this.chatList;
  var myViewMessageList = this.messageList;
  // Second, make sure we have reference to the current user's data:
  var me = this.auth.currentUser;
  var myRef = this.database.ref().child('chats/');
  var myChatData = this.chatItemData;

  myRef.on('child_added', snap => {
    // Creating the buttons to further load chat data:
      var container = document.createElement('div');
      container.innerHTML = FriendlyChat.CHAT_TEMPLATE;
      let button = container.firstChild;
      button.setAttribute('id', snap.key);
      button.innerHTML = snap.val().title;

      // Setting the events for when chat-thread button is clicked.
      button.addEventListener('click', function(){
        myViewMessageList.innerText = ''; // <-- Resetting the form;
        myChatData.innerText = snap.val().title;
        myChatData.innerHTML = "<h3 id='" + snap.key + "'>" + snap.val().title + '</h3>' +
                "<p>Click  to load messages.</p>" +
                "<h5>What?</h5>" +
                "<p>" + (snap.val().what || 'TBD') + "</p>" +
                "<h5>When?</h5>" +
                "<p>" + snap.val().whenDate + ' at ' + snap.val().whenTime + '</p>' +
                "<h5>Who?</h5>" +
                "<p>" + snap.val().who + '</p>' +
                "<h5>Where?</h5>" +
                "<p>" + snap.val().where + '</p>'
      });
      myView.appendChild(button);
  });
};

// Loads messages history and listens for upcoming ones:
FriendlyChat.prototype.loadMessages = function() {
  let user = this.auth.currentUser.uid;
  var chatIdCurrent = this.chatItemData.firstChild.id // <-- Refactor
  this.messagesRef = this.database.ref().child('messages/' + chatIdCurrent);

  // Make sure we remove all previous listeners.
  this.messagesRef.off();

  // Loads the last x number of messages and listen for new ones:
  var setMessage = function(data) {
    var val = data.val();
    this.displayMessage(data.key, val.name, val.text, val.photoUrl, val.imageUrl);
  }.bind(this);
  this.messagesRef.orderByKey().limitToLast(12).on('child_added', setMessage);
  this.messagesRef.orderByKey().limitToLast(12).on('child_changed', setMessage);
{'fraction_non_alphanumeric': 0.13366670412405887, 'fraction_numerical': 0.0057309810091021466, 'mean_word_length': 3.4056930693069307, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 43, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '744471', 'n_tokens_mistral': 5165, 'n_tokens_neox': 5022, 'n_words': 1203}