helper.js
3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* ADOBE CONFIDENTIAL
*
* Copyright 2016 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
/* globals com, request, resource, response, use */
use(['../../helper.js'], function(helper) {
'use strict';
var CHANNEL_DASHBOARD_PATH = '/screens/dashboard/channel';
var DEVICE_DASHBOARD_PATH = '/screens/dashboard/device';
var DISPLAY_DASHBOARD_PATH = '/screens/dashboard/display';
var resourceResolver = resource.resourceResolver;
var dataResourcePath = request.requestPathInfo.suffix;
if (!dataResourcePath) {
return;
}
var dataResource = resourceResolver.resolve(dataResourcePath);
if (!dataResource) {
return;
}
// Retrieve the associated page
var dataPage = dataResource.adaptTo(com.day.cq.wcm.api.Page);
// If the page is null, assume we are on the content resource
if (!dataPage) {
dataPage = dataResource.parent && dataResource.parent.adaptTo(com.day.cq.wcm.api.Page);
}
var dataContentResource = dataPage ? dataPage.getContentResource() : resourceResolver.getResource(dataResource, helper.constants.device.DEVICE_PROFILE_NAME);
var dashboardPath, suffix;
// Handle the device case
if (!dataPage && dataContentResource) {
dashboardPath = DEVICE_DASHBOARD_PATH;
suffix = dataResource.path;
}
// Handle the channel case
else if (dataContentResource && dataContentResource.isResourceType(helper.constants.screens.RT_CHANNEL)) {
dashboardPath = CHANNEL_DASHBOARD_PATH;
suffix = dataPage.path;
}
else if (dataContentResource) {
// Handle the display case
if (dataContentResource.isResourceType(helper.constants.screens.RT_DISPLAY)) {
dashboardPath = DISPLAY_DASHBOARD_PATH;
suffix = dataPage.path;
}
// Handle the device config case
else if (dataContentResource.isResourceType(helper.constants.screens.RT_DEVICE_CONFIG)) {
dashboardPath = DISPLAY_DASHBOARD_PATH;
suffix = dataPage.parent.path;
}
// Handle the screen case
else if (dataContentResource.isResourceType(helper.constants.screens.RT_SCREEN)) {
dashboardPath = DISPLAY_DASHBOARD_PATH;
suffix = dataPage.parent.parent.path;
}
}
// Only redirect if the suffix actually changed (to avoid infinite redirects)
if (suffix && String(suffix) !== String(dataResourcePath)) {
response.sendRedirect(request.getContextPath() + (dashboardPath || resource.path) + '.html' + suffix);
}
return;
});