All files / web/bundles/pimui/js/product-model/form/creation variant.js

100% Statements 35/35
91.67% Branches 11/12
100% Functions 6/6
100% Lines 35/35

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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134                  99x                 99x             230x   230x 230x             230x   230x             1719x 237x   237x 17x 17x         17x 17x   17x     220x 220x   220x                         17x   17x                     22x   22x             22x 22x 22x 22x                           282x   282x 17x 17x   17x 8x 8x   9x 9x              
/**
 * A select2 field displaying family variants dependent on the family field in the same parent form.
 *
 * @author    Tamara Robichet <tamara.robichet@akeneo.com>
 * @copyright 2017 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 */
'use strict';
 
define([
  'jquery',
  'underscore',
  'oro/translator',
  'pim/fetcher-registry',
  'pim/router',
  'pim/user-context',
  'pim/form/common/fields/simple-select-async',
], function($, _, __, FetcherRegistry, Routing, UserContext, SimpleSelectAsync) {
  return SimpleSelectAsync.extend({
    previousFamily: null,
 
    /**
     * {@inheritdoc}
     */
    initialize() {
      SimpleSelectAsync.prototype.initialize.apply(this, arguments);
 
      this.previousFamily = null;
      this.readOnly = true;
    },
 
    /**
     * {@inheritdoc}
     */
    configure() {
      this.listenTo(this.getRoot(), 'pim_enrich:form:entity:post_update', this.onPostUpdate.bind(this));
 
      return SimpleSelectAsync.prototype.configure.apply(this, arguments);
    },
 
    /**
     * Updates the choice URL when the model changed
     */
    onPostUpdate() {
      if (this.getFormData().family !== this.previousFamily) {
        this.previousFamily = this.getFormData().family;
 
        if (this.getFormData().family) {
          this.getFamilyIdFromCode(this.getFormData().family).then(familyId => {
            this.setChoiceUrl(
              Routing.generate(this.config.loadUrl, {
                family_id: familyId,
              })
            );
            this.readOnly = false;
            this.setData({[this.fieldName]: null});
 
            this.render();
          });
        } else {
          this.readOnly = true;
          this.setData({[this.fieldName]: null});
 
          this.render();
        }
      }
    },
 
    /**
     * Get the id for a given family code
     *
     * @param {String} code
     *
     * @return {Promise}
     */
    getFamilyIdFromCode(code) {
      return FetcherRegistry.getFetcher('family')
        .fetch(code)
        .then(family => family.meta.id);
    },
 
    /**
     * Get the label of a family variant from its code
     *
     * @param {String} code
     *
     * @return {Promise}
     */
    getFamilyVariantLabelFromCode(code) {
      return FetcherRegistry.getFetcher('family-variant')
        .fetch(code)
        .then(familyVariant => familyVariant.labels[UserContext.get('catalogLocale')]);
    },
 
    /**
     * {@inheritdoc}
     */
    select2InitSelection(element, callback) {
      const id = $(element).val();
      Eif ('' !== id) {
        this.getFamilyVariantLabelFromCode(id).then(function(label) {
          callback({
            id: id,
            text: label,
          });
        });
      }
    },
 
    /**
     * {@inheritdoc}
     *
     * We override this method to automatically select the first element when there is only 1 choice
     */
    postRender() {
      SimpleSelectAsync.prototype.postRender.apply(this, arguments);
 
      if (!this.getFormData()[this.fieldName] && this.choiceUrl) {
        $.getJSON(this.choiceUrl, this.select2Data.bind(this)).then(response => {
          const results = this.select2Results(response).results;
 
          if (results.length === 1) {
            this.setData({[this.fieldName]: results[0].id});
            this.$('.select2').select2('val', results[0].id);
          } else {
            this.setData({[this.fieldName]: null});
            this.$('.select2').select2('val', '');
          }
        });
      }
    },
  });
});