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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 300x 498x 498x 498x 498x 498x 498x 300x 498x 498x 498x 498x 498x 498x 498x 497x 497x 497x 497x 497x 497x 497x 497x 300x 497x 497x 497x 497x 300x 497x 492x 492x 300x 21x 21x 21x 21x 21x 300x 57x 57x 57x 57x 57x 228x 57x 300x 113x 113x 113x 113x 113x 300x 300x 300x 300x 300x 21x 21x 21x 5688x 632x 632x 632x 21x 300x 36x 36x 36x 324x 288x 36x 36x 36x 36x 36x 36x 36x 36x 36x 6x 300x 36x 300x 21x 300x 36x 36x 36x 36x 36x 36x 36x 36x 21x 15x 15x 15x 300x 213x 5717x 213x 5717x 300x 300x 249x 249x 249x 249x 249x 300x 300x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 300x 36x 213x 213x 213x 213x 213x 36x 300x 270x 7192x 7192x 2191x 7192x 648x 9030x 300x 21x 21x 21x 184x 21x 21x 21x 21x 21x 21x 300x 300x | import BaseView = require('pimui/js/view/base'); import * as _ from 'underscore'; import * as Backbone from 'backbone'; const mediator = require('oro/mediator'); const __ = require('oro/translator'); const Routing = require('routing'); const DatagridState = require('pim/datagrid/state'); const buttonTemplate = require('pim/template/datagrid/column-selector/button'); const columnsTemplate = require('pim/template/datagrid/column-selector/columns'); const innerModalTemplate = require('pim/template/datagrid/column-selector/modal'); const selectedTemplate = require('pim/template/datagrid/column-selector/selected'); const modalTemplate = require('pim/template/common/modal-centered'); interface AttributeGroup { code: string; label: string; count: number; } interface Column { name: string; code: string; selected: boolean; removable: boolean; sortOrder: number; displayed: boolean; } interface DatagridCollection extends Backbone.Collection<any> { decodeStateData: (name: string) => any; } class ColumnSelector extends BaseView { public attributeGroupSelector: string; public config: any; public datagridCollection: DatagridCollection; public datagridElement: any; public debounceSearchTimer: any; public loadedAttributeGroups: AttributeGroup[]; public loadedColumns: {[name: string]: Column}; public locale: string; public modal: any; public page: number = 1; public searchInputSelector: string; public hideButton: boolean; private buttonTemplate: ((...data: any[]) => string) = _.template(buttonTemplate); private innerModalTemplate: ((...data: any[]) => string) = _.template(innerModalTemplate); private columnsTemplate: ((...data: any[]) => string) = _.template(columnsTemplate); private selectedTemplate: ((...data: any[]) => string) = _.template(selectedTemplate); private modalTemplate: ((...data: any[]) => string) = _.template(modalTemplate); public events(): Backbone.EventsHash { return { 'click [data-open]': 'openModal', }; } constructor(options: {config: any}) { super({...options, ...{className: 'AknGridToolbar-right'}}); this.loadedAttributeGroups = []; this.loadedColumns = {}; this.searchInputSelector = 'input[type="search"]'; this.attributeGroupSelector = '[data-attributes] [data-group]'; this.config = {...this.config, ...options.config}; } /** * When the grid loads, store the datagrid collection and the locale */ configure() { mediator.once('datagrid_collection_set_after', (datagridCollection: any, datagridElement: any) => { this.datagridCollection = datagridCollection; this.datagridElement = datagridElement; this.hideButton = false === this.datagridElement.data('metadata').options.manageColumns; this.locale = this.getLocale(); this.renderColumnSelector(); }); return BaseView.prototype.configure.apply(this, arguments); } /** * Get the local from the datagrid */ getLocale(): string { const url = (<string>this.datagridCollection.url).split('?')[1]; const urlParams = this.datagridCollection.decodeStateData(url); const datagridParams = urlParams['product-grid'] || {}; return urlParams['dataLocale'] || datagridParams['dataLocale']; } /** * Render the 'columns' button */ renderColumnSelector(): BaseView { if (true === this.hideButton) return this; this.$el.html(this.buttonTemplate({label: __('pim_datagrid.column_configurator.label')})); return this; } /** * Fetch the attribute groups for the columns and cache them */ fetchAttributeGroups(): PromiseLike<AttributeGroup[]> { const url = Routing.generate('pim_datagrid_productgrid_available_columns_groups', {locale: this.locale}); Eif (_.isEmpty(this.loadedAttributeGroups)) { return $.get(url).then((groups: AttributeGroup[]) => { this.loadedAttributeGroups = groups; return groups; }); } return new Promise(resolve => resolve(this.loadedAttributeGroups)); } /** * Fetch the columns with these possible parameters: * * search - The current search term * attribute_group - The current selected attribute group * locale - The datagrid locale * page - The page number (can be reset) * */ fetchColumns(reset?: boolean): PromiseLike<{[name: string]: Column}> { const search = this.modal.$el .find(this.searchInputSelector) .val() .trim(); const group = this.modal.$el.find('.active[data-group]').data('value'); const url = Routing.generate('pim_datagrid_productgrid_available_columns'); Iif (true === reset) { this.page = 1; } const params = $.param( _.omit( { search, attribute_group: group, page: this.page, locale: this.locale, }, (param: any) => !param ) ); return $.get(`${url}?${params}`); } /** * Add the properties used by the front to the loaded columns */ normalizeColumn(column: Column) { const storedColumn = this.loadedColumns[column.code]; column.selected = false; Iif (storedColumn) { column.selected = storedColumn.selected; column.sortOrder = storedColumn.sortOrder; } column.removable = true; return column; } /** * Fetch the first page of columns and merge them with the selected ones */ fetchColumnsWithSelected() { this.fetchColumns(true).then(columns => { this.loadedColumns = Object.assign( _.mapObject(columns, this.normalizeColumn.bind(this)), this.getColumnsBySelected() ); this.renderColumns(); }); } /** * When an attribute group is clicked, set it as active and trigger a fetch */ filterByAttributeGroup(event: JQueryEventObject): void { this.modal.$el.find(this.attributeGroupSelector).removeClass('active'); $(event.currentTarget).addClass('active'); this.fetchColumnsWithSelected(); } /** * Clear the search input and trigger a fetch */ clearSearch() { this.modal.$el .find(this.searchInputSelector) .val('') .trigger('keyup'); } /** * Executes the search after a timeout */ debounceSearch(event: JQueryEventObject): void { if (null !== this.debounceSearchTimer) { clearTimeout(this.debounceSearchTimer); } if (27 === event.keyCode) { return this.clearSearch(); } if (13 === event.keyCode) { this.fetchColumnsWithSelected(); } else { this.debounceSearchTimer = setTimeout(this.fetchColumnsWithSelected.bind(this), 300); } } /** * Get the selected columns from the datagrid metadata and merge them with the passed ones, keeping the selected state */ setColumnsSelectedByDefault(columns: {[name: string]: Column}) { const metadataColumns = this.datagridElement.data('metadata').columns; const datagridColumns: {[name: string]: Column} = {}; _.each(Object.assign(columns, metadataColumns), (column: Column) => { const columnNames = metadataColumns.map((column: Column) => column.name); const label = column.name || column.code; const data = {code: label, selected: columnNames.includes(label), sortOrder: columnNames.indexOf(label), removable: true}; datagridColumns[label] = Object.assign(column, data); }); return datagridColumns; } /** * Render the two sections for searchable columns and the selected ones */ renderColumns(): void { const unSelectedColumns = this.getColumnsBySelected(false); const selectedColumns = this.getColumnsBySelected(); this.modal.$el .find('[data-columns]') .empty() .append(this.columnsTemplate({columns: unSelectedColumns})); const sortedColumns = _.map(selectedColumns, (column: Column) => column).sort((a: Column, b: Column) => { return a.sortOrder - b.sortOrder; }); this.modal.$el .find('[data-columns-selected]') .empty() .append(this.selectedTemplate({columns: sortedColumns})); this.modal.$el.on('click', '#column-selection .action', this.unselectColumn.bind(this)); this.setSortable(); this.setValidation(); this.listenToListScroll(); const scrollColumn = this.modal.$el.find('[data-columns]'); const scrollHeight = scrollColumn.get(0).scrollHeight; const columnHeight = scrollColumn.outerHeight(); if (scrollHeight === columnHeight) { this.fetchNextColumns(true); } } /** * Listen to the scroll for the searchable columns */ listenToListScroll() { this.modal.$el .find('[data-columns]') .off('scroll') .on('scroll', this.fetchNextColumns.bind(this)); } /** * Stop listening to the scroll event */ stopListeningToListScroll() { this.modal.$el .find('[data-columns]') .removeClass('more') .off(); } /** * Handle the infinite scroll - if the user scrolls to the bottom of the page, trigger * loading the next page of results */ fetchNextColumns(IloadNextPage: boolean = false): void { const list: any = this.modal.$el.find('[data-columns]').get(0); const scrollPosition = Math.max(0, list.scrollTop); const bottomPosition = list.scrollHeight - list.offsetHeight; const isBottom = bottomPosition === scrollPosition; Eif (isBottom || true === loadNextPage) { this.page = this.page + 1; this.fetchColumns().then((columns: {[name: string]: Column}) => { if (_.isEmpty(columns)) { return this.stopListeningToListScroll(); } const mergedColumns = _.mapObject(columns, this.normalizeColumn.bind(this)); this.loadedColumns = Object.assign(this.loadedColumns, mergedColumns); this.renderColumns(); }); } } /** * Set all columns selected to true or false given a matching code */ setColumnStatus(code: string, Iselected = true): void { this.loadedColumns = _.mapObject(this.loadedColumns, (column: Column) => { if (column.code === code) { column.selected = selected; } return column; }); } /** * When the user clicks 'x' on a selected column, move it to the correct list and * display the validation error if necessary */ unselectColumn(event: JQueryEventObject): void { const column = $(event.currentTarget).parent(); const code = $(event.currentTarget) .parents('[data-value]') .data('value'); column.appendTo(this.modal.$el.find('#column-list')); this.setColumnStatus(code, false); this.setValidation(); } /** * When the selected column list is empty show the validation error */ setValidation(): void { const selectedColumns = this.getColumnsBySelected(); const showValidationError = _.isEmpty(selectedColumns); const error = this.modal.$el.find('#column-selection .alert-error'); Iif (showValidationError) { return error.show(); } return error.hide(); } /** * Set all the columns selected to false when the user clicks on 'Clear' */ clearAllColumns(): void { this.loadedColumns = _.mapObject(this.loadedColumns, (column: Column) => { column.selected = false; return column; }); this.renderColumns(); } /** * Fetch the attribute groups, open the modal and start listening to the search/click events */ openModal(): void { this.page = 1; Iif (this.modal) { this.modal.$el.off(); this.modal.close(); this.modal.remove(); } this.fetchAttributeGroups().then(groups => { const modal = new (<any>Backbone).BootstrapModal({ okCloses: false, title: __('pim_datagrid.column_configurator.title'), innerDescription: __('pim_datagrid.column_configurator.description'), okText: __('pim_common.apply'), template: this.modalTemplate, innerClassName: 'AknFullPage--full', content: this.innerModalTemplate({ groups, attributeGroupsLabel: __('pim_enrich.entity.attribute_group.plural_label'), }), }); modal.open(); modal.on('ok', this.saveColumnsToDatagridState.bind(this)); this.modal = modal; this.modal.$el.on('keyup search', this.searchInputSelector, this.debounceSearch.bind(this)); this.modal.$el.on('click', this.attributeGroupSelector, this.filterByAttributeGroup.bind(this)); this.modal.$el.on('click', '.reset', this.clearAllColumns.bind(this)); this.fetchColumns().then((columns: {[name: string]: Column}) => { this.loadedColumns = this.setColumnsSelectedByDefault(columns); this.renderColumns(); }); }); } /** * Set the selected columns list as sortable and save the sort order */ setSortable(): void { this.modal.$el .find('#column-list, #column-selection') .sortable({ connectWith: '.connected-sortable', containment: this.modal.$el, tolerance: 'pointer', cursor: 'move', cancel: 'div.alert', receive: (_: any, ui: any) => { const code = ui.item.data('value'); const senderIsColumn = ui.sender.is('#column-list'); this.setColumnStatus(code, senderIsColumn); this.setColumnSortOrder(); this.setValidation(); }, }) .disableSelection(); this.setColumnSortOrder(); } /** * Get the column sort order from the DOM element and apply it to the loaded columns */ setColumnSortOrder(): void { this.loadedColumns = _.mapObject(this.loadedColumns, (column: Column) => { const sortOrder = this.modal.$el.find(`#column-selection [data-value="${column.code}"]`).index(); if (sortOrder > -1) { column.sortOrder = sortOrder; } return column; }); } /** * Get a list of the selected columns (from all the loaded ones) */ getColumnsBySelected(selected = true): {[name: string]: Column} { return _.pick(this.loadedColumns, (column: Column) => column.selected === selected); } /** * Save the list of selected columns (and their sort order) to the datagrid and reload the page */ saveColumnsToDatagridState(): void { this.setColumnSortOrder(); const columns = this.getColumnsBySelected(); const selected = Object.keys(columns) .sort((a, b) => { return columns[a].sortOrder - columns[b].sortOrder; }) .join() .trim(); Iif (!selected.length) { return; } DatagridState.set('product-grid', 'columns', selected); this.modal.close(); var url = window.location.hash; (<any>Backbone.history).fragment = new Date().getTime(); Backbone.history.navigate(url, true); } } export = ColumnSelector; |