This repository was archived by the owner on Sep 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathembed.js
More file actions
1917 lines (1874 loc) · 88 KB
/
embed.js
File metadata and controls
1917 lines (1874 loc) · 88 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* OA.Works embed library.
MIT licence.
by Mark MacGillivray.
Can be minified in various ways. E.g. use terser:
npm install -g terser
terser embed.js -c -m > embed.min.js
*/
/* Start with a simple set of helper functions.
To make the embed as simple and compatible as possible it doesn't rely on
anything else, so these helpers make it a bit easier to use default js to
do things like iterate over sets of elements, show/hide, alter css, make
ajax requests, get/set element values/attributes, or add/remove elements. */
var _OA, _oaw,
indexOf = [].indexOf;
_OA = {
gebi: function(id) {
return document.getElementById(id.replace('#', ''));
},
gebc: function(cls) {
return document.getElementsByClassName(cls.replace('.', ''));
},
gebn: function(n) {
var r;
r = document.getElementsByTagName(n.replace('<', '').replace('>', '')); // e.g. by the element name, like "div"
if (r != null) {
return r;
} else {
return document.getElementsByName(n); // otherwise by the "name" attribute matching n
}
}
};
_OA.each = function(elems, key, val) {
var elem, i, len, results;
if (typeof elems === 'string') {
if (elems.startsWith('#')) {
elems = [_OA.gebi(elems)];
} else if (elems.startsWith('.')) {
elems = _OA.gebc(elems);
} else {
elems = _OA.gebn(elems);
}
} else if (typeof elems === 'object') {
if (!Array.isArray(elems)) {
elems = [elems];
}
}
if (elems != null) {
results = [];
for (i = 0, len = elems.length; i < len; i++) {
elem = elems[i];
if (elem != null) {
if (typeof key === 'function') {
results.push(key(elem));
} else {
results.push(_OA.set(elem, key, val));
}
} else {
results.push(void 0);
}
}
return results;
}
};
_OA.listen = function(action, els, fn) {
return _OA.each(els, function(el) {
var wfn;
if (action === 'enter') {
action = 'keyup';
wfn = function(e) {
if (e.keyCode === 13) {
return fn(e);
}
};
} else {
wfn = fn;
}
if (!_OA.has(el, 'listen_' + action)) {
_OA.class(el, 'listen_' + action);
return el.addEventListener(action, function(e) {
return wfn(e);
});
}
});
};
_OA.show = function(els, html, append) {
return _OA.each(els, function(el) {
var was;
if (typeof html === 'string') {
el.innerHTML = (append ? el.innerHTML : '') + html;
}
was = _OA.get(el, '_l_display');
if (typeof was !== 'string' || was === 'none') { // TODO should be inline in which cases...
was = (el.tagName === 'DIV' ? 'block' : 'inline');
}
return el.style.display = was;
});
};
_OA.hide = function(els) {
return _OA.each(els, function(el) {
if (el.style.display !== 'none') {
_OA.set(el, '_l_display', el.style.display);
}
return el.style.display = 'none';
});
};
_OA.get = function(els, attr) {
var res;
res = void 0;
_OA.each(els, function(el) {
if (attr == null) {
try {
res = el.value;
} catch (err) {}
if (typeof res === 'string' && !res.length) {
res = void 0;
}
}
try {
return res != null ? res : res = el.getAttribute(attr);
} catch (err) {}
});
return res;
};
_OA.set = function(els, attr, val) {
return _OA.each(els, function(el) {
// TODO handle dot notation keys e.g if attr is style.display
if ((val == null) || attr === 'value' || attr === 'val') {
try {
return el.value = val == null ? attr : val;
} catch (err) {}
} else {
try {
return el.setAttribute(attr, val);
} catch (err) {}
}
});
};
_OA.checked = function(els) {
var res;
res = true;
_OA.each(els, function(el) {
return res = el.checked;
});
return res;
};
_OA.html = function(els, html, append, show) {
var rs = [];
_OA.each(els, function(el) {
if (typeof html === 'string') {
el.innerHTML = (append ? el.innerHTML : '') + html;
}
rs.push(el.innerHTML);
if (show) {
return _OA.show(el);
}
});
if (rs.length === 1) {
return rs[0];
} else if (rs.length) {
return rs;
} else {
return '';
}
};
_OA.append = function(els, html) {
return _OA.html(els, html, true);
};
_OA.remove = function(els) {
return _OA.each(els, function(el) {
return el.parentNode.removeChild(el);
});
};
_OA.class = function(el, cls) {
var c, classes, i, len, parts, rs;
rs = [];
classes = el.getAttribute('class');
if (classes == null) {
classes = '';
}
if (typeof cls === 'string') {
if (!classes.includes(cls)) {
if (classes.length) {
classes += ' ';
}
classes += cls;
} else {
classes = classes.replace(cls, '').trim().replace(/ /g, ' ');
}
el.setAttribute('class', classes);
}
parts = classes.split(' ');
for (i = 0, len = parts.length; i < len; i++) {
c = parts[i];
if (indexOf.call(rs, c) < 0) {
rs.push(c);
}
}
return rs;
};
_OA.has = function(el, cls) {
var classes;
classes = _OA.class(el);
if (cls.startsWith('.')) {
cls = cls.replace('.');
}
if (indexOf.call(classes, cls) >= 0) {
return true;
} else {
if (el.getAttribute(cls)) {
return true;
} else {
return false;
}
}
};
_OA.css = function(els, key, val) {
return _OA.each(els, function(el) {
var i, k, len, p, ps, parts, s, ss, style;
s = _OA.get(el, 'style');
style = {};
parts = (typeof s === 'string' ? s : '').split(';');
for (i = 0, len = parts.length; i < len; i++) {
p = parts[i];
ps = p.split(':');
if (ps.length === 2) {
style[ps[0].trim()] = ps[1].trim();
}
}
if (val != null) {
style[key] = val;
}
ss = '';
for (k in style) {
if (ss !== '') {
ss += ';';
}
ss += k + ':' + style[k];
}
return _OA.set(el, 'style', ss);
});
};
_OA.jx = function(url, data, success, error) {
var xhr;
if (typeof data === 'object' && typeof data.append !== 'function') {
// a FormData object will have an append function, a normal json object will not. FormData should be POSTable by xhr as-is
data = JSON.stringify(data);
}
xhr = new XMLHttpRequest();
xhr.open((data != null ? 'POST' : 'GET'), url);
xhr.send(data);
xhr.onload = function() {
if (xhr.status !== 200) {
try {
error(xhr);
} catch (err) {}
} else {
try {
success(JSON.parse(xhr.response), xhr);
} catch (err) {
console.log(err);
try {
success(xhr.response, xhr);
} catch (err) {
try {
try {
success(xhr);
} catch (err) {
error(xhr, err);
}
} catch (err) {}
}
}
}
if (typeof _OA.loaded === 'function') {
return _OA.loaded(xhr);
}
};
return xhr.onerror = function(err) {
try {
return error(err);
} catch (err) {}
};
};
/* =============================================================================
The main function definition, which configures the "plugin" to run based on
any option values provided at instantiation. See below at the bottom of this
file for examples of it being called to instantiate particular "kinds" of
plugin, for different features, for example instantill and shareyourpaper. */
_oaw = function(opts) {
var ap, c, configs, cs, csk, csv, eq, i, j, len, len1, o;
try { // set the default options
if (opts == null) {
opts = {};
}
for (o in opts) {
this[o] = opts[o];
}
if (this.api) {
// tidy up legacy domain names for simplifying use with legacy sites that have not had their config changed yet
this.api = this.api.replace('/service/oab', '');
this.api = this.api.replace('dev.api.cottagelabs.com', 'beta.oa.works');
this.api = this.api.replace('api.cottagelabs.com', 'beta.oa.works');
this.api = this.api.replace('api.openaccessbutton.org', 'api.oa.works');
}
if (this.api == null) { // default API URL to contact
this.api = window.location.host.includes('dev.') ? 'https://beta.oa.works' : 'https://api.oa.works';
}
if (this.plugin == null) {
this.plugin = 'instantill'; // has to be defined at startup, as either instantill or shareyourpaper
}
if (this.element == null) {
this.element = '#' + this.plugin; // which element on the page to insert the plugin into
}
if (this.pushstate == null) {
this.pushstate = true; // if true, the embed will try to add page state changes to the browser state manager
}
if (this.uid == null) {
this.uid = 'anonymous'; // optional user ID for config and tracking usage
}
if (this.config == null) {
this.config = {}; // a config object from the user account if available
}
if (this.local == null) {
this.local = false; // local storage of config turned off by default for now
}
if (this.data == null) { // a place to store data returned from API calls
this.data = {};
}
if (this.f == null) { // a place to store API find details
this.f = {};
}
if (this.template == null) { // the plugin html template to use
this.template = _oaw[this.plugin + '_template'];
}
if (this.css == null) { // there is default css defined below, or a custom set could be passed in.
this.css = _oaw.css;
}
this._loading = false; // tracks when loads are occurring
this.submit_after_metadata = false; // used by instantill to track if metadata has been provided by user
this.needmore = false; // used by instantill to track that more metadata is required (e.g. if title is too short)
this.file = false; // used by syp to store the file for sending to backend
if (this.demo == null) { // just allows for some demo usage on our own site / docs
this.demo = window.location.href.includes('/demo') && (window.location.href.includes('oa.works') || window.location.href.includes('openaccessbutton.') || window.location.href.includes('shareyourpaper.') || window.location.href.includes('instantill.'));
}
if (this.loaded != null) {
_OA.loaded = this.loaded; // if this is set to a function, it will be passed to _leviathan loaded, which gets run after every ajax call completes. It is also called directly after every configure
}
if (window.location.search.includes('local=')) { // can set local via the URL for testing
this.local = window.location.search.includes('local=true') ? true : false;
}
if (window.location.search.includes('clear=') || this.local === false) {
try { // can clear local storage via the URL, helps with testing
localStorage.removeItem('_oaw_config_' + this.plugin);
} catch (err) {}
}
if (window.location.search.includes('config=')) {
try { // can provide a config via the URL, mostly helpful for testing, could have some real world uses.
this.config = JSON.parse(window.location.search.split('config=')[1].split('&')[0].split('#')[0]);
} catch (err) {}
}
if (window.location.search.includes('config.')) { // allow for specific config variables as well as whole object above
configs = window.location.search.split('config.');
configs.shift();
for (i = 0, len = configs.length; i < len; i++) {
c = configs[i];
cs = c.split('=');
if (cs.length === 2) {
csk = cs[0].trim();
csv = cs[1].split('&')[0].split('#')[0].trim();
this.configure(csk, csv, false);
}
}
}
setTimeout(() => { // run the configure step, which attempts to set values based on the config, and also does a remote request for a config if appropriate (see below)
return this.configure();
}, 1);
if (!this.config.autorun_off) { // whether or not to trigger on page load, and what to do if so
ap = typeof this.config.autorunparams === 'string' && this.config.autorunparams.length ? this.config.autorunparams.split(',') : typeof this.config.autorunparams === 'object' ? this.config.autorunparams : ['doi', 'title', 'url', 'atitle', 'rft_id', 'journal', 'issn', 'year', 'author'];
if (typeof ap === 'string') {
ap = ap.replace(/"/g, '').replace(/'/g, '').split(',');
}
for (j = 0, len1 = ap.length; j < len1; j++) {
o = ap[j];
o = o.split('=')[0].trim();
if (o.includes('=')) {
eq = o.split('=')[1].trim();
}
if (window.location.search.replace('?', '&').includes('&' + o + '=')) {
this.data[eq != null ? eq : o] = decodeURIComponent(window.location.search.replace('?', '&').split('&' + o + '=')[1].split('&')[0].replace(/\+/g, ' '));
}
}
if (!this.data.doi && window.location.href.split('?')[0].includes('/10.') && window.location.href.split('?')[0].split('/10.')[1].indexOf('/') > 1 && window.location.href.split('?')[0].split('/10.')[1].trim().split('/').length > 1) {
this.data.doi = '10.' + window.location.href.split('?')[0].split('/10.')[1].replace(/\/$/, '');
}
}
if (window.location.search.includes('email=')) { // can provide the email of the requestee in URL
this.data.email = window.location.search.split('email=')[1].split('&')[0].split('#')[0];
_OA.remove('#_oaw_collect_email');
}
if (window.location.search.includes('confirmed=')) { // can state in the URL if the user already confirmed the file to upload
this.data.confirmed = window.location.search.split('confirmed=')[1].split('&')[0].split('#')[0];
}
if (window.location.search.includes('refresh=true')) { // optionally attempts to pass a refersh param to the API to get fresh results
this.data.refresh = true;
}
if (this.data.doi || (this.plugin === 'instantill' && (this.data.title || this.data.url))) { // if data was provided at instantiation, do a find on it immediately
this.find();
}
window.addEventListener("popstate", (pe) => {
return this.state(pe);
});
return this;
} catch (err) {
return this.ping('instantill_or_shareyourpaper_try_initialise_catch');
}
};
/* Now add methods to the _oaw prototype. Some methods here will be general and
used by multiple plugins, whilst others are specific to one */
_oaw.prototype.cml = function() { // a little helper to pick which email address to send to
return this.config.problem ? this.config.problem : (this.config.owner ? this.config.owner : (this.config.email ? this.config.email : ''));
};
_oaw.prototype.contact = function() { // a helper to set a "contact us" link depending on context
return 'Please try ' + (this.cml() ? '<a id="_oaw_contact_library" href="mailto:' + this.cml() + '">contacting us</a>' : 'contacting us') + ' directly';
};
_oaw.prototype.loading = function(load) {
// controls what is visible on the plugin when something is happening and the user is waiting
_OA.hide('#_oaw_error');
if (load !== true && (this._loading || load === false)) {
try {
clearInterval(this._loading);
} catch (err) {}
this._loading = false;
return _OA.each('._oaw_loading', (el) => {
if (_OA.has(el, '_oaw_continue')) {
return el.innerHTML = 'Continue';
} else if (_OA.has(el, '_oaw_submit')) {
return el.innerHTML = 'Complete request';
} else if (_OA.has(el, '_oaw_deposit')) {
return el.innerHTML = 'Deposit';
} else if (_OA.has(el, '_oaw_find')) {
return el.innerHTML = 'Next';
} else if (_OA.has(el, '_oaw_confirm')) {
return el.innerHTML = '<strong>My upload was an accepted manuscript</strong>';
} else {
return el.innerHTML = 'Find ' + (this.config.say_paper ? 'paper' : 'article'); // this would only happen on instantill, as "Next" above is the default for syp
}
});
} else {
_OA.html('._oaw_find', 'Searching .');
_OA.html('._oaw_submit', 'Submitting .');
_OA.html('._oaw_deposit', 'Depositing .');
_OA.html('._oaw_confirm', 'Depositing .');
return this._loading = setInterval((function() {
var button, dots, i, len, buttons, results;
buttons = _OA.gebc('._oaw_loading');
results = [];
for (i = 0, len = buttons.length; i < len; i++) {
button = buttons[i];
dots = button.innerHTML.split('.');
if (dots.length >= 4) {
results.push(button.innerHTML = dots[0]);
} else {
results.push(button.innerHTML = button.innerHTML + ' .');
}
}
return results;
}), 700);
}
};
_oaw.prototype.state = function(pop) { // keeps track of pushing state changes to the URL params (optional)
var extra, extras, i, k, len, u;
if (this.pushstate) {
try {
u = window.location.pathname;
if (pop == null) {
if (window.location.href.includes('shareyourpaper.org')) {
if (window.location.href.includes('/10.') || window.location.href.replace(/\//g, '').endsWith('.org')) {
u = window.location.href.split('10.')[0] + (this.data.doi ? this.data.doi : '') + window.location.search + window.location.hash;
} else {
u += window.location.search.split('?doi=')[0].split('&doi=')[0];
u += !u.includes('?') ? '?' : '&';
u += 'doi=' + this.data.doi;
if (window.location.search.split('?doi=')[1].includes('&')) {
extras = window.location.search.split('?doi=')[1].split('&');
extras.shift();
for (i = 0, len = extras.length; i < len; i++) {
extra = extras[i];
u += '&' + extra;
}
}
u += window.location.hash;
}
} else if (!window.location.href.includes('/setup') && !window.location.href.includes('/demo')) {
if ((this.data.doi != null) || (this.data.title != null) || (this.data.url != null)) {
k = this.data.doi ? 'doi' : this.data.title ? 'title' : 'url';
u += window.location.search.split('?' + k + '=')[0].split('&' + k + '=')[0];
u += !u.includes('?') ? '?' : '&';
u += k + '=' + this.data[k] + window.location.hash;
}
}
}
window.history.pushState("", (pop != null ? "search" : "find"), u);
if (pop != null) {
// what to do with the pop event? for now just triggers a restart if user tries to go back
return this.restart();
}
} catch (err) {}
}
};
_oaw.prototype.restart = function(e, val, err) {
// everything needed to restart the plugin back to original settings as if the page had just relaoded
var gf;
try {
if (e.target.parentElement.id !== '_oaw_permissionemail') {
e.preventDefault();
}
} catch (err) {}
this.data = {};
this.f = {};
this.needmore = false;
this.loading(false);
this.file = false;
if (gf = _OA.gebi("_oaw_file")) {
gf.value = '';
}
_OA.hide('._oaw_panel');
_OA.show('#_oaw_inputs');
this.configure();
this.state();
if (err) {
_OA.show('#_oaw_error', err);
}
if (val) {
_OA.set('#_oaw_input', val);
return this.find();
} else {
return _OA.set('#_oaw_input', '');
}
};
_oaw.prototype.ping = function(what) {
// sends a tracking message to the API which can be sent on certain events or errors
var url;
try {
if (!what.includes(this.plugin)) {
if (!what.startsWith('_')) {
what = '_' + what;
}
what = this.plugin + what;
}
url = this.api + '/ping?action=' + what + '&from=' + this.uid + '&url=' + encodeURIComponent(window.location.href);
if (this.config.pilot) {
url += '&pilot=' + this.config.pilot;
}
if (this.config.live) {
url += '&live=' + this.config.live;
}
return _OA.jx(url);
} catch (err) {}
};
/* panel and section are optionally used to control which template panel, and
which section of that panel, is visible at page load. Mostly useful for
testing */
_oaw.prototype.panel = function(panel, section) {
var he;
if (he = _OA.gebi('_oaw_' + (panel.startsWith('_oaw_') ? panel.replace('_oaw_', '') : panel))) {
_OA.hide('._oaw_panel');
_OA.show(he);
if (section) {
return this.section(section);
}
}
};
_oaw.prototype.section = function(section) {
var fe;
// useful for demo/test, just shows a specific section within a panel
fe = _OA.gebi('_oaw_' + (section.startsWith('_oaw_') ? section.replace('_oaw_', '') : section));
if (fe == null) {
fe = _OA.gebc('_oaw_' + (section.startsWith('_oaw_') ? section.replace('_oaw_', '') : section));
}
if (fe) {
_OA.hide('._oaw_section');
return _OA.show(fe);
}
};
_oaw.prototype.validate = function() {
// validate any email provided by the user before proceeding to deposit etc
var email;
if (this.config.terms && !_OA.checked('#_oaw_read_terms')) { // instantill terms
return _OA.show('#_oaw_error', '<p>Please agree to the terms first.</p>');
} else {
email = _OA.get('#_oaw_email');
if (typeof email !== 'string') email = '';
email = email.trim();
if (!email.length || email.split('@').length !== 2) {
_OA.show('#_oaw_error', '<p>Please provide your university email address.</p>');
_OA.css('#_oaw_email', 'border-color', '#f04717');
return _OA.gebi('#_oaw_email').focus();
} else {
this.data.email = email;
if (this.plugin === 'instantill') {
return this.submit();
} else {
return this.deposit();
}
}
}
};
_oaw.prototype.submit = function(e) {
// only used by instantill
// sends an API call to start an ILL request
var data, i, k, len, nfield, ou, keys;
try {
try {
e.preventDefault();
} catch (err) {}
if (!this.openurl() && !this.data.email && _OA.gebi('#_oaw_email')) {
return this.validate();
} else if (JSON.stringify(this.f) === '{}' || this.f.metadata == undefined || (!this.f.metadata.title || !this.f.metadata.journal || !this.f.metadata.year)) {
if (this.submit_after_metadata) {
return this.done(false);
} else {
this.submit_after_metadata = true;
return this.metadata();
}
} else {
this.loading();
data = {
match: this.f.input,
email: this.data.email,
from: this.uid,
plugin: this.plugin,
embedded: window.location.href
};
data.config = this.config;
data.metadata = this.f.metadata ? this.f.metadata : {};
keys = ['title', 'journal', 'year', 'doi'];
for (i = 0, len = keys.length; i < len; i++) {
k = keys[i];
if (!data.metadata[k] && this.data[k]) {
data.metadata[k] = this.data[k];
}
if (data.metadata.doi && data.metadata.doi.startsWith('http')) {
data.metadata.url = data.metadata.doi;
delete data.metadata.doi;
}
}
nfield = this.config.notes ? this.config.notes : 'notes';
if (this.data.usermetadata) {
data[nfield] = 'The user provided some metadata. ';
}
if (this.config.pilot) {
data.pilot = this.config.pilot;
}
if (this.config.live) {
data.live = this.config.live;
}
if ((this.f !== undefined && this.f.ill !== undefined && this.f.ill.subscription !== undefined) || (this.f !== undefined && this.f.url)) {
if (typeof data[nfield] !== 'string') {
data[nfield] = '';
} else {
data[nfield] += ' ';
}
if (this.f.ill !== undefined && this.f.ill.subscription) {
data[nfield] += 'Subscription check done, found ' + (this.f.ill.subscription.url ? this.f.ill.subscription.url : (this.f.ill.subscription.journal ? 'journal' : 'nothing')) + '. ';
}
if (this.f.metadata != null) {
data[nfield] += 'OA availability check done, found ' + (this.f.url ? this.f.url : 'nothing') + '. ';
}
}
ou = this.openurl();
if (ou && !data.email) {
data.forwarded = true;
}
if (this.demo === true) {
console.log('Not POSTing ILL and not forwarding to ' + ou + ' for demo purposes');
return console.log(data);
} else {
return _OA.jx(this.api + '/ill', data, (res) => {
return this.done(res);
}, () => {
return this.done(false);
});
}
}
} catch (err) {
return this.ping('instantill_try_submit_catch');
}
};
_oaw.prototype.metadata = function(submitafter) {
// only used by instantill
// sets the visible metadata on the page after a find
var i, len, m, keys;
keys = ['title', 'year', 'journal', 'doi'];
for (i = 0, len = keys.length; i < len; i++) {
m = keys[i];
if ((this.f !== undefined && this.f.metadata !== undefined && this.f.metadata[m] !== undefined) || this.data[m] !== undefined) {
_OA.set('#_oaw_' + m, (this.f.metadata ? this.f.metadata : this.data)[m]);
}
}
_OA.hide('._oaw_panel');
return _OA.show('#_oaw_metadata');
};
_oaw.prototype.openurl = function() {
// only used by instantill
// constructs an "open URL" to forward a user to, where they can request an article
var author, config, d, defaults, i, k, len, notes, url, v;
if (!this.config.ill_form) {
return '';
} else {
config = JSON.parse(JSON.stringify(this.config));
defaults = {
sid: 'sid',
title: 'atitle', // this is what iupui needs (title is also acceptable, but would clash with using title for journal title, which we set below, as iupui do that
doi: 'rft_id', // don't know yet what this should be
author: 'aulast', // author should actually be au, but aulast works even if contains the whole author, using aufirst just concatenates
journal: 'title', // this is what iupui needs
page: 'pages', // iupui uses the spage and epage for start and end pages, but pages is allowed in openurl, check if this will work for iupui
published: 'date', // this is what iupui needs, but in format 1991-07-01 - date format may be a problem
year: 'rft.year' // this is what IUPUI uses
};
for (d in defaults) {
if (!config[d]) {
config[d] = defaults[d];
}
}
url = config.ill_form;
url += !url.includes('?') ? '?' : '&';
if (config.ill_added_params) {
url += config.ill_added_params.replace('?', '') + '&';
}
url += config.sid + '=InstantILL&';
for (k in (this.f.metadata ? this.f.metadata : {})) {
v = false;
if (k === 'author') {
if (typeof this.f.metadata.author === 'string') {
v = this.f.metadata.author;
} else if (Array.isArray(this.f.metadata.author)) {
v = '';
for (i = 0, len = this.f.metadata.author.length; i < len; i++) {
author = this.f.metadata.author[i];
try {
if (v.length) {
v += ', ';
}
v += typeof author === 'string' ? author : typeof author === 'object' && author.family ? author.family + (author.given ? ', ' + author.given : '') : JSON.stringify(author);
} catch (err) {}
}
}
} else if (k === 'doi' || k === 'pmid' || k === 'pmc' || k === 'pmcid' || k === 'url' || k === 'journal' || k === 'title' || k === 'year' || k === 'issn' || k === 'volume' || k === 'issue' || k === 'page' || k === 'crossref_type' || k === 'type' || k === 'publisher' || k === 'published' || k === 'notes') {
v = this.f.metadata[k];
}
if (v) {
url += (config[k] ? config[k] : k) + '=' + encodeURIComponent(v) + '&';
}
}
notes = this.data.usermetadata ? 'The user provided some metadata. ' : '';
if (this.f.ill !== undefined && this.f.ill.subscription !== undefined) {
notes += 'Subscription check done, found ' + (this.f.ill.subscription.url ? this.f.ill.subscription.url : (this.f.ill.subscription.journal ? 'journal' : 'nothing')) + '. ';
}
if (this.f.metadata != null) {
notes += 'OA availability check done, found ' + (this.f.url ? this.f.url : 'nothing') + '. ';
}
if (notes) {
url += '&' + (this.config.notes ? this.config.notes : 'notes') + '=' + notes;
}
return url.replace('/&&/g', '&');
}
};
_oaw.prototype.done = function(res, msg) { // all the things to do when a plugin is done
var ou;
this.loading(false);
if (ou = this.openurl()) {
window.location = ou;
} else {
_OA.hide('._oaw_panel');
_OA.hide('._oaw_done');
if (typeof res === 'string' && _OA.gebi('_oaw_' + res)) {
_OA.show('#_oaw_' + res); // various done states for shareyourpaper
if (res === 'confirm') {
_OA.hide('#_oaw_done_restart');
} else {
_OA.show('#_oaw_done_restart');
}
} else if (res) {
_OA.html('#_oaw_done_header', '<h3>Thanks! Your request has been received</h3><p>Your confirmation code is: ' + res._id + ', this will not be emailed to you. The ' + (this.config.say_paper ? 'paper' : 'article') + ' will be sent to ' + this.data.email + ' as soon as possible.</p>'); // only instantill falls through to here
} else {
_OA.html('#_oaw_done_header', '<h3>Sorry, we were not able to create an Interlibrary Loan request for you.</h3><p>' + this.contact() + '</p>');
_OA.html('#_oaw_done_restart', 'Try another');
this.ping(msg != null ? msg : 'instantill_couldnt_submit_ill');
setTimeout((() => {
return this.restart();
}), 6000);
}
_OA.show('#_oaw_done');
}
if (typeof this.after === 'function') {
return this.after();
}
};
_oaw.prototype.deposit = function(e) {
// only used by shareyourpaper
// takes a file provided by user upload button and sends it to the API for
// deposit to institutional repository
var d, data, fl, info, md;
try {
try {
e.preventDefault();
} catch (err) {}
if (!this.data.email && _OA.gebi('#_oaw_email')) {
return this.validate();
} else if (this.demo === true && (this.data.doi != null) && this.data.doi.startsWith('10.1234/oab-syp-')) {
if (this.data.doi !== '10.1234/oab-syp-confirm') { // demo successful deposit
info = '<p>You’ll soon find your paper freely available in ' + (this.config.repo_name ? this.config.repo_name : 'ScholarWorks') + ', Google Scholar, Web of Science, and other popular tools.';
info += '<h3>Your paper is now freely available at this link:</h3>';
_OA.html('#_oaw_zenodo_embargo', info);
_OA.set('#_oaw_zenodo_url', 'https://zenodo.org/record/3703317');
this.done('zenodo'); // demo something wrong, please confirm
} else {
this.done('confirm');
}
if (typeof this.loaded === 'function') {
return this.loaded();
}
} else {
fl = _OA.gebi('#_oaw_file');
if ((fl != null) && (fl.files != null) && fl.files.length) {
this.file = new FormData();
this.file.append('file', fl.files[0]);
} else if (this.file !== true) { // can be set to true when dark deposit is being followed - no file required. Or a demo may set it to true
_OA.show('#_oaw_error', '<p aria-live="polite">Whoops, you need to give us a file! Check it’s uploaded.</p>');
_OA.css('#_oaw_file', 'border-color', '#f04717');
return;
}
this.loading();
// this could be just an email for a dark deposit, or a file for actual deposit
// if the file is acceptable and can go in zenodo then we don't bother getting the email address
data = {
from: this.uid,
plugin: this.plugin,
embedded: window.location.href,
metadata: (this.f !== undefined ? this.f.metadata : undefined)
};
if (this.demo === true) {
data.demo = true;
}
data.config = this.config;
if (this.data.email) {
data.email = this.data.email;
}
if (this.data.confirmed) {
data.confirmed = this.data.confirmed;
}
if (this.f !== undefined && typeof this.f.url === 'string' && !this.is_bronze_archivable) {
data.redeposit = this.f.url;
}
if (this.config.pilot) {
data.pilot = this.config.pilot;
}
if (this.config.live) {
data.live = this.config.live;
}
if (typeof this.file !== 'boolean') {
for (d in data) {
if (d === 'metadata') {
for (md in data[d]) {
if (typeof data[d][md] === 'string' || typeof data[d][md] === 'number') {
this.file.append(md, data[d][md]);
}
}
} else if (typeof data[d] === 'object') {
this.file.append(d, JSON.stringify(data[d]));
} else {
this.file.append(d, data[d]);
}
}
data = this.file;
}
return _OA.jx(this.api.replace('://', '://bg.') + '/deposit', data, (res) => {
this.loading(false);
if (typeof this.file !== 'boolean') {
if ((res.zenodo !== undefined && res.zenodo.already) || (this.data.confirmed && (res.zenodo === undefined || !res.zenodo.url))) {
return this.done('check');
} else if (res.error) {
// if we should be able to deposit but can't, we stick to the positive response and the file will be manually checked
return this.done('partial');
} else if (res.zenodo !== undefined && res.zenodo.url) {
// deposit was possible, show the user a congrats page with a link to the item in zenodo
_OA.set('#_oaw_zenodo_url', res.zenodo.url);
if (res.embargo) {
info = '<p>You’ve done your part for now. Unfortunately, ' + (this.f !== undefined && this.f.metadata !== undefined && this.f.metadata.shortname ? this.f.metadata.shortname : (this.f !== undefined && this.f.metadata !== undefined && this.f.metadata.journal ? this.f.metadata.journal : 'the journal')) + ' won’t let us make it public until ';
info += (new Date(res.embargo)).toLocaleString('en-GB', {year: 'numeric', month: 'long', day: 'numeric'}).replace(/(11|12|13) /, '$1th ').replace('1 ', '1st ').replace('2 ', '2nd ').replace('3 ', '3rd ').replace(/([0-9]) /, '$1th ');
info += '. After release, you’ll find your paper on ' + (this.config.repo_name ? this.config.repo_name : 'ScholarWorks') + ', Google Scholar, Web of Science.</p>';
info += '<h3>Your paper will be freely available at this link:</h3>';
} else {
info = '<p>You’ll soon find your paper freely available in ' + (this.config.repo_name ? this.config.repo_name : 'ScholarWorks') + ', Google Scholar, Web of Science, and other popular tools.';
info += '<h3>Your paper is now freely available at this link:</h3>';
}
_OA.html('#_oaw_zenodo_embargo', info);
return this.done('zenodo');
} else {
// if the file given is not a version that is allowed, show a page saying something looks wrong
// also the backend should create a dark deposit in this case, but delay it by six hours, and cancel if received in the meantime
return this.done('confirm');
}
} else if (res.type === 'redeposit') {
return this.done('redeposit');
} else {
return this.done('success');
}
}, () => {
this.loading(false);
_OA.show('#_oaw_error', '<p>Sorry, we were not able to deposit this paper for you. ' + this.contact() + '</p><p><a href="#" class="_oaw_restart" id="_oaw_sorry_try_again"><strong>Try again</strong></a></p>');
return this.ping('shareyourpaper_couldnt_submit_deposit');
});
}
} catch (err) {
return this.ping('shareyourpaper_try_deposit_catch');
}
};
_oaw.prototype.permissions = function(data) {
// only used by shareyourpaper
// requests the permissions for a particular article identified by DOI from the API
// then shows suitable next steps on screen
var nj, p, paper, ph, pm, refs, rm, tcs;
this.is_bronze_archivable = false;
try {
if (data != null) {
this.f = data;
}
if (!_OA.gebi(this.element)) {
return setTimeout((() => {
return this.permissions();
}), 100);
} else {
this.loading(false);
if (this.f === undefined) this.f = {};
if (this.f.metadata === undefined) this.f.metadata = {};
if (this.f.permissions === undefined) this.f.permissions = {};
if (this.f.permissions.best_permission === undefined) this.f.permissions.best_permission = {};
if (this.f.doi_not_in_crossref || this.f.doi_not_in_oadoi) {
this.f = {};
_OA.show('#_oaw_error', '<p>Double check your DOI, that doesn’t look right to us.</p>');
return _OA.gebi('_oaw_input').focus();
} else if ((this.f.metadata.crossref_type !== undefined && this.f.metadata.crossref_type !== 'journal-article' && this.f.metadata.crossref_type !== 'proceedings-article') || (this.f.metadata.type !== undefined && this.f.metadata.type !== 'journal-article' && this.f.metadata.type !== 'proceedings-article')) {
_OA.gebi('_oaw_input').focus();
nj = '<p>Sorry, right now this only works with academic journal articles.';
if (this.cml() || (this.config.old_way && this.config.old_way.includes('@'))) {
nj += ' To get help with depositing, <a href="';
nj += this.config.old_way ? (this.config.old_way.includes('@') ? 'mailto:' : '') + this.config.old_way : 'mailto:' + this.cml();
nj += "?subject=Help%20depositing%20&body=Hi%2C%0D%0A%0D%0AI'd%20like%20to%20deposit%3A%0D%0A%0D%0A%3C%3CPlease%20insert%20a%20full%20citation%3E%3E%0D%0A%0D%0ACan%20you%20please%20assist%20me%3F%0D%0A%0D%0AYours%20sincerely%2C" + '">click here</a>';
}
return this.restart(void 0, void 0, nj + '.</p>');
} else if (!this.f.metadata.title) {
_OA.show('#_oaw_error', '<h3>Unknown paper</h3><p>Sorry, we cannot find this paper or sufficient metadata. ' + this.contact() + '</p>');
return this.ping('shareyourpaper_unknown_article');
} else {
_OA.hide('._oaw_panel');
_OA.hide('._oaw_section');
_OA.show('#_oaw_permissions');
this.loading(false);
tcs = '<a id="_oaw_terms" href="https://oa.works/policies/terms/" target="_blank" rel="noopener">OA.Works terms<span class="sr-only visually-hidden"> (opens in a new tab)</span></a>';
if (this.config.terms) {
tcs += ' and <a id="_oaw_config_terms" href="' + this.config.terms + '" target="_blank" rel="noopener noreferrer">additional terms<span class="sr-only visually-hidden"> (opens in a new tab)</span></a>';
}
ph = '[email protected]';
if ((this.config.email_domains != null) && this.config.email_domains.length) {