<commit_before>/*
 idea: autosave happens on a per-component basis
 idea: for now, forms will have explicit save buttons
 question: if I PUT to /component/name/instances/id, is that idempotent? (yes)
 question: should we allow PATCH to /component/name/instances/id with partial data?
 */

var EditorToolbar,
  dom = require('../services/dom'),
  references = require('../services/references'),
  forms = require('../services/forms'),
  edit = require('../services/edit'),
  focus = require('../decorators/focus'),
  events = require('../services/events');

/**
 * Publish current page.
 */
function publish() {
  edit.publishPage()
    .then(console.log)
    .catch(console.error);
}

/**
 * Create a new page with the same layout as the current page.
 * currently, this just clones the current page
 * (cloning special "new" instances of the page-specific components)
 * e.g. /components/article/instances/new
 * @returns {Promise}
 */
function createPage() {
  // todo: allow users to choose their layout / components

  return edit.createPage();
}

/**
 * Remove querystring from current location
 */
function removeQuerystring() {
  location.href = location.href.split('?').shift();
}

/**
 * @class EditorToolbar
 * @param {Element} el
 * @property {Element} el
 */
EditorToolbar = function (el) {

  // grab the first component in the primary area
  this.main = dom.find('.main .primary [' + references.componentAttribute + ']');
  this.el = el;

  events.add(el, {
    '.close click': 'onClose',
    '.new click': 'onNewPage',
    '.settings click': 'onEditSettings',
    '.publish click': 'onPublish'
  }, this);

  window.addEventListener('beforeunload', function (e) {
    if (focus.hasCurrentFocus()) {
      e.returnValue = 'Are you sure you want to leave this page? Your data may not be saved.';
    }
  });
};

/**
 * @lends EditorToolbar#
 */
EditorToolbar.prototype = {
  /**
   * On close button
   */
  onClose: function () {
    removeQuerystring();
  },

  /**
   * On new page button
   */
  onNewPage: createPage,

  /**
   * On edit settings button
   */
  onEditSettings: function () {
    var primaryComponent = dom.find('.main .primary [' + references.referenceAttribute + ']'),
      ref = primaryComponent.getAttribute(references.referenceAttribute);

    forms.open(ref, document.body);
  },

  /**
   * On publish button
   */
  onPublish: function () {
    publish();
  }
};

module.exports = EditorToolbar;
<commit_msg>toolbar uses referenceAttr to get main component<commit_after>/*
 idea: autosave happens on a per-component basis
 idea: for now, forms will have explicit save buttons
 question: if I PUT to /component/name/instances/id, is that idempotent? (yes)
 question: should we allow PATCH to /component/name/instances/id with partial data?
 */

var EditorToolbar,
  dom = require('../services/dom'),
  references = require('../services/references'),
  forms = require('../services/forms'),
  edit = require('../services/edit'),
  focus = require('../decorators/focus'),
  events = require('../services/events');

/**
 * Publish current page.
 */
function publish() {
  edit.publishPage()
    .then(console.log)
    .catch(console.error);
}

/**
 * Create a new page with the same layout as the current page.
 * currently, this just clones the current page
 * (cloning special "new" instances of the page-specific components)
 * e.g. /components/article/instances/new
 * @returns {Promise}
 */
function createPage() {
  // todo: allow users to choose their layout / components

  return edit.createPage();
}

/**
 * Remove querystring from current location
 */
function removeQuerystring() {
  location.href = location.href.split('?').shift();
}

/**
 * @class EditorToolbar
 * @param {Element} el
 * @property {Element} el
 */
EditorToolbar = function (el) {

  // grab the first component in the primary area
  this.main = dom.find('.main .primary [' + references.referenceAttribute + ']');
  this.el = el;

  events.add(el, {
    '.close click': 'onClose',
    '.new click': 'onNewPage',
    '.settings click': 'onEditSettings',
    '.publish click': 'onPublish'
  }, this);

  window.addEventListener('beforeunload', function (e) {
    if (focus.hasCurrentFocus()) {
      e.returnValue = 'Are you sure you want to leave this page? Your data may not be saved.';
    }
  });
};

/**
 * @lends EditorToolbar#
 */
EditorToolbar.prototype = {
  /**
   * On close button
   */
  onClose: function () {
    removeQuerystring();
  },

  /**
   * On new page button
   */
  onNewPage: createPage,

  /**
   * On edit settings button
   */
  onEditSettings: function () {
    var primaryComponent = dom.find('.main .primary [' + references.referenceAttribute + ']'),
      ref = primaryComponent.getAttribute(references.referenceAttribute);

    forms.open(ref, document.body);
  },

  /**
   * On publish button
   */
  onPublish: function () {
    publish();
  }
};

module.exports = EditorToolbar;

{'fraction_non_alphanumeric': 0.1486732833704679, 'fraction_numerical': 0.0, 'mean_word_length': 3.2901824500434405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2826034', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1463, 'n_words': 424}