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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | 2x 2x 8x 8x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 12x 14x 12x 12x 12x 12x 12x 24x 24x 24x 24x 5x 10x 10x 10x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 30x 30x 2x 2x 2x 3x 2x 2x 3x 3x 2x 13x 13x 13x 13x 13x 13x 9x 9x 13x 13x 13x 14x 14x 3x 2x 50x 50x | 'use strict'; /** * Fields container of the create family variant form. * This module contains all the fields and the logic to update the family variant model. * * @author Adrien Pétremann <adrien.petremann@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) */ define( [ 'jquery', 'underscore', 'oro/translator', 'pim/user-context', 'pim/fetcher-registry', 'pim/initselect2', 'pim/form', 'pim/template/family-variant/add-variant-form-fields-container' ], function( $, _, __, UserContext, FetcherRegistry, initSelect2, BaseForm, template ) { return BaseForm.extend({ template: _.template(template), queryTimer: null, validationErrors: [], events: { 'change select, input[name="code"], input[name="label"]': function() { this.updateModel(); this.render(); } }, /** * {@inheritdoc} */ configure: function () { this.listenTo( this.getRoot(), 'pim_enrich:form:entity:pre_save', this.resetValidationErrors.bind(this) ); this.listenTo( this.getRoot(), 'pim_enrich:form:entity:validation_error', this.setValidationErrors.bind(this) ); this.listenTo( this.getRoot(), 'pim_enrich:form:entity:validation_error', this.render.bind(this) ); this.setData(this.getNewFamilyVariant()); return BaseForm.prototype.configure.apply(this, arguments); }, /** * {@inheritdoc} */ render() { const catalogLocale = UserContext.get('catalogLocale'); const familyVariant = _.defaults(this.getFormData(), this.getNewFamilyVariant()); const label = this.getEntityLabel(familyVariant, catalogLocale); const axes1 = familyVariant.variant_attribute_sets[0].axes.join(','); const axes2 = familyVariant.variant_attribute_sets[1] ? familyVariant.variant_attribute_sets[1].axes.join(',') : '' ; const globalErrors = this.validationErrors.filter((error) => { return true === error.global || undefined === error.path || 'variant_attribute_sets' === error.path; }); const fieldErrors = _.groupBy(this.validationErrors, 'path'); this.$el.html( this.template({ __: __, familyVariant: familyVariant, catalogLocale: catalogLocale, axes1: axes1, axes2: axes2, label: label, globalErrors: globalErrors, fieldErrors: fieldErrors }) ); this.initializeSelectWidgets(); }, /** * Initialize the select2 widgets for axis fields. These select2 fetch attributes available as axis * for the family variant. */ initializeSelectWidgets() { const $selects = this.$('input[type="hidden"]'); _.each($selects, (select) => { const $select = $(select); const options = { dropdownCssClass: '', closeOnSelect: false, multiple: true, query: this.queryAvailableAxes.bind(this), initSelection: this.initSelection.bind(this) }; const dropdown = initSelect2.init($select, options); dropdown.on('change', () => { this.updateModel(); }); }); }, /** * This methods fetches available axis attributes for the family this family variant is based on. * * The options parameter is the one needed by select2, it contains the 'term' for the search, and the * 'callback' method for the results. * * @param {Object} options */ queryAvailableAxes(options) { const familyCode = this.getFormData().family; clearTimeout(this.queryTimer); this.queryTimer = setTimeout(() => { FetcherRegistry .getFetcher('family') .fetchAvailableAxes(familyCode) .then((attributes) => { const catalogLocale = UserContext.get('catalogLocale'); const attributeResults = this.searchOnResults(options.term, attributes); const entities = _.map(attributeResults, (attribute) => { const label = this.getEntityLabel(attribute, catalogLocale); return { id: attribute.code, text: label }; }); const sortedEntities = _.sortBy(entities, 'text'); options.callback({ results: sortedEntities }); }); }, 400); }, /** * Return all attributes that have a label that match the given term. * * @param {string} term * @param {array} attributes * * @returns {array} */ searchOnResults(term, attributes) { const catalogLocale = UserContext.get('catalogLocale'); term = term.toLowerCase(); return attributes.filter((entity) => { const label = this.getEntityLabel(entity, catalogLocale).toLowerCase(); return -1 !== label.search(term); }); }, /** * Initialize selection of select2 widgets. We need to fetch labels of selected attributes, because we * only have their code. * * @param {Object} element * @param {Function} callback */ initSelection : function (element, callback) { const attributeCodes = element.val().split(','); const catalogLocale = UserContext.get('catalogLocale'); const fetchAttributesPromises = _.map(attributeCodes, (attributeCode) => { return FetcherRegistry.getFetcher('attribute').fetch(attributeCode).promise(); }); $.when.apply($, fetchAttributesPromises) .then(function () { const data = _.map(arguments, (attribute) => { const label = this.getEntityLabel(attribute, catalogLocale); return { id: attribute.code, text: label }; }); callback(data); }.bind(this)); }, /** * Update the family variant model */ updateModel() { const catalogLocale = UserContext.get('catalogLocale'); const axisLevelOne = _.compact(this.$('input[name="axes1"]').val().split(',')); const numberOfLevels = parseInt(this.$('select[name="numberOfLevels"]').val()); let attributeSets = []; attributeSets.push({level: 1, axes: axisLevelOne, attributes: []}); if (numberOfLevels > 1) { const axisLevelTwo = _.compact(this.$('input[name="axes2"]').val().split(',')); attributeSets.push({level: 2, axes: axisLevelTwo, attributes: []}); } let labels = {}; labels[catalogLocale] = this.$('input[name="label"]').val(); this.setData({ code: this.$('input[name="code"]').val(), labels: labels, variant_attribute_sets: attributeSets }); }, /** * Get a new empty family variant object. * * @returns {Object} */ getNewFamilyVariant() { const catalogLocale = UserContext.get('catalogLocale'); return { code: '', labels: { [catalogLocale]: '' }, variant_attribute_sets: [ {level: 1, axes: [], attributes: []} ] }; }, /** * Reset the validation errors of the form. */ resetValidationErrors() { this.validationErrors = []; }, /** * Set the validation errors of the form. */ setValidationErrors(errors) { this.validationErrors = errors; }, /** * Return the label/code of a serialized entity. * * @param {string} entity * @param {string} locale * @returns {string} */ getEntityLabel(entity, locale) { Iif (0 === entity.labels.length || entity.labels[locale] === undefined) { return '[' + entity.code + ']'; } return entity.labels[locale]; } }); } ); |