-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjquery.textpager.js
More file actions
232 lines (214 loc) · 11.2 KB
/
jquery.textpager.js
File metadata and controls
232 lines (214 loc) · 11.2 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
/**
*
* Text Pager - jQuery plugin for create pages on div
* Copyright (c) 2014 Dmitrij Waśkowski
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* https://github.com/dwaskowski/jquery_textpager
*
* Version: 1.1.0
*
*/
!function(e, t, o, n) {
e.fn.textpager = function(options) {
var parent = $('<div>').insertBefore($(this));
$(this).css('overflow','hidden').appendTo($(parent));
var eOptions = {
controlArrows: (typeof(options) !== "undefined" && options !== null && typeof(options.controlArrows) !== "undefined" && options.controlArrows !== null) ? options.controlArrows : '',
controlArrowsEnabel: (typeof(options) !== "undefined" && options !== null && typeof(options.controlArrowsEnabel) !== "undefined" && options.controlArrowsEnabel !== null) ? options.controlArrowsEnabel : true,
controlPages: (typeof(options) !== "undefined" && options !== null && typeof(options.controlPages) !== "undefined" && options.controlPages !== null) ? options.controlPages : '',
controlPagesEnabel: (typeof(options) !== "undefined" && options !== null && typeof(options.controlPagesEnabel) !== "undefined" && options.controlPagesEnabel !== null) ? options.controlPagesEnabel : true,
controlPagesContent: (typeof(options) !== "undefined" && options !== null && typeof(options.controlPagesContent) !== "undefined" && options.controlPagesContent !== null) ? options.controlPagesContent : 'div'
};
var fulltextHeight = $(this).prop('scrollHeight'),
textareaHeight = $(this).height(),
textareaWidth = $(this).width();
parseContent = function(parent, textareaHeight, fulltextHeight) {
var contentHtml = $(parent).html(),
html = $.parseHTML(contentHtml),
pages = 1;
// parser
childrenChecker = function(html) {
$.each(html, function(i, el) {
//console.log(boxNum+': '+el.nodeName.toLowerCase());
//console.log(el.attributes);
//console.log(el.childNodes);
//console.log(el.nodeValue);
var attributes = el.attributes,
nodeName = el.nodeName.toLowerCase();
if (nodeName == 'div') {
divLevel++;
divAttrs[divLevel-1] = attributes;
var newTmpPoint = $('<div>');
if (attributes) {
$.each(attributes, function(i, attr) {
newTmpPoint.attr(attr.name, attr.value);
});
}
newTmpPoint.appendTo(tmpBox);
tmpBox = newTmpPoint;
childrenChecker(el.childNodes);
var newTmpPoint = tmpBox.parent();
tmpBox = newTmpPoint;
divLevel--;
return;
}
var nodeClone = $(el).clone();
tmpBox.append(nodeClone);
if (tmpBox.height() > textareaHeight) {
nodeClone.detach();
if (divLevel > 0) {
for(var iter=0; iter<divLevel; iter++) {
var newTmpPoint = tmpBox.parent();
tmpBox = newTmpPoint;
}
}
tmpBox.addClass('tp-page-one').css('height',textareaHeight+'px').css('width',textareaWidth+'px');
pages++;
tmpBox = $('<div>').appendTo($(parent));
if (divLevel > 0) {
for(var iter=0; iter<divLevel; iter++) {
var newTmpPoint = $('<div>');
if (divAttrs[iter]) {
$.each(divAttrs[iter], function(i, attr) {
newTmpPoint.attr(attr.name, attr.value);
});
}
newTmpPoint.appendTo(tmpBox);
tmpBox = newTmpPoint;
}
}
tmpBox.append(nodeClone);
}
});
}
$(parent).html('');
var boxNum = 1,
divLevel = 0,
divAttrs = [],
tmpBox = $('<div>').appendTo($(parent));
childrenChecker(html);
tmpBox.addClass('tp-page-one').css('height',textareaHeight+'px').css('width',textareaWidth+'px');
return pages;
}
if(textareaHeight<fulltextHeight){
var self = this;
var pageNow = 1,
margTop = 0,
pages = parseContent(self, textareaHeight, fulltextHeight);
if (eOptions.controlArrows==='') {
$('<div>').addClass('tp-control-arrows').appendTo($(parent));
eOptions.controlArrows = $(parent).find('.tp-control-arrows');
$('<a>').addClass('tp-control-arrow-left').addClass('unactive').html('<span><</span>').appendTo($(eOptions.controlArrows));
$('<a>').addClass('tp-control-arrow-right').html('<span>></span>').appendTo($(eOptions.controlArrows));
}
if (eOptions.controlPages==='') {
$('<div>').addClass('tp-control-pages').appendTo($(parent));
eOptions.controlPages = $(parent).find('.tp-control-pages');
}
var controlElements = '';
for (var pageCount=0;pageCount<pages;pageCount++) {
controlElements += $('<'+eOptions.controlPagesContent+'>')
.attr('data-page',''+(pageCount+1))
.html('<span>'+(pageCount+1)+'</span>')
.addClass('tp-page')
.addClass(!pageCount ? 'active' : '')
.prop("outerHTML");
}
$(eOptions.controlPages).html(controlElements);
$(this).css('height',textareaHeight+'px').css('padding',0);
var contentHtml = $(this).html();
$(this).html('');
$('<div>').addClass('tp-horizontalbox').css('height',textareaHeight+'px').css('width',textareaWidth+'px').appendTo($(this));
$('<div>').addClass('tp-vertivalbox').html(contentHtml).css('width',textareaWidth+'px').appendTo($(this).find('.tp-horizontalbox'));
$(eOptions.controlArrows).find('.tp-control-arrow-left').unbind('click').click(function(){
var thisPage = pageNow-1;
if(thisPage<1){
return;
}
if(thisPage==1){
$(this).addClass('unactive');
}
$(eOptions.controlArrows).find('.tp-control-arrow-right').removeClass('unactive');
var nextPage = (thisPage-pageNow)*textareaHeight;
margTop -= nextPage;
pageNow = thisPage;
$(eOptions.controlPages).find(eOptions.controlPagesContent+'.tp-page').removeClass('active');
$(eOptions.controlPages).find(eOptions.controlPagesContent+'[data-page="'+thisPage+'"]').addClass('active');
self.animateStep(self, textareaWidth, margTop, false);
});
$(eOptions.controlArrows).find('.tp-control-arrow-right').unbind('click').click(function(){
var thisPage = pageNow+1;
if(thisPage>pages){
return;
}
if(thisPage==pages){
$(this).addClass('unactive');
}
$(eOptions.controlArrows).find('.tp-control-arrow-left').removeClass('unactive');
var nextPage = (thisPage-pageNow)*textareaHeight;
margTop -= nextPage;
pageNow = thisPage;
$(eOptions.controlPages).find(eOptions.controlPagesContent+'.tp-page').removeClass('active');
$(eOptions.controlPages).find(eOptions.controlPagesContent+'[data-page="'+thisPage+'"]').addClass('active');
self.animateStep(self, textareaWidth, margTop, true);
});
$(eOptions.controlPages).find(eOptions.controlPagesContent+'.tp-page').unbind('click').click(function(){
var thisPage = $(this).data('page');
if(thisPage===pageNow) {
return;
}
var goAhead = true;
if(thisPage<pageNow) {
goAhead = false;
}
if (thisPage==1) {
$(eOptions.controlArrows).find('.tp-control-arrow-left').addClass('unactive');
$(eOptions.controlArrows).find('.tp-control-arrow-right').removeClass('unactive');
} else if(thisPage==pages) {
$(eOptions.controlArrows).find('.tp-control-arrow-right').addClass('unactive');
$(eOptions.controlArrows).find('.tp-control-arrow-left').removeClass('unactive');
} else {
$(eOptions.controlArrows).find('.tp-control-arrow-right').removeClass('unactive');
$(eOptions.controlArrows).find('.tp-control-arrow-left').removeClass('unactive');
}
var nextPage = (thisPage-pageNow)*textareaHeight;
margTop -= nextPage;
pageNow = thisPage;
$(eOptions.controlPages).find(eOptions.controlPagesContent+'.tp-page').removeClass('active');
$(this).addClass('active');
self.animateStep(self, textareaWidth, margTop, goAhead);
});
}
this.animateStep = function(parent, textareaWidth, margTop, goAhead){
$(parent).find('.tp-horizontalbox')
.animate({
marginLeft: (goAhead ? "-="+textareaWidth : "+="+textareaWidth)
}, 400, function(){
$(this)
.css({
marginLeft: (goAhead ? "+="+(textareaWidth*2) : "-="+(textareaWidth*2))
})
.find('.tp-vertivalbox')
.css({
marginTop: margTop
});
$(this)
.animate({
marginLeft: (goAhead ? "-="+textareaWidth : "+="+textareaWidth)
}, 400)
.find('.tp-vertivalbox')
.animate({
opacity: 1
}, 400);
})
.find('.tp-vertivalbox')
.animate({
opacity: 0
}, 400);
}
};
}(jQuery, window, document);