<commit_before>(function (window, document) { // eslint-disable-line no-unused-vars
  'use strict';
  var lima = window.lima;
  var _ = lima._;

  function extractPaperTitleFromUrl(path) {
    // the path of a page for a paper will be '/email/title/*',
    // so extract the 'title' portion here:

    if (!path) path = window.location.pathname;

    var start = path.indexOf('/', 1) + 1;
    if (start === 0) throw new Error('page url doesn\'t have a title');

    var rest = path.indexOf('/', start);
    if (rest === -1) rest = Infinity;

    return path.substring(start, rest);
  }

  function updatePageURL() {
    if (extractPaperTitleFromUrl() == currentPaper.title) return; // all done

    // the path of a page for a paper will be '/email/title/*',
    // so update the 'title' portion here from the current paper (in case the user changes the title)
    var start = window.location.pathname.indexOf('/', 1) + 1;
    if (start === 0) throw new Error('page url doesn\'t have a title');

    var rest = window.location.pathname.indexOf('/', start);

    var url = window.location.pathname.substring(0, start) + currentPaper.title;
    if (rest > -1) url += window.location.pathname.substring(rest);

    if (lima.userLocalStorage) url += '?type=paper';

    window.history.replaceState({}, currentPaper.title, url);
  }

  function createPageURL(email, title) {
    return '/' + email + '/' + title;
  }


  /* paper list
   *
   *
   *        #####    ##   #####  ###### #####     #      #  ####  #####
   *        #    #  #  #  #    # #      #    #    #      # #        #
   *        #    # #    # #    # #####  #    #    #      #  ####    #
   *        #####  ###### #####  #      #####     #      #      #   #
   *        #      #    # #      #      #   #     #      # #    #   #
   *        #      #    # #      ###### #    #    ###### #  ####    #
   *
   *
   */
  function requestAndFillPaperList() {
    lima.getGapiIDToken()
    .then(function (idToken) {
      var email = lima.extractUserProfileEmailFromUrl();
      return fetch('/api/papers/' + email, _.idTokenToFetchOptions(idToken));
    })
    .then(function (response) {
      if (response.status === 404) return [];
      else return _.fetchJson(response);
    })
    .then(function (papers) {
      return papers.concat(loadAllLocalPapers());
    })
    .then(fillPapersList)
    .catch(function (err) {
      console.error("problem getting papers");
      console.error(err);
      throw _.apiFail();
    });
  }

  function fillPapersList(papers) {
    var list = _.findEl('.paper.list > ul');
    list.innerHTML = '';

    if (papers.length) {
      // todo sort
      papers.forEach(function (paper) {
        var li = _.cloneTemplate('paper-list-item-template').children[0];
        _.fillEls(li, '.name', paper.title);
        _.fillEls(li, '.reference', paper.reference);
        _.setProps(li, '.reference', 'title', paper.reference);
        _.fillEls(li, '.description', paper.description);
        _.setProps(li, '.description', 'title', paper.description);
        if (!paper.storedLocally) {
          _.setProps(li, 'a.mainlink', 'href', paper.title);
        } else {
          _.setProps(li, 'a.mainlink', 'href', '/' + lima.localStorageUserEmailAddress + '/' + paper.title + '?type=paper'); // hint in case of local storage
          li.classList.add('local');
          // todo do something with the above class - highlight local papers
        }
        _.fillTags(li, '.tags', paper.tags);
        list.appendChild(li);
      });
    } else {
      list.appendChild(_.cloneTemplate('empty-list-template'));
    }

    _.setYouOrName();
  }


  /* paper
   *
   *
   *           #####    ##   #####  ###### #####
   *           #    #  #  #  #    # #      #    #
   *           #    # #    # #    # #####  #    #
   *           #####  ###### #####  #      #####
   *           #      #    # #      #      #   #
   *           #      #    # #      ###### #    #
   *
   *
   */

  var currentPaper;

  function requestPaper(title) {
    var email = lima.extractUserProfileEmailFromUrl();

    if (lima.userLocalStorage) {
      return Promise.resolve()
      .then(loadLocalPapersList)
      .then(function() { return loadLocalPaper('/' + email + '/' + title); })
      .then(initPaper)
      .catch(function (err) {
        console.log("could not get local paper, trying server for " + title, err);
        return requestServerPaper(email, title);
      });
    }

    return requestServerPaper(email, title);
  }

  function requestPaperById(id) {
    if (lima.userLocalStorage) {
      return Promise.resolve()
      .then(function() { return loadLocalPaperById(id); })
      .then(initPaper)
      .catch(function (err) {
        console.log("problem getting local paper by id " + id, err);
        _.apiFail();
      });
    }

    return Promise.reject(new Error('cannot request paper by id from server yet'));
  }

  function requestServerPaper(email, title) {
    return lima.getGapiIDToken()
    .then(function (idToken) {
      var currentPaperUrl = '/api/papers/' + email + '/' + title;
      return fetch(currentPaperUrl, _.idTokenToFetchOptions(idToken));
    })
    .then(function (response) {
      if (response.status === 404) _.notFound();
      else return _.fetchJson(response);
    })
    .then(initPaper);
  }

  function requestAndFillPaper() {
    var title = lima.extractPaperTitleFromUrl();
    _.fillEls('#paper .title', title);

    lima.getColumns() // todo getColumns could run in parallel with everything before updatePaperView
    .then(function() { return requestPaper(title); })
    .then(setCurrentPaper)
    .then(updatePaperView)
    .then(function() {
      _.removeClass('body', 'loading');
      lima.onSignInChange(updatePaperView);<commit_msg>papers: fix overzealous use of apiFail<commit_after>(function (window, document) { // eslint-disable-line no-unused-vars
  'use strict';
  var lima = window.lima;
  var _ = lima._;

  function extractPaperTitleFromUrl(path) {
    // the path of a page for a paper will be '/email/title/*',
    // so extract the 'title' portion here:

    if (!path) path = window.location.pathname;

    var start = path.indexOf('/', 1) + 1;
    if (start === 0) throw new Error('page url doesn\'t have a title');

    var rest = path.indexOf('/', start);
    if (rest === -1) rest = Infinity;

    return path.substring(start, rest);
  }

  function updatePageURL() {
    if (extractPaperTitleFromUrl() == currentPaper.title) return; // all done

    // the path of a page for a paper will be '/email/title/*',
    // so update the 'title' portion here from the current paper (in case the user changes the title)
    var start = window.location.pathname.indexOf('/', 1) + 1;
    if (start === 0) throw new Error('page url doesn\'t have a title');

    var rest = window.location.pathname.indexOf('/', start);

    var url = window.location.pathname.substring(0, start) + currentPaper.title;
    if (rest > -1) url += window.location.pathname.substring(rest);

    if (lima.userLocalStorage) url += '?type=paper';

    window.history.replaceState({}, currentPaper.title, url);
  }

  function createPageURL(email, title) {
    return '/' + email + '/' + title;
  }


  /* paper list
   *
   *
   *        #####    ##   #####  ###### #####     #      #  ####  #####
   *        #    #  #  #  #    # #      #    #    #      # #        #
   *        #    # #    # #    # #####  #    #    #      #  ####    #
   *        #####  ###### #####  #      #####     #      #      #   #
   *        #      #    # #      #      #   #     #      # #    #   #
   *        #      #    # #      ###### #    #    ###### #  ####    #
   *
   *
   */
  function requestAndFillPaperList() {
    lima.getGapiIDToken()
    .then(function (idToken) {
      var email = lima.extractUserProfileEmailFromUrl();
      return fetch('/api/papers/' + email, _.idTokenToFetchOptions(idToken));
    })
    .then(function (response) {
      if (response.status === 404) return [];
      else return _.fetchJson(response);
    })
    .then(function (papers) {
      return papers.concat(loadAllLocalPapers());
    })
    .then(fillPapersList)
    .catch(function (err) {
      console.error("problem getting papers");
      console.error(err);
      throw _.apiFail();
    });
  }

  function fillPapersList(papers) {
    var list = _.findEl('.paper.list > ul');
    list.innerHTML = '';

    if (papers.length) {
      // todo sort
      papers.forEach(function (paper) {
        var li = _.cloneTemplate('paper-list-item-template').children[0];
        _.fillEls(li, '.name', paper.title);
        _.fillEls(li, '.reference', paper.reference);
        _.setProps(li, '.reference', 'title', paper.reference);
        _.fillEls(li, '.description', paper.description);
        _.setProps(li, '.description', 'title', paper.description);
        if (!paper.storedLocally) {
          _.setProps(li, 'a.mainlink', 'href', paper.title);
        } else {
          _.setProps(li, 'a.mainlink', 'href', '/' + lima.localStorageUserEmailAddress + '/' + paper.title + '?type=paper'); // hint in case of local storage
          li.classList.add('local');
          // todo do something with the above class - highlight local papers
        }
        _.fillTags(li, '.tags', paper.tags);
        list.appendChild(li);
      });
    } else {
      list.appendChild(_.cloneTemplate('empty-list-template'));
    }

    _.setYouOrName();
  }


  /* paper
   *
   *
   *           #####    ##   #####  ###### #####
   *           #    #  #  #  #    # #      #    #
   *           #    # #    # #    # #####  #    #
   *           #####  ###### #####  #      #####
   *           #      #    # #      #      #   #
   *           #      #    # #      ###### #    #
   *
   *
   */

  var currentPaper;

  function requestPaper(title) {
    var email = lima.extractUserProfileEmailFromUrl();

    if (lima.userLocalStorage) {
      return Promise.resolve()
      .then(loadLocalPapersList)
      .then(function() { return loadLocalPaper('/' + email + '/' + title); })
      .then(initPaper)
      .catch(function (err) {
        console.log("could not get local paper, trying server for " + title, err);
        return requestServerPaper(email, title);
      });
    }

    return requestServerPaper(email, title);
  }

  function requestPaperById(id) {
    if (lima.userLocalStorage) {
      return Promise.resolve()
      .then(function() { return loadLocalPaperById(id); })
      .then(initPaper)
      .catch(function (err) {
        console.log("problem getting local paper by id " + id, err);
        throw err;
      });
    }

    return Promise.reject(new Error('cannot request paper by id from server yet'));
  }

  function requestServerPaper(email, title) {
    return lima.getGapiIDToken()
    .then(function (idToken) {
      var currentPaperUrl = '/api/papers/' + email + '/' + title;
      return fetch(currentPaperUrl, _.idTokenToFetchOptions(idToken));
    })
    .then(function (response) {
      if (response.status === 404) _.notFound();
      else return _.fetchJson(response);
    })
    .then(initPaper);
  }

  function requestAndFillPaper() {
    var title = lima.extractPaperTitleFromUrl();
    _.fillEls('#paper .title', title);

    lima.getColumns() // todo getColumns could run in parallel with everything before updatePaperView
    .then(function() { return requestPaper(title); })
    .then(setCurrentPaper)
    .then(updatePaperView)
    .then(function() {
      _.removeClass('body', 'loading');
      lima.onSignInChange(updatePaperView);
{'fraction_non_alphanumeric': 0.16866637893828226, 'fraction_numerical': 0.002762192490289167, 'mean_word_length': 2.1673045379989064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4515499', 'n_tokens_mistral': 3640, 'n_tokens_neox': 3508, 'n_words': 743}