All files / web/bundles/pimuser/js/fields product-grid-filters.ts

74.42% Statements 32/43
28.57% Branches 2/7
45.45% Functions 5/11
74.36% Lines 29/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 11816x 16x 16x       16x 16x 16x 16x 16x             18x 18x 18x         18x 18x       18x           17x               16x 12x 12x 12x   12x     16x                                       16x                                                   16x 18x 18x   18x                   16x     16x   16x  
import * as $ from 'jquery';
import * as i18n from 'pimui/js/i18n';
import * as _ from 'underscore';
import NormalizedAttribute from 'pim/model/attribute';
import NormalizedAttributeGroup from 'pim/model/attribute-group';
 
const __ = require('oro/translator');
const BaseMultiSelectAsync = require('pim/form/common/fields/multi-select-async');
const UserContext = require('pim/user-context');
const FetcherRegistry = require('pim/fetcher-registry');
const LineTemplate = require('pim/template/attribute/attribute-line');
 
/**
 * Product grid filters select. It's a multi-select for attributes.
 *
 * @author Pierre Allard <pierre.allard@akeneo.com>
 */
class ProductGridFilters extends BaseMultiSelectAsync {
  private readonly lineView = _.template(LineTemplate);
  private attributeGroups: { [key: string]: NormalizedAttributeGroup } = {};
 
  /**
   * {@inheritdoc}
   */
  public configure(): JQueryPromise<any> {
    this.attributeGroups = {
      system: ProductGridFilters.getSystemAttributeGroup()
    };
 
    return $.when(
      BaseMultiSelectAsync.prototype.configure.apply(this, arguments),
      FetcherRegistry
        .getFetcher('attribute-group')
        .fetchAll()
        .then((attributeGroups: { [key: string]: NormalizedAttributeGroup }) => {
          this.attributeGroups = {...this.attributeGroups, ...attributeGroups};
        })
    );
  }
 
  /**
   * {@inheritdoc}
   */
  public getSelect2Options(): any {
    const parent = BaseMultiSelectAsync.prototype.getSelect2Options.apply(this, arguments);
    parent.formatResult = this.onGetResult.bind(this);
    parent.dropdownCssClass = 'select2--annotedLabels ' + parent.dropdownCssClass;
 
    return parent;
  }
 
  protected convertBackendItem(item: NormalizedAttribute): Object {
    return {
      id: item.code,
      text: i18n.getLabel(item.labels, UserContext.get('catalogLocale'), item.code),
      group: {
        text: (
          item.group ?
            i18n.getLabel(
              this.attributeGroups[item.group].labels,
              UserContext.get('catalogLocale'),
              item.group
            ) : ''
        )
      }
    };
  }
 
  /**
   * {@inheritdoc}
   */
  protected select2InitSelection(element: any, callback: any): void {
    const strValues = (<any> $(element)).val() as string;
    const values = strValues.split(',');
    if (values.length > 0) {
      $.ajax({
        url: this.choiceUrl,
        data: { identifiers: strValues },
        type: this.choiceVerb
      }).then(response => {
        let selecteds: NormalizedAttribute[] = <NormalizedAttribute[]> Object.values(response)
          .filter((item: NormalizedAttribute) => {
            return values.indexOf(item.code) > -1;
          });
 
        callback(selecteds.map((selected: NormalizedAttribute) => {
          return this.convertBackendItem(selected);
        }));
      });
    }
  }
 
  /**
   * Returns a fake attribute group for system filters
   *
   * @returns {NormalizedAttributeGroup}
   */
  private static getSystemAttributeGroup(): NormalizedAttributeGroup {
    const result: NormalizedAttributeGroup = {labels: {}};
    result['labels'][UserContext.get('catalogLocale')] = __('pim_datagrid.filters.system');
 
    return result;
  }
 
  /**
   * Formats and updates list of items
   *
   * @param {Object} item
   *
   * @return {Object}
   */
  private onGetResult(item: { text: string, group: { text: string } }): Object {
    return this.lineView({item});
  }
}
 
export = ProductGridFilters