forked from alswl/shiu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappui.js
416 lines (362 loc) · 9.81 KB
/
appui.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* global iScroll */
// AppUi
(function () {
'use strict';
var AppUi = {
//类成员
$heroUnit: $('#hero_unit'),
$alert: $('#alert'),
$temp: $('#temp'),
CHAPTER_SELECTOR: '#content .chapter',
$chapter: null, // 章 dom节点,修改后需要重新载入
SCREEN_WIDTH: 320, // 默认宽度
SCREEN_HEIGHT: 480, // 默认高度
SCREEN_PADDING: 8,
SCREEN_BOTTOM_HEIGHT: 10,
SCREEN_COLUMN_GAP: 12, // 默认分栏页面间隔
init: function (app) {
var self = this;
self.app = app;
self.setScreen();
},
// 浏览模式
displayHeroUnit: function () {
this.$heroUnit.show();
},
displayDownload: function () {
this.$heroUnit.find('.book_desc').css('height', '120px');
$('#download').show();
},
displayStandalone: function () {
var self = this,
p = window.shiu.ui;
$("body").bind('touchmove', function (e) { // 静止触摸反弹
e.preventDefault();
});
self.sidebar = Object.create(p.Sidebar).init('#sidebar', self);
self.buttons = Object.create(p.Buttons).init('#sidebar .buttons', self);
self.content = Object.create(window.shiu.ui.Content).init('#content', self);
self.$heroUnit.one('click', function () {
self.app.startRead();
self.content.bindScroll(); // 延迟绑定
});
},
displayRead: function () {
this.$heroUnit.hide();
$('#main').show();
},
// 设定横屏 / 适应尺寸 // TODO
setScreen: function (isOrientation) {
isOrientation = isOrientation || false;
var x = window.screen.width,
y = document.body.scrollHeight;
if (isOrientation) {
this.SCREEN_WIDTH = y;
this.SCREEN_HEIGHT = x;
} else {
this.SCREEN_WIDTH = x;
this.SCREEN_HEIGHT = y;
}
//this.setPage(0);
},
// 更新下载百分比
updateDownloadPercent: function (percent) {
$("#download .percent").text(percent);
},
// 下载完成
updateDownloadComplete: function () {
this.updateDownloadPercent(100);
$("#download .tip").text("下载完成");
$("#download .complete").show();
},
alert: function (message, level, delay) {
var self = this;
if (level === 'disable') {
self.$alert.hide();
self.$alert.children().html('').removeClass();
return;
}
self.$alert.show();
self.$alert.children().html(message).addClass(level);
if (delay === undefined) {
$('body').one('click', function () {
self.$alert.hide();
self.$alert.children().html('').removeClass();
});
} else {
setTimeout(function () {
self.$alert.hide();
self.$alert.children().html('').removeClass();
}, 3000);
}
},
setChapter: function (html) {
this.content.setChapter(html);
},
// 设定当前页面到指定页数
setPage: function (page) {
this.content.setPage(page);
},
// 获取章节总页码
getPageCount: function () {
return this.content.getPageCount();
}
};
window.shiu.ui.AppUi = AppUi;
}());
// 侧边栏
(function () {
'use strict';
var Sidebar = {
init: function (selector, ui) {
var self = this;
self.$ = $(selector);
self.ui = ui;
self.$indexs = $('#indexs');
self.initIndexs();
self.bindTouchStart();
self.bindAClick();
$('#index_wrapper').css('height', (self.ui.SCREEN_HEIGHT) + 'px');
return self;
},
initIndexs: function () {
var self = this;
self.ui.$temp.html(self.ui.app.book.getIndexsHtml());
self.$indexs.append(self.ui.$temp.children());
},
// 目录滚动
bindIndexsScroll: function () {
this.iScroll = new iScroll('index_wrapper', {});
},
fadeX: function (x) {
this.$.css('-webkit-transform', 'translate3d(' + x + 'px, 0, 0)');
this.$.css('-moz-transform', 'translate3d(' + x + 'px, 0, 0)');
},
hide: function () { // TODO 设计成分状态隐藏
this.fadeX(0);
this.$.css('background', 'none');
},
show: function () {
if (this.iScroll === undefined) {
this.bindIndexsScroll(); // 延迟绑定
}
this.fadeX(320);
this.$.css('background', 'transparent');
},
isVisiable: function () {
return this.$.offset().left >= 0;
},
toggle: function () {
if (this.isVisiable()) {
this.hide();
} else {
this.show();
}
},
bindTouchStart: function () {
var self = this;
self.$.bind('touchstart', function (e) {
if (e.touches[0].clientX - self.$.offset().left > 280) {
self.hide();
return false;
}
});
},
bindAClick: function () {
var self = this;
self.$.find('#indexs li a').click(function (e) {
self.ui.app.book.setCurrentChapterIndex(parseInt(this.rel, 10));
self.ui.app.setChapter();
self.hide();
});
}
};
window.shiu.ui.Sidebar = Sidebar;
}());
// 菜单按钮
(function () {
'use strict';
var Buttons = {
init: function (selector, ui) {
var self = this;
self.$ = $(selector);
self.$index = $(selector).children('.index_btn');
self.$fontZi = $(selector).children('.font_zi_btn');
self.$fontZo = $(selector).children('.font_zo_btn');
self.ui = ui;
self.bindTouchStart();
return self;
},
hide: function () {
this.ui.sidebar.hide();
},
show: function () {
this.ui.sidebar.fadeX(40);
},
isVisiable: function () {
// Mozilla 这时 left 会误判
return this.ui.sidebar.$.offset().left >= -280;
},
toggle: function () {
if (this.isVisiable()) {
this.hide();
} else {
this.show();
}
},
bindTouchStart: function () {
var self = this;
self.$index.bind('touchstart', function (e) {
self.ui.sidebar.toggle();
e.stopPropagation();
return false;
});
self.$fontZi.bind('touchstart', function (e) {
self.ui.content.incFontSize();
e.stopPropagation();
return false;
});
self.$fontZo.bind('touchstart', function (e) {
self.ui.content.decFontSize();
e.stopPropagation();
return false;
});
},
bindDragEnd: function () { // TODO 暂时不用
var self = this;
self.$index.ondragend = function (e) {
self.ui.sidebar.show();
};
}
};
window.shiu.ui.Buttons = Buttons;
}());
// 书籍内容模块 Content
(function () {
'use strict';
var Content = {
CHAPTER_SELECTOR: '#content .chapter',
init: function (selector, ui) {
var self = this;
self.ui = ui;
self.$ = $(selector);
self.$chapter = $(selector).find('.chapter');
self.isTouchEvent = false;
self.isTouchLock = false;
self.$.css('height', self.ui.SCREEN_HEIGHT + 'px');
self.bindClick();
return self;
},
bindClick: function () {
var self = this;
self.$.click(function (e) {
var x = e.clientX,
y = e.clientY;
if (!self.isTouchEvent) {
if (x < self.ui.SCREEN_WIDTH * 0.25) {
self.ui.app.prePage();
self.ui.buttons.hide();
} else if (x > self.ui.SCREEN_WIDTH * 0.75) {
self.ui.app.nextPage();
self.ui.buttons.hide();
} else {
self.ui.buttons.toggle();
}
}
});
},
bindScroll: function () {
var self = this;
self.$.bind('touchstart', function (e) {
self.startX = e.touches[0].pageX;
self.startLeft = self.$chapter.offset().left;
});
self.$.bind('touchmove', function (e) {
if (self.isTouchLock) { // 锁住
return false;
}
self.isTouchLock = true;
if (e.touches.length > 1) {
return false;
}
self.transformX(self.startLeft
- self.startX + e.touches[0].pageX);
self.isTouchLock = false;
//console.log('move: ' + e.touches[0].pageX);
});
self.$.bind('touchend', function (e) {
var offset = e.changedTouches[0].pageX - self.startX;
self.isTouchEvent = true;
if (offset < 0 && Math.abs(offset) >= 60) {
if (!self.ui.app.nextPage()) { // 判断是否有下一章
self.transformX(self.startLeft);
}
} else if (offset > 0 && Math.abs(offset) >= 60) {
if (!self.ui.app.prePage()) {
self.transformX(self.startLeft);
}
} else {
if (Math.abs(offset) < 10) { // 单击事件
self.isTouchEvent = false;
}
self.transformX(self.startLeft); // 回退
}
});
self.$.bind('touchcancel', function (e) {
self.transformX(self.startLeft);
return false;
});
},
transformX: function (x) {
this.$chapter.css('-webkit-transform', 'translate3d(' +
x + 'px, 0, 0)'); // 硬件加速
this.$chapter.css('-moz-transform', 'translate3d(' +
x + 'px, 0, 0)');
},
setChapter: function (html) {
var self = this;
self.ui.app.info('载入中…', true);
self.ui.$temp.html(html);
self.$.children().remove();
self.$.append(self.ui.$temp.children());
self.$chapter = $(self.CHAPTER_SELECTOR);
self.$chapter.css('height',
(self.ui.SCREEN_HEIGHT - self.ui.SCREEN_BOTTOM_HEIGHT) + 'px');
self.$chapter.css('-webkit-column-width', self.ui.SCREEN_WIDTH + 'px');
self.$chapter.children('p').css('font-size', self.ui.app.book.getFontSize());
},
setPage: function (page) {
var self = this,
left = -(page * (self.ui.SCREEN_WIDTH + self.ui.SCREEN_COLUMN_GAP));
self.transformX(left);
},
getPageCount: function () {
return ((this.$chapter.find('.end').offset().left - this.$chapter.children().first().offset().left))
/ (this.ui.SCREEN_WIDTH + this.ui.SCREEN_COLUMN_GAP)
+ 1;
},
incFontSize: function () {
var self = this,
size = parseInt(self.$chapter.children('p').css('font-size'), 10);
if (size < 32) {
self.$chapter.children('p').css('font-size', (size + 2) + 'px');
}
self.ui.app.book.setPageCount(self.getPageCount());
self.ui.app.book.setFontSize(size);
},
decFontSize: function () {
var self = this,
size = parseInt(self.$chapter.children('p').css('font-size'), 10);
if (size > 12) {
self.$chapter.children('p').css('font-size', (size - 2) + 'px');
}
self.ui.app.book.setPageCount(self.getPageCount());
self.ui.app.book.setFontSize(size);
// 过载保护
if (self.ui.app.book.getPageCount() <= self.ui.app.book.getCurrentPage()) {
self.ui.app.prePage();
}
}
};
window.shiu.ui.Content = Content;
}());