-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
126 lines (117 loc) · 4.16 KB
/
main.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
'use strict';
;(function(){
var App = {
findElements: function(){
this.content = document.getElementById('content').children[0];
this.fileInput = document.getElementById('file');
this.fakeContent = document.getElementById('fake');
this.prevBtn = document.getElementById('prev');
this.nextBtn = document.getElementById('next');
this.pageInput = document.getElementById('set-page');
this.pagination = document.getElementById('pagination');
this.reader = new FileReader();
this.textRegex = /(<([^>]+)>)/ig;
},
setSettings: function(){
this.currentPageNum = 0;
this.charSize = {
height: this.fakeContent.offsetHeight,
width: this.fakeContent.offsetWidth
};
},
attachEvents: function(){
var self = this;
this.fileInput.addEventListener('change', function(){
if(this.files && this.files[0]){
self.reader.readAsText(this.files[0], 'utf-8');
}
}, false);
this.reader.addEventListener('load', function(e){
this.originalText = e.target.result.replace(this.textRegex, '')/*.replace(/\s{2,}/g, ' ')*/.split(' ');
this.copiedText = this.originalText.slice(0);
this.calculatePageSize();
}.bind(this), false);
this.reader.addEventListener('error', function(){
alert('Failed to open Book');
}, false);
this.prevBtn.addEventListener('click', function(e){
if(this.currentPageNum > 0){
this.currentPageNum--;
this.switchPage();
}
}.bind(this), false);
this.nextBtn.addEventListener('click', function(e){
if(this.pagesSize && this.currentPageNum < this.pagesSize - 1){
this.currentPageNum++;
this.switchPage();
}
}.bind(this), false);
this.pageInput.addEventListener('keyup', function(e){
if(e.keyCode === 13){
console.log(Math.min(Math.min(this.value.replace(/[^\d,]/g, '') - 1, 0), self.pagesSize - 1))
return;
self.currentPageNum = Math.min(Math.min(this.value.replace(/[^\d,]/g, '') - 1, 0), self.pagesSize - 1);
self.switchPage();
}
}, false);
window.addEventListener('resize', function(){
if(this.originalText){
this.copiedText = this.originalText.slice(0);
this.calculatePageSize();
}
}.bind(this), false);
},
calculatePageSize: function(){
this.contentHeight = this.content.offsetHeight;
this.contentWidth = this.content.offsetWidth;
this.rowsSize = Math.floor(this.contentHeight/this.charSize.height);
this.colsSize = Math.floor(this.contentWidth/this.charSize.width);
this.formatingText();
},
updatePagination: function(){
this.pagination.innerHTML = (this.currentPageNum + 1) + '/' + this.pagesSize;
},
switchPage: function(){
this.content.innerHTML = this.formatedText[this.currentPageNum].join('');
this.getLastPosition();
this.updatePagination();
},
formatingText: function(){
this.formatedText = [[]];
var arrIndex = 0;
var counter = 0;
var rowCount = 1;
var arr = this.formatedText[arrIndex];
while(this.copiedText.length){
var word = this.copiedText.splice(0, 1)[0] + ' ';
counter += word.length;
//console.log(counter, colsSize, word, word.length)
if(counter - 1 > this.colsSize){
counter = word.length;
rowCount++;
//console.log(counter, rowCount, 'Column', word)
if(rowCount > this.rowsSize){
rowCount = 1;
//console.log( counter, '----Row----')
this.formatedText.push([]);
arrIndex++;
arr = this.formatedText[arrIndex];
}
}
arr.push(word + ' ');
}
this.pagesSize = this.formatedText.length;
this.switchPage();
},
getLastPosition: function(){
console.log(this.formatedText[this.currentPageNum])
console.log(this.formatedText.indexOf(this.formatedText[this.currentPageNum][0]))
},
init: function(){
this.findElements();
this.setSettings();
this.attachEvents();
}
}
App.init();
})();