forked from adsabs/bumblebee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
295 lines (257 loc) · 9.19 KB
/
main.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* eslint-disable global-require */
/**
* Discovery application: main bootstrapping routine
*
* Here we will bring up to life the discovery application,
* all configuration is provided through the discovery.config.js
*
* Inside the config, there are sections for:
*
* - where to find js libraries
* - which widgets to load (for this application)
* - which environmental variables are used
* (and how to bootstrap run-time values)
*
*/
define(['config/discovery.config', 'module'], function(config, module) {
// eslint-disable-next-line import/no-dynamic-require
require([
'router',
'js/components/application',
'js/mixins/discovery_bootstrap',
'js/mixins/api_access',
'js/components/api_feedback',
'analytics',
], function(
Router,
Application,
DiscoveryBootstrap,
ApiAccess,
ApiFeedback,
analytics
) {
const updateProgress =
typeof window.__setAppLoadingProgress === 'function'
? window.__setAppLoadingProgress
: function() {};
Application.prototype.shim();
// at the beginning, we don't know anything about ourselves...
var debug = window.location.href.indexOf('debug=true') > -1;
// app object will load everything
var app = new (Application.extend(DiscoveryBootstrap))({
debug: debug,
// This is a general timeout which is only used per-module (plugins, controllers, etc.)
timeout: 60 * 1000, // 60 * 1000, // 60 seconds
});
// load the objects/widgets/modules (using discovery.config.js)
var appPromise = app.loadModules(module.config());
updateProgress(30, 'Loading Modules');
var startApp = function() {
updateProgress(80, 'App Loaded');
// this will activate all loaded modules
app.activate();
var pubsub = app.getService('PubSub');
pubsub.publish(pubsub.getCurrentPubSubKey(), pubsub.APP_LOADED);
// set some important urls, parameters before doing anything
app.configure();
updateProgress(95, 'Finishing Up...');
app.bootstrap().done(function(data) {
updateProgress(100);
app.onBootstrap(data);
var dynConf = app.getObject('DynamicConfig');
if (dynConf && dynConf.debugExportBBB) {
window.bbb = app;
}
pubsub.publish(pubsub.getCurrentPubSubKey(), pubsub.APP_BOOTSTRAPPED);
pubsub.publish(pubsub.getCurrentPubSubKey(), pubsub.APP_STARTING);
app.start(Router).done(function() {
pubsub.publish(pubsub.getCurrentPubSubKey(), pubsub.APP_STARTED);
var getUserData = function() {
try {
var beehive = _.isFunction(this.getBeeHive) && this.getBeeHive();
var user =
_.isFunction(beehive.getObject) && beehive.getObject('User');
if (user) {
return user.getUserData('USER_DATA');
}
} catch (e) {
// do nothing
}
return {};
};
// handle user preferences for external link actions
var updateExternalLinkBehavior = _.debounce(
function() {
var userData = getUserData.call(app);
var action =
(userData.externalLinkAction &&
userData.externalLinkAction.toUpperCase()) ||
'AUTO';
if (action === 'OPEN IN CURRENT TAB') {
var max = 10;
var timeout;
(function updateLinks(count) {
clearTimeout(timeout);
if (count < max) {
$('a[target="_blank"]').attr('target', '');
timeout = setTimeout(updateLinks, 1000, count + 1);
}
})(0);
}
},
3000,
{ leading: true, trailing: false },
false
);
pubsub.subscribe(
pubsub.getCurrentPubSubKey(),
pubsub.NAVIGATE,
updateExternalLinkBehavior
);
updateExternalLinkBehavior();
const toggle = ($sidebar, $content, $button) => {
$sidebar.toggleClass('show');
let text =
'<i class="fa fa-close" aria-hidden="true"></i> Close Menu';
if ($sidebar.hasClass('show')) {
$content.removeClass('full-width');
} else {
text = '<i class="fa fa-bars" aria-hidden="true"></i> Show Menu';
$content.addClass('full-width');
}
$button.html(text);
};
// some global event handlers, not sure if right place
$('body').on('click', 'button.toggle-menu', function(e) {
var $button = $(e.target);
var $sidebar = $button
.parents()
.eq(1)
.find('.nav-container');
var $content = $button
.parents()
.eq(1)
.find('.user-pages__main-content');
toggle($sidebar, $content, $button);
$('a', $sidebar).on('click', () => {
toggle($sidebar, $content, $button);
});
});
$('body').on('click', '#abs-full-txt-toggle', function() {
$('#resources-container').toggleClass('show');
if ($('#resources-container').hasClass('show')) {
$('#abs-full-txt-toggle').text('Hide Sources');
} else {
$('#abs-full-txt-toggle').text('Full Text Sources');
}
});
$('body').on('click', '#results-actions-toggle', function() {
$('#query-info-container').toggleClass('show');
if ($('#query-info-container').hasClass('show')) {
$('#results-actions-toggle').html(
'<i class="fa fa-times" title="close actions" aria-hidden="true"></i> Actions'
);
} else {
$('#results-actions-toggle').html(
'<i class="fa fa-book" title="open actions" aria-hidden="true"></i> Actions'
);
}
});
// accessibility: skip to main content
$('body').on('click', '#skip-to-main-content', function() {
$('#main-content').trigger('focus');
return false;
});
// check for is-proxied class, and if present, send alert
if ($('body').hasClass('is-proxied')) {
const url = window.getCanonicalUrl();
const msg = `
<p>
You are using a proxied version of ADS, we recommend you switch to the regular non-proxied URL:
<a href="${url}${location.pathname}" rel="noopener noreferrer">${url}</a></p>
<p>
Configure authenticated access to publisher content via the Library Link Server in your account
<a href="${url}/user/settings/librarylink" rel="noopener noreferrer">preferences</a>.
</p>
`;
pubsub.publish(
pubsub.getCurrentPubSubKey(),
pubsub.ALERT,
new ApiFeedback({
type: 'danger',
msg,
})
);
}
// app is loaded, send timing event
if (window.__PAGE_LOAD_TIMESTAMP) {
var time = new Date() - window.__PAGE_LOAD_TIMESTAMP;
analytics('send',
'timing',
{
timingCategory: 'Application',
timingVar: 'Loaded',
timingValue: time,
});
// send to analytics the theme user is using
const darkSwitch = localStorage.getItem('darkSwitch');
let action = 'systemSetting';
let label = 'light';
if (darkSwitch !== null) {
if (darkSwitch === 'on') {
action = 'appSetting';
label = 'dark';
} else {
action = 'appSetting';
label = 'light';
}
} else if (
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
action = 'systemSetting';
label = 'dark';
}
analytics(
'send',
'event',
'uitheme', // category
action,
label
);
if (debug) {
console.log('Application Started: ' + time + 'ms');
}
}
// clear the app loading timer
window.clearTimeout(window.APP_LOADING_TIMER);
});
});
};
var failedLoad = function() {
analytics('send', 'event', 'introspection', 'failed-load', arguments);
if (!debug) {
app.redirect('500.html');
}
};
var failedReload = function() {
analytics(
'send',
'event',
'introspection',
'failed-reloading',
arguments
);
if (debug) {
// so error messages remain in the console
return;
}
// if we failed loading, retry *once again* (and give up eventually)
app.reload('404.html');
};
// after they are loaded; we'll kick off the application
appPromise
.done(startApp)
.fail(failedLoad)
.fail(failedReload);
});
});