amatriain/feedbunch

View on GitHub
FeedBunch-app/app/assets/javascripts/workers/tours_worker.js.erb

Summary

Maintainability
Test Coverage
/**
 * Web Worker to load or hide tours in a separate thread.
 */

importScripts('<%= asset_path 'workers/common/do_get' %>');
importScripts('<%= asset_path 'workers/common/do_put' %>');

// Callback for messages from the main thread
onmessage = function(e){
  // Constants for the different operations the web worker can perform
  var LOAD_TOUR = "load_tour";
  var HIDE_TOUR = "hide_tour";
  var RESET_TOURS = "reset_tours";

  // Operation to perform: load or hide
  var operation = e.data.operation;

  // CSRF token
  var token = e.data.token;

  if (operation == LOAD_TOUR){
    // Which tour to load. Mandatory for this operation
    var tour = e.data.tour;
    var url = "/api/tours/" + tour + ".json";
    do_get(operation, url, token, {tour: tour}, 0);
  }
  else if (operation == HIDE_TOUR){
    // Show the main tour?
    var show_main_tour = e.data.show_main_tour;

    // Show mobile tour?
    var show_mobile_tour = e.data.show_mobile_tour;

    // Show feed tour?
    var show_feed_tour = e.data.show_feed_tour;

    // Show entry tour?
    var show_entry_tour = e.data.show_entry_tour;

    // Show keyboard shortcuts tour?
    var show_kb_shortcuts_tour = e.data.show_kb_shortcuts_tour;

    // Compose JSON to send in the HTTP PUT with the new config
    var data = {user_config: {show_main_tour: show_main_tour, show_mobile_tour: show_mobile_tour,
      show_feed_tour: show_feed_tour, show_entry_tour: show_entry_tour, show_kb_shortcuts_tour: show_kb_shortcuts_tour}};

    do_put(operation, "/api/user_config.json", token, data, 0);
  }
  else if (operation == RESET_TOURS){
    // Compose JSON to send in the HTTP PUT with the new config
    var data = {user_config: {show_main_tour: "true", show_mobile_tour: "true",
      show_feed_tour: "true", show_entry_tour: "true", show_kb_shortcuts_tour: "true"}};
    do_put(operation, "/api/user_config.json", token, data, 0);
  }

}