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