display.js 3.01 KB
/*
 * ADOBE CONFIDENTIAL
 *
 * Copyright 2015 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 org, request, resource, use, com */
use(['../../helper.js'], function(helper) {
    'use strict';

    var resourceResolver = resource.resourceResolver;

    // Retrieve the display resource and its properties from the request suffix
    var displayPath = request.requestPathInfo.suffix;
    if (displayPath == null) { // eslint-disable-line
        return null;
    }

    var displayResource = resourceResolver.resolve(displayPath);
    var displayContentResource = displayResource.getChild(helper.constants.cq.NN_CONTENT);
    if (displayContentResource == null) { // eslint-disable-line
        return null;
    }

    var displayContentProperties = displayContentResource.adaptTo(org.apache.sling.api.resource.ValueMap);
    if (displayContentProperties == null) { // eslint-disable-line
        return null;
    }

    var activeChannelResource, displayAssignment, displayAssignmentProps, resolvedResource, role;
    var activeChannel = displayContentProperties.get(helper.constants.screens.PN_ACTIVE_CHANNEL);
    var displayService = resourceResolver.adaptTo(com.adobe.cq.screens.display.DisplayService);
    var assignmentService = resourceResolver.adaptTo(com.adobe.cq.screens.assignment.AssignmentService);
    var displayAssignments = displayService.getAssignments(resourceResolver, displayResource);

    while (displayAssignments.hasNext()) {
        displayAssignment = displayAssignments.next();
        displayAssignmentProps = displayAssignment.adaptTo(org.apache.sling.api.resource.ValueMap);
        role = displayAssignmentProps.get(helper.constants.screens.PN_ROLE);
        resolvedResource = assignmentService.resolveEntity(displayAssignment, displayResource);

        // Ignore schedule assignments for now as they would break the "current channel" logic.
        if (displayAssignment.isResourceType(com.adobe.cq.screens.binding.ScreensConstants.RT_SCHEDULE_ASSIGNMENT_ABSOLUTE)) {
            continue;
        }

        if (!resolvedResource || !role) {
            continue;
        }

        if (displayAssignmentProps.get(helper.constants.screens.PN_ROLE).equals(activeChannel)) {
            activeChannelResource = resolvedResource.adaptTo(com.day.cq.wcm.api.Page).contentResource;
        }
    }

    return {
        windowPreference: helper.windowPreference,
        properties: displayContentProperties,
        activeChannel: activeChannelResource
    };

});