<commit_before>/**
 * Global object for settings and storage
 */
var Cedar = {
  initialized: false,
  store: null,
  auth: null,
  config: {
    api: '',
    server: '',
    debug: false
  }
};


/**
* Cedar.Init
*
* take care of global initializations
*
* @param <Array> options
*   - server : 'test.cdr.plyinc.com' - *required
*   - debug : true | false
*/
Cedar.Init = function(options) {
  if ( Cedar.initialized ) {
    return;
  }

  if ( typeof options.server === 'undefined' ) {
    throw 'Cedar Error: must provide "server" value on Init()';
  }

  Cedar.config.server = 'http://' + options.server;
  Cedar.config.api = 'http://' + options.server + '/api';

  if ( typeof options.debug !== 'undefined' && options.debug ) {
    Cedar.config.debug = true;
  }

  if ( Cedar.store === null ) {
    Cedar.store = new Cedar.Store();
  }

  if ( Cedar.auth === null ) {
    Cedar.auth = new Cedar.Auth();
  }

  if (Cedar.auth.isEditMode()) {
    $(document).ready(function() {
      var $body = $('body');
      var globalActions = '<div class="cedar-cms-global-actions">' +
        '<a href="#" class="cedar-cms-global-action" onclick="window.location.reload();">' +
        '<span class="cedar-cms-icon cedar-cms-icon-edit"></span> ' +
        '<span class="cedar-cms-global-action-label">Refresh</span>' +
        '</a><br>' +
        '<a class="cedar-cms-global-action" href="' + Cedar.auth.getLogOffURL() + '">' +
        '<span class="cedar-cms-icon cedar-cms-icon-edit"></span> ' +
        '<span class="cedar-cms-global-action-label">Log Off Cedar</span>' +
        '</a>' +
        '</div>';
      $body.append(globalActions);
    });
  }

  Cedar.initialized = true;
}
/**
 * display message to console if Cedar.debug = true
 *
 * @param <string> message
 */
Cedar.debug = function(msg) {
  if (Cedar.config.debug) {
    console.log(msg);
  }
}


/**
 * Cedar.Auth
 *
 * responsible for determining if we're in edit mode
 */
Cedar.Auth = function() {
}
Cedar.Auth.prototype.isEditMode = function() {
  return this.isEditUrl();
}
Cedar.Auth.prototype.isEditUrl = function() {
  var sPageURL = window.location.search.substring(1);
  var sURLVariables = sPageURL.split('&');
  for (var i = 0; i < sURLVariables.length; i++) {
    if (sURLVariables[i] == 'cdrlogin') {
      return true;
    }
  }
  return false;
}
Cedar.Auth.prototype.getLogOffURL = function() {
  return this.removeURLParameter(window.location.href, 'cdrlogin');
}
// adapted from stackoverflow: http://stackoverflow.com/questions/1634748/how-can-i-delete-a-query-string-parameter-in-javascript
Cedar.Auth.prototype.removeURLParameter = function(url, parameter) {
  var splitUrl = url.split('#');
  var serverUrl = splitUrl[0];
  var clientUrl = splitUrl[1];
  //prefer to use l.search if you have a location/link object
  var splitServerUrl= serverUrl.split('?');
  if (splitServerUrl.length>=2) {

    var prefix = encodeURIComponent(parameter); //+'=';
    var pars = splitServerUrl[1].split(/[&;]/g);

    //reverse iteration as may be destructive
    for (var i= pars.length; i-- > 0;) {
      //idiom for string.startsWith
      if (pars[i].lastIndexOf(prefix, 0) !== -1) {
        pars.splice(i, 1);
      }
    }

    var updatedServerUrl= splitServerUrl[0];
    if (pars.length > 0) {
      updatedServerUrl += '?'+pars.join('&');
    }
    return updatedServerUrl + "#" + clientUrl;
  } else {
    return url;
  }
}


/**
 * Cedar.Store
 *
 * responsible for retrieving Cedar elements from server or local cache.
 *
 * different cedar types may use different api paths, therefore the paths
 * are passed into some functions
 */
Cedar.Store = function() {
  this.loaded = false;

  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  } catch (e) {
    return false;
  }
}
/**
 * store a single json item by key
 *
 * @param <string> 'key'
 * @param <json> 'item'
 */
Cedar.Store.prototype.put = function ( key, item ) {
  localStorage[key] = JSON.stringify(item);
}
/**
 * get a single item based on id (key)
 * if the item isn't in the local store, then get it from the server
 *
 * @param <Cedar> 'object'
 * @param <string> 'key'
 */
Cedar.Store.prototype.get = function ( object, key ) {
  if ( object.apiGet() === null ) {
    throw 'Cedar Error: must provide api "get" path';
  }
  if (typeof localStorage[key] !== "undefined") {
    Cedar.debug('get from cache: ' + key);
    return $.Deferred().resolve(localStorage[key]);
  } else {
    Cedar.debug('getting from server...');
    return $.getJSON(Cedar.config.api + object.apiGet() + key, function(json) {
      Cedar.debug('get from server: ' + key);
      if ( typeof json.id !== 'undefined' && typeof json.settings.content !== 'undefined') {
        Cedar.store.put(key, json);
      }
    });
  }
}
/**
 * fetch records from the server if necessary
 *
 * @param options <Array>
 *  - filter:<string> return only objects which match object IDs containing the string
 *  - path:<string> provide an api path other than the default (content entry query)
 *  - clear:<bool> clear local storage first
 */
Cedar.Store.prototype.fetch = function ( options ) {
  var apiPath = '/queries/contententries/';
  var queryParams = {};<commit_msg>realized this was causing server checks on new objects<commit_after>/**
 * Global object for settings and storage
 */
var Cedar = {
  initialized: false,
  store: null,
  auth: null,
  config: {
    api: '',
    server: '',
    debug: false
  }
};


/**
* Cedar.Init
*
* take care of global initializations
*
* @param <Array> options
*   - server : 'test.cdr.plyinc.com' - *required
*   - debug : true | false
*/
Cedar.Init = function(options) {
  if ( Cedar.initialized ) {
    return;
  }

  if ( typeof options.server === 'undefined' ) {
    throw 'Cedar Error: must provide "server" value on Init()';
  }

  Cedar.config.server = 'http://' + options.server;
  Cedar.config.api = 'http://' + options.server + '/api';

  if ( typeof options.debug !== 'undefined' && options.debug ) {
    Cedar.config.debug = true;
  }

  if ( Cedar.store === null ) {
    Cedar.store = new Cedar.Store();
  }

  if ( Cedar.auth === null ) {
    Cedar.auth = new Cedar.Auth();
  }

  if (Cedar.auth.isEditMode()) {
    $(document).ready(function() {
      var $body = $('body');
      var globalActions = '<div class="cedar-cms-global-actions">' +
        '<a href="#" class="cedar-cms-global-action" onclick="window.location.reload();">' +
        '<span class="cedar-cms-icon cedar-cms-icon-edit"></span> ' +
        '<span class="cedar-cms-global-action-label">Refresh</span>' +
        '</a><br>' +
        '<a class="cedar-cms-global-action" href="' + Cedar.auth.getLogOffURL() + '">' +
        '<span class="cedar-cms-icon cedar-cms-icon-edit"></span> ' +
        '<span class="cedar-cms-global-action-label">Log Off Cedar</span>' +
        '</a>' +
        '</div>';
      $body.append(globalActions);
    });
  }

  Cedar.initialized = true;
}
/**
 * display message to console if Cedar.debug = true
 *
 * @param <string> message
 */
Cedar.debug = function(msg) {
  if (Cedar.config.debug) {
    console.log(msg);
  }
}


/**
 * Cedar.Auth
 *
 * responsible for determining if we're in edit mode
 */
Cedar.Auth = function() {
}
Cedar.Auth.prototype.isEditMode = function() {
  return this.isEditUrl();
}
Cedar.Auth.prototype.isEditUrl = function() {
  var sPageURL = window.location.search.substring(1);
  var sURLVariables = sPageURL.split('&');
  for (var i = 0; i < sURLVariables.length; i++) {
    if (sURLVariables[i] == 'cdrlogin') {
      return true;
    }
  }
  return false;
}
Cedar.Auth.prototype.getLogOffURL = function() {
  return this.removeURLParameter(window.location.href, 'cdrlogin');
}
// adapted from stackoverflow: http://stackoverflow.com/questions/1634748/how-can-i-delete-a-query-string-parameter-in-javascript
Cedar.Auth.prototype.removeURLParameter = function(url, parameter) {
  var splitUrl = url.split('#');
  var serverUrl = splitUrl[0];
  var clientUrl = splitUrl[1];
  //prefer to use l.search if you have a location/link object
  var splitServerUrl= serverUrl.split('?');
  if (splitServerUrl.length>=2) {

    var prefix = encodeURIComponent(parameter); //+'=';
    var pars = splitServerUrl[1].split(/[&;]/g);

    //reverse iteration as may be destructive
    for (var i= pars.length; i-- > 0;) {
      //idiom for string.startsWith
      if (pars[i].lastIndexOf(prefix, 0) !== -1) {
        pars.splice(i, 1);
      }
    }

    var updatedServerUrl= splitServerUrl[0];
    if (pars.length > 0) {
      updatedServerUrl += '?'+pars.join('&');
    }
    return updatedServerUrl + "#" + clientUrl;
  } else {
    return url;
  }
}


/**
 * Cedar.Store
 *
 * responsible for retrieving Cedar elements from server or local cache.
 *
 * different cedar types may use different api paths, therefore the paths
 * are passed into some functions
 */
Cedar.Store = function() {
  this.loaded = false;

  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  } catch (e) {
    return false;
  }
}
/**
 * store a single json item by key
 *
 * @param <string> 'key'
 * @param <json> 'item'
 */
Cedar.Store.prototype.put = function ( key, item ) {
  localStorage[key] = JSON.stringify(item);
}
/**
 * get a single item based on id (key)
 * if the item isn't in the local store, then get it from the server
 *
 * @param <Cedar> 'object'
 * @param <string> 'key'
 */
Cedar.Store.prototype.get = function ( object, key ) {
  if ( object.apiGet() === null ) {
    throw 'Cedar Error: must provide api "get" path';
  }
  if (typeof localStorage[key] !== "undefined") {
    Cedar.debug('get from cache: ' + key);
    return $.Deferred().resolve(localStorage[key]);
  } else {
    Cedar.debug('checking server...');
    return $.getJSON(Cedar.config.api + object.apiGet() + key, function(json) {
      Cedar.debug('get from server: ' + key);
      Cedar.store.put(key, json);
    });
  }
}
/**
 * fetch records from the server if necessary
 *
 * @param options <Array>
 *  - filter:<string> return only objects which match object IDs containing the string
 *  - path:<string> provide an api path other than the default (content entry query)
 *  - clear:<bool> clear local storage first
 */
Cedar.Store.prototype.fetch = function ( options ) {
  var apiPath = '/queries/contententries/';
  var queryParams = {};
{'fraction_non_alphanumeric': 0.1661517667503379, 'fraction_numerical': 0.0036686619038424407, 'mean_word_length': 3.0591692789968654, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 59, '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': '2623359', 'n_tokens_mistral': 3560, 'n_tokens_neox': 3417, 'n_words': 852}