<commit_before>(function() {
  'use strict';
  angular.module('dimApp')
    .directive('dimLoadoutPopup', LoadoutPopup);

  LoadoutPopup.$inject = [];

  function LoadoutPopup() {
    return {
      controller: LoadoutPopupCtrl,
      controllerAs: 'vm',
      bindToController: true,
      restrict: 'A',
      scope: {
        store: '=dimLoadoutPopup'
      },
      replace: true,
      template: [
        '<div class="loadout-popup-content">',
        '  <ul class="loadout-list">',
        '    <li class="loadout-set">',
        '      <span ng-click="vm.newLoadout($event)">+ Create Loadout</span>',
        '      <span ng-click="vm.newLoadoutFromEquipped($event)">From Equipped</span>',
        '    </li>',
        '    <li ng-repeat="loadout in vm.loadouts track by loadout.id" class="loadout-set">',
        '      <span title="{{ loadout.name }}" ng-click="vm.applyLoadout(loadout, $event)">{{ loadout.name }}</span>',
        '      <span ng-click="vm.deleteLoadout(loadout, $event)"><i class="fa fa-trash-o"></i></span>',
        '      <span ng-click="vm.editLoadout(loadout, $event)"><i class="fa fa-pencil"></i></span>',
        '    </li>',
        '    <li class="loadout-set" ng-if="!vm.store.isVault">',
        '      <span ng-click="vm.maxLightLoadout($event)"><i class="fa fa-star"></i> Maximize Light</span>',
        '    </li>',
        '    <li class="loadout-set" ng-if="!vm.store.isVault">',
        '      <span ng-click="vm.itemLevelingLoadout($event)"><i class="fa fa-level-up"></i> Item Leveling</span>',
        '    </li>',
        '    <li class="loadout-set">',
        '      <span ng-click="vm.gatherEngramsLoadout($event)"><img class="fa" src="/images/engram.svg" height="12" width="12"/> Gather Engrams</span>',
        '    </li>',
        '    <li class="loadout-set" ng-if="!vm.store.isVault">',
        '      <span ng-click="vm.startFarmingEngrams($event)"><img class="fa" src="/images/engram.svg" height="12" width="12"/> Engrams to Vault</span>',
        '    </li>',
        '    <li class="loadout-set" ng-if="vm.previousLoadout">',
        '      <span title="{{ vm.previousLoadout.name }}" ng-click="vm.applyLoadout(vm.previousLoadout, $event, true)"><i class="fa fa-undo"></i> {{vm.previousLoadout.name}}</span>',
        '      <span ng-click="vm.applyLoadout(vm.previousLoadout, $event)">All items</span>',
        '    </li>',
        '  </ul>',
        '</div>'
      ].join('')
    };
  }

  LoadoutPopupCtrl.$inject = ['$rootScope', 'ngDialog', 'dimLoadoutService', 'dimItemService', 'dimItemTier', 'toaster', 'dimEngramFarmingService'];

  function LoadoutPopupCtrl($rootScope, ngDialog, dimLoadoutService, dimItemService, dimItemTier, toaster, dimEngramFarmingService) {
    var vm = this;
    vm.previousLoadout = dimLoadoutService.previousLoadouts[vm.store.id];

    vm.classTypeId = {
      'warlock': 0,
      'titan': 1,
      'hunter': 2
    }[vm.store.class] || -1;

    function initLoadouts() {
      dimLoadoutService.getLoadouts()
        .then(function(loadouts) {
          vm.loadouts = _.sortBy(loadouts, 'name') || [];

          vm.loadouts = _.filter(vm.loadouts, function(item) {
            return vm.classTypeId === -1 || ((item.classType === -1) || (item.classType === vm.classTypeId));
          });
        });
    }
    $rootScope.$on('dim-save-loadout', initLoadouts);
    $rootScope.$on('dim-delete-loadout', initLoadouts);
    initLoadouts();

    vm.newLoadout = function newLoadout($event) {
      ngDialog.closeAll();
      $rootScope.$broadcast('dim-create-new-loadout', { });
    };

    vm.newLoadoutFromEquipped = function newLoadout($event) {
      ngDialog.closeAll();

      var loadout = loadoutFromCurrentlyEquipped(vm.store.items, "");
      // We don't want to prepopulate the loadout with a bunch of cosmetic junk
      // like emblems and ships and horns.
      loadout.items = _.pick(loadout.items,
                             'class',
                             'primary',
                             'special',
                             'heavy',
                             'helmet',
                             'gauntlets',
                             'chest',
                             'leg',
                             'classitem',
                             'artifact',
                             'ghost');
      loadout.classType = vm.classTypeId;
      vm.editLoadout(loadout, $event);
    };

    vm.deleteLoadout = function deleteLoadout(loadout, $event) {
      if (confirm("Are you sure you want to delete '" + loadout.name + "'?")) {
        dimLoadoutService.deleteLoadout(loadout);
      }
    };

    vm.editLoadout = function editLoadout(loadout, $event) {
      ngDialog.closeAll();
      $rootScope.$broadcast('dim-edit-loadout', {
        loadout: loadout
      });
    };

    function loadoutFromCurrentlyEquipped(items, name) {
      return {
        classType: -1,
        name: name,
        items: _(items)
          .chain()
          .select(function(item) {
            return item.canBeInLoadout();
          })
          .map(function (i) {
            return angular.copy(i);
          })
          .groupBy(function(i) {
            return i.type.toLowerCase();
          }).value()
      };
    }

    vm.applyLoadout = function applyLoadout(loadout, $event, filterToEquipped) {
      ngDialog.closeAll();
      dimEngramFarmingService.stop();

      if (!vm.store.isVault) {
        if (loadout === vm.previousLoadout) {
          vm.previousLoadout = undefined;
        } else {
          vm.previousLoadout = loadoutFromCurrentlyEquipped(vm.store.items, 'Before "' + loadout.name + '"');
        }
        dimLoadoutService.previousLoadouts[vm.store.id] = vm.previousLoadout; // ugly hack
      }

      if (filterToEquipped) {
        loadout = _.mapObject(loadout, function(items) {
          return _.select(items, 'equipped');
        });
      }

      dimLoadoutService.applyLoadout(vm.store, loadout);
    };

    // A dynamic loadout set up to level weapons and armor
    vm.itemLevelingLoadout = function itemLevelingLoadout($event) {<commit_msg>Better filtering of loadout to equipped<commit_after>(function() {
  'use strict';
  angular.module('dimApp')
    .directive('dimLoadoutPopup', LoadoutPopup);

  LoadoutPopup.$inject = [];

  function LoadoutPopup() {
    return {
      controller: LoadoutPopupCtrl,
      controllerAs: 'vm',
      bindToController: true,
      restrict: 'A',
      scope: {
        store: '=dimLoadoutPopup'
      },
      replace: true,
      template: [
        '<div class="loadout-popup-content">',
        '  <ul class="loadout-list">',
        '    <li class="loadout-set">',
        '      <span ng-click="vm.newLoadout($event)">+ Create Loadout</span>',
        '      <span ng-click="vm.newLoadoutFromEquipped($event)">From Equipped</span>',
        '    </li>',
        '    <li ng-repeat="loadout in vm.loadouts track by loadout.id" class="loadout-set">',
        '      <span title="{{ loadout.name }}" ng-click="vm.applyLoadout(loadout, $event)">{{ loadout.name }}</span>',
        '      <span ng-click="vm.deleteLoadout(loadout, $event)"><i class="fa fa-trash-o"></i></span>',
        '      <span ng-click="vm.editLoadout(loadout, $event)"><i class="fa fa-pencil"></i></span>',
        '    </li>',
        '    <li class="loadout-set" ng-if="!vm.store.isVault">',
        '      <span ng-click="vm.maxLightLoadout($event)"><i class="fa fa-star"></i> Maximize Light</span>',
        '    </li>',
        '    <li class="loadout-set" ng-if="!vm.store.isVault">',
        '      <span ng-click="vm.itemLevelingLoadout($event)"><i class="fa fa-level-up"></i> Item Leveling</span>',
        '    </li>',
        '    <li class="loadout-set">',
        '      <span ng-click="vm.gatherEngramsLoadout($event)"><img class="fa" src="/images/engram.svg" height="12" width="12"/> Gather Engrams</span>',
        '    </li>',
        '    <li class="loadout-set" ng-if="!vm.store.isVault">',
        '      <span ng-click="vm.startFarmingEngrams($event)"><img class="fa" src="/images/engram.svg" height="12" width="12"/> Engrams to Vault</span>',
        '    </li>',
        '    <li class="loadout-set" ng-if="vm.previousLoadout">',
        '      <span title="{{ vm.previousLoadout.name }}" ng-click="vm.applyLoadout(vm.previousLoadout, $event, true)"><i class="fa fa-undo"></i> {{vm.previousLoadout.name}}</span>',
        '      <span ng-click="vm.applyLoadout(vm.previousLoadout, $event)">All items</span>',
        '    </li>',
        '  </ul>',
        '</div>'
      ].join('')
    };
  }

  LoadoutPopupCtrl.$inject = ['$rootScope', 'ngDialog', 'dimLoadoutService', 'dimItemService', 'dimItemTier', 'toaster', 'dimEngramFarmingService'];

  function LoadoutPopupCtrl($rootScope, ngDialog, dimLoadoutService, dimItemService, dimItemTier, toaster, dimEngramFarmingService) {
    var vm = this;
    vm.previousLoadout = dimLoadoutService.previousLoadouts[vm.store.id];

    vm.classTypeId = {
      'warlock': 0,
      'titan': 1,
      'hunter': 2
    }[vm.store.class] || -1;

    function initLoadouts() {
      dimLoadoutService.getLoadouts()
        .then(function(loadouts) {
          vm.loadouts = _.sortBy(loadouts, 'name') || [];

          vm.loadouts = _.filter(vm.loadouts, function(item) {
            return vm.classTypeId === -1 || ((item.classType === -1) || (item.classType === vm.classTypeId));
          });
        });
    }
    $rootScope.$on('dim-save-loadout', initLoadouts);
    $rootScope.$on('dim-delete-loadout', initLoadouts);
    initLoadouts();

    vm.newLoadout = function newLoadout($event) {
      ngDialog.closeAll();
      $rootScope.$broadcast('dim-create-new-loadout', { });
    };

    vm.newLoadoutFromEquipped = function newLoadout($event) {
      ngDialog.closeAll();

      var loadout = filterLoadoutToEquipped(loadoutFromCurrentlyEquipped(vm.store.items, ""));
      // We don't want to prepopulate the loadout with a bunch of cosmetic junk
      // like emblems and ships and horns.
      loadout.items = _.pick(loadout.items,
                             'class',
                             'primary',
                             'special',
                             'heavy',
                             'helmet',
                             'gauntlets',
                             'chest',
                             'leg',
                             'classitem',
                             'artifact',
                             'ghost');
      loadout.classType = vm.classTypeId;
      vm.editLoadout(loadout, $event);
    };

    vm.deleteLoadout = function deleteLoadout(loadout, $event) {
      if (confirm("Are you sure you want to delete '" + loadout.name + "'?")) {
        dimLoadoutService.deleteLoadout(loadout);
      }
    };

    vm.editLoadout = function editLoadout(loadout, $event) {
      ngDialog.closeAll();
      $rootScope.$broadcast('dim-edit-loadout', {
        loadout: loadout
      });
    };

    function loadoutFromCurrentlyEquipped(items, name) {
      return {
        classType: -1,
        name: name,
        items: _(items)
          .chain()
          .select(function(item) {
            return item.canBeInLoadout();
          })
          .map(function (i) {
            return angular.copy(i);
          })
          .groupBy(function(i) {
            return i.type.toLowerCase();
          }).value()
      };
    }

    function filterLoadoutToEquipped(loadout) {
      var filteredLoadout = angular.copy(loadout);
      filteredLoadout.items = _.mapObject(filteredLoadout.items, function(items) {
        return _.select(items, 'equipped');
      });
      return filteredLoadout;
    }

    vm.applyLoadout = function applyLoadout(loadout, $event, filterToEquipped) {
      ngDialog.closeAll();
      dimEngramFarmingService.stop();

      if (!vm.store.isVault) {
        if (loadout === vm.previousLoadout) {
          vm.previousLoadout = undefined;
        } else {
          vm.previousLoadout = loadoutFromCurrentlyEquipped(vm.store.items, 'Before "' + loadout.name + '"');
        }
        dimLoadoutService.previousLoadouts[vm.store.id] = vm.previousLoadout; // ugly hack
      }

      if (filterToEquipped) {
        loadout = filterLoadoutToEquipped(loadout);
      }

      dimLoadoutService.applyLoadout(vm.store, loadout);
    };

    // A dynamic loadout set up to level weapons and armor
    vm.itemLevelingLoadout = function itemLevelingLoadout($event) {
{'fraction_non_alphanumeric': 0.16204111013356795, 'fraction_numerical': 0.0023994241382068304, 'mean_word_length': 2.2152224222165082, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 0, '>': 107, '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': '6881174', 'n_tokens_mistral': 3928, 'n_tokens_neox': 3892, 'n_words': 650}