This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/
router.js
126 lines (108 loc) · 2.95 KB
/
router.js
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
var POS = require('lib/utilities/global');
var Router = require('lib/config/router');
var LayoutView = require('./layout');
var Products = require('./products/route');
var CartRoute = require('./cart/route');
var CheckoutRoute = require('./checkout/route');
var ReceiptRoute = require('./receipt/route');
var Radio = require('backbone.radio');
var bb = require('backbone');
var POSRouter = Router.extend({
routes: {
'(cart)(/:id)(/)' : 'showCart',
'checkout(/:id)(/)': 'showCheckout',
'receipt(/:id)(/)' : 'showReceipt'
},
initialize: function(options) {
this.container = options.container;
this.channel.reply({
'show:cart' : this.showCart,
'show:checkout' : this.showCheckout,
'show:receipt' : this.showReceipt,
'add:to:cart' : this.addToCart
}, this);
},
onBeforeEnter: function() {
this.layout = new LayoutView();
this.container.show(this.layout);
this.initOrders();
},
initOrders: function(){
if(this.orders){ return; }
// attach orders
this.orders = Radio.request('entities', 'get', {
type: 'collection',
name: 'orders'
});
// listen to order collection
this.listenTo(this.orders, {
'add' : this.addOrder,
'remove' : this.removeOrder
});
},
onBeforeRoute: function(){
//this.setActiveTab();
this.showProducts();
Radio.request('header', 'update:title', '');
},
setActiveTab: function(){
var tab = bb.history.getFragment() === '' ? 'left' : 'right';
this.container.tabs.setActive(tab);
},
showProducts: function(){
if( this.layout.getRegion('left').hasView() ){
return;
}
var products = new Products({
container : this.layout.getRegion('left')
});
products.enter();
},
showCart: function() {
return new CartRoute({
container : this.layout.getRegion('right'),
collection: this.orders
});
},
showCheckout: function() {
return new CheckoutRoute({
container : this.layout.getRegion('right'),
collection: this.orders
});
},
showReceipt: function() {
var autoPrint = Radio.request('entities', 'get', {
type: 'option',
name: 'auto_print'
});
return new ReceiptRoute({
container : this.layout.getRegion('right'),
collection: this.orders,
autoPrint : autoPrint
});
},
/**
* Add to cart only when cart route is active
*/
addToCart: function(options){
if(this._currentRoute instanceof CartRoute){
this.orders.addToCart(options);
}
},
addOrder: function(order){
if(this._currentRoute instanceof CartRoute){
//bb.history.navigate('cart/' + order.id);
if(order.isEditable()){
this.execute(this.showCart, [order.id]);
}
}
},
removeOrder: function(){
if(this._currentRoute instanceof CartRoute){
bb.history.navigate('');
this.execute(this.showCart);
}
}
});
module.exports = POSRouter;
POS.attach('POSApp.Router', POSRouter);