[EOS/CODE] PageManager Extensions (pages.contains / call / returnToCaller / reload)
Posted: Tue Oct 13, 2020 4:10 pm
I decided to consolidate and share all the EOS Page Manager extensions I use in my teases in case anyone else finds them useful.
Adds the following methods to the PageManager:
pages.call(pageId, [Optional Alternate Page Callback]);
And here's a minified version.
Just add the above to the top of your EOS Init Script to enable use in your tease.
Adds the following methods to the PageManager:
pages.call(pageId, [Optional Alternate Page Callback]);
- Spoiler: show
- Spoiler: show
- Spoiler: show
- Spoiler: show
- Spoiler: show
Code: Select all
// EOS PageManager extensions v1.1
// See: https://milovana.com/forum/viewtopic.php?p=288665
if (!pages.call) {
/**
* Set "pages.debug = true;" to enable debug output
*/
pages.debug = false;
/**
* Page call stack.
*/
pages.stack = [];
/**
* Check if current list of pages contains a given page id
* @pageId The name of the page you're testing for existance.
* @return true if page exists, false if page does not
*/
pages.contains = function (pageId) {
// Store the original state of the page, if any.
// (pages.isEnabled will always return true for pages that don't exist)
var origState = pages.isEnabled(pageId)
// Disable the page (will not store disabled state for pages that don't exist)
pages.disable(pageId)
// If the page is now disabled, it exists. Missing pages will always return true.
var hasPage = !pages.isEnabled(pageId)
// If the page exists, restore its state to what it was when we got here
if (hasPage && origState) pages.enable(pageId)
// Return our result
return hasPage
}
/**
* Record the current page on the page stack, and goto the given page.
* @pageId Page you're calling
* @altReturn Optional alternate return page. Can be a function.
* @return false if called page is invalid. true if able to call page.
*/
pages.call = function (pageId, altReturn) {
if (pages.contains(pageId)) {
pages.stack.push({
caller: pages.getCurrentPageId(),
altReturn: altReturn
});
if (pages.debug) console.log('Calling page: ', pageId, pages.stack[pages.stack.length-1]);
pages.goto(pageId);
return true;
} else {
console.error("Unable to call page. Invalid pageId:", pageId)
return false;
}
}
/**
* Pulls last page called from the page stack.
* (Don't use this unless you know what you're doing.)
* @return page stack entry of last page called. undefined if no pages on stack.
*/
pages.popStack = function () {
var poppedPage = pages.stack.pop();
if (!poppedPage) {
if (pages.debug) console.warn("Page stack is empty. Unable to pop page.");
}
if (pages.debug) console.log('Pulled page from stack: ', poppedPage);
return poppedPage;
}
/**
* Clears page stack.
* (Don't use this unless you know what you're doing.)
*/
pages.clearStack = function () {
if (pages.debug) console.log('Clearing ' + pages.stack.length + ' pages from page stack.', pages.stack);
pages.stack = [];
}
/**
* Return to last page called, or altReturn if in stack entry, after first executing altReturn if it's a function.
*/
pages.returnToCaller = function () {
var lastPage = pages.popStack();
if (pages.debug) console.log('Returning to page caller: ', lastPage);
if (lastPage) {
var altReturn = lastPage.altReturn;
if (typeof altReturn === 'function') {
if (pages.debug) console.log('Executing altReturn callback');
altReturn = altReturn();
}
if (altReturn) {
if ((typeof altReturn === 'string') && pages.contains(altReturn)) {
if (pages.debug) console.log('Returning to alternate caller page "' + altReturn
+ '" instead of actual caller "' + lastPage.caller + '"');
pages.goto(altReturn);
return true;
} else {
if (pages.debug) console.warn('Alternate caller page "' + altReturn
+ '" does not exist. Skipping return to any caller."', lastPage);
return false;
}
} else {
if (pages.debug) console.warn('Returning to page caller:', lastPage.caller);
pages.goto(lastPage.caller);
return true;
}
} else {
console.error('Unable to return to calling page. Page stack is empty.');
return false;
}
}
/**
* Reload current page.
*/
pages.reload = function () {
if (pages.debug) console.log('Realoding current page: ', pages.getCurrentPageId());
pages.goto(pages.getCurrentPageId());
}
}Code: Select all
// EOS PageManager extensions v1.1
// See: https://milovana.com/forum/viewtopic.php?p=288665
pages.call||(pages.debug=!1,pages.stack=[],pages.contains=function(e){var a=pages.isEnabled(e);pages.disable(e);var g=!pages.isEnabled(e);return g&&a&&pages.enable(e),g},pages.call=function(e,a){return pages.contains(e)?(pages.stack.push({caller:pages.getCurrentPageId(),altReturn:a}),pages.debug&&console.log("Calling page: ",e,pages.stack[pages.stack.length-1]),pages.goto(e),!0):(console.error("Unable to call page. Invalid pageId:",e),!1)},pages.popStack=function(){var e=pages.stack.pop();return e||pages.debug&&console.warn("Page stack is empty. Unable to pop page."),pages.debug&&console.log("Pulled page from stack: ",e),e},pages.clearStack=function(){pages.debug&&console.log("Clearing "+pages.stack.length+" pages from page stack.",pages.stack),pages.stack=[]},pages.returnToCaller=function(){var e=pages.popStack();if(pages.debug&&console.log("Returning to page caller: ",e),e){var a=e.altReturn;return"function"==typeof a&&(pages.debug&&console.log("Executing altReturn callback"),a=a()),a?"string"==typeof a&&pages.contains(a)?(pages.debug&&console.log('Returning to alternate caller page "'+a+'" instead of actual caller "'+e.caller+'"'),pages.goto(a),!0):(pages.debug&&console.warn('Alternate caller page "'+a+'" does not exist. Skipping return to any caller."',e),!1):(pages.debug&&console.warn("Returning to page caller:",e.caller),pages.goto(e.caller),!0)}return console.error("Unable to return to calling page. Page stack is empty."),!1},pages.reload=function(){pages.debug&&console.log("Realoding current page: ",pages.getCurrentPageId()),pages.goto(pages.getCurrentPageId())});