-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
router.js
655 lines (565 loc) · 22.4 KB
/
router.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
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
"use strict";
define(["jquery", "underscore", "backbone"], function ($, _, Backbone) {
/**
* @class UIRouter
* @classdesc MetacatUI Router
* @extends Backbone.Router
* @constructor
*/
var UIRouter = Backbone.Router.extend(
/** @lends UIRouter.prototype */ {
routes: {
"": "renderData", // the default route
"data/my-data(/page/:page)(/)": "renderMyData", // data search page
"data(/mode=:mode)(/query=:query)(/page/:page)(/)": "renderData", // data search page
"profile(/*username)(/s=:section)(/s=:subsection)(/)": "renderProfile",
"my-profile(/s=:section)(/s=:subsection)(/)": "renderMyProfile",
"signout(/)": "logout",
"signin(/)": "renderSignIn",
"signinsuccess(/)": "renderSignInSuccess",
"signin-help": "renderSignInHelp", //The Sign In troubleshotting page
"share(/*pid)(/)": "renderEditor", // registry page
"submit(/*pid)(/)": "renderEditor", // registry page
"quality(/s=:suiteId)(/:pid)(/)": "renderMdqRun", // MDQ page
"api(/:anchorId)(/)": "renderAPI", // API page
"projects(/:portalId)(/:portalSection)(/)": "renderPortal", // portal page
"edit/:portalTermPlural(/:portalIdentifier)(/:portalSection)(/)":
"renderPortalEditor",
drafts: "renderDrafts",
},
helpPages: {
search: "searchTips",
defaultPage: "searchTips",
},
initialize: function () {
// Add routes to portal dynamically using the appModel portal term
var portalTermPlural = MetacatUI.appModel.get("portalTermPlural");
this.route(
portalTermPlural + "(/:portalId)(/:portalSection)(/)",
["portalId", "portalSection"],
this.renderPortal,
);
this.listenTo(
Backbone.history,
"routeNotFound",
this.navigateToDefault,
);
// This route handler replaces the route handler we had in the
// routes table before which was "view/*pid". The * only finds URL
// parts until the ? but DataONE PIDs can have ? in them so we need
// to make this route more inclusive.
this.route(/^view\/(.*)$/, "renderMetadata");
//Track the history of pathnames
this.on("route", this.trackPathName);
// Clear stale JSONLD and meta tags
this.on("route", this.clearJSONLD);
this.on("route", this.clearHighwirePressMetaTags);
},
//Keep track of navigation movements
routeHistory: new Array(),
pathHistory: new Array(),
// Will return the last route, which is actually the second to last item in the route history,
// since the last item is the route being currently viewed
lastRoute: function () {
if (
typeof this.routeHistory === "undefined" ||
this.routeHistory.length <= 1
)
return false;
else return this.routeHistory[this.routeHistory.length - 2];
},
trackPathName: function (e) {
if (_.last(this.pathHistory) != window.location.pathname)
this.pathHistory.push(window.location.pathname);
},
//If the user or app cancelled the last route, call this function to revert the window location pathname back to the correct value
undoLastRoute: function () {
this.routeHistory.pop();
// Remove the last route and pathname from the history
if (_.last(this.pathHistory) == window.location.pathname)
this.pathHistory.pop();
//Change the pathname in the window location back
this.navigate(_.last(this.pathHistory), { replace: true });
},
renderAPI: function (anchorId) {
this.routeHistory.push("api");
MetacatUI.appModel.set("anchorId", anchorId);
var options = {
pageName: "api",
anchorId: anchorId,
};
if (!MetacatUI.appView.textView) {
require(["views/TextView"], function (TextView) {
MetacatUI.appView.textView = new TextView();
MetacatUI.appView.showView(MetacatUI.appView.textView, options);
});
} else MetacatUI.appView.showView(MetacatUI.appView.textView, options);
},
renderData: function (mode, query, page) {
this.routeHistory.push("data");
// Check for a page URL parameter
page = parseInt(page);
page = isNaN(page) || page < 1 ? 1 : page;
MetacatUI.appModel.set("page", page - 1);
// Check if we are using the new CatalogSearchView
if (!MetacatUI.appModel.get("useDeprecatedDataCatalogView")) {
require(["views/search/CatalogSearchView"], function (
CatalogSearchView,
) {
MetacatUI.appView.catalogSearchView = new CatalogSearchView({
initialQuery: query,
});
MetacatUI.appView.showView(MetacatUI.appView.catalogSearchView);
});
return;
}
// Check for a query URL parameter
if (typeof query !== "undefined" && query) {
MetacatUI.appSearchModel.set("additionalCriteria", [query]);
}
require(["views/DataCatalogView"], function (DataCatalogView) {
if (!MetacatUI.appView.dataCatalogView) {
MetacatUI.appView.dataCatalogView = new DataCatalogView();
}
if (mode) MetacatUI.appView.dataCatalogView.mode = mode;
MetacatUI.appView.showView(MetacatUI.appView.dataCatalogView);
});
},
renderMyData: function (page) {
//Only display this is the user is logged in
if (
!MetacatUI.appUserModel.get("loggedIn") &&
MetacatUI.appUserModel.get("checked")
)
this.navigate("data", { trigger: true });
else if (!MetacatUI.appUserModel.get("checked")) {
var router = this;
this.listenToOnce(
MetacatUI.appUserModel,
"change:checked",
function () {
if (MetacatUI.appUserModel.get("loggedIn"))
router.renderMyData(page);
else this.navigate("data", { trigger: true });
},
);
return;
}
this.routeHistory.push("data");
///Check for a page URL parameter
if (typeof page === "undefined") MetacatUI.appModel.set("page", 0);
else MetacatUI.appModel.set("page", page);
if (!MetacatUI.appView.dataCatalogView) {
require(["views/DataCatalogView"], function (DataCatalogView) {
MetacatUI.appView.dataCatalogView = new DataCatalogView();
MetacatUI.appView.dataCatalogView.searchModel =
MetacatUI.appUserModel.get("searchModel").clone();
MetacatUI.appView.showView(MetacatUI.appView.dataCatalogView);
});
} else {
MetacatUI.appView.dataCatalogView.searchModel = MetacatUI.appUserModel
.get("searchModel")
.clone();
MetacatUI.appView.showView(MetacatUI.appView.dataCatalogView);
}
},
renderMetadata: function (pid) {
pid = decodeURIComponent(pid);
this.routeHistory.push("metadata");
MetacatUI.appModel.set("lastPid", MetacatUI.appModel.get("pid"));
var seriesId;
//Check for a seriesId
if (pid.indexOf("version:") > -1) {
seriesId = pid.substr(0, pid.indexOf(", version:"));
pid = pid.substr(pid.indexOf(", version: ") + ", version: ".length);
}
//Save the id in the app model
MetacatUI.appModel.set("pid", pid);
if (!MetacatUI.appView.metadataView) {
require(["views/MetadataView"], function (MetadataView) {
MetacatUI.appView.metadataView = new MetadataView();
//Send the id(s) to the view
MetacatUI.appView.metadataView.seriesId = seriesId;
MetacatUI.appView.metadataView.pid = pid;
MetacatUI.appView.showView(MetacatUI.appView.metadataView);
});
} else {
//Send the id(s) to the view
MetacatUI.appView.metadataView.seriesId = seriesId;
MetacatUI.appView.metadataView.pid = pid;
// MetacatUI resets the dataPackage and dataPackageSynced
// attributes before rendering the view. These attributes are
// initialized on a per-dataset basis to prevent displaying the
// same dataset repeatedly.
MetacatUI.appView.metadataView.dataPackage = null;
MetacatUI.appView.metadataView.dataPackageSynced = false;
MetacatUI.appView.showView(MetacatUI.appView.metadataView);
}
},
renderProfile: function (username, section, subsection) {
this.closeLastView();
if (!username || !MetacatUI.appModel.get("enableUserProfiles")) {
this.routeHistory.push("summary");
// flag indicating /profile view
var viewOptions = { nodeSummaryView: true };
if (!MetacatUI.appView.statsView) {
require(["views/StatsView"], function (StatsView) {
MetacatUI.appView.statsView = new StatsView({
userType: "repository",
});
MetacatUI.appView.showView(
MetacatUI.appView.statsView,
viewOptions,
);
});
} else
MetacatUI.appView.showView(
MetacatUI.appView.statsView,
viewOptions,
);
} else {
this.routeHistory.push("profile");
MetacatUI.appModel.set("profileUsername", username);
if (section || subsection) {
var viewOptions = { section: section, subsection: subsection };
}
if (!MetacatUI.appView.userView) {
require(["views/UserView"], function (UserView) {
MetacatUI.appView.userView = new UserView();
MetacatUI.appView.showView(
MetacatUI.appView.userView,
viewOptions,
);
});
} else
MetacatUI.appView.showView(MetacatUI.appView.userView, viewOptions);
}
},
renderMyProfile: function (section, subsection) {
if (
MetacatUI.appUserModel.get("checked") &&
!MetacatUI.appUserModel.get("loggedIn")
)
this.renderSignIn();
else if (!MetacatUI.appUserModel.get("checked")) {
this.listenToOnce(
MetacatUI.appUserModel,
"change:checked",
function () {
if (MetacatUI.appUserModel.get("loggedIn"))
this.renderProfile(
MetacatUI.appUserModel.get("username"),
section,
subsection,
);
else this.renderSignIn();
},
);
} else if (
MetacatUI.appUserModel.get("checked") &&
MetacatUI.appUserModel.get("loggedIn")
) {
this.renderProfile(
MetacatUI.appUserModel.get("username"),
section,
subsection,
);
}
},
/*
* Renders the editor view given a root package identifier,
* or a metadata identifier. If the latter, the corresponding
* package identifier will be queried and then rendered.
*/
renderEditor: function (pid) {
//If there is no EML211EditorView yet, create one
if (!MetacatUI.appView.eml211EditorView) {
var router = this;
//Load the EML211EditorView file
require(["views/metadata/EML211EditorView"], function (
EML211EditorView,
) {
//Add the submit route to the router history
router.routeHistory.push("submit");
//Create a new EML211EditorView
MetacatUI.appView.eml211EditorView = new EML211EditorView({
pid: pid,
});
//Set the pid from the pid given in the URL
MetacatUI.appView.eml211EditorView.pid = pid;
//Render the EML211EditorView
MetacatUI.appView.showView(MetacatUI.appView.eml211EditorView);
});
} else {
//Set the pid from the pid given in the URL
MetacatUI.appView.eml211EditorView.pid = pid;
//Add the submit route to the router history
this.routeHistory.push("submit");
//Render the Editor View
MetacatUI.appView.showView(MetacatUI.appView.eml211EditorView);
}
},
/**
* Renders the Drafts view which is a simple view backed by LocalForage that
* lists drafts created in the Editor so users can recover any failed
* submissions.
*/
renderDrafts: function () {
require(["views/DraftsView"], function (DraftsView) {
MetacatUI.appView.draftsView = new DraftsView();
MetacatUI.appView.showView(MetacatUI.appView.draftsView);
});
},
/**
* Renders the PortalEditorView
* @param {string} [portalTermPlural] - This should match the `portalTermPlural` configured in the AppModel.
* @param {string} [portalIdentifier] - The id or labebl of the portal
* @param {string} [portalSection] - The name of the section within the portal to navigate to (e.g. "data")
*/
renderPortalEditor: function (
portalTermPlural,
portalIdentifier,
portalSection,
) {
//If the user navigated to a route with a portal term other than the one supported, then this is not a portal editor route.
if (portalTermPlural != MetacatUI.appModel.get("portalTermPlural")) {
this.navigateToDefault();
return;
}
// Add the overall class immediately so the navbar is styled correctly right away
$("body").addClass("Editor").addClass("Portal");
// Look up the portal document seriesId by its registered name if given
if (portalSection) {
this.routeHistory.push(
"edit/" +
MetacatUI.appModel.get("portalTermPlural") +
"/" +
portalIdentifier +
"/" +
portalSection,
);
} else {
if (!portalIdentifier) {
this.routeHistory.push(
"edit/" + MetacatUI.appModel.get("portalTermPlural"),
);
} else {
this.routeHistory.push(
"edit/" +
MetacatUI.appModel.get("portalTermPlural") +
"/" +
portalIdentifier,
);
}
}
require(["views/portals/editor/PortalEditorView"], function (
PortalEditorView,
) {
MetacatUI.appView.portalEditorView = new PortalEditorView({
portalIdentifier: portalIdentifier,
activeSectionLabel: portalSection,
});
MetacatUI.appView.showView(MetacatUI.appView.portalEditorView);
});
},
renderMdqRun: function (suiteId, pid) {
this.routeHistory.push("quality");
if (!MetacatUI.appView.mdqRunView) {
require(["views/MdqRunView"], function (MdqRunView) {
MetacatUI.appView.mdqRunView = new MdqRunView();
MetacatUI.appView.mdqRunView.suiteId = suiteId;
MetacatUI.appView.mdqRunView.pid = pid;
MetacatUI.appView.showView(MetacatUI.appView.mdqRunView);
});
} else {
MetacatUI.appView.mdqRunView.suiteId = suiteId;
MetacatUI.appView.mdqRunView.pid = pid;
MetacatUI.appView.showView(MetacatUI.appView.mdqRunView);
}
},
logout: function (param) {
//Clear our browsing history when we log out
this.routeHistory.length = 0;
if (
(typeof MetacatUI.appModel.get("tokenUrl") == "undefined" ||
!MetacatUI.appModel.get("tokenUrl")) &&
!MetacatUI.appView.registryView
) {
require(["views/RegistryView"], function (RegistryView) {
MetacatUI.appView.registryView = new RegistryView();
if (
MetacatUI.appView.currentView &&
MetacatUI.appView.currentView.onClose
)
MetacatUI.appView.currentView.onClose();
MetacatUI.appUserModel.logout();
});
} else {
if (
MetacatUI.appView.currentView &&
MetacatUI.appView.currentView.onClose
)
MetacatUI.appView.currentView.onClose();
MetacatUI.appUserModel.logout();
}
},
renderSignIn: function () {
var router = this;
//If there is no SignInView yet, create one
if (!MetacatUI.appView.signInView) {
require(["views/SignInView"], function (SignInView) {
MetacatUI.appView.signInView = new SignInView({
el: "#Content",
fullPage: true,
});
router.renderSignIn();
});
return;
}
//If the user status has been checked and they are already logged in, we will forward them to their profile
if (
MetacatUI.appUserModel.get("checked") &&
MetacatUI.appUserModel.get("loggedIn")
) {
this.navigate("my-profile", { trigger: true });
return;
}
//If the user status has been checked and they are NOT logged in, show the SignInView
else if (
MetacatUI.appUserModel.get("checked") &&
!MetacatUI.appUserModel.get("loggedIn")
) {
this.routeHistory.push("signin");
MetacatUI.appView.showView(MetacatUI.appView.signInView);
}
//If the user status has not been checked yet, wait for it
else if (!MetacatUI.appUserModel.get("checked")) {
this.listenToOnce(
MetacatUI.appUserModel,
"change:checked",
this.renderSignIn,
);
}
},
renderSignInSuccess: function () {
$("body").html("Sign-in successful.");
setTimeout(window.close, 1000);
},
renderSignInHelp: function () {
this.routeHistory.push("signin-help");
this.renderText({ pageName: "signInHelp" });
},
/**
* Renders the Portals Search view.
*/
renderPortalsSearch: function () {
require(["views/portals/PortalsSearchView"], function (
PortalsSearchView,
) {
MetacatUI.appView.showView(new PortalsSearchView({ el: "#Content" }));
});
},
/**
* renderPortal - Render the portal view based on the given name or id, as
* well as optional section
*
* @param {string} label The portal ID or name
* @param {string} portalSection A specific section within the portal
*/
renderPortal: function (label, portalSection) {
//If no portal was specified, go to the portal search view
if (!label) {
this.renderPortalsSearch();
return;
}
// Add the overall class immediately so the navbar is styled correctly right away
$("body").addClass("PortalView");
// Look up the portal document seriesId by its registered name if given
if (portalSection) {
this.routeHistory.push(
MetacatUI.appModel.get("portalTermPlural") +
"/" +
label +
"/" +
portalSection,
);
} else {
this.routeHistory.push(
MetacatUI.appModel.get("portalTermPlural") + "/" + label,
);
}
require(["views/portals/PortalView"], function (PortalView) {
MetacatUI.appView.portalView = new PortalView({
label: label,
activeSectionLabel: portalSection,
});
MetacatUI.appView.showView(MetacatUI.appView.portalView);
});
},
renderText: function (options) {
if (!MetacatUI.appView.textView) {
require(["views/TextView"], function (TextView) {
MetacatUI.appView.textView = new TextView();
MetacatUI.appView.showView(MetacatUI.appView.textView, options);
});
} else MetacatUI.appView.showView(MetacatUI.appView.textView, options);
},
/*
* Gets an array of route names that are set on this router.
* @return {Array} - An array of route names, not including any special characters
*/
getRouteNames: function () {
var router = this;
var routeNames = _.map(Object.keys(this.routes), function (routeName) {
return router.getRouteName(routeName);
});
//The "view" and portals routes are not included in the route hash (they are set up during initialize),
// so we have to manually add it here.
routeNames.push("view");
if (!routeNames.includes(MetacatUI.appModel.get("portalTermPlural"))) {
routeNames.push(MetacatUI.appModel.get("portalTermPlural"));
}
return routeNames;
},
/*
* Gets the route name based on the route pattern given
* @param {string} routePattern - A string that represents the route pattern e.g. "view(/pid)"
* @return {string} - The name of the route without any pattern special characters e.g. "view"
*/
getRouteName: function (routePattern) {
var specialChars = ["/", "(", "*", ":"];
_.each(specialChars, function (specialChar) {
var substring = routePattern.substring(
0,
routePattern.indexOf(specialChar),
);
if (substring && substring.length < routePattern.length) {
routePattern = substring;
}
});
return routePattern;
},
navigateToDefault: function () {
//Navigate to the default view
this.navigate(MetacatUI.appModel.defaultView, { trigger: true });
},
closeLastView: function () {
//Get the last route and close the view
var lastRoute = _.last(this.routeHistory);
if (lastRoute == "summary") MetacatUI.appView.statsView.onClose();
else if (lastRoute == "profile") MetacatUI.appView.userView.onClose();
},
clearJSONLD: function () {
MetacatUI.appView.schemaOrg.removeExistingJsonldEls();
MetacatUI.appView.schemaOrg.setSchemaFromTemplate();
},
clearHighwirePressMetaTags: function () {
$("head > meta[name='citation_title']").remove();
$("head > meta[name='citation_authors']").remove();
$("head > meta[name='citation_publisher']").remove();
$("head > meta[name='citation_date']").remove();
},
},
);
return UIRouter;
});