-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4af1046
commit 4d2f60a
Showing
44 changed files
with
873 additions
and
331 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
181 changes: 181 additions & 0 deletions
181
cartridges/int_styla/cartridge/controllers/StylaMagazine.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Controller exposes methods for injecting Styla JavaScript and SEO content. | ||
* | ||
* @module controllers/StylaMagazine | ||
* | ||
* | ||
* Do not reference the storefront controller cartridge here (e.g to re-use 'app' or 'guard'): | ||
* This montroller module is also called via remote include even when the storefront uses pipelines. | ||
* IOW: the storefront controller cartridge may or may not exist when this controller executes. | ||
* | ||
*/ | ||
|
||
var Logger = require('dw/system/Logger').getLogger('styla', 'StylaMagazine'); | ||
var ISML = require('dw/template/ISML'); | ||
|
||
var StylaMain = require('/int_styla/cartridge/scripts/StylaMain'); | ||
|
||
|
||
const CONFIG_CO_TYPE = 'StylaMagazineConfiguration'; // custom object type for storing magazine configurations | ||
const CONFIG_CO_KEY_ATTR = 'Key_and_Sort_Order'; // name of the custom object's key attribute | ||
const CONFIG_CO_SORT_ORRDER = 'custom.' + CONFIG_CO_KEY_ATTR + ' asc'; // sort order of custom objects | ||
|
||
|
||
|
||
function renderContent(template) { | ||
var pdict = StylaMain.GetRenderContent(); | ||
if (pdict) { | ||
ISML.renderTemplate(template, pdict); | ||
} | ||
else { | ||
ISML.renderTemplate('styla/empty'); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Inject Styla JavaScript library and SEO content into page header. Use as remote include. | ||
*/ | ||
function headerContent() { | ||
renderContent('styla/headercontent'); | ||
} | ||
|
||
|
||
/** | ||
* Inject Styla JavaScript library and SEO content into page body. Use as remote include. | ||
*/ | ||
function bodyContent() { | ||
renderContent('styla/bodycontent'); | ||
} | ||
|
||
|
||
/** | ||
* Render the Styla cartridge version. | ||
*/ | ||
function cartridgeVersion() { | ||
|
||
var versionInfo = { | ||
version: require('~/package.json').cartridgeVersion | ||
}, | ||
str; | ||
|
||
if (request.httpParameterMap.isParameterSubmitted('username')) { | ||
var StylaMain = require('/int_styla/cartridge/scripts/StylaMain'); | ||
var userName = request.httpParameterMap.get('username').stringValue; | ||
versionInfo.seoContent = StylaMain.GetContentVersion({username: userName}); | ||
} | ||
|
||
str = JSON.stringify(versionInfo, null, '\t'); | ||
|
||
response.setContentType('application/json'); | ||
response.setExpires(5 * 60 * 1000); // 5 minutes | ||
response.writer.print(str); | ||
} | ||
|
||
|
||
/** | ||
* If the current URL is part of a magazine, then jump to the corresponding | ||
* controller method so that the original URL is preserved in the browser. | ||
* | ||
* This is called from RedirectURL.start() if no matching redirect rule was found. | ||
* | ||
* E.g. assume we have an alias 'magazine' assigned to a pipeline which renders a Styla magazine. | ||
* When interacting with the magazine the Styla JavaScript will modify the URL in the | ||
* customer's browser to e.g. 'magazine/stories/5'. | ||
* Because RedirectUrl.start() doesn't find a matching rule for 'magazine/stories/5' | ||
* it calls this function. | ||
* | ||
* @param path Original URL before redirect. | ||
* @returns True, if a matching magazine configuration was found and the configured | ||
* controller method was called successfully. | ||
*/ | ||
function alias(path) { | ||
var result = false, | ||
magazineConfig = StylaMain.GetConfigForAlias(path), | ||
parts; | ||
|
||
if (magazineConfig) { | ||
// read controller method method from configuration | ||
parts = magazineConfig.pipeline.split('-'); | ||
if (parts.length === 3) { | ||
var controllerCartridge = parts[0], | ||
controllerName = parts[1], | ||
controllerMethod = parts[2], | ||
controllerPath = controllerCartridge + '/cartridge/controllers/' + controllerName, | ||
controller; | ||
controller = require(controllerPath); | ||
if (typeof controller[controllerMethod] === 'function') { | ||
// store category ID in case the controller method is Search.show(), | ||
// and header template uses this to set HTTP status | ||
request.custom.MagazineConfiguration = magazineConfig; | ||
// call controller method | ||
controller[controllerMethod](); | ||
result = true; | ||
} | ||
else { | ||
Logger.error('alias: method not found or not a function: ' + magazineConfig.pipeline); | ||
} | ||
} | ||
else { | ||
var pip = magazineConfig.pipeline; | ||
if (typeof magazineConfig.pipeline === 'undefined') { | ||
pip = '(undefined)'; | ||
} | ||
else if (magazineConfig.pipeline === null) { | ||
pip = '(null)'; | ||
} | ||
Logger.error('alias: invalid controller method specified: "{0}"', pip); | ||
} | ||
} | ||
else { | ||
Logger.debug('no matching config found for path: ' + path); | ||
} | ||
|
||
|
||
return result; | ||
} | ||
|
||
|
||
|
||
|
||
/* | ||
* Module exports | ||
*/ | ||
|
||
|
||
/* | ||
* Web exposed methods - only used via 'remote include' | ||
*/ | ||
|
||
/** Renders Styla header fragment. | ||
* @see module:controllers/StylaMagazine~headerContent */ | ||
exports.HeaderContent = headerContent; | ||
exports.HeaderContent.public = true; | ||
|
||
/** Renders Styla body fragment. | ||
* @see module:controllers/StylaMagazine~bodyContent */ | ||
exports.BodyContent = bodyContent; | ||
exports.BodyContent.public = true; | ||
|
||
|
||
/* | ||
* Web exposed methods | ||
*/ | ||
|
||
/** Renders Styla cartridge version. | ||
* @see module:controllers/StylaMagazine~cartridgeVersion */ | ||
exports.CartridgeVersion = cartridgeVersion; | ||
exports.CartridgeVersion.public = true; | ||
|
||
|
||
/* | ||
* Local methods | ||
*/ | ||
|
||
/** Redirect logic. Called from storefront controller RedirectURL.js. | ||
* @see module:controllers/StylaMagazine~alias */ | ||
exports.Alias = alias; | ||
|
||
|
183 changes: 183 additions & 0 deletions
183
cartridges/int_styla/cartridge/example/pipelines/RedirectURL.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<?demandware-pipeline version="2.0"?> | ||
|
||
<pipeline group="Application"> | ||
<branch basename="_ANONYMOUS_BRANCH_1"> | ||
<segment> | ||
<node> | ||
<text-node> | ||
<description>Pipeline is called by the system to handle URL mappings (static mappings and mapping rules). The mappings are configured in Business Manager. This Pipeline is highly performance cricitcal, because it is frequently called in case of explot scans. Please follow these rules: | ||
- no or only few database calls | ||
- simple (static) template response | ||
- caching the result page is a must | ||
|
||
In: | ||
OriginalURL</description> | ||
</text-node> | ||
<node-display width="4" x="1" y="1"/> | ||
</node> | ||
</segment> | ||
</branch> | ||
<branch basename="Start"> | ||
<segment> | ||
<node> | ||
<start-node name="Start" secure="false"/> | ||
<node-display x="1" y="2"/> | ||
</node> | ||
<simple-transition> | ||
<transition-display> | ||
<bend-point relative-to="source" x="0" y="1"/> | ||
</transition-display> | ||
</simple-transition> | ||
<node> | ||
<pipelet-node pipelet-name="RedirectURL" pipelet-set-identifier="bc_api"> | ||
<key-binding alias="Location" key="Location"/> | ||
<key-binding alias="OriginalURL" key="OriginalURL"/> | ||
</pipelet-node> | ||
<node-display x="0" y="1"/> | ||
<branch basename="b2" source-connector="error"> | ||
<transition target-connector="in"/> | ||
<segment> | ||
<node> | ||
<decision-node condition-key="dw.system.Site.current.getCustomPreferenceValue('stylaEnabled') !== true" condition-operator="expr"/> | ||
<node-display orientation="horizontal" x="1" y="0"/> | ||
<branch basename="b2" source-connector="yes"> | ||
<transition target-connector="in2" target-path="./b3.1"> | ||
<transition-display> | ||
<bend-point relative-to="source" x="4" y="0"/> | ||
</transition-display> | ||
</transition> | ||
</branch> | ||
</node> | ||
<simple-transition> | ||
<transition-display> | ||
<bend-point relative-to="source" x="0" y="1"/> | ||
</transition-display> | ||
</simple-transition> | ||
<node> | ||
<call-node start-name-ref="StylaMagazine-Alias"/> | ||
<node-display x="0" y="1"/> | ||
</node> | ||
<simple-transition> | ||
<transition-display> | ||
<bend-point relative-to="source" x="0" y="2"/> | ||
<bend-point relative-to="target" x="-1" y="0"/> | ||
</transition-display> | ||
</simple-transition> | ||
<node> | ||
<decision-node condition-key="empty(PipelineName)" condition-operator="expr"/> | ||
<node-display orientation="horizontal" x="1" y="1"/> | ||
<branch basename="b3" source-connector="yes"> | ||
<transition target-connector="in1"/> | ||
<segment> | ||
<node> | ||
<join-node/> | ||
<node-display x="1" y="0"/> | ||
</node> | ||
<simple-transition> | ||
<transition-display> | ||
<bend-point relative-to="source" x="1" y="0"/> | ||
</transition-display> | ||
</simple-transition> | ||
<node> | ||
<jump-node start-name-ref="Home-ErrorNotFound"/> | ||
<node-display orientation="horizontal" x="1" y="0"/> | ||
</node> | ||
</segment> | ||
</branch> | ||
</node> | ||
<simple-transition> | ||
<transition-display> | ||
<bend-point relative-to="source" x="0" y="2"/> | ||
<bend-point relative-to="target" x="-1" y="0"/> | ||
</transition-display> | ||
</simple-transition> | ||
<node> | ||
<jump-node start-name-key="PipelineName"/> | ||
<node-display orientation="horizontal" x="2" y="1"/> | ||
</node> | ||
</segment> | ||
</branch> | ||
</node> | ||
<simple-transition> | ||
<transition-display> | ||
<bend-point relative-to="target" x="0" y="-1"/> | ||
</transition-display> | ||
</simple-transition> | ||
<node> | ||
<interaction-node transaction-required="false"> | ||
<template buffered="true" dynamic="false" name="util/redirectpermanent"/> | ||
</interaction-node> | ||
<node-display x="0" y="3"/> | ||
</node> | ||
</segment> | ||
</branch> | ||
<branch basename="_ANONYMOUS_BRANCH_3"> | ||
<segment> | ||
<node> | ||
<text-node> | ||
<description>This handles URLs that are deep links into Styla magazines, e.g. /magazine/story/chapter/5. | ||
|
||
If the Redirect pipelet errors, then the Styla cartridge looks for a magazine configuration which matches the URL. | ||
|
||
If a magazine config is found then we redirect to the pipeline specified in the magazine configuration.</description> | ||
</text-node> | ||
<node-display height="2" width="2" x="6" y="3"/> | ||
</node> | ||
</segment> | ||
</branch> | ||
<branch basename="_ANONYMOUS_BRANCH_4"> | ||
<segment> | ||
<node> | ||
<text-node> | ||
<description>Hostname-only URLs (e.g. http://sitegenesis.com/) cannot be redirected using the URL mapping framework. Instead specify this pipeline in site's aliases in Busines Manager. Per default a redirect to the homepage is performed. The hostname in the URL is site's HTTP Hostname - if configured in Business Manager. Also, you can provide an URL to redirect to (parameter Location). | ||
|
||
In: | ||
Location (optional)</description> | ||
</text-node> | ||
<node-display width="4" x="1" y="9"/> | ||
</node> | ||
</segment> | ||
</branch> | ||
<branch basename="Hostname"> | ||
<segment> | ||
<node> | ||
<start-node name="Hostname" secure="false"/> | ||
<node-display x="1" y="10"/> | ||
</node> | ||
<simple-transition/> | ||
<node> | ||
<pipelet-node pipelet-name="Script" pipelet-set-identifier="bc_api"> | ||
<config-property key="Transactional" value="false"/> | ||
<config-property key="OnError" value="PIPELET_ERROR"/> | ||
<config-property key="ScriptFile" value="app_storefront_core:util/Redirect.js"/> | ||
<key-binding alias="ScriptLog" key="ScriptLog"/> | ||
<key-binding alias="CurrentRequest" key="CurrentRequest"/> | ||
<key-binding alias="CurrentHttpParamMap" key="CurrentHttpParamMap"/> | ||
<key-binding alias="Location" key="Location"/> | ||
</pipelet-node> | ||
<node-display x="0" y="1"/> | ||
<branch basename="b2" source-connector="error"> | ||
<transition target-connector="in"> | ||
<transition-display> | ||
<bend-point relative-to="source" x="1" y="0"/> | ||
</transition-display> | ||
</transition> | ||
<segment> | ||
<node> | ||
<end-node/> | ||
<node-display orientation="horizontal" x="1" y="0"/> | ||
</node> | ||
</segment> | ||
</branch> | ||
</node> | ||
<simple-transition/> | ||
<node> | ||
<interaction-node transaction-required="false"> | ||
<template buffered="true" dynamic="false" name="util/redirectpermanent"/> | ||
</interaction-node> | ||
<node-display x="0" y="1"/> | ||
</node> | ||
</segment> | ||
</branch> | ||
</pipeline> |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.