page.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var PageUtils = require('./page-utils.js');
  2. var CommandWrapper = require('./command-wrapper.js');
  3. /**
  4. * Class that all pages subclass from
  5. *
  6. * @param {Object} options Page options defined in page object
  7. * @constructor
  8. */
  9. function Page(options, commandLoader, api, client) {
  10. this.commandLoader = commandLoader;
  11. this.api = api;
  12. this.client = client;
  13. this.name = options.name;
  14. this.url = options.url;
  15. PageUtils
  16. .createProps(this, options.props || {})
  17. .createElements(this, options.elements || {})
  18. .createSections(this, options.sections || {})
  19. .addCommands(this, options.commands || []);
  20. CommandWrapper.addWrappedCommands(this, this.commandLoader);
  21. }
  22. Page.prototype = {
  23. /**
  24. * Returns the url passed as an argument (or null if no arguments are passed).
  25. * If the supplied url is a function, it invokes that function with the page as its context.
  26. *
  27. * @method getUrl
  28. * @param {string} url
  29. * @returns {string|null}
  30. */
  31. getUrl: function(url) {
  32. if (typeof url === 'function') {
  33. return url.call(this);
  34. } else if (typeof url === 'string') {
  35. return url;
  36. }
  37. return null;
  38. },
  39. /**
  40. * This command is an alias to url and also a convenience method because when called without any arguments
  41. * it performs a call to .url() with passing the value of `url` property on the page object.
  42. * Uses `url` protocol command.
  43. *
  44. * @method navigate
  45. * @param {Object} [url=this.url] Url to navigate to.
  46. * @param {function} [callback] Optional callback function to be called when the command finishes.
  47. * @returns {*}
  48. */
  49. navigate: function(url, callback) {
  50. var goToUrl = this.getUrl(url || this.url);
  51. if (goToUrl === null) {
  52. throw new Error('Invalid URL: You must either add a url property to "' +
  53. this.name + '" or provide a url as an argument');
  54. }
  55. this.api.url(goToUrl, callback);
  56. return this;
  57. }
  58. };
  59. module.exports = Page;