-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
172 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,173 @@ | ||
/* | ||
* adapt-pageIncompletePrompt | ||
* License - https://github.com/cgkineo/adapt_framework/blob/master/LICENSE | ||
* Maintainers - Thomas Taylor <thomas.taylor@kineo.com> | ||
*/ | ||
define(function(require) { | ||
var Adapt = require("coreJS/adapt"); | ||
var PLUGIN_NAME = "_pageIncompletePrompt"; | ||
var model; | ||
|
||
var pageComponents; | ||
|
||
var isEnabled = function() { | ||
var pageModel = Adapt.findById(Adapt.location._currentId); | ||
var isEnabledForCourse = model && !!model._isEnabled; | ||
var isEnabledForPage = pageModel.get("_pageIncompletePrompt") && !!pageModel.get("_pageIncompletePrompt")._isEnabled; | ||
return (isEnabledForCourse && isEnabledForPage !== false) || isEnabledForPage; | ||
}; | ||
|
||
var allComponentsComplete = function() { | ||
var allComplete = true; | ||
|
||
_.each(pageComponents, function(component) { | ||
var hasPageProgress = component.get("_pageLevelProgress") && component.get("_pageLevelProgress")._isEnabled; | ||
var isComplete = component.get("_isComplete"); | ||
if(hasPageProgress && !isComplete) allComplete = false; | ||
}); | ||
|
||
return allComplete; | ||
}; | ||
|
||
var enableRouterNav = function(value) { | ||
Adapt.router.set("_canNavigate", value,{ pluginName: PLUGIN_NAME }); | ||
}; | ||
|
||
var handleNav = function(event) { | ||
if(!allComponentsComplete()) { | ||
handlingRoute = true; | ||
var promptObject = { | ||
title: model.title, | ||
body: model.message, | ||
_prompts:[{ | ||
promptText: model._buttons.yes, | ||
_callbackEvent: "pageIncompletePrompt:" + event, | ||
},{ | ||
promptText: model._buttons.no, | ||
_callbackEvent: "pageIncompletePrompt:cancel" | ||
}], | ||
_showIcon: true | ||
} | ||
Adapt.trigger("notify:prompt", promptObject); | ||
define([ | ||
'coreJS/adapt' | ||
], function(Adapt) { | ||
|
||
|
||
var PageIncompletePrompt = _.extend({ | ||
|
||
PLUGIN_NAME: "_pageIncompletePrompt", | ||
|
||
handleRoute: true, | ||
inPage: false, | ||
inPopup: false, | ||
pageComponents: null, | ||
pageModel: null, | ||
routeArguments: null, | ||
model: null, | ||
|
||
_ignoreAccessibilityNavigation: false, | ||
|
||
initialize: function() { | ||
this.setupEventListeners(); | ||
}, | ||
|
||
setupEventListeners: function() { | ||
this.listenToOnce(Adapt, "app:dataLoaded", this.setupModel); | ||
this.listenTo(Adapt, "pageView:ready", this.onPageViewReady); | ||
this.listenTo(Adapt, "pageIncompletePrompt:leavePage", this.onLeavePage); | ||
this.listenTo(Adapt, "pageIncompletePrompt:cancel", this.onLeaveCancel); | ||
this.listenTo(Adapt, "router:navigate", this.onRouterNavigate); | ||
}, | ||
|
||
setupModel: function() { | ||
this.model = Adapt.course.get(this.PLUGIN_NAME); | ||
this.listenTo(Adapt, "accessibility:toggle", this.onAccessibilityToggle); | ||
}, | ||
|
||
onPageViewReady: function() { | ||
this.inPage = true; | ||
this.pageModel = Adapt.findById(Adapt.location._currentId); | ||
this.pageComponents = this.pageModel.findDescendants("components").where({"_isAvailable": true}); | ||
}, | ||
|
||
onLeavePage: function() { | ||
if (!this.inPopup) return; | ||
this.inPopup = false; | ||
|
||
this.enableRouterNavigation(true); | ||
this.handleRoute = false; | ||
this.inPage = false; | ||
|
||
Adapt.trigger("router:navigateTo", this.routeArguments); | ||
|
||
this.handleRoute = true; | ||
}, | ||
|
||
onLeaveCancel: function() { | ||
if (!this.inPopup) return; | ||
this.routeArguments = undefined; | ||
this.enableRouterNavigation(true); | ||
this.handleRoute = true; | ||
this.inPopup = false; | ||
}, | ||
|
||
onRouterNavigate: function(routeArguments) { | ||
if(!this.isEnabled() || this.allComponentsComplete()) return; | ||
|
||
if (routeArguments[0]) { | ||
//check if routing to current page child | ||
//exit if on same page | ||
try { | ||
var id = routeArguments[0]; | ||
var model = Adapt.findById(id); | ||
var parent = model.findAncestor("contentObjects"); | ||
if (parent.get("_id") == this.pageModel.get("_id")) return; | ||
} catch (e) {} | ||
} | ||
|
||
if (this._ignoreAccessibilityNavigation) { | ||
this._ignoreAccessibilityNavigation = false; | ||
return; | ||
} | ||
|
||
this.enableRouterNavigation(false) | ||
this.routeArguments = routeArguments; | ||
this.inPopup = true; | ||
|
||
var promptObject; | ||
var pageIncompletePromptConfig = this.pageModel.get("_pageIncompletePrompt"); | ||
if (pageIncompletePromptConfig && pageIncompletePromptConfig._buttons) { | ||
promptObject = { | ||
title: pageIncompletePromptConfig.title, | ||
body: pageIncompletePromptConfig.message, | ||
_prompts:[{ | ||
promptText: pageIncompletePromptConfig._buttons.yes, | ||
_callbackEvent: "pageIncompletePrompt:leavePage", | ||
},{ | ||
promptText: pageIncompletePromptConfig._buttons.no, | ||
_callbackEvent: "pageIncompletePrompt:cancel" | ||
}], | ||
_showIcon: true | ||
}; | ||
} else { | ||
promptObject = { | ||
title: this.model.title, | ||
body: this.model.message, | ||
_prompts:[{ | ||
promptText: this.model._buttons.yes, | ||
_callbackEvent: "pageIncompletePrompt:leavePage", | ||
},{ | ||
promptText: this.model._buttons.no, | ||
_callbackEvent: "pageIncompletePrompt:cancel" | ||
}], | ||
_showIcon: true | ||
}; | ||
} | ||
|
||
this.listenToOnce(Adapt, "notify:closed", this.onLeaveCancel); | ||
|
||
Adapt.trigger("notify:prompt", promptObject); | ||
}, | ||
|
||
onAccessibilityToggle: function() { | ||
if (Adapt.device.touch) { | ||
//accessibility is always on for touch devices | ||
//ignore toggle | ||
this._ignoreAccessibilityNavigation = false; | ||
} else { | ||
//skip renavigate for accessibility on desktop | ||
this._ignoreAccessibilityNavigation = true; | ||
} | ||
}, | ||
|
||
isEnabled: function() { | ||
if (!Adapt.location._currentId) return false; | ||
if (!this.handleRoute) return false; | ||
if (!this.inPage) return false; | ||
if (this.inPopup) return false; | ||
|
||
switch (Adapt.location._contentType) { | ||
case "menu": case "course": | ||
this.inPage = false; | ||
return false; | ||
} | ||
var pageModel = Adapt.findById(Adapt.location._currentId); | ||
if (pageModel.get("_isOptional")) return false; | ||
var isEnabledForCourse = this.model && !!this.model._isEnabled; | ||
var isEnabledForPage = pageModel.get("_pageIncompletePrompt") && !!pageModel.get("_pageIncompletePrompt")._isEnabled; | ||
return (isEnabledForCourse && isEnabledForPage !== false) || isEnabledForPage; | ||
}, | ||
|
||
allComponentsComplete: function() { | ||
var allComplete = true; | ||
|
||
_.each(this.pageComponents, function(component) { | ||
var hasPageProgress = component.get("_pageLevelProgress") && component.get("_pageLevelProgress")._isEnabled; | ||
var isComplete = component.get("_isComplete"); | ||
if(hasPageProgress && !isComplete) allComplete = false; | ||
}); | ||
|
||
return allComplete; | ||
}, | ||
|
||
enableRouterNavigation: function(value) { | ||
Adapt.router.set("_canNavigate", value, { pluginName: this.PLUGIN_NAME }); | ||
} | ||
else enableRouterNav(true); | ||
}; | ||
/** | ||
* Adapt events | ||
*/ | ||
|
||
Adapt.on("app:dataLoaded", function() { | ||
model = Adapt.course.get(PLUGIN_NAME); | ||
}); | ||
|
||
|
||
Adapt.on("pageView:ready", function (){ | ||
var pageModel = Adapt.findById(Adapt.location._currentId); | ||
pageComponents = pageModel.findDescendants("components").where({"_isAvailable": true}); | ||
if(isEnabled() && !allComponentsComplete()) enableRouterNav(false); | ||
}); | ||
|
||
Adapt.on("pageIncompletePrompt:leavePage", function() { | ||
enableRouterNav(true); | ||
Adapt.trigger("navigation:backButton"); | ||
}); | ||
|
||
var handlingRoute = false; | ||
|
||
Adapt.on("navigation:backButton", function() { | ||
if (handlingRoute) return; | ||
handleNav("backButton"); | ||
}); | ||
Adapt.on("navigation:menuButton", function() { | ||
if (handlingRoute) return; | ||
handleNav("menuButton"); | ||
}); | ||
|
||
Adapt.on("pageIncompletePrompt:backButton", function() { | ||
enableRouterNav(true); | ||
Adapt.trigger("navigation:backButton"); | ||
handlingRoute = false; | ||
}); | ||
Adapt.on("pageIncompletePrompt:menuButton", function() { | ||
enableRouterNav(true); | ||
Adapt.trigger("navigation:menuButton"); | ||
handlingRoute = false; | ||
}); | ||
Adapt.on("pageIncompletePrompt:cancel", function() { | ||
handlingRoute = false; | ||
}); | ||
|
||
}, Backbone.Events); | ||
|
||
PageIncompletePrompt.initialize(); | ||
|
||
return PageIncompletePrompt; | ||
|
||
}); |