page-utils.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. module.exports = new (function() {
  2. /**
  3. * Returns the properties object passed as an argument (or null if no arguments are passed).
  4. * If the supplied properties argument is a function, it invokes that function with the page as its context.
  5. *
  6. * @method createProps
  7. * @param {Page|Section} parent The page object or section instance
  8. * @param {Object|Function} props Object or Function that returns an object
  9. * @returns {null}
  10. */
  11. this.createProps = function(parent, props) {
  12. parent.props = typeof props === 'function' ? props.call(parent) : props;
  13. return this;
  14. };
  15. /**
  16. * Assigns the `elements` property for a page or section object.
  17. * For each object in the passed array, it creates a new element object by instantiating Element with its options
  18. *
  19. * @param {Page|Section} parent The page object or section instance
  20. * @param {Object|Array} elements Object or array of objects to become element objects
  21. * @returns {null}
  22. */
  23. this.createElements = function(parent, elements) {
  24. var Element = require('./element.js');
  25. var elementObjects = {};
  26. var el;
  27. if (!Array.isArray(elements)) {
  28. elements = [elements];
  29. }
  30. elements.forEach(function(els) {
  31. Object.keys(els).forEach(function(e) {
  32. el = typeof els[e] === 'string' ? { selector: els[e] } : els[e];
  33. el.parent = parent;
  34. el.name = e;
  35. elementObjects[el.name] = new Element(el);
  36. });
  37. });
  38. parent.elements = elementObjects;
  39. return this;
  40. };
  41. /**
  42. * Assigns the `section` property for a page or section object.
  43. * For each object in the passed array, it creates a new section object by instantiating Section with its options
  44. *
  45. * @param {Page|Section} parent The page object or section instance
  46. * @param {Array} sections Array of objects to become section objects
  47. * @returns {null}
  48. */
  49. this.createSections = function(parent, sections) {
  50. var Section = require('./section.js');
  51. var sectionObjects = {};
  52. var sec;
  53. Object.keys(sections).forEach(function(s) {
  54. sec = sections[s];
  55. sec.parent = parent;
  56. sec.name = s;
  57. sectionObjects[sec.name] = new Section(sec);
  58. });
  59. parent.section = sectionObjects;
  60. return this;
  61. };
  62. /**
  63. * Mixes in the passed functions to the page or section object.
  64. *
  65. * @param {Page|Section} parent The page object or section instance
  66. * @param {Object} command Array of commands that will be added to the age or section
  67. * @returns {null}
  68. */
  69. this.addCommands = function(parent, commands) {
  70. commands.forEach(function(m) {
  71. Object.keys(m).forEach(function(k) {
  72. parent[k] = m[k];
  73. });
  74. });
  75. return this;
  76. };
  77. })();