Skip to content

Commit

Permalink
redid history management object, ads login now redirects to page prio…
Browse files Browse the repository at this point in the history
…r to auth page, logout no longer redirects if not necessary, and orcid log in loads prior page from local storage/current query from local storage if possible
  • Loading branch information
aholachek committed Dec 21, 2015
1 parent 246bbb6 commit 4b68eff
Show file tree
Hide file tree
Showing 20 changed files with 226 additions and 160 deletions.
10 changes: 10 additions & 0 deletions common-patterns.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

//widget can publish an alert

var pubsub = that.getBeeHive().getPubSub();
pubsub.publish(pubsub.ALERT, new ApiFeedback({
code: ApiFeedback.CODES.ALERT,
msg: "Logged in to ADS",
type: "success",
modal : true
}));
6 changes: 3 additions & 3 deletions src/discovery.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ require.config({
QueryMediator: 'js/components/query_mediator',
Diagnostics: 'js/bugutils/diagnostics',
AlertsController: 'js/wraps/alerts_mediator',
Orcid: 'js/modules/orcid/module'
Orcid: 'js/modules/orcid/module',
},
services: {
Api: 'js/services/api',
PubSub: 'js/services/pubsub',
Navigator: 'js/apps/discovery/navigator',
PersistentStorage: 'js/services/storage'
PersistentStorage: 'js/services/storage',
HistoryManager: 'js/components/history_manager',
},
objects: {
User: 'js/components/user',
Session: 'js/components/session',
DynamicConfig: 'discovery.vars',
HistoryManager: 'js/components/history_manager',
MasterPageManager: 'js/page_managers/master',
AppStorage: 'js/components/app_storage',
RecaptchaManager : 'js/components/recaptcha_manager',
Expand Down
3 changes: 2 additions & 1 deletion src/js/apps/discovery/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ define(['config', 'module'], function(config, module) {
'js/mixins/api_access',
'es5-shim'
],
function(Router,
function(
Router,
Application,
DiscoveryBootstrap,
ApiAccess
Expand Down
3 changes: 2 additions & 1 deletion src/js/apps/discovery/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,11 @@ define([
}
});

this.set('results-page', function() {
this.set('results-page', function(widget, args) {

app.getObject('MasterPageManager').show('SearchPage',
searchPageAlwaysVisible);
//allowing widgets to override appstorage query (so far only used for orcid redirect)
var q = app.getObject('AppStorage').getCurrentQuery();
var route = '#search/' + queryUpdater.clean(q).url();

Expand Down
2 changes: 0 additions & 2 deletions src/js/apps/discovery/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ define([

initialize : function(options){
options = options || {};
this.history = options.history;
this.queryUpdater = new ApiQueryUpdater('Router');
},

Expand Down Expand Up @@ -91,7 +90,6 @@ define([

paperForm : function(){
this.routerNavigate('PaperSearchForm');

},

search: function (query, widgetName) {
Expand Down
19 changes: 17 additions & 2 deletions src/js/components/app_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ define([
var AppStorage = Backbone.Model.extend({

activate: function(beehive) {
var that = this;
this.setBeeHive(beehive);
_.bindAll(this, "onPaperSelection", "onBulkPaperSelection");
var pubsub = this.getPubSub();
pubsub.subscribe(pubsub.PAPER_SELECTION, this.onPaperSelection);
pubsub.subscribe(pubsub.BULK_PAPER_SELECTION, this.onBulkPaperSelection);
//see if we can load an api query from app storage
if (this.getBeeHive().getService("PersistentStorage")){
var stashedQuery = this.getBeeHive().getService("PersistentStorage").get("currentQuery");
this.setCurrentQuery(new ApiQuery(stashedQuery));
}
},

initialize: function() {
Expand All @@ -49,6 +55,9 @@ define([
throw new Error('You must save ApiQuery instance');
}
this.set('currentQuery', apiQuery);
//save to storage
if (this.getBeeHive().getService("PersistentStorage"))
this.getBeeHive().getService("PersistentStorage").set("currentQuery", apiQuery.toJSON());
},

setCurrentNumFound : function(numFound){
Expand Down Expand Up @@ -163,11 +172,12 @@ define([

//this is used when authentication requires an interruption in user flow
//stashedPage val should be the name of a navigate command
//should by called with 1 param: an array of args used for pubsub.navigate command

setStashedNav : function(){
setStashedNav : function(pageInstructions){
var storage = this.getBeeHive().getService('PersistentStorage');
if (storage){
storage.set("stashedNavArgs", [].slice.apply(arguments));
storage.set("stashedNavArgs", pageInstructions);
}
else {
console.warn("no persistent storage service available");
Expand All @@ -179,6 +189,11 @@ define([
if (storage){
var args = storage.get("stashedNavArgs");
if (!args) return false;
//it's a results page, so initiate a start search
if (args[0] == "results-page" && this.getCurrentQuery()){
//this feels hackish, but only easy way to run the stored query
this.getPubSub().publish(this.getPubSub().START_SEARCH, this.getCurrentQuery());
}
this.getPubSub().publish.apply(this.getPubSub(), [this.getPubSub().NAVIGATE].concat(args));
storage.remove("stashedNavArgs");
return true;
Expand Down
114 changes: 37 additions & 77 deletions src/js/components/history_manager.js
Original file line number Diff line number Diff line change
@@ -1,92 +1,52 @@
define(["underscore"], function (_) {

var HistoryManager = function () {
this.history = []
}
define([
'js/components/generic_module',
'js/mixins/dependon',
'js/mixins/hardened',
'js/components/pubsub_key'
],
function(
GenericModule,
Dependon,
Hardened,
PubSubKey
) {

_.extend(HistoryManager.prototype, {
var History = GenericModule.extend({

initialize: function () {
this._history = [];
},

/*
* This will return the prior PAGE--so the history entry before the current
* one. If you are on the abstract page and were on the results page before that,
* that is what will be returned. Even if you have been on several routes within
* the abstract page, these will be ignored and you will be given the
* actual page name.
*
* */
getPriorPage : function () {
activate: function (beehive) {

if (this.history.length <=1){
return undefined
}
var currentPage, p, priorPage;
this.setBeeHive(beehive);
var pubsub = this.getPubSub();
pubsub.subscribe(pubsub.NAVIGATE, _.bind(this.recordNav, this));

currentPage = _.last(this.history).page;
//first, simplify the dict to include only pages, not subpages
p = 1;
while (p < this.history.length){
},

priorPage = this.history[(this.history.length -1) -p].page
recordNav: function () {
this._history.push([].slice.apply(arguments));
},

if (priorPage !== currentPage){
return priorPage
}
p+=1

}
//nothing was found that differs from currentPage
return undefined
},
getPriorPageVal : function () {

if (this.history.length <=1){
return undefined
}
getCurrentNav : function(){
return this._history[this._history.length-1];
},

var currentPage, p, prior, priorPage;
getPreviousNav: function(){
return this._history[this._history.length-2];
},

currentPage = _.last(this.history).page;
//first, simplify the dict to include only pages, not subpages
p = 1;

while (p < this.history.length){
prior= this.history[(this.history.length -1) -p]
priorPage = prior.page

if (priorPage !== currentPage){
return prior.data
hardenedInterface: {
getPreviousNav : "",
getCurrentNav : "",
}

p+=1
}
return undefined
},

/*
* Unlike get prior page, this will give you whatever the prior
* route was, even if it was within the current page.
* */

getPriorRoute : function(){

var subPage = this.history[this.history.length -1].subPage;
var page = this.history[this.history.length -1].page;

return {page: page, subPage : subPage}
},

getPriorRouteVal : function(){

return this.history[this.history.length -1].data

},
});

addEntry : function (item) {
this.history.push(item)
}
})
_.extend(History.prototype, Dependon.BeeHive, Hardened);

return HistoryManager
return History;

})
});
21 changes: 14 additions & 7 deletions src/js/components/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ define([
var options = options || {};
//right now, this will only be used if someone forgot their password
this.model = new SessionModel();

this.test = options.test ? true : undefined;

_.bindAll(this, [
Expand All @@ -68,6 +67,9 @@ define([

login: function (data) {

var d = $.Deferred(),
that = this;

this.sendRequestWithNewCSRF(function(csrfToken){
var request = new ApiRequest({
target : ApiTargets.USER,
Expand All @@ -77,16 +79,24 @@ define([
data: JSON.stringify(data),
contentType : "application/json",
headers : {'X-CSRFToken' : csrfToken },
done : this.loginSuccess,
fail : this.loginFail,
done : function(){
//allow widgets to listen for success or failure
d.resolve.apply(arguments);
//session response to success
that.loginSuccess.apply(arguments);
},
fail : function(){
d.reject.apply(arguments);
that.loginFail.apply(arguments);
},
beforeSend: function(jqXHR, settings) {
jqXHR.session = this;
}
}
});
return this.getBeeHive().getService("Api").request(request);

});
return d.promise();
},

/*
Expand Down Expand Up @@ -200,9 +210,6 @@ define([
//reset auth token by contacting Bootstrap, which will log user in
var that = this;
this.getApiAccess({reconnect: true}).done(function(){
//user has just authenticated themselves using the form, so redirect them to their account
var pubsub = that.getPubSub();
pubsub.publish(pubsub.NAVIGATE, "UserPreferences");
});
},

Expand Down
9 changes: 5 additions & 4 deletions src/js/components/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,12 @@ define([

redirectIfNecessary : function(){
var pubsub = this.getPubSub();

//redirect user to wherever they were before authentication page
if (this.getBeeHive().getObject("MasterPageManager").currentChild === "AuthenticationPage" && this.isLoggedIn()){
pubsub.publish(pubsub.NAVIGATE, "index-page");
//so that navigator can redirect to the proper page
var previousNav = this.getBeeHive().getService("HistoryManager").getPreviousNav();
pubsub.publish.apply(pubsub, [pubsub.NAVIGATE].concat(previousNav));
}
else if (this.getBeeHive().getObject("MasterPageManager").currentChild === "SettingsPage" && !this.isLoggedIn()){
pubsub.publish(pubsub.NAVIGATE, "authentication-page");
Expand All @@ -311,11 +315,8 @@ define([
//this function is called immediately after the logout is confirmed
completeLogOut : function(){
this.userModel.clear();
//navigate to the index page
this.getPubSub().publish(this.getPubSub().NAVIGATE, "index-page");
},


// publicly accessible

deleteAccount : function(){
Expand Down
6 changes: 6 additions & 0 deletions src/js/modules/orcid/orcid_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ define([
+ "&redirect_uri=" + encodeURIComponent(this.config.redirectUrlBase +
(targetRoute || '/#/user/orcid'))
});
//make sure to redirect to the proper page after sign in
this.getPubSub().publish(this.getPubSub().ORCID_ANNOUNCEMENT, "login");
var currentPage = this.getBeeHive().getService("HistoryManager").getCurrentNav();
this.getBeeHive().getObject("AppStorage").setStashedNav(currentPage);

},

/*
Expand Down Expand Up @@ -215,6 +220,7 @@ define([
*/
signOut: function () {
this.saveAccessData(null);
this.getPubSub().publish(this.getPubSub().ORCID_ANNOUNCEMENT, "logout");
},

hasExchangeCode: function (searchString) {
Expand Down
Loading

0 comments on commit 4b68eff

Please sign in to comment.