Page 1 of 1

[EOS/CODE] PageManager Extensions (pages.contains / call / returnToCaller / reload)

Posted: Tue Oct 13, 2020 4:10 pm
by fapnip
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]);
Spoiler: show
use:

Code: Select all

pages.call('another-page');
Will push current page on the call stack and goto given page.
pages.returnToCaller();
Spoiler: show
use:

Code: Select all

pages.returnToCaller();
Will pull page that called us from call stack, and goto that page.
If altReturn argument was passed with pages.call(...), will try to goto that page instead. If altReturn argument was a function, will call that function. If function returns a truthy result, will try to goto that result. If altReturn result is truthy but not a valid page, returnToCaller will return false and not goto any page.
pages.contains(pageId)
Spoiler: show
use:

Code: Select all

if (pages.contains('my-page-name')) {
  console.log('Yay, this page exists!');
} else {
  console.warn('Boo! I don't have that page!');
}
Returns true or false depedning on if the given page exists or not.
pages.reload();
Spoiler: show
use:

Code: Select all

pages.reload();
Reloads current page.
pages.debug = true;
Spoiler: show
use:

Code: Select all

pages.debug = true;
Enables verbose console logging for Page Manager extensiosn, helpful for debugging code.
Note: Don't leave debugging enabled when you publish your tease.
Here's the full source, with comments, etc.:

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());
  }
}
And here's a minified version.

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())});
Just add the above to the top of your EOS Init Script to enable use in your tease.

Re: EOS PageManager Extensions (call(pageId), returnToCaller(), etc.)

Posted: Wed Oct 14, 2020 3:25 pm
by fapnip
(BTW: If you find this useful, please let me know. It would help me know if it's worth taking the time to share more advanced EOS tips and tricks. If no one is using them but, me, there's really no point in sharing.)