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 | 39x 39x 39x 37x 39x 241x 241x 241x 241x 352x 352x 88x 88x 88x 264x 264x 352x 352x 352x 23x 23x 23x 23x 88x 88x 88x 3x 3x 3x 24x 2x 2x 1x 1x 22x 4x 4x 4x 24x 24x 24x 22x 22x 22x 22x 22x 11x 11x 2x 2x 2x 2x 191x 303x 303x 303x 303x 608x 608x 303x 303x 303x 48x 191x 191x 133x 39x 37x 37x 37x 37x 37x 37x 37x 37x 85x 85x 85x 85x 85x 202x 202x 85x 14x 85x 37x 37x 85x 85x 85x 85x 37x 37x 37x 37x 37x 241x 241x 39x 202x 241x 241x 241x 241x 241x 85x 85x 241x 241x 241x 88x 88x 3x 88x 88x 23x 2x 2x 4x 4x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 178x 3x 175x 85x 85x 52x 33x 11x 11x 11x 36x 11x 130x 65x 65x 65x 65x 39x 37x 37x | define( [ 'jquery', 'underscore', 'backbone', 'oro/translator', 'routing', 'oro/mediator', 'oro/loading-mask', 'pim/dialog', 'pim/template/attribute-option/index', 'pim/template/attribute-option/edit', 'pim/template/attribute-option/show', 'jquery-ui' ], function ( $, _, Backbone, __, Routing, mediator, LoadingMask, Dialog, indexTemplate, editTemplate, showTemplate ) { 'use strict'; var AttributeOptionItem = Backbone.Model.extend({ defaults: { code: '', optionValues: {} } }); var ItemCollection = Backbone.Collection.extend({ model: AttributeOptionItem, initialize: function (options) { this.url = options.url; } }); var EditableItemView = Backbone.View.extend({ tagName: 'tr', className: 'AknGrid-bodyRow editable-item-row', showTemplate: _.template(showTemplate), editTemplate: _.template(editTemplate), events: { 'click .show-row': 'stopEditItem', 'click .edit-row': 'startEditItem', 'click .delete-row': 'deleteItem', 'click .update-row': 'updateItem', 'keyup input': 'soil', 'keydown': 'cancelSubmit' }, editable: false, parent: null, loading: false, locales: [], initialize: function (options) { this.locales = options.locales; this.parent = options.parent; this.model.urlRoot = this.parent.updateUrl; this.render(); }, render: function () { var template = null; if (this.editable) { this.clean(); this.$el.addClass('in-edition'); template = this.editTemplate; } else { this.$el.removeClass('in-edition'); template = this.showTemplate; } this.$el.html(template({ item: this.model.toJSON(), locales: this.locales })); this.$el.attr('data-item-id', this.model.id); return this; }, showReadableItem: function () { this.editable = false; this.parent.showReadableItem(this); this.clean(); this.render(); }, showEditableItem: function () { this.editable = true; this.render(); this.model.set(this.loadModelFromView().attributes); }, startEditItem: function () { var rowIsEditable = this.parent.requestRowEdition(this); Eif (rowIsEditable) { this.showEditableItem(); } }, stopEditItem: function () { if (!this.model.id || this.dirty) { Eif (this.dirty) { Dialog.confirm( __('pim_enrich.entity.attribute_option.module.edit.cancel_description'), __('pim_enrich.entity.attribute_option.module.edit.cancel_title'), function () { this.showReadableItem(this); Iif (!this.model.id) { this.deleteItem(); } }.bind(this)); } else { if (!this.model.id) { this.deleteItem(); } else { this.showReadableItem(); } } } else { this.showReadableItem(); } }, deleteItem: function () { var itemCode = this.el.firstChild.innerText; Dialog.confirmDelete( __('pim_enrich.entity.fallback.module.delete.item_placeholder', {'itemName': itemCode}), __('pim_enrich.entity.fallback.module.delete.title', {'itemName': itemCode}), function () { this.parent.deleteItem(this); }.bind(this), __('pim_enrich.entity.attribute.plural_label') ); }, updateItem: function () { this.inLoading(true); var editedModel = this.loadModelFromView(); editedModel.save( {}, { url: this.model.url(), success: function () { this.inLoading(false); this.model.set(editedModel.attributes); this.clean(); this.stopEditItem(); if (!this.parent.sortable) { this.parent.render(); } else { this.parent.updateSorting(); } }.bind(this), error: this.showValidationErrors.bind(this) } ); }, showValidationErrors: function (data, xhr) { this.inLoading(false); var response = xhr.responseJSON; Eif (response.code) { this.$el.find('.validation-tooltip') .attr('data-original-title', response.code) .removeClass('AknIconButton--hide') .tooltip('destroy') .tooltip('show'); } else { Dialog.alert( __('alert.attribute_option.error_occured_during_submission'), __('pim_enrich.entity.attribute_option.flash.update.fail') ); } }, cancelSubmit: function (e) { Iif (e.keyCode === 13) { this.updateItem(); return false; } }, loadModelFromView: function () { var attributeOptions = {}; var editedModel = this.model.clone(); editedModel.urlRoot = this.model.urlRoot; _.each(this.$el.find('.attribute-option-value'), function (input) { var locale = input.dataset.locale; attributeOptions[locale] = { locale: locale, value: input.value, id: this.model.get('optionValues')[locale] ? this.model.get('optionValues')[locale].id : null }; }.bind(this)); editedModel.set('code', this.$el.find('.attribute_option_code').val()); editedModel.set('optionValues', attributeOptions); return editedModel; }, inLoading: function (loading) { this.parent.inLoading(loading); }, soil: function () { Eif (JSON.stringify(this.model.attributes) !== JSON.stringify(this.loadModelFromView().attributes)) { this.dirty = true; } else { this.dirty = false; } }, clean: function () { this.dirty = false; } }); var ItemCollectionView = Backbone.View.extend({ tagName: 'table', className: 'AknGrid AknGrid--unclickable table attribute-option-view', template: _.template(indexTemplate), events: { 'click .option-add': 'addItem' }, $target: null, locales: [], sortable: true, sortingUrl: '', updateUrl: '', currentlyEditedItemView: null, itemViews: [], rendered: false, initialize: function (options) { this.$target = options.$target; this.collection = new ItemCollection({url: options.updateUrl}); this.locales = options.locales; this.updateUrl = options.updateUrl; this.sortingUrl = options.sortingUrl; this.sortable = options.sortable; this.render(); this.load(); }, render: function () { this.$el.empty(); this.currentlyEditedItemView = null; this.updateEditionStatus(); this.$el.html(this.template({ locales: this.locales, add_option_label: __('pim_enrich.entity.product.module.attribute.add_option'), code_label: __('pim_common.code') })); _.each(_.sortBy(this.collection.models, function (attributeOptionItem) { return this.sortable ? 0 : attributeOptionItem.attributes.code; }.bind(this)), function (attributeOptionItem) { this.addItem({item: attributeOptionItem}); }.bind(this)); if (0 === this.collection.length) { this.addItem(); } if (!this.rendered) { this.$target.html(this.$el); this.rendered = true; } this.setSortable(); this.updateSortableStatus(this.sortable); return this; }, setSortable: function() { this.$el.sortable({ items: 'tbody tr', handle: '.handle', axis: 'y', connectWith: this.$el, containment: this.$el, distance: 5, cursor: 'move', helper: function (e, ui) { ui.children().each(function () { $(this).width($(this).width()); }); return ui; }, stop: function () { this.updateSorting(); }.bind(this) }); }, load: function () { this.itemViews = []; this.inLoading(true); this.collection .fetch({ success: function () { this.inLoading(false); this.render(); }.bind(this) }); }, addItem: function (opts) { var options = opts || {}; //If no item model provided we create one var itemToAdd; if (!options.item) { itemToAdd = new AttributeOptionItem(); } else { itemToAdd = options.item; } var newItemView = this.createItemView(itemToAdd); Eif (newItemView) { this.$el.children('tbody').append(newItemView.$el); } }, createItemView: function (item) { var itemView = new EditableItemView({ model: item, url: this.updateUrl, locales: this.locales, parent: this }); //If the item is new the view is changed to edit mode if (!item.id) { Iif (!this.requestRowEdition(itemView)) { return; } else { itemView.showEditableItem(); } } this.collection.add(item); this.itemViews.push(itemView); return itemView; }, requestRowEdition: function (attributeOptionRow) { Iif (this.currentlyEditedItemView) { if (this.currentlyEditedItemView.dirty) { Dialog.alert(__('alert.attribute_option.save_before_edit_other')); return false; } else { this.currentlyEditedItemView.stopEditItem(); this.currentlyEditedItemView = null; this.updateEditionStatus(); } } if (attributeOptionRow.model.id) { this.currentlyEditedItemView = attributeOptionRow; } this.updateEditionStatus(); return true; }, showReadableItem: function (item) { if (item === this.currentlyEditedItemView) { this.currentlyEditedItemView = null; this.updateEditionStatus(); } }, deleteItem: function (item) { this.inLoading(true); item.model.destroy({ success: function () { this.inLoading(false); this.collection.remove(item); this.currentlyEditedItemView = null; this.updateEditionStatus(); Iif (0 === this.collection.length) { this.addItem(); item.$el.hide(0); } else Iif (!item.model.id) { item.$el.hide(0); } else { item.$el.hide(500); } }.bind(this), error: function (data, response) { this.inLoading(false); var message; Eif (response.responseJSON) { message = response.responseJSON.message; } else { message = response.responseText; } Dialog.alert(message, __('pim_enrich.entity.attribute_option.flash.delete.fail')); }.bind(this) }); }, updateEditionStatus: function () { if (this.currentlyEditedItemView) { this.$el.addClass('in-edition'); } else { this.$el.removeClass('in-edition'); } }, updateSortableStatus: function (sortable) { this.sortable = sortable; if (sortable) { this.$el.sortable('enable'); } else { this.$el.sortable('disable'); } }, updateSorting: function () { var sorting = []; var rows = this.$el.find('tbody tr'); for (var i = rows.length - 1; i >= 0; i--) { sorting[i] = rows[i].dataset.itemId; } $.ajax({ url: this.sortingUrl, type: 'PUT', data: JSON.stringify(sorting) }); }, inLoading: function (loading) { if (loading) { var loadingMask = new LoadingMask(); loadingMask.render().$el.appendTo(this.$el); loadingMask.show(); } else { this.$el.find('.loading-mask').remove(); } } }); return function ($element) { var itemCollectionView = new ItemCollectionView( { $target: $element, updateUrl: Routing.generate( 'pim_enrich_attributeoption_index', {attributeId: $element.data('attribute-id')} ), sortingUrl: Routing.generate( 'pim_enrich_attributeoption_update_sorting', {attributeId: $element.data('attribute-id')} ), locales: $element.data('locales'), sortable: $element.data('sortable') }); mediator.on('attribute:auto_option_sorting:changed', function (autoSorting) { itemCollectionView.setSortable(); itemCollectionView.updateSortableStatus(!autoSorting); itemCollectionView.render(); }.bind(this)); }; } ); |