helper.js 3.08 KB
/*
 * 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;
});