<commit_before>/**
 * SubmissionQueue
 * Holds the respondents scheduled for submission.
 * Is kept in sync with the localStorage, adding data to it and removing
 * it when is submitted.
 * 
 * On initialization build the queue from the localStorage and tries to
 * submit data.
 * 
 * Trigger Event submission_queue_change
 *   Event is triggered after syncing with the local storage, even if
 *   the queue is empty.
 * 
 * IMPORTANT: The SubmissionQueue is directly dependent on the Connection
 * and assumes a var con exists.
 */
SubmissionQueue = function() {
  this.queue = [];
  this.is_uploading = false;
  
  if (Connection.supportsLocalStorage() == false) {
    alert("Your browser is outdated.\nYou will be redirected to a page to upgrade your browser.");
    window.location = 'http://browsehappy.com/';
    return null;
  }
  
  this.init();
  $(window).trigger('submission_queue_change', this);
  this.submit();
};

/**
 * Initializes the submission queue syncing with the local storage.
 */
SubmissionQueue.prototype.init = function() {
  var self = this;
  console.log('Initializing Submission queue');
  
  var data = localStorage.getItem('aw_submission_queue');
  if (data != null) {
    try{
      var parsed = JSON.parse(data);
      if ($.isArray(parsed) && parsed.length > 0){
        this.queue = parsed;
      }
    }
    catch(e) {
      // Something went wrong with the parsing.
      // Probably the data is not correctly stored.
      // Initialize as empty.
      this.queue = [];
    }
  }
};

/**
 * Adds respondent to the submission queue.
 * The added respondent will be stored in the localStorage and
 * a submission will be tried.
 * 
 * Triggers Event submission_queue_change
 *   After storing respondent on localStorage.
 * 
 * @param {array} value
 */
SubmissionQueue.prototype.add = function(value) {
  console.log('Adding value to submission queue.');
  this.queue.push(value);
  this.store();
  $(window).trigger('submission_queue_change', this);
  this.submit();
};

/**
 * Removes the first respondent from the submission queue.
 * The changes will be written to the localStorage.
 * This method is called after a successful submission. It's always the first
 * element in the queue to be submitted, and after the submission it can be
 * removed.
 * 
 * Triggers Event submission_queue_change
 *   After removing respondent from localStorage.
 * 
 * @param {array} value
 */
SubmissionQueue.prototype.shift = function() {
  console.log('Shifting value from submission queue.');
  this.queue.shift();
  this.store();
  $(window).trigger('submission_queue_change', this);
};

/**
 * Returns all the respondents in queue.
 * 
 * @return {array}
 */
SubmissionQueue.prototype.getQueue = function() {
  return this.queue;
};

/**
 * Stores the queue on localStorage overriding the existing one. Since it is
 * synced when the SubmissionQueue is initialized the data is safe.
 */
SubmissionQueue.prototype.store = function() {
  console.log('Storing on localStorage.');
  var to_store = JSON.stringify(this.queue);
  localStorage.setItem('aw_submission_queue', to_store);
};

/**
 * Submits the first respondent in the queue.
 * When successful will remove the submitted respondent from the queue.
 * 
 * Trigger Event submission_queue_change
 * Trigger Event submission_queue_submit_success
 *   Events are triggered after a successful submission.
 * 
 */
SubmissionQueue.prototype.submit = function() {
  var self = this;
  console.log('Pre submission checks..');
  console.log('Online: ' + con.isOnline());
  console.log('Uploading:' + self.is_uploading);
  console.log('Queue: ' + self.queue.length);
  
  // Only try to submit if the following condition are met:
  // There's an active connection.
  // There's no concurrent submission.
  // There's something to submit.
  if (con.isOnline() && !self.is_uploading && self.queue.length > 0) {
    console.log('Submitting.');
    
    // Check if there is a CSRF token.
    if (con.getCSRF() == null) {
      // Request CSRF token.
      con.requestCSRF(function() {
        // Now that we have a token, submit again.
        console.log('CSRF token requested. Calling submit().');
        self.submit();
      });
      return null;
    }
    
    // TODO: SubmissionQueue.prototype.submit - Remove MOCK
    ///////////////////////MOCK
    // Simulate errors
    /*if (always_fail && Math.random() < 1) {
      console.warn('error');
      self.is_uploading = true;
      $.get(Aw.settings.base_url + 'survey/delay/4', function() {
        // yeah. do nothing.
      }).fail(function(r){
        if (r.status == 404) {
          // Not found means no connection.
          con.setOnlineStatus( false );
        }
      }).always(function() {<commit_msg>Simulates errors when using enketo<commit_after>/**
 * SubmissionQueue
 * Holds the respondents scheduled for submission.
 * Is kept in sync with the localStorage, adding data to it and removing
 * it when is submitted.
 * 
 * On initialization build the queue from the localStorage and tries to
 * submit data.
 * 
 * Trigger Event submission_queue_change
 *   Event is triggered after syncing with the local storage, even if
 *   the queue is empty.
 * 
 * IMPORTANT: The SubmissionQueue is directly dependent on the Connection
 * and assumes a var con exists.
 */
SubmissionQueue = function() {
  this.queue = [];
  this.is_uploading = false;
  
  if (Connection.supportsLocalStorage() == false) {
    alert("Your browser is outdated.\nYou will be redirected to a page to upgrade your browser.");
    window.location = 'http://browsehappy.com/';
    return null;
  }
  
  this.init();
  $(window).trigger('submission_queue_change', this);
  this.submit();
};

/**
 * Initializes the submission queue syncing with the local storage.
 */
SubmissionQueue.prototype.init = function() {
  var self = this;
  console.log('Initializing Submission queue');
  
  var data = localStorage.getItem('aw_submission_queue');
  if (data != null) {
    try{
      var parsed = JSON.parse(data);
      if ($.isArray(parsed) && parsed.length > 0){
        this.queue = parsed;
      }
    }
    catch(e) {
      // Something went wrong with the parsing.
      // Probably the data is not correctly stored.
      // Initialize as empty.
      this.queue = [];
    }
  }
};

/**
 * Adds respondent to the submission queue.
 * The added respondent will be stored in the localStorage and
 * a submission will be tried.
 * 
 * Triggers Event submission_queue_change
 *   After storing respondent on localStorage.
 * 
 * @param {array} value
 */
SubmissionQueue.prototype.add = function(value) {
  console.log('Adding value to submission queue.');
  this.queue.push(value);
  this.store();
  $(window).trigger('submission_queue_change', this);
  this.submit();
};

/**
 * Removes the first respondent from the submission queue.
 * The changes will be written to the localStorage.
 * This method is called after a successful submission. It's always the first
 * element in the queue to be submitted, and after the submission it can be
 * removed.
 * 
 * Triggers Event submission_queue_change
 *   After removing respondent from localStorage.
 * 
 * @param {array} value
 */
SubmissionQueue.prototype.shift = function() {
  console.log('Shifting value from submission queue.');
  this.queue.shift();
  this.store();
  $(window).trigger('submission_queue_change', this);
};

/**
 * Returns all the respondents in queue.
 * 
 * @return {array}
 */
SubmissionQueue.prototype.getQueue = function() {
  return this.queue;
};

/**
 * Stores the queue on localStorage overriding the existing one. Since it is
 * synced when the SubmissionQueue is initialized the data is safe.
 */
SubmissionQueue.prototype.store = function() {
  console.log('Storing on localStorage.');
  var to_store = JSON.stringify(this.queue);
  localStorage.setItem('aw_submission_queue', to_store);
};

/**
 * Submits the first respondent in the queue.
 * When successful will remove the submitted respondent from the queue.
 * 
 * Trigger Event submission_queue_change
 * Trigger Event submission_queue_submit_success
 *   Events are triggered after a successful submission.
 * 
 */
SubmissionQueue.prototype.submit = function() {
  var self = this;
  console.log('Pre submission checks..');
  console.log('Online: ' + con.isOnline());
  console.log('Uploading:' + self.is_uploading);
  console.log('Queue: ' + self.queue.length);
  
  // Only try to submit if the following condition are met:
  // There's an active connection.
  // There's no concurrent submission.
  // There's something to submit.
  if (con.isOnline() && !self.is_uploading && self.queue.length > 0) {
    console.log('Submitting.');
    
    // Check if there is a CSRF token.
    if (con.getCSRF() == null) {
      // Request CSRF token.
      con.requestCSRF(function() {
        // Now that we have a token, submit again.
        console.log('CSRF token requested. Calling submit().');
        self.submit();
      });
      return null;
    }
    
    // TODO: SubmissionQueue.prototype.submit - Remove MOCK
    ///////////////////////MOCK
    // Simulate errors
    if (always_fail) {
      console.warn('error');
      self.is_uploading = true;
      $.get(Aw.settings.base_url + 'survey/delay/4', function() {
        // yeah. do nothing.
      }).fail(function(r){
        if (r.status == 404) {
          // Not found means no connection.
          con.setOnlineStatus( false );
        }
      }).always(function() {
{'fraction_non_alphanumeric': 0.12099494097807757, 'fraction_numerical': 0.0013701517706576729, 'mean_word_length': 3.060333761232349, 'pattern_counts': {'":': 0, '<': 4, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7631350', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2667, 'n_words': 880}