<commit_before>//Voice to text functionality

//set variables and initial recognition behavior
var recognizing = false;
var ignore_onend;
//this finds the correct button on the page
var button;
//this finds the correct information span on the page
var voiceSpan;

if (window.hasOwnProperty('webkitSpeechRecognition')){
  var recognition = new webkitSpeechRecognition();
  recognition.continuous = true;
  recognition.interimResults = true;

  recognition.onstart = function(){
    recognizing = true;
    button.src = "<%= asset_path('mic-on-icon.png')%>";
    voiceSpan.text("Listening");
  };

  //change image when stop
  recognition.onend = function(){
    recognizing = false;
    if (ignore_onend){
      return;
    }
    button.src = "<%= asset_path('mic-icon.png') %>";
    voiceSpan.text("Click microphone for dictation");
  };

  recognition.onerror = function(e){
    recognizing = false;
    ignore_onend = true;
    recognition.stop();
    button.src = "<%= asset_path('mic-error-icon.png') %>";
    voiceSpan.text("Recording Error");
    voiceSpan.addClass('voice-error');
  };
}

//toggle speech to text on and off
function startButton(e){
  e.preventDefault();
  if (recognizing) {
    recognition.stop();
    return;
  }
  ignore_onend = null;
  startDictation(e.target);
}

//use to actually get the dictation
function startDictation(target){
  recognition.lang = window.lang;
  recognizing = true;
  button = target;
  var form = $(button.form);
  var voiceData = form.find('textarea');
  voiceSpan = form.find('.voice-info');
  if (voiceSpan.hasClass('voice-error')){
    voiceSpan.removeClass('voice-error');
  }
  var initialText

  if (voiceData.text() != '') {
    initialText = voiceData.text() + '\n';
  } else {
    initialText = '';
  }
  var final_transcript = initialText;
  var interim_transcript = initialText;

  recognition.start();
  recognition.onresult = function(e){
    for (var i = e.resultIndex; i < e.results.length; ++i){
      if (e.results[i].isFinal){
        final_transcript += e.results[i][0].transcript;
      }
    }
    voiceData.text(final_transcript);
  };
}

//Check for unsaved notes before save
function unsavedNotes(e){
  var notes = $('textarea#note_body');
  var msg = "You have unsaved notes. Please save or discard notes before saving transcription."
  checkForUnsaved(notes, msg, e);
}

//check for unsaved transcriptions before changing page
function unsavedTranscription(e){
    //had to use string instead of boolean b/c of slim/javascript issues
  var msg = "You have unsaved changes. Please save or discard your work before going to the next page.";
  var inputs = $('textarea').add($('.field-input'));
  checkForUnsaved(inputs, msg, e);
}

//actual function to check for unsaved changes
function checkForUnsaved(inputs, msg, e){
  debugger;
  inputs.each(function(){
    if (this.value != this.defaultValue){
      e.preventDefault();
      alert(msg);
      return false;
    }
  })
}<commit_msg>unsaved fields js<commit_after>//Voice to text functionality

//set variables and initial recognition behavior
var recognizing = false;
var ignore_onend;
//this finds the correct button on the page
var button;
//this finds the correct information span on the page
var voiceSpan;

if (window.hasOwnProperty('webkitSpeechRecognition')){
  var recognition = new webkitSpeechRecognition();
  recognition.continuous = true;
  recognition.interimResults = true;

  recognition.onstart = function(){
    recognizing = true;
    button.src = "<%= asset_path('mic-on-icon.png')%>";
    voiceSpan.text("Listening");
  };

  //change image when stop
  recognition.onend = function(){
    recognizing = false;
    if (ignore_onend){
      return;
    }
    button.src = "<%= asset_path('mic-icon.png') %>";
    voiceSpan.text("Click microphone for dictation");
  };

  recognition.onerror = function(e){
    recognizing = false;
    ignore_onend = true;
    recognition.stop();
    button.src = "<%= asset_path('mic-error-icon.png') %>";
    voiceSpan.text("Recording Error");
    voiceSpan.addClass('voice-error');
  };
}

//toggle speech to text on and off
function startButton(e){
  e.preventDefault();
  if (recognizing) {
    recognition.stop();
    return;
  }
  ignore_onend = null;
  startDictation(e.target);
}

//use to actually get the dictation
function startDictation(target){
  recognition.lang = window.lang;
  recognizing = true;
  button = target;
  var form = $(button.form);
  var voiceData = form.find('textarea');
  voiceSpan = form.find('.voice-info');
  if (voiceSpan.hasClass('voice-error')){
    voiceSpan.removeClass('voice-error');
  }
  var initialText

  if (voiceData.text() != '') {
    initialText = voiceData.text() + '\n';
  } else {
    initialText = '';
  }
  var final_transcript = initialText;
  var interim_transcript = initialText;

  recognition.start();
  recognition.onresult = function(e){
    for (var i = e.resultIndex; i < e.results.length; ++i){
      if (e.results[i].isFinal){
        final_transcript += e.results[i][0].transcript;
      }
    }
    voiceData.text(final_transcript);
  };
}

//Check for unsaved notes before save
function unsavedNotes(e){
  var notes = $('textarea#note_body');
  var msg = "You have unsaved notes. Please save or discard notes before saving transcription."
  checkForUnsaved(notes, msg, e);
}

//check for unsaved transcriptions before changing page
function unsavedTranscription(e){
  var msg = "You have unsaved changes. Please save or discard your work before going to the next page.";
  var inputs = $('textarea').add($('.field-input'));
  checkForUnsaved(inputs, msg, e);
}

//actual function to check for unsaved changes
function checkForUnsaved(inputs, msg, e){
  inputs.each(function(){
    if (this.type == 'select-one'){
      if(!(this.options[this.selectedIndex].defaultSelected)){
        e.preventDefault();
        alert(msg);
        return false;
      }
    }
    else {
      if (this.value != this.defaultValue){
        e.preventDefault();
        alert(msg);
        return false;
      };
    };
  });
}
{'fraction_non_alphanumeric': 0.13583910319815365, 'fraction_numerical': 0.0003297065611605671, 'mean_word_length': 3.565086531226486, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, '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': '4028476', 'n_tokens_mistral': 1864, 'n_tokens_neox': 1831, 'n_words': 491}