Source: popup.js

/**
 * @file The javascript file of popup.html (which is what is displayed when clicking the plugin-icon).
 * Communicates with the injected code (content.js) via message passing.
 * @author Ulrik Schremser
 */

$(document).ready(function() {

  // LISTENER FOR ENABLED-BUTTON
  $("#enabled-switch-label").click(function(event) {

    // If it is checked but now clicked, this means:
    // GOING INACTIVE
    if($("#enabled-switch").get(0).checked){

      chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {action: "disable"});
      });

    // Deactivating input elements
      disableUi();

    }

    // GOING ACTIVE
    else{

      chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.sendMessage(tabs[0].id, {action: "enable"});
      });

      // Activating input elements
      enableUi();
    }
  });



  // LISTENER FOR TOOLTIP-BUTTON
  $("#tooltip-switch-label").click(function(event) {


    // Only react if we are active!
    if( ! $("#tooltip-switch").get(0).disabled){
      // If it is checked but now clicked, this means:
      // GOING INACTIVE
      if($("#tooltip-switch").get(0).checked){

        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
          chrome.tabs.sendMessage(tabs[0].id, {action: "disable-tooltip"});
        });
      }

      // GOING ACTIVE
      else{

        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
          chrome.tabs.sendMessage(tabs[0].id, {action: "enable-tooltip"});
        });
      }
    }
  });


  // ADDING LISTENERS TO SLIDERS
  $(".feature-slider").each(function(){
    $(this).on("input",  function(){

      correctSliders('#' + this.id);

    });


    $(this).on("change",  function(){

      updateScore();


     // Update the colors if they are set!
      if($("#enabled-switch").get(0).checked){

        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
          chrome.tabs.sendMessage(tabs[0].id, {action: "enable"});
        });
      }

    });
 
  });
});


/**
 * Updates the score inside the injected content.js by sending a message to it containing the new values of the sliders.
 * @memberof popup.js
 */
function updateScore(){

  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {action: "update-score",
     sentenceLengthInfluence: $("#sentence-length-influence").get(0).value,
     wordLengthInfluence: $("#word-length-influence").get(0).value,
     ntvRatioInfluence: $("#ntv-ratio-influence").get(0).value,
     sentenceComplexityInfluence: $("#sentence-complexity-influence").get(0).value,
     wordComplexityInfluence: $("#word-complexity-influence").get(0).value
   });
  });
}


/**
 * Disables the UA components of the plugin
 * @memberof popup.js
 */
function disableUi(){

    $(".feature-slider").each(function(){
      $(this).attr('disabled', 'disabled');
    });

    $("#tooltip-switch").attr('disabled', 'disabled');
  
    $('.inactive').each(function(){
      $(this).removeClass("hidden");
    });

    $('.active').each(function(){
      $(this).addClass('hidden');
    });

}

/**
 * Enables the UI components of the plugin
 * @memberof popup.js
 */
function enableUi(){

    $("#tooltip-switch").removeAttr("disabled");


    $(".feature-slider").each(function(){
      $(this).removeAttr("disabled");
   });

    $('.inactive').each(function(){
      $(this).addClass('hidden');
    });

    $('.active').each(function(){
      $(this).removeClass("hidden");
    });

}

/**
 * Changes the values of the other slides to sum the max value
 * @param {String} idCurrent - The id of the slider that has been changed
 * @memberof popup.js
 */
function correctSliders(idCurrent){


  var restShould = parseFloat($(idCurrent).get(0).max) - parseFloat($(idCurrent).get(0).value);

  // Use 1 to avoid division by 0.
  var sumRest = 0;

  $(".feature-slider").each(function(){

    if(('#' + this.id) != idCurrent){
      sumRest += parseFloat(this.value);
    }
  });


  // Should sum to restShould
  var scale = sumRest / restShould;

    $(".feature-slider").each(function(){

    if(('#' + this.id) != idCurrent){

      this.value /= scale;
    }
  });
}


// Load values from content script
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {action: "get-values"}, function(vals){
       $("#enabled-switch").get(0).checked = vals[0];

       if(vals[0]){
        enableUi();
       }
       else{
        disableUi();
       }

       $("#tooltip-switch").get(0).checked = vals[1];


       $("#sentence-length-influence").get(0).value = vals[2];
       $("#word-length-influence").get(0).value = vals[3];
       $("#ntv-ratio-influence").get(0).value = vals[4];
       $("#sentence-complexity-influence").get(0).value = vals[5];
       $("#word-complexity-influence").get(0).value = vals[6];
  });
});