From 37801851096b8c475321f5885c99ae0d1a0074e4 Mon Sep 17 00:00:00 2001 From: Tim Hostetler Date: Mon, 25 Jun 2018 16:10:24 -0400 Subject: [PATCH] Add Timer --- src/js/modules/orcid/extension.js | 2 ++ src/js/widgets/navbar/widget.js | 60 ++++++++++++++++++------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/js/modules/orcid/extension.js b/src/js/modules/orcid/extension.js index 820ca396b..035b7b06f 100644 --- a/src/js/modules/orcid/extension.js +++ b/src/js/modules/orcid/extension.js @@ -610,6 +610,8 @@ function ( var action = data.action; var orcidApi = this.getBeeHive().getService('OrcidApi'); var oldOrcid = _.clone(data.model.get('orcid') || {}); + var pubsub = this.getPubSub(); + pubsub.publish(pubsub.CUSTOM_EVENT, 'orcid-action', action); var handlers = { 'orcid-add': function (model) { diff --git a/src/js/widgets/navbar/widget.js b/src/js/widgets/navbar/widget.js index 5d5daad60..ed9591810 100644 --- a/src/js/widgets/navbar/widget.js +++ b/src/js/widgets/navbar/widget.js @@ -31,7 +31,8 @@ define([ orcidFirstName: undefined, orcidLastName: undefined, //should it show hourly banner? - hourly: false + hourly: false, + timeout: 600000 // 10 minutes } } }); @@ -90,25 +91,11 @@ define([ this.trigger("user-change-orcid-mode"); }, - changeOrcidMode: function () { - var that = this; - //allow animation to run before rerendering - setTimeout(function () { - - if (that.$(".orcid-mode").is(":checked")) { - that.model.set("orcidModeOn", true); - } - else { - that.model.set("orcidModeOn", false); - } - - //need to explicitly trigger to widget that this has changed - //otherwise it will be ignored, since it can also be changed - //from outside - that.trigger("user-change-orcid-mode"); - - that.render(); - }, 400); + changeOrcidMode: function (ev) { + var checked = _.isBoolean(ev) ? ev : ev && ev.currentTarget && ev.currentTarget.checked; + this.model.set('orcidModeOn', checked); + this.trigger('user-change-orcid-mode'); + this.render(); }, onRender: function () { @@ -156,12 +143,22 @@ define([ activate: function (beehive) { this.setBeeHive(beehive); - _.bindAll(this, ["handleUserAnnouncement", "getOrcidUserInfo", "storeLatestPage"]); + _.bindAll(this, ["handleUserAnnouncement", "getOrcidUserInfo", "storeLatestPage", 'onCustomEvent']); var pubsub = this.getPubSub(); pubsub.subscribe(pubsub.USER_ANNOUNCEMENT, this.handleUserAnnouncement); pubsub.subscribe(pubsub.APP_STARTED, this.getOrcidUserInfo); - pubsub.subscribe(pubsub.NAVIGATE, this.storeLatestPage) + pubsub.subscribe(pubsub.NAVIGATE, this.storeLatestPage); + pubsub.subscribe(pubsub.CUSTOM_EVENT, this.onCustomEvent); this.setInitialVals(); + if (!this.model.get('timer')) { + this.resetOrcidTimer(); + } + }, + + onCustomEvent: function (ev) { + if (ev === 'orcid-action') { + this.resetOrcidTimer(); + } }, storeLatestPage : function(page){ @@ -169,6 +166,19 @@ define([ this._latestPage = page; }, + resetOrcidTimer: function () { + var timer = this.model.get('timer'); + var timeout = this.model.get('timeout'); + if (timer) { + clearTimeout(timer); + } + + // only start the timer if orcid mode is actually on + if (this.model.get('orcidModeOn')) { + timer = setTimeout(_.bind(this.toggleOrcidMode, this, false), timeout); + this.model.set('timer', timer); + } + }, viewEvents: { //dealing with authentication/user @@ -307,12 +317,13 @@ define([ //we don't want to respond to changes from pubsub or user object with this, //only changes that the user has initiated using the navbar widget, //otherwise things will be toggled incorrectly - toggleOrcidMode: function () { + toggleOrcidMode: function (val) { var user = this.getBeeHive().getObject('User'), orcidApi = this.getBeeHive().getService("OrcidApi"); - var newVal = this.model.get("orcidModeOn"); + var newVal = _.isBoolean(val) ? val : this.model.get("orcidModeOn"); user.setOrcidMode(newVal); + this.model.set('orcidModeOn', newVal); if (newVal) { //sign into orcid api if not signed in already @@ -320,6 +331,7 @@ define([ orcidApi.signIn(); } } + this.resetOrcidTimer(); }, searchAuthor: function () {