<commit_before>Template.residents.created = function () {
  // Get reference to template instance
  var instance = this;

  // Reactive variable to toggle resident subscription based on departed status
  instance.includeDeparted = new ReactiveVar();

  // Subscribe to all homes, for data table
  instance.subscribe("allHomes");

  // Toggle resident subscription based on departed status
  instance.autorun(function () {
    // If include departed is checked
    if (instance.includeDeparted.get() === true) {
      // Show all residents
      instance.subscribe("allResidents");
    } else {
      // Otherwise, show only current residents
      instance.subscribe("allCurrentResidents");
    }
  });
};

Template.residents.helpers({
  'residents': function () {
    // Get reference to template instance
    var instance = Template.instance();

    // Make sure subscriptions are ready
    if (instance.subscriptionsReady()) {
      // Create a placeholder array for resident objects
      var residentsArray = [];

      // Get all residents
      var residents = Residents.find().fetch();

        // Iterate through residents
        // set full name and home name from collection helpers
        residents.forEach(function (resident) {
          var residentObject = {
            fullName: resident.fullName(),
            homeName: resident.homeName(),
            residentId: resident._id,
            homeId: resident.homeId,
          };

          // Add resident object to residents list
          residentsArray.push(residentObject);

        });

      return residentsArray;
    }

    // return an empty array if no data is available
    return [];
  },
  'tableSettings': function () {
    // Create placeholder object for filter labels
    const tableLabels = {};

    // Get translation strings for filter values
    tableLabels.fullName = TAPi18n.__("residents-tableLabels-fullName");
    tableLabels.homeName = TAPi18n.__("residents-tableLabels-homeName");

    var tableSettings = {
      showFilter: false,
      fields: [
        {
          key: 'fullName',
          label: tableLabels.fullName,
          sortOrder: 0,
          sortDirection: 'ascending'
        },
        {
          key: 'homeName',
          label: tableLabels.homeName,
          sortOrder: 1,
          sortDirection: 'ascending'
        },
        {
          key: 'homeId',
          label: 'Residency',
          tmpl: Template.residentCurrentResidency
        }
      ],
      filters: ['nameFilter', 'homeFilter']
    };

    return tableSettings;
  },
  "nameFilterFields": function () {
    // Return relevant field name(s) for name filter
    return ['fullName'];
  },
  "homeFilterFields": function () {
    // Return relevant field name(s) for home filter
    return ['homeName'];
  },
  "filterLabels": function () {
    // Create placeholder object for filter labels
    const filterLabels = {};

    // Get translation strings for filter values
    filterLabels.fullName = TAPi18n.__("residents-filterLabels-fullName");
    filterLabels.homeName = TAPi18n.__("residents-filterLabels-homeName");

    return filterLabels;
  }
});

Template.residents.events({
  // TODO: add a 'view resident' button to trigger this event<commit_msg>Hide residency column unless user is admin<commit_after>Template.residents.created = function () {
  // Get reference to template instance
  var instance = this;

  // Reactive variable to toggle resident subscription based on departed status
  instance.includeDeparted = new ReactiveVar();

  // Subscribe to all homes, for data table
  instance.subscribe("allHomes");

  // Toggle resident subscription based on departed status
  instance.autorun(function () {
    // If include departed is checked
    if (instance.includeDeparted.get() === true) {
      // Show all residents
      instance.subscribe("allResidents");
    } else {
      // Otherwise, show only current residents
      instance.subscribe("allCurrentResidents");
    }
  });
};

Template.residents.helpers({
  'residents': function () {
    // Get reference to template instance
    var instance = Template.instance();

    // Make sure subscriptions are ready
    if (instance.subscriptionsReady()) {
      // Create a placeholder array for resident objects
      var residentsArray = [];

      // Get all residents
      var residents = Residents.find().fetch();

        // Iterate through residents
        // set full name and home name from collection helpers
        residents.forEach(function (resident) {
          var residentObject = {
            fullName: resident.fullName(),
            homeName: resident.homeName(),
            residentId: resident._id,
            homeId: resident.homeId,
          };

          // Add resident object to residents list
          residentsArray.push(residentObject);

        });

      return residentsArray;
    }

    // return an empty array if no data is available
    return [];
  },
  'tableSettings': function () {
    // Create placeholder object for filter labels
    const tableLabels = {};

    // Get translation strings for filter values
    tableLabels.fullName = TAPi18n.__("residents-tableLabels-fullName");
    tableLabels.homeName = TAPi18n.__("residents-tableLabels-homeName");

    var tableSettings = {
      showFilter: false,
      fields: [
        {
          key: 'fullName',
          label: tableLabels.fullName,
          sortOrder: 0,
          sortDirection: 'ascending'
        },
        {
          key: 'homeName',
          label: tableLabels.homeName,
          sortOrder: 1,
          sortDirection: 'ascending'
        },
        {
          key: 'homeId',
          label: 'Residency',
          tmpl: Template.residentCurrentResidency,
          hidden: function () {
            var currentUserId = Meteor.userId();

            // Check if current user has Admin role
            var currentUserIsAdmin = Roles.userIsInRole(currentUserId, ["admin"]);

            // Only show edit column for users with Admin role
            if (currentUserIsAdmin) {
              return false;
            } else {
              return true;
            }
          },
        }
      ],
      filters: ['nameFilter', 'homeFilter']
    };

    return tableSettings;
  },
  "nameFilterFields": function () {
    // Return relevant field name(s) for name filter
    return ['fullName'];
  },
  "homeFilterFields": function () {
    // Return relevant field name(s) for home filter
    return ['homeName'];
  },
  "filterLabels": function () {
    // Create placeholder object for filter labels
    const filterLabels = {};

    // Get translation strings for filter values
    filterLabels.fullName = TAPi18n.__("residents-filterLabels-fullName");
    filterLabels.homeName = TAPi18n.__("residents-filterLabels-homeName");

    return filterLabels;
  }
});

Template.residents.events({
  // TODO: add a 'view resident' button to trigger this event
{'fraction_non_alphanumeric': 0.10279297437374028, 'fraction_numerical': 0.0028793550244745176, 'mean_word_length': 2.3625363020329138, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, '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': '5377201', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1758, 'n_words': 519}