1 | define(function(require) { |
||
2 | 'use strict'; |
||
3 | |||
4 | var AddProductsAction; |
||
5 | var MassAction = require('oro/datagrid/action/mass-action'); |
||
6 | var mediator = require('oroui/js/mediator'); |
||
7 | var _ = require('underscore'); |
||
8 | |||
9 | /** |
||
10 | * Add products to shopping list |
||
11 | * |
||
12 | * @export oro/datagrid/action/add-products-mass-action |
||
13 | * @class oro.datagrid.action.AddProductsAction |
||
14 | * @extends oro.datagrid.action.MassAction |
||
15 | */ |
||
16 | AddProductsAction = MassAction.extend({ |
||
17 | shoppingLists: null, |
||
18 | |||
19 | /** |
||
20 | * @inheritDoc |
||
21 | */ |
||
22 | constructor: function AddProductsAction() { |
||
23 | AddProductsAction.__super__.constructor.apply(this, arguments); |
||
24 | }, |
||
25 | |||
26 | /** |
||
27 | * @inheritDoc |
||
28 | */ |
||
29 | initialize: function(options) { |
||
0 ignored issues
–
show
|
|||
30 | AddProductsAction.__super__.initialize.apply(this, arguments); |
||
31 | |||
32 | this.datagrid.on('action:add-products-mass:shopping-list', this._onAddProducts, this); |
||
33 | }, |
||
34 | |||
35 | /** |
||
36 | * @inheritDoc |
||
37 | */ |
||
38 | dispose: function() { |
||
39 | this.datagrid.off(null, null, this); |
||
40 | return AddProductsAction.__super__.dispose.apply(this, arguments); |
||
41 | }, |
||
42 | |||
43 | /** |
||
44 | * Override function to change URL |
||
45 | * |
||
46 | * @inheritDoc |
||
47 | */ |
||
48 | _handleWidget: function() { |
||
49 | if (this.dispatched) { |
||
50 | return; |
||
51 | } |
||
52 | this.frontend_options = this.frontend_options || {}; |
||
53 | this.frontend_options.url = this.getLink(); |
||
54 | this.frontend_options.title = this.frontend_options.title || this.label; |
||
55 | |||
56 | require(['oro/' + this.frontend_handle + '-widget'], _.bind(this._createHandleWidget, this)); |
||
57 | }, |
||
58 | |||
59 | _createHandleWidget: function(WidgetType) { |
||
60 | var widget = new WidgetType(this.frontend_options); |
||
61 | widget.render(); |
||
62 | |||
63 | var datagrid = this.datagrid; |
||
64 | var selectionState = datagrid.getSelectionState(); |
||
65 | |||
66 | widget.on('formSave', _.bind(function(response) { |
||
67 | datagrid.resetSelectionState(selectionState); |
||
68 | this.listenToOnce(datagrid.massActions, 'reset', function() { |
||
69 | this._onSaveHandleWidget(response, datagrid); |
||
70 | }); |
||
71 | }, this)); |
||
72 | }, |
||
73 | |||
74 | _onSaveHandleWidget: function(response, datagrid) { |
||
75 | datagrid.trigger('action:add-products-mass:shopping-list', response.savedId); |
||
76 | }, |
||
77 | |||
78 | _onAddProducts: function(shoppingListId) { |
||
79 | if (this.route_parameters.shoppingList === shoppingListId) { |
||
80 | this.run({}); |
||
81 | } |
||
82 | }, |
||
83 | |||
84 | /** |
||
85 | * Get action parameters |
||
86 | * |
||
87 | * @returns {Object} |
||
88 | * @private |
||
89 | */ |
||
90 | getActionParameters: function() { |
||
91 | var selectionState = this.datagrid.getSelectionState(); |
||
92 | var collection = this.datagrid.collection; |
||
93 | var stateKey = collection.stateHashKey(); |
||
94 | |||
95 | var unitsAndQuantities = {}; |
||
96 | _.each(selectionState.selectedIds, function(productModel) { |
||
97 | var attributes = productModel.attributes; |
||
98 | unitsAndQuantities[attributes.sku] = {}; |
||
99 | unitsAndQuantities[attributes.sku][attributes.unit] = attributes.quantity; |
||
100 | }); |
||
101 | |||
102 | var selectedIds = _.map(selectionState.selectedIds, function(productModel) { |
||
103 | return productModel.id; |
||
104 | }); |
||
105 | |||
106 | var params = { |
||
107 | inset: selectionState.inset ? 1 : 0, |
||
108 | values: selectedIds.join(','), |
||
109 | units_and_quantities: JSON.stringify(unitsAndQuantities) |
||
110 | }; |
||
111 | |||
112 | params[stateKey] = collection.stateHashValue(); |
||
113 | params = collection.processFiltersParams(params, null, 'filters'); |
||
114 | |||
115 | return params; |
||
116 | }, |
||
117 | |||
118 | _handleAjax: function() { |
||
119 | if (this.dispatched) { |
||
120 | return; |
||
121 | } |
||
122 | |||
123 | mediator.execute('showLoading'); |
||
124 | this._doAjaxRequest(); |
||
125 | }, |
||
126 | |||
127 | _onAjaxSuccess: function(data, textStatus, jqXHR) { |
||
128 | var datagrid = this.datagrid; |
||
129 | |||
130 | var models = _.map(data.products, function(product) { |
||
131 | return datagrid.collection.get(product.id); |
||
132 | }); |
||
133 | |||
134 | datagrid.resetSelectionState(); |
||
135 | |||
136 | mediator.trigger('shopping-list:line-items:update-response', models, data); |
||
137 | mediator.execute('hideLoading'); |
||
138 | } |
||
139 | }); |
||
140 | |||
141 | return AddProductsAction; |
||
142 | }); |
||
143 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.