{"size": 2416, "ext": "js", "max_stars_repo_path": "app/content-editor/src/js/editor.js", "max_stars_repo_name": "YuhuiCF/cms", "max_stars_repo_stars_event_min_datetime": "2020-08-08T15:29:08.000Z", "max_stars_repo_stars_event_max_datetime": "2020-08-08T15:29:08.000Z", "max_issues_repo_path": "app/content-editor/src/js/editor.js", "max_issues_repo_name": "YuhuiCF/cms", "max_issues_repo_issues_event_min_datetime": "2021-03-01T21:27:46.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T22:07:10.000Z", "max_forks_repo_path": "app/content-editor/src/js/editor.js", "max_forks_repo_name": "yuhuiX/cms", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 6, "max_forks_count": null, "avg_line_length": 21.1929824561, "max_line_length": 56, "alphanum_fraction": 0.5836092715}
/* eslint-disable no-var */
/* eslint-env browser */

/**
 * @description disable given field in the editor
 * @param {string} field
 * @return {undefined}
 */
function disableField(field) {
  var subEditor = editor.getEditor('root.' + field);
  if (subEditor) {
    subEditor.disable();
  }
}

/**
 * @description disable fields in the editor
 * @return {undefined}
 */
function disableFields() {
  disableField('_lastModified');
}

/**
 * @description get value from given request parameter
 * @param {string} key
 * @return {string}
 */
function getQueryParam(key) {
  var output = '';
  var qs = window.location.search;

  if (qs) {
    output = qs
      .substr(1)
      .split('&')
      .map(function(item) {
        var items = item.split('=');
        var key = items[0];
        var value = items[1];
        return {
          key: key,
          value: value,
        };
      })
      .filter(function(item) {
        return item.key === key;
      })
      .map(function(item) {
        return item.value;
      })
    ;
    if (output.length === 1) {
      output = output[0];
    }
  }

  return output;
}

/**
 * @description set _lastModified with current timestamp
 * @param {object} obj
 * @return {object} obj with _lastModified set
 */
function setLastModified(obj) {
  obj._lastModified = +(new Date());
  return obj;
}


disableFields();

$('#preview').bind('click', function() {
  var pagePath = getQueryParam('pagePath');
  window.open('/' + pagePath, 'fgDev');
});

$('#submit').bind('click', function() {
  let contentConfig = {
    data: editor.getValue(),
  };
  contentConfig = setLastModified(contentConfig);
  var pagePath = getQueryParam('pagePath');

  // TODO: and json is valid
  if (pagePath) {
    var $button = $(this);
    var $previewButton = $('#preview');
    var $errorMessage = $('#errorMessage');
    $errorMessage.empty();

    $button.prop('disabled', true);
    $previewButton.prop('disabled', true);

    $.ajax({
      type: 'PUT',
      url: '/api/content?pagePath=' + pagePath,
      data: JSON.stringify(contentConfig),
      contentType: 'application/json',
      success: function() {
        window.location.reload(true);
      },
      error: function(err) {
        $errorMessage.html(err.responseText);
      },
      complete: function() {
        $button.prop('disabled', false);
        $previewButton.prop('disabled', false);
      },
    });
  }
});
