-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisqus-lazy_load-1508786-5_fit_in_other_patches.patch
278 lines (268 loc) · 8.69 KB
/
disqus-lazy_load-1508786-5_fit_in_other_patches.patch
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
diff --git a/disqus.admin.inc b/disqus.admin.inc
index 94a2346..2a1016b 100644
--- a/disqus.admin.inc
+++ b/disqus.admin.inc
@@ -195,6 +195,45 @@ function disqus_admin_settings() {
),
),
);
+ $form['advanced']['lazy_loading'] = array(
+ '#weight' => 6,
+ '#type' => 'fieldset',
+ '#title' => t('Lazy Loading'),
+ '#collapsible' => FALSE,
+ '#collapsed' => FALSE,
+ );
+ $form['advanced']['lazy_loading']['disqus_lazy_load'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Use Lazy Loading'),
+ '#description' => t('Prevent the Disqus script from loading until triggered by user activity.'),
+ '#default_value' => variable_get('disqus_lazy_load', 0),
+ );
+ $form['advanced']['lazy_loading']['disqus_lazy_load_method'] = array(
+ '#type' => 'select',
+ '#title' => t('Lazy Loading Trigger'),
+ '#description' => t('The type of user activity used to determine when the Disqus script is loaded.'),
+ '#default_value' => variable_get('disqus_lazy_load_method', DISQUS_LAZY_LOAD_ON_SCROLL),
+ '#options' => array(
+ DISQUS_LAZY_LOAD_ON_SCROLL => t('When the Disqus block scrolls into view'),
+ DISQUS_LAZY_LOAD_ON_CLICK => t('When the user clicks on an HTML element'),
+ ),
+ '#states' => array(
+ 'visible' => array(
+ 'input[name="disqus_lazy_load"]' => array('checked' => TRUE),
+ ),
+ ),
+ );
+ $form['advanced']['lazy_loading']['disqus_lazy_load_class'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Triggering Class'),
+ '#description' => t('The HTML class of the elements that trigger the lazy loading.'),
+ '#default_value' => variable_get('disqus_lazy_load_class', 'disqus-lazy-load'),
+ '#states' => array(
+ 'visible' => array(
+ 'select[name="disqus_lazy_load_method"]' => array('value' => DISQUS_LAZY_LOAD_ON_CLICK),
+ ),
+ ),
+ );
// Make sure the validation is called to handle the custom sso logo.
$form['#submit'][] = 'disqus_admin_settings_submit';
return system_settings_form($form);
diff --git a/disqus.install b/disqus.install
index e4eeb37..cca05c3 100644
--- a/disqus.install
+++ b/disqus.install
@@ -51,6 +51,9 @@ function disqus_uninstall() {
'disqus_developer',
'disqus_domain',
'disqus_inherit_login',
+ 'disqus_lazy_load',
+ 'disqus_lazy_load_class',
+ 'disqus_lazy_load_method',
'disqus_localization',
'disqus_location',
'disqus_logo',
diff --git a/disqus.js b/disqus.js
index 7332e3f..1dce7c2 100644
--- a/disqus.js
+++ b/disqus.js
@@ -15,11 +15,32 @@ var disqus_config;
(function ($) {
+Drupal.disqus = {
+ loadCommentScript: function (shortname) {
+ // Make the AJAX call to get the Disqus comments.
+ this.loadScript(shortname, 'embed.js')
+ },
+ loadCountScript: function (shortname) {
+ // Make the AJAX call to get the number of comments.
+ this.loadScript(shortname, 'count.js');
+ },
+ loadScript: function (shortname, scriptName) {
+ $.ajax({
+ type: 'GET',
+ url: '//' + shortname + '.disqus.com/' + scriptName,
+ dataType: 'script',
+ cache: false
+ });
+ }
+};
+
/**
* Drupal Disqus behavior.
*/
Drupal.behaviors.disqus = {
attach: function (context, settings) {
+ var disqus = Drupal.disqus;
+
$('body').once('disqus', function() {
// Load the Disqus comments.
if (settings.disqus || false) {
@@ -54,7 +75,7 @@ Drupal.behaviors.disqus = {
for (var j = 0; j < callback.length; j++) {
fn = fn[callback[j]];
}
- if(typeof fn === 'function') {
+ if (typeof fn === 'function') {
this.callbacks[key].push(fn);
}
}
@@ -63,24 +84,34 @@ Drupal.behaviors.disqus = {
};
// Make the AJAX call to get the Disqus comments.
- jQuery.ajax({
- type: 'GET',
- url: '//' + disqus_shortname + '.disqus.com/embed.js',
- dataType: 'script',
- cache: false
- });
+ if (settings.disqus.lazyLoadMethod || '') {
+ switch (settings.disqus.lazyLoadMethod) {
+ case 'click':
+ $('.' + (settings.disqus.lazyLoadClass || ''))
+ .css('cursor', 'pointer')
+ .bind('click', function (event) {
+ $(event.currentTarget).remove();
+ disqus.loadCommentScript(disqus_shortname);
+ });
+ break;
+ case 'scroll':
+ $(window).scroll(function scrollHandler(event) {
+ if ($(window).scrollTop() + $(window).height() > $('#disqus_thread').offset().top) {
+ disqus.loadCommentScript(disqus_shortname);
+ $(window).unbind('scroll', scrollHandler);
+ }
+ });
+ break;
+ }
+ }
+ else {
+ disqus.loadCommentScript(disqus_shortname);
+ }
}
// Load the comment numbers JavaScript.
if (settings.disqusComments || false) {
- disqus_shortname = settings.disqusComments;
- // Make the AJAX call to get the number of comments.
- jQuery.ajax({
- type: 'GET',
- url: '//' + disqus_shortname + '.disqus.com/count.js',
- dataType: 'script',
- cache: false
- });
+ disqus.loadCountScript(settings.disqusComments);
}
});
}
diff --git a/disqus.module b/disqus.module
index 2b80720..4bf61ba 100644
--- a/disqus.module
+++ b/disqus.module
@@ -21,6 +21,16 @@ define('DISQUS_API_CLOSE', 1);
define('DISQUS_API_REMOVE', 2);
/**
+ * Lazy load the Disqus script when the Disqus comment block becomes visible.
+ */
+define('DISQUS_LAZY_LOAD_ON_SCROLL', 0);
+
+/**
+ * Lazy load the Disqus script when an element is clicked.
+ */
+define('DISQUS_LAZY_LOAD_ON_CLICK', 1);
+
+/**
* Implements hook_help().
*/
function disqus_help($path, $arg) {
@@ -101,7 +111,7 @@ function disqus_closewindow() {
function disqus_element_info() {
$types['disqus'] = array(
'#disqus' => array(),
- '#theme_wrappers' => array('disqus_noscript', 'container'),
+ '#theme_wrappers' => array('disqus_noscript', 'disqus_lazy_load', 'container'),
'#attributes' => array('id' => 'disqus_thread'),
'#post_render' => array('disqus_element_post_render'),
);
@@ -144,6 +154,10 @@ function disqus_element_post_render($children, &$element) {
$disqus += disqus_sso_disqus_settings($user);
}
+ if (variable_get('disqus_lazy_load', FALSE)) {
+ $disqus += disqus_lazy_load_settings();
+ }
+
/**
* Pass callbacks on if needed. Callbacks array is two dimensional array
* with callback type as key on first level and array of JS callbacks on the
@@ -759,6 +773,9 @@ function disqus_theme() {
'disqus_noscript' => array(
'variables' => array('disqus' => NULL),
),
+ 'disqus_lazy_load' => array(
+ 'variables' => array('children' => NULL),
+ ),
);
}
@@ -779,6 +796,24 @@ function theme_disqus_noscript($variables = array()) {
}
/**
+ * Prepares the comment load prompt when lazy loading is enabled.
+ *
+ * @param $variables
+ * An array containing a "children" element, which may contain
+ * rendered content.
+ *
+ * @return string
+ */
+function theme_disqus_lazy_load($variables) {
+ if (variable_get('disqus_lazy_load', FALSE) && variable_get('disqus_lazy_load_method', '') == DISQUS_LAZY_LOAD_ON_CLICK) {
+ return '<p class="disqus-lazy-load">' . t('View the discussion thread.') . '</p>' . $variables['children'];
+ }
+ else {
+ return $variables['children'];
+ }
+}
+
+/**
* Implements hook_libraries_info().
*/
function disqus_libraries_info() {
@@ -922,6 +957,14 @@ function disqus_variable_info($options) {
'default' => '',
'group' => 'disqus'
);
+ $variable['disqus_lazy_load'] = array(
+ 'type' => 'string',
+ 'title' => t('Lazy Load'),
+ 'description' => t('Prevent the Disqus script from loading until triggered by user activity.'),
+ 'default' => '',
+ 'group' => 'disqus'
+ );
+
return $variable;
}
@@ -986,6 +1029,27 @@ function disqus_sso_disqus_settings($account = NULL) {
}
/**
+ * Assembles data about lazy loading.
+ *
+ * @return array
+ */
+function disqus_lazy_load_settings() {
+ $data = array();
+
+ switch (variable_get('disqus_lazy_load_method', NULL)) {
+ case DISQUS_LAZY_LOAD_ON_SCROLL:
+ $data['lazyLoadMethod'] = 'scroll';
+ break;
+ case DISQUS_LAZY_LOAD_ON_CLICK:
+ $data['lazyLoadClass'] = check_plain(variable_get('disqus_lazy_load_class', 'disqus-lazy-load'));
+ $data['lazyLoadMethod'] = 'click';
+ break;
+ }
+
+ return $data;
+}
+
+/**
* Assembles the full private key for use in SSO authentication.
*
* @param array $data