-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.infinitescroll.plugin.php
More file actions
219 lines (184 loc) · 8.67 KB
/
class.infinitescroll.plugin.php
File metadata and controls
219 lines (184 loc) · 8.67 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
<?php
class InfiniteScrollPlugin extends Gdn_Plugin {
public function discussionController_render_before($sender) {
if (!$sender->data('Page') || !$this->enabled('Discussion')) {
return;
}
$countComments = $sender->data('Discussion')->CountComments;
$totalPages = calculateNumberOfPages($countComments, Gdn::config('Vanilla.Comments.PerPage', 30));
$sender->addDefinition('InfiniteScroll.InDiscussion', true);
$sender->addDefinition('InfiniteScroll.CountItems', $countComments + 1);
$sender->addDefinition('InfiniteScroll.Page', $sender->data('Page'));
$sender->addDefinition('InfiniteScroll.TotalPages', $totalPages);
$sender->addDefinition('InfiniteScroll.PerPage', (int)Gdn::config('Vanilla.Comments.PerPage', 30));
$sender->addDefinition('InfiniteScroll.Url', $sender->data('Discussion')->Url);
$this->resources($sender);
$this->buildNavigation($sender, $countComments + 1, $totalPages);
}
public function discussionsController_render_before($sender) {
if (strtolower($sender->RequestMethod) == 'index' && $this->enabled('DiscussionList')) {
$this->prepareDiscussionList($sender);
$sender->addDefinition('InfiniteScroll.Url', url('discussions', true));
$this->resources($sender);
}
}
public function categoriesController_render_before($sender) {
if ($sender->Category && $this->enabled('DiscussionList')) {
$this->prepareDiscussionList($sender);
$sender->addDefinition('InfiniteScroll.Url', categoryUrl($sender->Category, '', true));
$this->resources($sender);
}
}
// Builds the page navigation
private function buildNavigation($sender, $countItems, $totalPages) {
if ($countItems <= Gdn::config('InfiniteScroll.StartAt', 1)) {
return;
}
$index = wrap(
wrap('?', 'span', ['class' => 'NavIndex'])
.wrap('/', 'span', ['class' => 'slash'])
.wrap(
Gdn::config('InfiniteScroll.PageNumber') ? $totalPages : $countItems,
'span',
['class' => 'small']
),
'span',
['class' => 'PageCount']
);
$jumpTo = wrap(
Gdn::translate('jump to page')
.'<br><input type="number" maxlength="4" id="InfScrollJT" class="InputBox" value="1"> '
.sprintf(Gdn::translate('of %s'), $totalPages),
'form', ['class' => 'JumpTo small']
);
$controls =
anchor('', '#', ['class' => 'JTT'])
.$index
.$jumpTo
.anchor('', '#', ['class' => 'JTB']);
$position = ['BottomRight', 'TopRight', 'BottomLeft', 'TopLeft'];
$style = ['', ' StyleDisc'];
$options = [
'id' => 'InfScrollNav',
'class' => $position[Gdn::config('InfiniteScroll.NavPosition', 0)]
.$style[Gdn::config('InfiniteScroll.NavStyle', 0)]
];
$sender->addAsset('Foot', wrap($controls, 'div', $options), 'InfiniteScrollNavigation');
// Progress indicator
if ($totalPages > 1) {
$sender->addAsset('Foot', wrap('', 'span', [
'id' => 'PageProgress',
'class' => 'Progress'
]));
}
}
// Add definitions for discussion lists.
private function prepareDiscussionList($sender) {
// Make the table view render just the inner content, similar to the modern view.
if (Gdn::config('Vanilla.Discussions.Layout') == 'table' && Gdn::request()->get('InnerList')) {
$sender->View = $sender->fetchViewLocation('inner_table', '', 'plugins/InfiniteScroll');
}
$page = (int)filter_var($sender->data('_Page'), FILTER_SANITIZE_NUMBER_INT);
$page = ($page > 1) ? $page : 1;
$countDiscussions = $sender->data('CountDiscussions');
$totalPages = calculateNumberOfPages(
$countDiscussions, Gdn::config('Vanilla.Discussions.PerPage', 30)
);
$sender->addDefinition('InfiniteScroll.CountItems', $countDiscussions);
$sender->addDefinition('InfiniteScroll.Page', $page);
$sender->addDefinition('InfiniteScroll.TotalPages', $totalPages);
$sender->addDefinition('InfiniteScroll.PerPage', (int)Gdn::config('Vanilla.Discussions.PerPage', 30));
$this->buildNavigation($sender, $countDiscussions, $totalPages);
}
// Check for mobile view and user preference
private function enabled($section = false) {
if ($section && !Gdn::config('InfiniteScroll.'.$section, true)) {
return false;
}
$session = Gdn::session();
return !(
($session->IsValid() && !$this->getUserMeta($session->UserID, 'Enable', true, true)) ||
(!Gdn::config('InfiniteScroll.Mobile', false) && isMobile())
);
}
// Attach the resources.
private function resources($sender) {
$sender->addDefinition('InfiniteScroll.HideHead', Gdn::config('InfiniteScroll.HideHead', true));
$sender->addDefinition('InfiniteScroll.Treshold', (int)Gdn::config('InfiniteScroll.Treshold', 200));
$sender->addDefinition('InfiniteScroll.Hotkey', Gdn::config('InfiniteScroll.Hotkey', 'j'));
$sender->addDefinition('InfiniteScroll.PageNumber', Gdn::config('InfiniteScroll.PageNumber'));
$sender->addDefinition('InfiniteScroll.NavStyle', (int)Gdn::config('InfiniteScroll.NavStyle', 0));
$sender->addDefinition('InfiniteScroll.ProgressBg', Gdn::config('InfiniteScroll.ProgressColor', '#38abe3'));
$sender->addDefinition('InfiniteScroll.Mobile', isMobile());
$sender->addJsFile('nanobar.min.js', 'plugins/InfiniteScroll');
$sender->addJsFile('infinitescroll.js', 'plugins/InfiniteScroll');
$sender->addCssFile('infinitescroll.css', 'plugins/InfiniteScroll');
}
// User preference checkbox for infinite scrolling on the "Edit Profile" page.
public function profileController_editMyAccountAfter_handler($sender) {
$sender->Form->setValue(
'InfiniteScroll',
$this->getUserMeta($sender->User->UserID, 'Enable', true, true)
);
echo wrap(
$sender->Form->checkbox('InfiniteScroll', 'Enable Infinite Scrolling'),
'li',
['class' => 'InfiniteScroll']
);
}
public function userModel_afterSave_handler($sender, $args) {
if (array_key_exists('InfiniteScroll', $args['FormPostValues'])) {
$this->setUserMeta(
val('UserID', $args['FormPostValues']),
'Enable',
val('InfiniteScroll', $args['FormPostValues'], false)
);
}
}
// Settings page
public function settingsController_infiniteScroll_create($sender) {
$sender->permission('Garden.Settings.Manage');
$sender->title(Gdn::translate('InfiniteScroll Settings'));
$conf = new ConfigurationModule($sender);
$conf->initialize([
'InfiniteScroll.Discussion' => [
'Control' => 'CheckBox',
'LabelCode' => 'Enable on discussions',
'Default' => true
],
'InfiniteScroll.DiscussionList' => [
'Control' => 'CheckBox',
'LabelCode' => 'Enable on discussion lists',
'Default' => true
],
'InfiniteScroll.Mobile' => [
'Control' => 'CheckBox',
'LabelCode' => 'Enable on mobile devices',
'Default' => false
],
'InfiniteScroll.Hotkey' => [
'Control' => 'textbox',
'LabelCode' => 'Page Jump Hotkey',
'Default' => 'j',
'Options' => ['maxlength' => '1', 'style' => 'width:30px;']
],
'InfiniteScroll.NavPosition' => [
'Control' => 'dropdown',
'Items' => ['bottom right', 'top right', 'bottom left', 'top left'],
'LabelCode' => 'Navigation Position'
],
'InfiniteScroll.NavStyle' => [
'Control' => 'dropdown',
'Items' => ['Basic', 'Discourse-inspired'],
'LabelCode' => 'Navigation Style'
],
'InfiniteScroll.ProgressColor' => [
'Control' => 'color',
'LabelCode' => 'Progress Bar Color',
'Description' => Gdn::translate('Can be any CSS color. If you don\'t want the bar to be visible, simply enter "transparent".'),
'Default' => '#38abe3'
]
]);
$conf->renderAll();
}
}