<commit_before>import add from '../support/task';
import calf from '../support/calf';
import doTable from './table';
import dropItem from '../ajax/dropItem';
import getForage from '../ajax/getForage';
import getInventory from '../ajax/getInventory';
import getMembrList from '../ajax/getMembrList';
import moveItem from '../ajax/moveItem';
import sendItem from '../ajax/sendItem';
import setForage from '../ajax/setForage';
import {equipItem, queueRecallItem, queueTakeItem} from '../support/ajax';
import * as assets from './assets';
import * as dataObj from '../support/dataObj';
import * as debug from '../support/debug';
import * as filters from './filters';
import * as system from '../support/system';

/* jshint latedef: nofunc */
export var options;
export var showQuickDropLinks;
export var showQuickSendLinks;
export var theInv;

function doSpinner() { // jQuery
  $('#pCC').html('<span id="fshInvMan"><img src = "' +
  system.imageServer + '/world/actionLoadingSpinner.gif">&nbsp;' +
    'Getting inventory data...</span>');
}

function rekeyMembrList() {
  calf.membrList = Object.keys(calf.membrList)
    // Using reduce() to rekey the membrList from names to id's
    .reduce(function(prev, curr) {
      if (curr !== 'lastUpdate') {
        prev[calf.membrList[curr].id] =
          calf.membrList[curr];
      }
      return prev;
    }, {});
}

function decorate() {
  if (theInv.folders) {
    theInv.folders['-1'] = 'Main';
  }
  // Hide composed potions until Zorg fixes the feed
  theInv.items =
    theInv.items.filter(function(obj) {
      return obj.type !== '15';
    });
  //
}

function headers() { // jQuery
  var reportTitle;
  if (theInv.player_id) {
    reportTitle = '<b>&nbsp;Inventory Manager</b> ' +
      theInv.items.length +
      ' items (green = worn, blue = backpack)';
  } else {
    reportTitle = '<b>&nbsp;Guild Inventory Manager</b> ' +
      theInv.items.length +
      ' items (maroon = in BP, blue=guild store)';
  }
  var myHtml = assets.invManFilter.replace('@@reportTitle@@', reportTitle);
  $('#pCC').html(myHtml);
}

function setChecks() {
  Array.prototype.forEach.call(
    document.querySelectorAll('table.fshInvFilter input[type="checkbox"]'),
    function(el) {
      el.checked =
        options.checkedElements[el.getAttribute('item')] === 1;
    });
  setForage('fsh_inventory', options);
}

function setLvls() { // jQuery
  $('#fshMinLvl').val(options.fshMinLvl);
  $('#fshMaxLvl').val(options.fshMaxLvl);
}

function refresh() {
  doSpinner();
  // eslint-disable-next-line no-use-before-define
  syncInvMan();
}

function changeLvls() { // jQuery
  var minLvl = parseInt($('#fshMinLvl').val(), 10);
  var maxLvl = parseInt($('#fshMaxLvl').val(), 10);
  if (isNaN(minLvl) || isNaN(maxLvl)) {return;}
  options.fshMinLvl = minLvl;
  options.fshMaxLvl = maxLvl;
  setForage('fsh_inventory', options);
  $('#fshInv').DataTable().draw(false);
}

function resetLvls() { // jQuery
  options.fshMinLvl = dataObj.defaults.inventoryMinLvl;
  options.fshMaxLvl = dataObj.defaults.inventoryMaxLvl;
  setForage('fsh_inventory', options);
  $('#fshMinLvl').val(options.fshMinLvl);
  $('#fshMaxLvl').val(options.fshMaxLvl);
  $('#fshInv').DataTable().draw(false);
}

function getChecks() { // jQuery
  options.checkedElements = {};
  Array.prototype.forEach.call(
    document.querySelectorAll(
      'table.fshInvFilter input[type="checkbox"][item]:checked'),
    function(el) {
      options.checkedElements[el.getAttribute('item')] = 1;
    });
  setForage('fsh_inventory', options);
  $('#fshInv').DataTable().draw(false);
}

function allChecks() { // jQuery
  options.checkedElements = assets.inventoryCheckAll;
  setChecks();
  $('#fshInv').DataTable().draw(false);
}

function clearGearOnly(checkedElements) {
  var newEle = {};
  Object.keys(checkedElements).forEach(function(key) {
    if (parseInt(key, 10) >= 100) {
      newEle[key] = checkedElements[key];
    }
  });
  return newEle;
}

function clearChecks() { // jQuery
  options.checkedElements = clearGearOnly(options.checkedElements);
  setChecks();
  $('#fshInv').DataTable().draw();
}

function resetChecks() { // jQuery
  options.checkedElements = dataObj.defaults.inventoryCheckedElements;
  setChecks();
  $('#fshInv').DataTable().draw(false);
}

function setName(e) { // jQuery
  $('#fshInv').DataTable().search($(e.target).attr('set')).draw();
  $('#fshInv_filter input').focus();
}

function removeClass(self) {
  self.closest('tr')
    .find('.takeItem, .recallItem, .wearItem, .dropItem, .sendItem')
    .removeClass().qtip('hide');
}

function killRow(self) { // jQuery
  var tr = self.closest('tr');
  var td = $('td', tr);
  td.eq(2).empty(); // Where
  td.eq(12).empty(); // BP - GS
  td.eq(13).empty(); // GS - W/U
  td.eq(14).empty(); // W/U - Tag
  td.eq(15).empty(); // Tag - Drop
  td.eq(16).empty(); // ? - Send
  tr.css('text-decoration', 'line-through');
}

function anotherSpinner(self) {
  self.empty().append('<img src="' + system.imageServer +
    '/skin/loading.gif" width="11" height="11">');
}

function takeItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  queueTakeItem(self.attr('invid'), self.attr('action'))
    .done(function(data) {
      if (data.r === 1) {return;}
      killRow(self);
    });
  anotherSpinner(self);
}

function recallItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  queueRecallItem({
    invId: self.attr('invid'),
    playerId: self.attr('playerid'),
    mode: self.attr('mode'),
    action: self.attr('action')
  })
    .done(function(data) {
      if (data.r === 1) {return;}
      killRow(self);
    });
  anotherSpinner(self);
}

function wearItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  equipItem(self.attr('invid')).done(function(data) {
    if (data.r === 1) {return;}
    killRow(self);
  });
  anotherSpinner(self);
}

function useItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  useItem(self.attr('invid')).done(function(data) {
    if (data.r === 1) {return;}
    killRow(self);
  });
  anotherSpinner(self);
}

function doMoveItem(e) { // jQuery
  var self = $(e.target);
  moveItem([self.data('inv')], self.val());
}

function doDropItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  dropItem([self.data('inv')]).done(function(data) {
    if (data.r === 1) {return;}
    killRow(self);
  });
  anotherSpinner(self);
}

function doSendItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  sendItem([self.data('inv')]).done(function(data) {
    if (data.r === 1) {return;}
    killRow(self);
  });
  anotherSpinner(self);
}

function eventHandlers() { // jQuery
  $('#fshRefresh').click(refresh);
  $('#fshMinLvl, #fshMaxLvl').keyup(changeLvls);
  $('#fshReset').click(resetLvls);
  $('table.fshInvFilter').on('click', 'input[type="checkbox"]', getChecks);
  $('#fshAll').click(allChecks);
  $('#fshNone').click(clearChecks);
  $('#fshDefault').click(resetChecks);
  $('#fshInv').on('click', 'span.setName', setName);
  $('#fshInv').on('click', 'span.takeItem', takeItem);
  $('#fshInv').on('click', 'span.recallItem', recallItem);
  $('#fshInv').on('click', 'span.wearItem', wearItem);
  $('#fshInv').on('click', 'span.useItem', useItem);
  $('#fshInv').on('change', 'select.moveItem', doMoveItem);
  $('#fshInv').on('click', 'span.dropItem', doDropItem);
  $('#fshInv').on('click', 'span.sendItem', doSendItem);
}

function clearButton() { // jQuery
  var input = $('#fshInv_filter input');
  input.prop('type', 'text');
  var clear = $('<span>&times;</span>');
  input.wrap($('<span class="text-input-wrapper"/>'));
  input.after(clear);
  clear.click(function() {
    input.val('');
    $('#fshInv').DataTable().search('').draw();
  });
}

function getInvMan() {

  debug.time('inventory.getInvMan');

  showQuickDropLinks = system.getValue('showQuickDropLinks');
  showQuickSendLinks = system.getValue('showQuickSendLinks');

  if (calf.membrList) {rekeyMembrList();}

  decorate();
  filters.lvlFilter();<commit_msg>BUGFIX use item from inventory - obviously noone using that...<commit_after>import add from '../support/task';
import calf from '../support/calf';
import doTable from './table';
import dropItem from '../ajax/dropItem';
import getForage from '../ajax/getForage';
import getInventory from '../ajax/getInventory';
import getMembrList from '../ajax/getMembrList';
import moveItem from '../ajax/moveItem';
import sendItem from '../ajax/sendItem';
import setForage from '../ajax/setForage';
import {
  equipItem,
  queueRecallItem,
  queueTakeItem,
  useItem
} from '../support/ajax';
import * as assets from './assets';
import * as dataObj from '../support/dataObj';
import * as debug from '../support/debug';
import * as filters from './filters';
import * as system from '../support/system';

/* jshint latedef: nofunc */
export var options;
export var showQuickDropLinks;
export var showQuickSendLinks;
export var theInv;

function doSpinner() { // jQuery
  $('#pCC').html('<span id="fshInvMan"><img src = "' +
  system.imageServer + '/world/actionLoadingSpinner.gif">&nbsp;' +
    'Getting inventory data...</span>');
}

function rekeyMembrList() {
  calf.membrList = Object.keys(calf.membrList)
    // Using reduce() to rekey the membrList from names to id's
    .reduce(function(prev, curr) {
      if (curr !== 'lastUpdate') {
        prev[calf.membrList[curr].id] =
          calf.membrList[curr];
      }
      return prev;
    }, {});
}

function decorate() {
  if (theInv.folders) {
    theInv.folders['-1'] = 'Main';
  }
  // Hide composed potions until Zorg fixes the feed
  theInv.items =
    theInv.items.filter(function(obj) {
      return obj.type !== '15';
    });
  //
}

function headers() { // jQuery
  var reportTitle;
  if (theInv.player_id) {
    reportTitle = '<b>&nbsp;Inventory Manager</b> ' +
      theInv.items.length +
      ' items (green = worn, blue = backpack)';
  } else {
    reportTitle = '<b>&nbsp;Guild Inventory Manager</b> ' +
      theInv.items.length +
      ' items (maroon = in BP, blue=guild store)';
  }
  var myHtml = assets.invManFilter.replace('@@reportTitle@@', reportTitle);
  $('#pCC').html(myHtml);
}

function setChecks() {
  Array.prototype.forEach.call(
    document.querySelectorAll('table.fshInvFilter input[type="checkbox"]'),
    function(el) {
      el.checked =
        options.checkedElements[el.getAttribute('item')] === 1;
    });
  setForage('fsh_inventory', options);
}

function setLvls() { // jQuery
  $('#fshMinLvl').val(options.fshMinLvl);
  $('#fshMaxLvl').val(options.fshMaxLvl);
}

function refresh() {
  doSpinner();
  // eslint-disable-next-line no-use-before-define
  syncInvMan();
}

function changeLvls() { // jQuery
  var minLvl = parseInt($('#fshMinLvl').val(), 10);
  var maxLvl = parseInt($('#fshMaxLvl').val(), 10);
  if (isNaN(minLvl) || isNaN(maxLvl)) {return;}
  options.fshMinLvl = minLvl;
  options.fshMaxLvl = maxLvl;
  setForage('fsh_inventory', options);
  $('#fshInv').DataTable().draw(false);
}

function resetLvls() { // jQuery
  options.fshMinLvl = dataObj.defaults.inventoryMinLvl;
  options.fshMaxLvl = dataObj.defaults.inventoryMaxLvl;
  setForage('fsh_inventory', options);
  $('#fshMinLvl').val(options.fshMinLvl);
  $('#fshMaxLvl').val(options.fshMaxLvl);
  $('#fshInv').DataTable().draw(false);
}

function getChecks() { // jQuery
  options.checkedElements = {};
  Array.prototype.forEach.call(
    document.querySelectorAll(
      'table.fshInvFilter input[type="checkbox"][item]:checked'),
    function(el) {
      options.checkedElements[el.getAttribute('item')] = 1;
    });
  setForage('fsh_inventory', options);
  $('#fshInv').DataTable().draw(false);
}

function allChecks() { // jQuery
  options.checkedElements = assets.inventoryCheckAll;
  setChecks();
  $('#fshInv').DataTable().draw(false);
}

function clearGearOnly(checkedElements) {
  var newEle = {};
  Object.keys(checkedElements).forEach(function(key) {
    if (parseInt(key, 10) >= 100) {
      newEle[key] = checkedElements[key];
    }
  });
  return newEle;
}

function clearChecks() { // jQuery
  options.checkedElements = clearGearOnly(options.checkedElements);
  setChecks();
  $('#fshInv').DataTable().draw();
}

function resetChecks() { // jQuery
  options.checkedElements = dataObj.defaults.inventoryCheckedElements;
  setChecks();
  $('#fshInv').DataTable().draw(false);
}

function setName(e) { // jQuery
  $('#fshInv').DataTable().search($(e.target).attr('set')).draw();
  $('#fshInv_filter input').focus();
}

function removeClass(self) {
  self.closest('tr')
    .find('.takeItem, .recallItem, .wearItem, .dropItem, .sendItem')
    .removeClass().qtip('hide');
}

function killRow(self) { // jQuery
  var tr = self.closest('tr');
  var td = $('td', tr);
  td.eq(2).empty(); // Where
  td.eq(12).empty(); // BP - GS
  td.eq(13).empty(); // GS - W/U
  td.eq(14).empty(); // W/U - Tag
  td.eq(15).empty(); // Tag - Drop
  td.eq(16).empty(); // ? - Send
  tr.css('text-decoration', 'line-through');
}

function anotherSpinner(self) {
  self.empty().append('<img src="' + system.imageServer +
    '/skin/loading.gif" width="11" height="11">');
}

function takeItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  queueTakeItem(self.attr('invid'), self.attr('action'))
    .done(function(data) {
      if (data.r === 1) {return;}
      killRow(self);
    });
  anotherSpinner(self);
}

function recallItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  queueRecallItem({
    invId: self.attr('invid'),
    playerId: self.attr('playerid'),
    mode: self.attr('mode'),
    action: self.attr('action')
  })
    .done(function(data) {
      if (data.r === 1) {return;}
      killRow(self);
    });
  anotherSpinner(self);
}

function wearItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  equipItem(self.attr('invid')).done(function(data) {
    if (data.r === 1) {return;}
    killRow(self);
  });
  anotherSpinner(self);
}

function doUseItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  useItem(self.attr('invid')).done(function(data) {
    if (data.r === 1) {return;}
    killRow(self);
  });
  anotherSpinner(self);
}

function doMoveItem(e) { // jQuery
  var self = $(e.target);
  moveItem([self.data('inv')], self.val());
}

function doDropItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  dropItem([self.data('inv')]).done(function(data) {
    if (data.r === 1) {return;}
    killRow(self);
  });
  anotherSpinner(self);
}

function doSendItem(e) { // jQuery
  var self = $(e.target);
  removeClass(self);
  sendItem([self.data('inv')]).done(function(data) {
    if (data.r === 1) {return;}
    killRow(self);
  });
  anotherSpinner(self);
}

function eventHandlers() { // jQuery
  $('#fshRefresh').click(refresh);
  $('#fshMinLvl, #fshMaxLvl').keyup(changeLvls);
  $('#fshReset').click(resetLvls);
  $('table.fshInvFilter').on('click', 'input[type="checkbox"]', getChecks);
  $('#fshAll').click(allChecks);
  $('#fshNone').click(clearChecks);
  $('#fshDefault').click(resetChecks);
  $('#fshInv').on('click', 'span.setName', setName);
  $('#fshInv').on('click', 'span.takeItem', takeItem);
  $('#fshInv').on('click', 'span.recallItem', recallItem);
  $('#fshInv').on('click', 'span.wearItem', wearItem);
  $('#fshInv').on('click', 'span.useItem', doUseItem);
  $('#fshInv').on('change', 'select.moveItem', doMoveItem);
  $('#fshInv').on('click', 'span.dropItem', doDropItem);
  $('#fshInv').on('click', 'span.sendItem', doSendItem);
}

function clearButton() { // jQuery
  var input = $('#fshInv_filter input');
  input.prop('type', 'text');
  var clear = $('<span>&times;</span>');
  input.wrap($('<span class="text-input-wrapper"/>'));
  input.after(clear);
  clear.click(function() {
    input.val('');
    $('#fshInv').DataTable().search('').draw();
  });
}

function getInvMan() {

  debug.time('inventory.getInvMan');

  showQuickDropLinks = system.getValue('showQuickDropLinks');
  showQuickSendLinks = system.getValue('showQuickSendLinks');

  if (calf.membrList) {rekeyMembrList();}

  decorate();
  filters.lvlFilter();
{'fraction_non_alphanumeric': 0.20214613571517182, 'fraction_numerical': 0.004341893065376504, 'mean_word_length': 4.0653471567703425, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, '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': '4919570', 'n_tokens_mistral': 5638, 'n_tokens_neox': 5631, 'n_words': 1002}