<commit_before>import SearchIndex from 'appkit/utils/search-index';

export default Ember.ArrayController.extend({
  sortProperties : ['itemData._sort_create_date'],
  sortAscending  : false,

  contentType: null,

  lockedItems: Ember.A([]),
  lockedRef  : null,

  searchQuery: null,
  isSearchResults: false,

  recordLimit: 0,
  endReached: false,

  isLimited: function () {
    return this.get('content.length') >= this.get('recordLimit') && !this.get('endReached') && !this.get('isSearchResults');
  }.property('content.length', 'endReached', 'isSearchResults'),

  filterQuery: '',

  columnChoices: function () {
    return this.get('contentType.controls').rejectBy('name', 'name').rejectBy('name', 'preview_url').rejectBy('name', 'instruction');
  }.property('contentType.controls.@each'),

  cmsControls: function () {
    var controls = this.get('contentType.controls').filterBy('showInCms');
    controls.forEach(function (control) {
      control.set('isSortable', control.get('controlType.valueType') === 'string');
    });
    return controls;
  }.property('contentType.controls.@each.showInCms'),

  _updateItemControls: function (item) {
    var cmsControls = Ember.A([]);
    this.get('cmsControls').forEach(function (control) {
      cmsControls.pushObject({
        value: item.get('itemData')[control.get('name')],
        controlType: control.get('controlType'),
        control: control
      });
    });
    item.set('cmsControls', cmsControls);
    return item;
  },

  cmsItems: Ember.arrayComputed('content.@each.itemData', 'cmsControls.@each.showInCms', {
    addedItem: function (array, item, changeMeta) {

      if (item.constructor.typeKey === 'control') {
        array.forEach(this._updateItemControls.bind(this));
      } else {
        array.pushObject(this._updateItemControls(item));
      }

      return array;
    },
    removedItem: function (array, item) {
      if (item.constructor.typeKey !== 'control') {
        array.removeObject(item);
      }
      return array;
    }
  }),

  locksChanged: function () {
    this.get('cmsItems').setEach('lockedBy', null);
    this.get('lockedItems').forEach(function (lock) {
      var item = this.get('cmsItems').findBy('id', lock.get('id'));
      item.set('lockedBy', lock.get('email'));
    }, this);
  }.observes('lockedItems.@each'),

  refreshContent: function () {
    this.set('isLoading', true);
    this.set('isSearchResults', false);
    this.set('content', Ember.A([]));
    this.set('endReached', false);

    var controller = this;
    this.store.find(this.get('itemModelName'), {
      limit: this.get('recordLimit'),
      orderBy: this.get('sortProperties.firstObject').replace('itemData.', ''),
      desc: !this.get('sortAscending')
    }).then(function (records) {
      controller.set('endReached', records.get('length') < controller.get('recordLimit'));
      controller.set('isLoading', false);
      controller.set('content', records);
    });
  },

  searchPlaceholder: function () {
    return 'Search ' + this.get('contentType.name');
  }.property('contentType'),

  debouncedSearchQueryObserver: Ember.debouncedObserver(function() {<commit_msg>handle if lockedItem out of view<commit_after>import SearchIndex from 'appkit/utils/search-index';

export default Ember.ArrayController.extend({
  sortProperties : ['itemData._sort_create_date'],
  sortAscending  : false,

  contentType: null,

  lockedItems: Ember.A([]),
  lockedRef  : null,

  searchQuery: null,
  isSearchResults: false,

  recordLimit: 0,
  endReached: false,

  isLimited: function () {
    return this.get('content.length') >= this.get('recordLimit') && !this.get('endReached') && !this.get('isSearchResults');
  }.property('content.length', 'endReached', 'isSearchResults'),

  filterQuery: '',

  columnChoices: function () {
    return this.get('contentType.controls').rejectBy('name', 'name').rejectBy('name', 'preview_url').rejectBy('name', 'instruction');
  }.property('contentType.controls.@each'),

  cmsControls: function () {
    var controls = this.get('contentType.controls').filterBy('showInCms');
    controls.forEach(function (control) {
      control.set('isSortable', control.get('controlType.valueType') === 'string');
    });
    return controls;
  }.property('contentType.controls.@each.showInCms'),

  _updateItemControls: function (item) {
    var cmsControls = Ember.A([]);
    this.get('cmsControls').forEach(function (control) {
      cmsControls.pushObject({
        value: item.get('itemData')[control.get('name')],
        controlType: control.get('controlType'),
        control: control
      });
    });
    item.set('cmsControls', cmsControls);
    return item;
  },

  cmsItems: Ember.arrayComputed('content.@each.itemData', 'cmsControls.@each.showInCms', {
    addedItem: function (array, item, changeMeta) {

      if (item.constructor.typeKey === 'control') {
        array.forEach(this._updateItemControls.bind(this));
      } else {
        array.pushObject(this._updateItemControls(item));
      }

      return array;
    },
    removedItem: function (array, item) {
      if (item.constructor.typeKey !== 'control') {
        array.removeObject(item);
      }
      return array;
    }
  }),

  locksChanged: function () {
    this.get('cmsItems').setEach('lockedBy', null);
    this.get('lockedItems').forEach(function (lock) {
      var item = this.get('cmsItems').findBy('id', lock.get('id'));
      // locked item isn't necessarily on the same page as what you are looking at
      if (item) {
        item.set('lockedBy', lock.get('email'));
      }
    }, this);
  }.observes('lockedItems.@each'),

  refreshContent: function () {
    this.set('isLoading', true);
    this.set('isSearchResults', false);
    this.set('content', Ember.A([]));
    this.set('endReached', false);

    var controller = this;
    this.store.find(this.get('itemModelName'), {
      limit: this.get('recordLimit'),
      orderBy: this.get('sortProperties.firstObject').replace('itemData.', ''),
      desc: !this.get('sortAscending')
    }).then(function (records) {
      controller.set('endReached', records.get('length') < controller.get('recordLimit'));
      controller.set('isLoading', false);
      controller.set('content', records);
    });
  },

  searchPlaceholder: function () {
    return 'Search ' + this.get('contentType.name');
  }.property('contentType'),

  debouncedSearchQueryObserver: Ember.debouncedObserver(function() {
{'fraction_non_alphanumeric': 0.17250464972101673, 'fraction_numerical': 0.00030998140111593303, 'mean_word_length': 3.8849356548069642, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '3856256', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1926, 'n_words': 327}