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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | 45x 45x 55x 55x 55x 38x 38x 38x 55x 55x 185x 185x 185x 130x 377x 377x 377x 377x 377x 18x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 377x 275x 275x 102x 102x 92x 92x 92x 92x 92x 92x 92x 92x 629x 629x 3x 626x 92x 92x 92x 39x 261x 55x 55x 55x 55x 55x 55x 55x 92x 277x 92x 616x 92x 130x 920x 920x 920x 920x 130x 322x 322x 322x 322x 1093x 4222x 322x 2390x 1140x 1140x 2723x 1140x 49x 1091x 322x 322x 2x 2x 14x 2x 2x 348x | 'use strict'; /** * This extension manages the data filter collection and its generation. * * @author Julien Sanchez <julien@akeneo.com> * @author Yohan Blain <yohan.blain@akeneo.com> * @copyright 2016 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/template/export/product/edit/content/data', 'pim/form', 'pim/fetcher-registry', 'pim/form-builder', 'pim/common/property' ], function ( $, _, __, template, BaseForm, fetcherRegistry, formBuilder, PropertyAccessor ) { return BaseForm.extend({ filterViews: [], template: _.template(template), /** * {@inheritdoc} */ initialize: function (config) { this.config = config.config; BaseForm.prototype.initialize.apply(this, arguments); }, /** * {@inheritdoc} */ configure: function () { this.onExtensions('add-attribute:add', function (event) { this.addFilters(event.codes).then(function () { this.updateModel(); this.render(); }.bind(this)); }.bind(this)); this.filterViews = []; return $.when( BaseForm.prototype.configure.apply(this, arguments), this.addConfigFilters() ); }, /** * Add a collection of filters * * @param {array} fieldCodes Can be attributes or product field codes */ addFilters: function (fieldCodes) { // We pre-fetch the attributes to add to avoid multiple http requests return fetcherRegistry.getFetcher('attribute').fetchByIdentifiers(fieldCodes) .then(function () { return $.when.apply($, _.map(fieldCodes, this.addFilter.bind(this))); }.bind(this)) .then(function () { if (!_.isEmpty(this.getFormData())) { this.updateFiltersData(_.extend({}, this.getFilters().data)); } }.bind(this)); }, /** * Add a single filter * * @param {string} fieldCode */ addFilter: function (fieldCode) { var deferred = $.Deferred(); this.getFilterConfig(fieldCode) .then(this.buildFilterView.bind(this)) .then(function (filterView) { this.listenTo(filterView, 'pim_enrich:form:entity:post_update', this.updateModel.bind(this)); this.listenTo(filterView, 'filter:remove', this.removeFilter.bind(this)); this.listenTo(this.getRoot(), 'channel:update:after', function (scope) { filterView.trigger('channel:update:after', scope); }.bind(this)); this.filterViews.push(filterView); }.bind(this)) .always(function () { deferred.resolve(); }); return deferred.promise(); }, /** * Build a filter view * * @param {Object} filterConfig * * @return {Promise} */ buildFilterView: function (filterConfig) { return formBuilder.getFormMeta(filterConfig.view) .then(formBuilder.buildForm) .then(function (filterView) { filterView.setRemovable(filterConfig.isRemovable); filterView.setType(filterConfig.view); filterView.setParentForm(this); filterView.setCode(filterConfig.field); return filterView.configure().then(function () { var data = {}; filterView.trigger('pim_enrich:form:entity:pre_update', data); filterView.setData(data, {silent: true}); return filterView; }); }.bind(this)); }, /** * Get filter configuration for the giver field * * @param {string} fieldCode * * @return {Promise} */ getFilterConfig: function (fieldCode) { var filterConfig = _.findWhere(this.config.filters, {field: fieldCode}); if (undefined !== filterConfig) { filterConfig.isRemovable = false; return $.Deferred().resolve(filterConfig).promise(); } return fetcherRegistry.getFetcher('attribute').fetch(fieldCode) .then(function (attribute) { return { field: attribute.code, view: attribute.filter_types['product-export-builder'], isRemovable: true }; }); }, /** * {@inheritdoc} */ render: function () { Iif (!this.configured || _.isEmpty(this.getFormData())) { return this; } this.$el.html(this.template({__: __})); $.when( fetcherRegistry.getFetcher('attribute').getIdentifierAttribute(), this.addExistingFilters() ).then(function (identifier) { var filtersContainer = this.$('.filters').empty(); var configuredFieldCodes = _.pluck(this.config.filters, 'field'); var savedFieldCodes = _.pluck(this.filterViews, 'filterCode').sort(); var fieldCodes = _.union( configuredFieldCodes, _.without(savedFieldCodes, identifier.code), [identifier.code] ); var filterViews = _.map(fieldCodes, function (code) { var view = _.findWhere(this.filterViews, {filterCode: code}); if (undefined === view) { return; } return view.render().$el; }.bind(this)); filtersContainer.append(filterViews); this.renderExtensions(); }.bind(this)); return this; }, /** * Returns the current filters as an array of fields. * * @return {array} */ getCurrentFilters: function () { return _.map(this.filterViews, function (filterView) { return filterView.getCode(); }); }, /** * Add filters from the configuration (the field filters and identifier) */ addConfigFilters: function () { var promises = []; this.getRoot().trigger('pim_enrich:form:filter:set-default', promises); return $.when.apply($, promises).then(function () { var defaultFieldCodes = 0 !== arguments.length ? _.union(_.flatten(_.toArray(arguments))) : []; var configFilterCodes = _.pluck(this.config.filters, 'field'); return _.union(configFilterCodes, defaultFieldCodes); }.bind(this)) .then(function (defaultFilterCodes) { return this.addFilters(defaultFilterCodes); }.bind(this)); }, /** * Add filter stored in the backend (filters added by the user and saved) */ addExistingFilters: function () { var filterCodes = _.map(_.pluck(this.getFilters().data, 'field'), function (field) { return field.replace(/\.code$/, ''); }); var existingFilterCodes = _.map(this.filterViews, function (filterView) { return filterView.getCode(); }); return this.addFilters(_.difference(filterCodes, existingFilterCodes)); }, /** * Returns default filter fields. They can be set by config or other extensions. * * @returns {Promise} */ getDefaultFilterFields: function () { var promises = []; this.getRoot().trigger('pim_enrich:form:filter:set-default', promises); return $.when.apply($, promises).then(function () { var defaultFields = 0 !== arguments.length ? _.union(_.flatten(_.toArray(arguments))) : []; var configFields = _.pluck(this.config.filters, 'field'); return _.union(configFields, defaultFields); }.bind(this)); }, /** * Update the model of each filter views * * @param {Object} data */ updateFiltersData: function (data) { _.each(this.filterViews, function (filterView) { var filterData = _.findWhere(data, {field: filterView.getField()}); filterData = filterData || {}; filterView.trigger('pim_enrich:form:entity:pre_update', filterData); filterView.setData(filterData, {silent: true}); }.bind(this)); this.updateModel(); }, /** * Updates the form model by iterating over filter views */ updateModel: function () { const data = this.getFormData(); Iif (_.isEmpty(data)) { return; } let dataFilterCollection = PropertyAccessor.accessProperty(data, 'configuration.filters.data', []); // Remove deleted filters dataFilterCollection = dataFilterCollection.filter((filter) => { return this.filterViews.findIndex((filterView) => { return !filterView.isEmpty() && filterView.getFormData().field === filter.field; }) !== -1; }); // Update or add new filters _.each(this.filterViews, function (filterView) { if (!filterView.isEmpty()) { const field = filterView.getFormData().field; const index = dataFilterCollection.findIndex((data) => { return data.field === field; }); if (index === -1) { dataFilterCollection.push(filterView.getFormData()); } else { dataFilterCollection[index] = filterView.getFormData(); } } }); const newData = PropertyAccessor.updateProperty( data, 'configuration.filters.data', dataFilterCollection ); this.setData(newData); }, /** * Removes the filter for the given field then renders the whole view. * * @param {string} fieldCode */ removeFilter: function (fieldCode) { var cleanedFieldCode = fieldCode.replace(/\.code$/, ''); this.filterViews = _.filter(this.filterViews, function (filterView) { return filterView.getCode() !== cleanedFieldCode; }); this.updateModel(); this.render(); }, /** * Get filters * * @return {object} */ getFilters: function () { return this.getFormData().configuration.filters; } }); } ); |