<commit_before>import Component from '@ember/component';
import { get, set, computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import { inject as service } from '@ember/service';

import { task } from 'ember-concurrency';

import Slotted from 'block-slots';

export default Component.extend(Slotted, {
  onchange: function() {},
  tagName: '',

  error: function() {},
  type: '',

  dom: service('dom'),
  formContainer: service('form'),

  item: alias('form.data'),

  selectedOptions: alias('items'),

  init: function() {
    this._super(...arguments);
    this._listeners = this.dom.listeners();
    this.form = this.formContainer.form(this.type);
    this.form.clear({ Datacenter: this.dc, Namespace: this.nspace });
  },
  willDestroyElement: function() {
    this._super(...arguments);
    this._listeners.remove();
  },
  options: computed('selectedOptions.[]', 'allOptions.[]', function() {
    // It's not massively important here that we are defaulting `items` and
    // losing reference as its just to figure out the diff
    let options = this.allOptions || [];
    const items = this.selectedOptions || [];
    if (get(items, 'length') > 0) {
      // filter out any items from the available options that have already been
      // selected/added
      // TODO: find a proper ember-data diff
      options = options.filter(item => !items.findBy('ID', get(item, 'ID')));
    }
    return options;
  }),
  save: task(function*(item, items, success = function() {}) {
    const repo = this.repo;
    try {
      item = yield repo.persist(item);
      this.actions.change.apply(this, [
        {
          target: {
            name: 'items[]',
            value: items,
          },
        },
        items,
        item,
      ]);
      success();
    } catch (e) {
      this.error({ error: e });
    }
  }),
  actions: {
    reset: function() {
      this.form.clear({ Datacenter: this.dc, Namespace: this.nspace, Partition: this.partition });
    },

    remove: function(item, items) {
      const prop = this.repo.getSlugKey();
      const value = get(item, prop);
      const pos = items.findIndex(function(item) {
        return get(item, prop) === value;
      });
      if (pos !== -1) {
        return items.removeAt(pos, 1);
      }
      this.onchange({ target: this });
    },
    change: function(e, value, item) {
      const event = this.dom.normalizeEvent(...arguments);
      const items = value;
      switch (event.target.name) {
        case 'items[]':
          set(item, 'CreateTime', new Date().getTime());
          // this always happens synchronously
          items.pushObject(item);
          // TODO: Fire a proper event
          this.onchange({ target: this });
          break;
        default:<commit_msg>Revert "ui: Add partition parameter when clearing child-selector forms in ACLs"<commit_after>import Component from '@ember/component';
import { get, set, computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import { inject as service } from '@ember/service';

import { task } from 'ember-concurrency';

import Slotted from 'block-slots';

export default Component.extend(Slotted, {
  onchange: function() {},
  tagName: '',

  error: function() {},
  type: '',

  dom: service('dom'),
  formContainer: service('form'),

  item: alias('form.data'),

  selectedOptions: alias('items'),

  init: function() {
    this._super(...arguments);
    this._listeners = this.dom.listeners();
    this.form = this.formContainer.form(this.type);
    this.form.clear({ Datacenter: this.dc, Namespace: this.nspace });
  },
  willDestroyElement: function() {
    this._super(...arguments);
    this._listeners.remove();
  },
  options: computed('selectedOptions.[]', 'allOptions.[]', function() {
    // It's not massively important here that we are defaulting `items` and
    // losing reference as its just to figure out the diff
    let options = this.allOptions || [];
    const items = this.selectedOptions || [];
    if (get(items, 'length') > 0) {
      // filter out any items from the available options that have already been
      // selected/added
      // TODO: find a proper ember-data diff
      options = options.filter(item => !items.findBy('ID', get(item, 'ID')));
    }
    return options;
  }),
  save: task(function*(item, items, success = function() {}) {
    const repo = this.repo;
    try {
      item = yield repo.persist(item);
      this.actions.change.apply(this, [
        {
          target: {
            name: 'items[]',
            value: items,
          },
        },
        items,
        item,
      ]);
      success();
    } catch (e) {
      this.error({ error: e });
    }
  }),
  actions: {
    reset: function() {
      this.form.clear({ Datacenter: this.dc, Namespace: this.nspace });
    },

    remove: function(item, items) {
      const prop = this.repo.getSlugKey();
      const value = get(item, prop);
      const pos = items.findIndex(function(item) {
        return get(item, prop) === value;
      });
      if (pos !== -1) {
        return items.removeAt(pos, 1);
      }
      this.onchange({ target: this });
    },
    change: function(e, value, item) {
      const event = this.dom.normalizeEvent(...arguments);
      const items = value;
      switch (event.target.name) {
        case 'items[]':
          set(item, 'CreateTime', new Date().getTime());
          // this always happens synchronously
          items.pushObject(item);
          // TODO: Fire a proper event
          this.onchange({ target: this });
          break;
        default:
{'fraction_non_alphanumeric': 0.15320135544854646, 'fraction_numerical': 0.001070090957731407, 'mean_word_length': 2.520401757689893, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '826694', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1631, 'n_words': 441}