Workspace action addition and List actions for Issue to Shipping
[xtuple] / lib / enyo-x / source / views / transaction_list_container.js
1 /*jshint bitwise:true, indent:2, curly:true, eqeqeq:true, immed:true,
2 latedef:true, newcap:true, noarg:true, regexp:true, undef:true,
3 trailing:true, white:true, strict:false*/
4 /*global XT:true, XM:true, XV:true, _:true, enyo:true */
5
6 (function () {
7
8   /**
9     Expected to a have a parameter widget that contains an order and
10     a transaction date.
11
12     @name XV.TransactionListContainer
13     @extends XV.SearchContainer
14    */
15   var transactionListContainer =  /** @lends XV.TransactionListContainer# */ {
16     name: "XV.TransactionListContainer",
17     kind: "Panels",
18     classes: "app enyo-unselectable",
19     arrangerKind: "CollapsingArranger",
20     published: {
21       prerequisite: "",
22       notifyMessage: "",
23       list: null,
24       actions: null,
25       transactionDate: null,
26       model: null,
27       issueAll: null
28     },
29     events: {
30       onPrevious: "",
31       onWorkspace: ""
32     },
33     handlers: {
34       onListItemMenuTap: "showListItemMenu",
35       onParameterChange: "requery",
36       onProcessingChanged: "processingChanged",
37       onSelectionChanged: "selectionChanged"
38     },
39     init: false,
40     components: [
41       {name: "parameterPanel", kind: "FittableRows", classes: "left",
42         components: [
43         {kind: "onyx.Toolbar", classes: "onyx-menu-toolbar", components: [
44           {kind: "onyx.Button", name: "backButton", content: "_back".loc(), ontap: "close"},
45           {kind: "onyx.MenuDecorator", style: "margin: 0;",
46             onSelect: "actionSelected", components: [
47             {kind: "XV.IconButton", src: "/assets/menu-icon-gear.png",
48               content: "_actions".loc(), name: "actionButton"},
49             {kind: "onyx.Menu", name: "actionMenu"}
50           ]}
51         ]},
52         {kind: "Scroller", name: "parameterScroller"}
53       ]},
54       {name: "listPanel", kind: "FittableRows", components: [
55         // the onyx-menu-toolbar class keeps the popups from being hidden
56         {kind: "onyx.MoreToolbar", name: "contentToolbar",
57           classes: "onyx-menu-toolbar", movedClass: "xv-toolbar-moved", components: [
58           {kind: "onyx.Grabber", classes: "left-float"},
59           {name: "rightLabel", content: "_search".loc(), classes: "left-float"},
60           {name: "space", fit: true},
61           {kind: "onyx.Button", name: "printButton", showing: false,
62             content: "_print".loc(), onclick: "print"},
63           {kind: "onyx.Button", name: "refreshButton", disabled: false,
64             content: "_refresh".loc(), onclick: "requery"},
65           {kind: "onyx.Button", name: "postButton",
66             disabled: true, classes: "save", showing: false,
67             content: "_post".loc(), onclick: "post"},
68           {name: "listItemMenu", kind: "onyx.Menu", floating: true,
69             onSelect: "listActionSelected", maxHeight: 500}
70         ]},
71         {name: "contentPanels", kind: "Panels", margin: 0, fit: true, draggable: false,
72           panelCount: 0},
73         {kind: "onyx.Popup", name: "spinnerPopup", centered: true,
74             modal: true, floating: true, scrim: true,
75             onHide: "popupHidden", components: [
76           {kind: "onyx.Spinner"},
77           {name: "spinnerMessage", content: "_processing".loc() + "..."}
78         ]}
79       ]}
80     ],
81     actionSelected: function (inSender, inEvent) {
82       var action = inEvent.originator.action,
83         method = action.method || action.name;
84
85       this[method](inSender, inEvent);
86     },
87     close: function () {
88       this.doPrevious();
89     },
90     buildMenu: function () {
91       if (!this.getActions()) {
92         return;
93       }
94       var actionMenu = this.$.actionMenu,
95         actions = this.getActions().slice(0),
96         that = this;
97
98       // reset the menu
99       actionMenu.destroyClientControls();
100
101       // then add whatever actions are applicable
102       _.each(actions, function (action) {
103         var name = action.name,
104           prerequisite = action.prerequisite,
105           isDisabled = prerequisite ? !that[prerequisite]() : false;
106         actionMenu.createComponent({
107           name: name,
108           kind: XV.MenuItem,
109           content: action.label || ("_" + name).loc(),
110           action: action,
111           disabled: isDisabled
112         });
113
114       });
115       actionMenu.render();
116       this.$.actionButton.setShowing(actions.length);
117     },
118     create: function () {
119       this.inherited(arguments);
120       var disabled = !XT.session.privileges.get("AlterTransactionDates"),
121         parameterWidget;
122       this.setList({list: this.getList()});
123       parameterWidget = this.$.parameterWidget;
124       parameterWidget.$.transactionDate.$.input.setDisabled(disabled);
125       if (!this.getActions()) {
126         this.setActions([]);
127       }
128       this.buildMenu();
129     },
130     fetch: function (options) {
131       if (!this.init) { return; }
132       options = options ? _.clone(options) : {};
133       var list = this.$.list,
134         query,
135         parameterWidget,
136         parameters;
137       if (!list) { return; }
138       query = list.getQuery() || {};
139       parameterWidget = this.$.parameterWidget;
140       parameters = parameterWidget && parameterWidget.getParameters ?
141         parameterWidget.getParameters() : [];
142       options.showMore = _.isBoolean(options.showMore) ?
143         options.showMore : false;
144
145       // Build conditions
146       if (parameters.length) {
147         query.parameters = parameters;
148       } else {
149         delete query.parameters;
150       }
151       list.setQuery(query);
152       list.fetch(options);
153     },
154     /**
155       Capture order changed and transaction date changed events.
156       Depends on a very specific implementation of parameter widget
157       that includes `order` and `transactionDate` parameters.
158     */
159     parameterChanged: function (inSender, inEvent) {
160       var originator = inEvent ? inEvent.originator : false,
161         name = originator ? originator.name : false,
162         that = this,
163         options,
164         value;
165
166       if (name === "transactionDate") {
167         value = originator.$.input.getValue();
168         value = XT.date.applyTimezoneOffset(value, true);
169         value = XT.date.toMidnight(value);
170         this.setTransactionDate(value);
171         this.buildMenu();
172         return;
173       } else if (name === "order") {
174         value = originator.getParameter().value;
175         this.setModel(value);
176         this.buildMenu();
177       } else if (name === "shipment") {
178         return;
179       }
180
181       options = {
182         success: function () {
183           that.selectionChanged();
184         }
185       };
186       this.fetch(options);
187     },
188     popupHidden: function (inSender, inEvent) {
189       if (!this._popupDone) {
190         inEvent.originator.show();
191       }
192     },
193     processingChanged: function (inSender, inEvent) {
194       if (inEvent.isProcessing) {
195         this.spinnerShow();
196       } else {
197         this.requery();
198         this.spinnerHide();
199       }
200     },
201     /**
202       Overload: Piggy back on existing handler for `onParameterChanged event`
203       by forwarding this requery to `parameterChanged`.
204     */
205     requery: function (inSender, inEvent) {
206       this.parameterChanged(inSender, inEvent);
207       return true;
208     },
209     /**
210       Whenever a user makes a selection, rebuild the menu
211       and set the transaction date on the selected models
212       to match what has been selected here.
213     */
214     selectionChanged: function () {
215       this.transactionDateChanged();
216       this.buildMenu();
217     },
218     /**
219       @param {Object} Options
220       @param {String} [options.list] Class name
221     */
222     setList: function (options) {
223       var component,
224       list = options.list;
225
226       component = this.createComponent({
227         name: "list",
228         container: this.$.contentPanels,
229         kind: list,
230         fit: true
231       });
232       this.$.rightLabel.setContent(component.label);
233       if (component) {
234         this.createComponent({
235           name: "parameterWidget",
236           classes: "xv-groupbox xv-parameter",
237           showSaveFilter: false,
238           showLayout: false,
239           defaultParameters: null,
240           container: this.$.parameterScroller,
241           kind: component.getParameterWidget(),
242           memoizeEnabled: false,
243           fit: true
244         });
245       }
246
247       this.init = true;
248       this.render();
249     },
250     spinnerHide: function () {
251       this._popupDone = true;
252       this.$.spinnerPopup.hide();
253     },
254     spinnerShow: function () {
255       this._popupDone = false;
256       this.$.spinnerPopup.show();
257     },
258     transactionDateChanged: function () {
259       var transDate = this.getTransactionDate(),
260         collection = this.$.list.getValue(),
261         i;
262
263       // Update the transaction dates on all models to match
264       // What has been selected
265       for (i = 0; i < collection.length; i++) {
266         collection.at(i).transactionDate = transDate;
267       }
268     }
269   };
270
271   enyo.mixin(transactionListContainer, XV.ListMenuManagerMixin);
272   enyo.kind(transactionListContainer);
273
274 }());