<commit_before>var TW = chrome.extension.getBackgroundPage().TW;

TW.optionsTab = {};

/**
 * Initialization for options tab.
 * @param context
 *  Optionally used to limit jQueries
 */
TW.optionsTab.init = function(context) {
  $('#saveOptionsBtn', context).click(TW.optionsTab.saveOption);

  function onBlurInput() {
    var key = this.id;
    TW.optionsTab.saveOption(key, $(this).val());
  }
  
  function onChangeCheckBox() {
    var key = this.id;
    if ($(this).attr('checked')) {
      TW.optionsTab.saveOption(key, $(this).val());
    } else {
      TW.optionsTab.saveOption(key, false);
    }
  }
  
  $('#minutesInactive').keyup(_.debounce(onBlurInput, 200));
  $('#minTabs').keyup(_.debounce(onBlurInput, 200));
  $('#purgeClosedTabs').change(onChangeCheckBox);

  TW.optionsTab.loadOptions();
}


TW.optionsTab.saveOption = function (key, value) {

  var errors = new Array();

  try {
    TW.settings.set(key, value);
  } catch (err) {
    errors.push(err);
  }
  
  
  $('#status').removeClass();

  if (errors.length == 0) {
    $('#status').html('Saving...');

    $('#status').addClass('alert-success').addClass('alert');
  } else {
    var $errorList = $('<ul></ul>');
    for (var i in errors) {
      $errorList.append('<li>' + errors[i].message + '</li>');
    }
    $('#status').append($errorList).addClass('alert-error').addClass('alert');
  }
  $('#status').css('visibility', 'visible');
  $('#status').css('opacity', '100');
  $('#status').delay(50).animate({opacity:0});
  return false;
}

TW.optionsTab.loadOptions = function () {
  $('#minutesInactive').val(TW.settings.get('minutesInactive'));
  $('#minTabs').val(TW.settings.get('minTabs'));
  if (TW.settings.get('purgeClosedTabs') != false) {
    $('#purgeClosedTabs').attr('checked', true);
  }
  

  $('#whitelist').addOption = function(key, val) {
    this.append(
    $('<option />')
    .value(val)
    .text(key)
  );
  }

  var whitelist = TW.settings.get('whitelist');
  TW.optionsTab.buildWLTable(whitelist);

  $('#addToWL').click(function() {
    whitelist.push($('#wl-add').val());
    TW.optionsTab.saveOption('whitelist', whitelist);
    TW.optionsTab.buildWLTable(whitelist);
    return false;
  });

}

TW.optionsTab.buildWLTable = function(whitelist) {
  var $wlTable = $('table#whitelist tbody');
  $wlTable.html('');
  for (var i=0; i < whitelist.length; i++) {
    $tr = $('<tr></tr>');
    $urlTd = $('<td></td>').text(whitelist[i]);
    $deleteLink = $('<a class="deleteLink" href="#">Remove</a>')
    .click(function() {
      whitelist.remove(whitelist.indexOf($(this).data('pattern')));
      TW.optionsTab.saveOption('whitelist', whitelist);
      TW.optionsTab.buildWLTable(whitelist);
    })
    .data('pattern', whitelist[i]);

    $tr.append($urlTd);
    $tr.append($('<td></td>').append($deleteLink));
    $wlTable.append($tr);
  }
}

// Active Tab
// @todo: rename this to lock tab, that's what it's for.;
TW.activeTab = {};

TW.activeTab.init = function(context) {
  this.context = context;
  chrome.tabs.getAllInWindow(null, function(tabs) { TW.activeTab.buildTabLockTable(tabs);});
}

TW.activeTab.saveLock = function(tabId) {
  TW.TabManager.lockTab(tabId);
}

TW.activeTab.removeLock = function(tabId) {
  TW.TabManager.unlockTab();
}


/**
 * @param tabs
 * @return {Boolean}
 */
TW.activeTab.buildTabLockTable = function (tabs) {
  var self = this;

  var tabNum = tabs.length;
  var $tbody = $('#activeTabs tbody');
  $tbody.html('');

  var lockedIds = TW.settings.get("lockedIds");
  for (var i = 0; i < tabNum; i++) {

    // Create a new row.
    var $tr = $('<tr></tr>');

    // Checkbox to lock it.
    //@todo: put the handler in its own function
    var $lock_box = $('<input />')
    .attr('type', 'checkbox')
    .attr('id', "cb" + tabs[i].id)
    .attr('value', tabs[i].id)
    .attr('checked', tabs[i].pinned || TW.TabManager.isWhitelisted(tabs[i].url) || lockedIds.indexOf(tabs[i].id) != -1)
    .click(function () {
      if (this.checked) {
        self.saveLock(parseInt(this.value));
      } else {
        self.removeLock(parseInt(this.value));
      }
    });
    $tr.append($('<td></td>').append($lock_box));

    // Image cell.
    var $img_td = $('<td></td>');
    if (tabs[i].favIconUrl != null && tabs[i].favIconUrl != undefined && tabs[i].favIconUrl.length > 0) {
      // We have an image to show.
      var $img_icon = $('<img />')
      .attr('class', 'favicon')
      .attr('src', tabs[i].favIconUrl)
      $img_td.append($img_icon);
    } else {
      $img_td.text('-');
    }

    $tr.append($img_td);

    // Page title.
    $tr.append($('<td>' + tabs[i].title.shorten(70) + '</td>'));
    // Url
    $tr.append($('<td>' + tabs[i].url.shorten(70) + '</td>'));

    var cutOff = new Date().getTime() - TW.settings.get('stayOpen');

    var lastModified = TW.TabManager.tabTimes[tabs[i].id];
    var timeLeft = -1 * (Math.round((cutOff - lastModified) / 1000)).toString();
    
    function secondsToMinutes(seconds) {
      var s = seconds % 60;
      s = s > 10 ? String(s) : "0" + String(s);
      return String(Math.floor(seconds / 60)) + ":" + s;
    }
    $tr.append($('<td class="time-left">' + secondsToMinutes(timeLeft) + '</td>'));


    // Append the row.
    $tbody.append($tr);
  }

  return true;
}

TW.corralTab = {};

TW.corralTab.filters = {}
TW.corralTab.filters.keyword = function(keyword) {
  return function(tab) {
    var test = new RegExp(keyword, 'i');
    return test.exec(tab.title) || test.exec(tab.url);
  }
  
}

TW.corralTab.init = function(context) {
  var self = this;
  // @todo: use context to select table.
  this.getTabs(this.buildTable, []);
  $('.clearCorralLink').click(function() {
    TW.TabManager.closedTabs.clear();
    TW.TabManager.updateClosedCount();
    TW.corralTab.loadClosedTabs();
  });
  
  
  $('.corral-search').keyup(_.debounce(
  function() {
    var keyword = $(this).val();
    self.getTabs(self.buildTable, [self.filters.keyword(keyword)]);
  }, 200));
}

TW.corralTab.getTabs = function (cb, filters) {
  var tabs = TW.TabManager.closedTabs.tabs;
  if (filters) {
    for (i = 0; i < filters.length; i++) {
      tabs = _.filter(tabs, filters[i]);
    }
  }
  cb(tabs);
}

TW.corralTab.buildTable = function(closedTabs) {
  $('#autocloseMessage').hide();
  $('.clearCorralMessage').hide();

  /**
   * @todo: add this back in
   *
   * function openExtTab() {
   chrome.tabs.create({'url':'chrome://extensions/'});
   }

   function openNewTab() {
   chrome.tabs.create({'url':'chrome://newtab/'});
   }
   */
  
  // Clear out the table.
  var $tbody = $('#corralTable tbody');<commit_msg>closes #26, focus on search field<commit_after>var TW = chrome.extension.getBackgroundPage().TW;

TW.optionsTab = {};

/**
 * Initialization for options tab.
 * @param context
 *  Optionally used to limit jQueries
 */
TW.optionsTab.init = function(context) {
  $('#saveOptionsBtn', context).click(TW.optionsTab.saveOption);

  function onBlurInput() {
    var key = this.id;
    TW.optionsTab.saveOption(key, $(this).val());
  }
  
  function onChangeCheckBox() {
    var key = this.id;
    if ($(this).attr('checked')) {
      TW.optionsTab.saveOption(key, $(this).val());
    } else {
      TW.optionsTab.saveOption(key, false);
    }
  }
  
  $('#minutesInactive').keyup(_.debounce(onBlurInput, 200));
  $('#minTabs').keyup(_.debounce(onBlurInput, 200));
  $('#purgeClosedTabs').change(onChangeCheckBox);

  TW.optionsTab.loadOptions();
}


TW.optionsTab.saveOption = function (key, value) {

  var errors = new Array();

  try {
    TW.settings.set(key, value);
  } catch (err) {
    errors.push(err);
  }
  
  
  $('#status').removeClass();

  if (errors.length == 0) {
    $('#status').html('Saving...');

    $('#status').addClass('alert-success').addClass('alert');
  } else {
    var $errorList = $('<ul></ul>');
    for (var i in errors) {
      $errorList.append('<li>' + errors[i].message + '</li>');
    }
    $('#status').append($errorList).addClass('alert-error').addClass('alert');
  }
  $('#status').css('visibility', 'visible');
  $('#status').css('opacity', '100');
  $('#status').delay(50).animate({opacity:0});
  return false;
}

TW.optionsTab.loadOptions = function () {
  $('#minutesInactive').val(TW.settings.get('minutesInactive'));
  $('#minTabs').val(TW.settings.get('minTabs'));
  if (TW.settings.get('purgeClosedTabs') != false) {
    $('#purgeClosedTabs').attr('checked', true);
  }
  

  $('#whitelist').addOption = function(key, val) {
    this.append(
    $('<option />')
    .value(val)
    .text(key)
  );
  }

  var whitelist = TW.settings.get('whitelist');
  TW.optionsTab.buildWLTable(whitelist);

  $('#addToWL').click(function() {
    whitelist.push($('#wl-add').val());
    TW.optionsTab.saveOption('whitelist', whitelist);
    TW.optionsTab.buildWLTable(whitelist);
    return false;
  });

}

TW.optionsTab.buildWLTable = function(whitelist) {
  var $wlTable = $('table#whitelist tbody');
  $wlTable.html('');
  for (var i=0; i < whitelist.length; i++) {
    $tr = $('<tr></tr>');
    $urlTd = $('<td></td>').text(whitelist[i]);
    $deleteLink = $('<a class="deleteLink" href="#">Remove</a>')
    .click(function() {
      whitelist.remove(whitelist.indexOf($(this).data('pattern')));
      TW.optionsTab.saveOption('whitelist', whitelist);
      TW.optionsTab.buildWLTable(whitelist);
    })
    .data('pattern', whitelist[i]);

    $tr.append($urlTd);
    $tr.append($('<td></td>').append($deleteLink));
    $wlTable.append($tr);
  }
}

// Active Tab
// @todo: rename this to lock tab, that's what it's for.;
TW.activeTab = {};

TW.activeTab.init = function(context) {
  this.context = context;
  chrome.tabs.getAllInWindow(null, function(tabs) { TW.activeTab.buildTabLockTable(tabs);});
}

TW.activeTab.saveLock = function(tabId) {
  TW.TabManager.lockTab(tabId);
}

TW.activeTab.removeLock = function(tabId) {
  TW.TabManager.unlockTab();
}


/**
 * @param tabs
 * @return {Boolean}
 */
TW.activeTab.buildTabLockTable = function (tabs) {
  var self = this;

  var tabNum = tabs.length;
  var $tbody = $('#activeTabs tbody');
  $tbody.html('');

  var lockedIds = TW.settings.get("lockedIds");
  for (var i = 0; i < tabNum; i++) {

    // Create a new row.
    var $tr = $('<tr></tr>');

    // Checkbox to lock it.
    //@todo: put the handler in its own function
    var $lock_box = $('<input />')
    .attr('type', 'checkbox')
    .attr('id', "cb" + tabs[i].id)
    .attr('value', tabs[i].id)
    .attr('checked', tabs[i].pinned || TW.TabManager.isWhitelisted(tabs[i].url) || lockedIds.indexOf(tabs[i].id) != -1)
    .click(function () {
      if (this.checked) {
        self.saveLock(parseInt(this.value));
      } else {
        self.removeLock(parseInt(this.value));
      }
    });
    $tr.append($('<td></td>').append($lock_box));

    // Image cell.
    var $img_td = $('<td></td>');
    if (tabs[i].favIconUrl != null && tabs[i].favIconUrl != undefined && tabs[i].favIconUrl.length > 0) {
      // We have an image to show.
      var $img_icon = $('<img />')
      .attr('class', 'favicon')
      .attr('src', tabs[i].favIconUrl)
      $img_td.append($img_icon);
    } else {
      $img_td.text('-');
    }

    $tr.append($img_td);

    // Page title.
    $tr.append($('<td>' + tabs[i].title.shorten(70) + '</td>'));
    // Url
    $tr.append($('<td>' + tabs[i].url.shorten(70) + '</td>'));

    var cutOff = new Date().getTime() - TW.settings.get('stayOpen');

    var lastModified = TW.TabManager.tabTimes[tabs[i].id];
    var timeLeft = -1 * (Math.round((cutOff - lastModified) / 1000)).toString();
    
    function secondsToMinutes(seconds) {
      var s = seconds % 60;
      s = s > 10 ? String(s) : "0" + String(s);
      return String(Math.floor(seconds / 60)) + ":" + s;
    }
    $tr.append($('<td class="time-left">' + secondsToMinutes(timeLeft) + '</td>'));


    // Append the row.
    $tbody.append($tr);
  }

  return true;
}

TW.corralTab = {};

TW.corralTab.filters = {}
TW.corralTab.filters.keyword = function(keyword) {
  return function(tab) {
    var test = new RegExp(keyword, 'i');
    return test.exec(tab.title) || test.exec(tab.url);
  }
  
}

TW.corralTab.init = function(context) {
  var self = this;
  // @todo: use context to select table.
  this.getTabs(this.buildTable, []);
  $('.clearCorralLink').click(function() {
    TW.TabManager.closedTabs.clear();
    TW.TabManager.updateClosedCount();
    TW.corralTab.loadClosedTabs();
  });
  
  if(location.search !== "?foo") {
    location.search = "?foo";
    throw new Error;  // load everything on the next page;
    // stop execution on this page
  }
  
  $('.corral-search').keyup(_.debounce(
  function() {
    var keyword = $(this).val();
    self.getTabs(self.buildTable, [self.filters.keyword(keyword)]);
  }, 200));
  
  $('.corral-search').delay(1000).focus();
}

TW.corralTab.getTabs = function (cb, filters) {
  var tabs = TW.TabManager.closedTabs.tabs;
  if (filters) {
    for (i = 0; i < filters.length; i++) {
      tabs = _.filter(tabs, filters[i]);
    }
  }
  cb(tabs);
}

TW.corralTab.buildTable = function(closedTabs) {
  $('#autocloseMessage').hide();
  $('.clearCorralMessage').hide();

  /**
   * @todo: add this back in
   *
   * function openExtTab() {
   chrome.tabs.create({'url':'chrome://extensions/'});
   }

   function openNewTab() {
   chrome.tabs.create({'url':'chrome://newtab/'});
   }
   */
  
  // Clear out the table.
  var $tbody = $('#corralTable tbody');
{'fraction_non_alphanumeric': 0.21264410561547042, 'fraction_numerical': 0.005950167348456675, 'mean_word_length': 3.2944746087511976, 'pattern_counts': {'":': 2, '<': 63, '<?xml version=': 0, '>': 61, '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': '1281373', 'n_tokens_mistral': 5027, 'n_tokens_neox': 4772, 'n_words': 813}