SFRA Server-side Javascript - Source: app_storefront_base/cartridge/controllers/Page.js menu

SFRA / Server-side JS / Source: app_storefront_base/cartridge/controllers/Page.js

  1. 'use strict';
  2. /**
  3. * @namespace Page
  4. */
  5. var server = require('server');
  6. var cache = require('*/cartridge/scripts/middleware/cache');
  7. var consentTracking = require('*/cartridge/scripts/middleware/consentTracking');
  8. var pageMetaData = require('*/cartridge/scripts/middleware/pageMetaData');
  9. /**
  10. * Page-Include : This end point is triggered when content assets are embedded within a rendered page ( eg footer content)
  11. * @name Base/Page-Include
  12. * @function
  13. * @memberof Page
  14. * @param {middleware} - server.middleware.include
  15. * @param {middleware} - cache.applyDefaultCache
  16. * @param {querystringparameter} - cid - the id of the content asset to be embeded in a full page
  17. * @param {category} - non-sensitive
  18. * @param {serverfunction} - get
  19. */
  20. server.get(
  21. 'Include',
  22. server.middleware.include,
  23. cache.applyDefaultCache,
  24. function (req, res, next) {
  25. var ContentMgr = require('dw/content/ContentMgr');
  26. var Logger = require('dw/system/Logger');
  27. var ContentModel = require('*/cartridge/models/content');
  28. var apiContent = ContentMgr.getContent(req.querystring.cid);
  29. if (apiContent) {
  30. var content = new ContentModel(apiContent, 'components/content/contentAssetInc');
  31. if (content.template) {
  32. res.render(content.template, { content: content });
  33. } else {
  34. Logger.warn('Content asset with ID {0} is offline', req.querystring.cid);
  35. res.render('/components/content/offlineContent');
  36. }
  37. } else {
  38. Logger.warn('Content asset with ID {0} was included but not found',
  39. req.querystring.cid);
  40. res.render('/components/content/offlineContent');
  41. }
  42. next();
  43. }
  44. );
  45. /**
  46. * Page-IncludeHeaderMenu : This is a local include that includes the navigation in the header
  47. * @name Base/Page-IncludeHeaderMenu
  48. * @function
  49. * @memberof Page
  50. * @param {middleware} - server.middleware.include
  51. * @param {middleware} - cache.applyDefaultCache
  52. * @param {category} - non-sensitive
  53. * @param {serverfunction} - get
  54. */
  55. server.get(
  56. 'IncludeHeaderMenu',
  57. server.middleware.include,
  58. cache.applyDefaultCache,
  59. function (req, res, next) {
  60. var catalogMgr = require('dw/catalog/CatalogMgr');
  61. var Categories = require('*/cartridge/models/categories');
  62. var siteRootCategory = catalogMgr.getSiteCatalog().getRoot();
  63. var topLevelCategories = siteRootCategory.hasOnlineSubCategories() ?
  64. siteRootCategory.getOnlineSubCategories() : null;
  65. res.render('/components/header/menu', new Categories(topLevelCategories));
  66. next();
  67. }
  68. );
  69. /**
  70. * Page-SetLocale : This end point is used to change the locale, language and currency of the site, it is not used in the base site, but it is in the base cartridge
  71. * @name Base/Page-SetLocale
  72. * @function
  73. * @memberof Page
  74. * @param {querystringparameter} - action - the end point that it should load after changing the locale
  75. * @param {querystringparameter} - code - the locale code to switch to
  76. * @param {querystringparameter} - CurrencyCode - the currency code to be assigned to the site
  77. * @param {querystringparameter} - queryString - the query string of the Current request so that it be reloaded in the new locale (eg pdp)
  78. * @param {category} - non-sensitive
  79. * @param {serverfunction} - get
  80. */
  81. server.get('SetLocale', function (req, res, next) {
  82. var URLUtils = require('dw/web/URLUtils');
  83. var Currency = require('dw/util/Currency');
  84. var Site = require('dw/system/Site');
  85. var BasketMgr = require('dw/order/BasketMgr');
  86. var Transaction = require('dw/system/Transaction');
  87. var currentBasket = BasketMgr.getCurrentBasket();
  88. var QueryString = server.querystring;
  89. var currency;
  90. var currentSite = Site.getCurrent();
  91. var allowedCurrencies = currentSite.allowedCurrencies;
  92. var queryStringObj = new QueryString(req.querystring.queryString || '');
  93. if (Object.hasOwnProperty.call(queryStringObj, 'lang')) {
  94. delete queryStringObj.lang;
  95. }
  96. if (req.setLocale(req.querystring.code)) {
  97. currency = Currency.getCurrency(req.querystring.CurrencyCode);
  98. if (allowedCurrencies.indexOf(req.querystring.CurrencyCode) > -1
  99. && (req.querystring.CurrencyCode !== req.session.currency.currencyCode)) {
  100. req.session.setCurrency(currency);
  101. if (currentBasket && currency && currentBasket.currencyCode !== currency.currencyCode) {
  102. Transaction.wrap(function () {
  103. currentBasket.updateCurrency();
  104. });
  105. }
  106. }
  107. var redirectUrl = URLUtils.url(req.querystring.action).toString();
  108. var qsConnector = redirectUrl.indexOf('?') >= 0 ? '&' : '?';
  109. redirectUrl = Object.keys(queryStringObj).length === 0
  110. ? redirectUrl += queryStringObj.toString()
  111. : redirectUrl += qsConnector + queryStringObj.toString();
  112. res.json({
  113. success: true,
  114. redirectUrl: redirectUrl
  115. });
  116. } else {
  117. res.json({ error: true }); // TODO: error message
  118. }
  119. next();
  120. });
  121. /**
  122. * Page-Locale : The Page-Locale endpoint is used as a remote include that renders the country selector ISML template
  123. * @name Base/Page-Locale
  124. * @function
  125. * @memberof Page
  126. * @param {querystringparameter} - mobile - if this value is truthy it will use the mobile version else it will load a more desktop freindly version
  127. * @param {category} - non-sensitive
  128. * @param {serverfunction} - get
  129. */
  130. server.get('Locale', function (req, res, next) {
  131. var LocaleModel = require('*/cartridge/models/locale');
  132. var Locale = require('dw/util/Locale');
  133. var Site = require('dw/system/Site');
  134. var currentSite = Site.getCurrent();
  135. var siteId = currentSite.getID();
  136. var allowedLocales = currentSite.allowedLocales;
  137. var currentLocale = Locale.getLocale(req.locale.id);
  138. var localeModel = new LocaleModel(currentLocale, allowedLocales, siteId);
  139. var template = req.querystring.mobile
  140. ? '/components/header/mobileCountrySelector'
  141. : '/components/header/countrySelector';
  142. res.render(template, { localeModel: localeModel });
  143. next();
  144. });
  145. /**
  146. * Page-Show : This end point will render a content asset in full storefront page
  147. * @name Base/Page-Show
  148. * @function
  149. * @memberof Page
  150. * @param {middleware} - cache.applyDefaultCache
  151. * @param {middleware} - consentTracking.consent
  152. * @param {querystringparameter} - cid - the id of the content asset to be displayed in a full page
  153. * @param {category} - non-sensitive
  154. * @param {serverfunction} - get
  155. */
  156. server.get('Show', cache.applyDefaultCache, consentTracking.consent, function (req, res, next) {
  157. var ContentMgr = require('dw/content/ContentMgr');
  158. var Logger = require('dw/system/Logger');
  159. var PageMgr = require('dw/experience/PageMgr');
  160. var ContentModel = require('*/cartridge/models/content');
  161. var pageMetaHelper = require('*/cartridge/scripts/helpers/pageMetaHelper');
  162. var page = PageMgr.getPage(req.querystring.cid);
  163. if (page != null && page.isVisible()) {
  164. if (!page.hasVisibilityRules()) {
  165. res.cachePeriod = 168; // eslint-disable-line no-param-reassign
  166. res.cachePeriodUnit = 'hours'; // eslint-disable-line no-param-reassign
  167. }
  168. res.page(page.ID, {});
  169. } else {
  170. var apiContent = ContentMgr.getContent(req.querystring.cid);
  171. if (apiContent) {
  172. var content = new ContentModel(apiContent, 'content/contentAsset');
  173. pageMetaHelper.setPageMetaData(req.pageMetaData, content);
  174. pageMetaHelper.setPageMetaTags(req.pageMetaData, content);
  175. if (content.template) {
  176. res.render(content.template, { content: content });
  177. } else {
  178. Logger.warn('Content asset with ID {0} is offline', req.querystring.cid);
  179. res.render('/components/content/offlineContent');
  180. }
  181. } else {
  182. Logger.warn('Content asset with ID {0} was included but not found', req.querystring.cid);
  183. }
  184. }
  185. next();
  186. }, pageMetaData.computedPageMetaData);
  187. module.exports = server.exports();