view.js
6.83 KB
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
const loading = require('../../plugin/loading'),
tip = require('plugin/tip'),
lazyLoad = require('yoho-jquery-lazyload');
require('../../common');
import {EventEmitter, api} from './store';
import appBridge from 'yoho-app';
const LeaveMSGView = function(elem) {
this.$elem = $(elem);
this.$input = this.$elem.find('.leave-text');
this.$doc = $(document);
this.bindEvents();
};
LeaveMSGView.prototype = $.extend({}, EventEmitter.prototype, {
/**
* 绑定事件
* 1. 提交按钮事件
**/
bindEvents() {
let self = this;
this.$elem
.on('click', '.chat-page-back', function() {
self.toggleHide(true);
})
.on('click.LeavseMSG.submit', '.leave-submit', function() {
self.submit();
});
this.on('show.LeaveMSGView', $.proxy(this.toggleHide, this, false));
},
/**
* method: show/hide 留言
* @param boolean show;
*/
toggleHide(willHide) {
this.$elem.toggleClass('chat-page-hide', willHide);
return this;
},
/**
* 提交留言的hanlder
*/
submit() {
let self = this;
let content = $.trim(this.$input.val());
if (!content) {
return;
}
api.leaveMsg(content)
.done(function() {
self.trigger('save.LeaveMSGView', '留言成功,');
})
.fail(function() {
self.trigger('save.LeaveMSGView', '留言失败');
});
self.toggleHide(true);
}
});
LeaveMSGView.prototype.constructer = LeaveMSGView;
/*
评价view
*/
const RatingView = function(elem, socket) {
this.elem = $(elem);
this.$ranks = this.elem.find('.stars i');
this.$label = this.elem.find('.rank');
this.rank = 3;
this.bindEvents();
this.socket = socket;
};
RatingView.prototype = $.extend({}, EventEmitter.prototype, {
bindEvents() {
const self = this;
this.elem
.on('click.RatingView.rating', '.stars i', function(event) {
let starVal = $(this).index();
self.rating(starVal);
})
.on('click.RatingView.close', '.close', $.proxy(this.toggle, this, false));
},
rating(starVal) {
const rankMap = {
0: '非常不满意',
1: '不满意',
2: '一般',
3: '满意',
4: '非常满意',
};
this.rank = starVal;
this.rankText = rankMap[starVal] || rankMap[3];
this.$label.text(this.rankText);
this.$ranks.removeClass('rated');
this.$ranks.toggleClass(index => {
return index <= starVal ? 'rated' : null;
});
},
post(data) {
const self = this,
elem = this.elem;
var params = {
stars: ++this.rank,
promoter: 1,
reasonMsg: elem.find('.words-to-say').val(),
};
Object.assign(params, data);
api.saveEvalute(params)
.done(res => {
if (res && res.code === 200) {
elem.hide();
elem.trigger('rating-success', [self.rankText]);
return;
}
tip.show('评价失败');
})
.fail(()=> {
tip.show('评价失败');
});
},
toggle(willShow) {
this.elem.toggle(willShow);
this.rating(4);
}
});
RatingView.prototype.constructer = RatingView;
const OrderListView = function(elem) {
this.$elem = $(elem);
this.orderListT = require('service/chat/orders.hbs');
this.$localOrders = this.$elem.find('[data-tab=local]');
this.$globalOrders = this.$elem.find('[data-tab=global]');
// 分页数据
this.state = {
global: {
curCount: 0,
totalCount: null
},
order: {
curCount: 0,
totalCount: null
}
};
this.bindEvents();
// this.fetchOrders();
};
OrderListView.prototype = $.extend({}, EventEmitter.prototype, {
bindEvents: function() {
const self = this;
this.$elem
.on('click.OrderListView.back', '.chat-page-back', function() {
self.toggleHide(true);
})
.on('click.OrderListView.select', '[data-action=select]', function() {
self.selectOrder($(event.target));
});
// 被通知显示,执行显示
this.on('show.OderListView', (event, type) => {
self.toggleHide(false, type);
});
},
/**
* method: 选择订单,然后关闭
*/
selectOrder: function($btn) {
let $order = $btn.closest('.order-page');
// 订单数据
let data = {
imgSrc: $order.find('.thumb').attr('src'),
orderCode: $order.find('[data-id]').attr('data-id'),
cost: $order.find('.sum-cost').text(),
createTime: $order.find('.js-create-time').val(),
orderStatus: $order.find('.order-status').text()
};
// 通知chat,订单列表已选择订单
this.trigger('selectOrder.OrderListView', [data]);
// 关闭
this.toggleHide(true);
},
/**
* method: show/hide 订单列表
* @param boolean willHide;
* @return this
*/
toggleHide: function(willHide, type) {
this.$elem.toggleClass('chat-page-hide', willHide);
if (!willHide) {
let orderState = this.state[type];
let {totalCount} = orderState;
totalCount === null && this.fetchOrderByType(type, orderState);
if (type === 'global') {
this.$globalOrders.show();
this.$localOrders.hide();
} else if (type === 'order') {
this.$globalOrders.hide();
this.$localOrders.show();
}
} else {
this.$globalOrders.hide();
this.$localOrders.hide()
}
return this;
},
fetchOrderByType: function(type, orderState) {
let self = this;
let $order = type === 'global' ? this.$globalOrders : this.$localOrders;
let {totalCount} = orderState;
// TODO: 分页
if (totalCount === null) { // 还未加载数据
api.fetchOrders(type).done(
result => {
let html = self.orderListT({
orders: result.data,
isApp: appBridge.isApp
});
orderState.totalCount = result.data.length;
orderState.curCount = result.data.length;
$order.append(html);
}
);
}
}
});
OrderListView.prototype.constructer = OrderListView;
export {RatingView, LeaveMSGView, OrderListView};