-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.js
141 lines (108 loc) · 3.37 KB
/
books.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
/**
* Created by asrmamrna on 23.09.2014.
*/
function booksFormatter() {
var body = document.getElementsByTagName('body')[0];
var text = document.getElementsByClassName('books');
//do it for every book
for (var textNumber = 0; textNumber<text.length; textNumber++){
body.appendChild(book());
// get the book to fill with text
var currentBook = body.lastChild;
//get text of a book
var bookText = text[textNumber].innerHTML;
//set index from which start getting words
var countFrom = 0;
//this is a new text for a book
var newText = '';
//set the first page for the book. This page is temporal.
currentBook.appendChild(page());
var phantomPage = currentBook.getElementsByClassName('page')[0];
//create first page in book. This page is permanent.
currentBook.appendChild(page());
//get all words before last ' ' in text;
while (newText.length<bookText.lastIndexOf(' ')){
// get div of current page
var currentPage = currentBook.lastChild;
//get untouched part of text for book
var booferText = bookText.substr(newText.length, bookText.length - newText.length);
//get the index of the nearest ' '
var counter = booferText.indexOf(' ');
var newWord = booferText.substr(countFrom, counter);
//set everything before nearest ' ' and add ' ' after it.
newText += newWord + ' ';
textSetter(newWord, currentPage, phantomPage, currentBook);
//alert the result of a book word by word
// alert(newText);
}
pagePosition(currentBook);
currentBook.removeChild(phantomPage);
// add the last word
//
//
//
//
// currentBook.innerHTML = (newText + bookText.substr(bookText.lastIndexOf(' ')+1, bookText.length-(bookText.lastIndexOf(' ')+1)));
}
}
////////////////////////////////////////////
//
// New Book
//
////////////////////////////////////////////
function book(){
var newBook = document.createElement('div');
newBook.style.position = 'fixed';
newBook.style.top = '60px';
newBook.style.left = '20px';
newBook.style.width = '800px';
newBook.style.height = '300px';
newBook.style.zIndex = 20;
newBook.style.border = 'dashed green 2px';
newBook.style.transition = '1s';
newBook.className = 'newBook';
return newBook;
}
/////////////////////////////
//
// New Page
//
/////////////////////////////
function page() {
var newPage = document.createElement('div');
newPage.className = 'page';
return newPage
}
/////////////////////////////////
//
// Set new word into book's page
//
/////////////////////////////////
function textSetter (word, currentPage, phantomPage, currentBook) {
phantomPage.innerHTML = currentPage.innerHTML + word + ' ';
if (phantomPage.offsetHeight < 298){
currentPage.innerHTML+= word + ' ';
}
else{
currentBook.appendChild(page());
var newPage = currentBook.lastChild;
newPage.innerHTML = word + ' ';
phantomPage.innerHTML = word + ' ';
}
}
//////////////////////////////////////
//
// Set position for pages in book
//
//////////////////////////////////////
function pagePosition(book) {
//get current book
var children = book.childNodes;
var positioner = 1;
for (var pages = 0; pages<children.length; pages++){
if (positioner>1)
positioner = 0;
children[pages].style.left = (positioner * children[pages].offsetWidth) + 'px';
positioner++;
}
}