forked from culturebase/cbui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoreless.js
More file actions
144 lines (133 loc) · 4.6 KB
/
moreless.js
File metadata and controls
144 lines (133 loc) · 4.6 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
/* This file is part of cbui.
* Copyright © 2010-2012 stiftung kulturserver.de ggmbh <github@culturebase.org>
*
* cbui is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cbui is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cbui. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Frame to show more or less content and associated "more" and "less" buttons
* depending on length of content. This is a frame where you can place all kinds
* of buttons, titles, annotations and other things. It should contain one
* element with the class "CbUiMoreLessContent" which will react to the buttons.
*/
jQuery.CbWidget.moreLess = jQuery.CbWidget.frame.extend({
handleMore : function() {
jQuery('.__CbUiMoreButton', this.element()).hide();
return this;
},
handleLess : function() {
jQuery('.__CbUiLessButton', this.element()).hide();
return this;
},
maxLength : function(max) {
if (typeof(max) != 'undefined') this.max_length = max;
return this.max_length;
},
handleReady : function() {
var self = this;
if (this.hidden_content.length == 0) {
jQuery('.__CbUiMoreButton', this.element()).hide();
}
jQuery('.__CbUiMoreButton', this.element()).click(function() {
self.more();
});
jQuery('.__CbUiLessButton', this.element()).click(function() {
self.less();
});
this.base();
return this.less();
}
}, {
init : function() {
/**
* Show full version of the content.
*/
jQuery.CbEvent(this, 'more');
/**
* Show abbreviated version of the content.
*/
jQuery.CbEvent(this, 'less');
return this.base();
}
});
/**
* More/Less frame for text. Abbreviated text will be shown with a trailing
* '...'. All text is expected to be placed directly in '.CbUiMoreLessContent'.
* Maximum length refers to number of characters here.
*/
jQuery.CbWidget.moreLessText = jQuery.CbWidget.moreLess.extend({
constructor : function(element) {
this.maxLength(jQuery.CbWidget.moreLessText.default_max_length);
this.hidden_content = '';
return this.base(element);
},
handleMore : function() {
var content = jQuery('.__CbUiMoreLessContent', this.element());
if (this.hidden_content != '') {
content.html(this.hidden_content);
this.hidden_content = '';
}
if (content.text().length > this.maxLength()) {
jQuery('.__CbUiLessButton', this.element()).show();
}
return this.base();
},
handleLess : function() {
if (this.hidden_content.length) return this;
var content = jQuery('.__CbUiMoreLessContent', this.element());
if (content.text().length > this.maxLength()) {
this.hidden_content = content.html();
content.text(content.text().substr(0, this.maxLength()) + '...');
jQuery('.__CbUiMoreButton', this.element()).show();
}
return this.base();
}
}, {
default_max_length : 250
});
/**
* More/Less frame for nested elements. Maximum length refers to
* CbUiMoreLessContent's number of children here.
*/
jQuery.CbWidget.moreLessElements = jQuery.CbWidget.moreLess.extend({
constructor : function(element) {
this.maxLength(jQuery.CbWidget.moreLessElements.default_max_length);
this.hidden_content = [];
return this.base(element);
},
handleMore : function() {
var content = jQuery('.__CbUiMoreLessContent', this.element());
content.append(this.hidden_content);
this.hidden_content = [];
if (content.children().length > this.maxLength()) {
jQuery('.__CbUiLessButton', this.element()).show();
}
return this.base();
},
handleLess : function() {
if (this.hidden_content.length) return this;
var length = 0;
var self = this;
jQuery('.__CbUiMoreLessContent', this.element()).children().each(function(i, child) {
if (++length > self.maxLength()) {
self.hidden_content.push(jQuery(child).remove()[0]);
}
});
if (length > this.maxLength()) {
jQuery('.__CbUiMoreButton', self.element()).show();
}
return this.base();
}
}, {
default_max_length : 3
});