resource.util.js 8.51 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 com, org, properties, request, resource, use */
use(['helper.js', 'actionRelationships.js'], function(helper, actionRels) {
    'use strict';

    var resourceResolver = resource.resourceResolver;
    var isFolder = false;
    var isApp = false;
    var isLocation = false;
    var isDisplay = false;
    var isScreen = false;
    var isDevice = false;
    var isDeviceConfig = false;
    var isChannel = false;
    var isSchedule = false;
    var hasChildren = false;
    var canDrillDown = true;
    var isOmnisearch = request.getAttribute('isOmnisearch');
    var path, thumbnailPath;

    // Check if the resource is a folder
    isFolder = helper.isResourceType(resourceResolver.resolve(resource.path),
        [helper.constants.sling.NT_SLING_FOLDER, helper.constants.sling.NT_SLING_ORDERED_FOLDER]);

    if (isFolder) {
        path = resource.path;
        return {
            image: path + '.folderthumbnail.jpg',
            path: path,
            properties: properties,
            isFolder: isFolder,
            hasChildren: helper.hasChildren(resource),
            navigationHref: '/screens.html' + path
        };
    }

    // The resource is a page, so get its content resource properties
    var res, locationProperties, mapProperties, deviceProperties, props;
    var page = resource.adaptTo(com.day.cq.wcm.api.Page);

    if (page == null) { // eslint-disable-line
        page = resource.parent.adaptTo(com.day.cq.wcm.api.Page);
    }

    // Handle Resources that are not cq:pages
    if (page == null) { // eslint-disable-line
        res = resource.resourceResolver.getResource(resource, helper.constants.device.DEVICE_PROFILE_NAME);
        if (res) {
            props = res.adaptTo(org.apache.sling.api.resource.ValueMap);
            path = resource.path;
            thumbnailPath = '/libs/screens/dcc/content/common/layouts/icons/240x180/device.png';
        }
        // Other Resources like Assignments
        else {
            res = resource;
            props = res.adaptTo(org.apache.sling.api.resource.ValueMap);
            path = resource.path;
            thumbnailPath = '';
        }
    }
    // Default pages
    else {
        props = page.properties;
        path = page.path;
        res = page.getContentResource();
        thumbnailPath = props && props.get(helper.constants.screens.PN_THUMBNAIL_PATH, page.path);
    }

    hasChildren = helper.hasChildren(resource);

    // Check if the page is an app
    isApp = helper.isResourceType(res, helper.constants.screens.RT_APPLICATION);

    // Check if the page is a location
    isLocation = helper.isResourceType(res, helper.constants.screens.RT_LOCATION);

    isDisplay = helper.isResourceType(res, helper.constants.screens.RT_DISPLAY);

    // Check if the page is a device
    isDevice = helper.isResourceType(res, helper.constants.screens.RT_DEVICE);

    // Check if the page is a device
    isDeviceConfig = helper.isResourceType(res, helper.constants.screens.RT_DEVICE_CONFIG);

    // Check if the page is a screen
    isScreen = helper.isResourceType(res, helper.constants.screens.RT_SCREEN);

    // Check if the page is a channel
    isChannel = helper.isResourceType(res, helper.constants.screens.RT_CHANNEL);

    // Check if the page is a schedule
    isSchedule = helper.isResourceType(res, helper.constants.screens.RT_SCHEDULE);

    if (props) {
        // Set map properties
        mapProperties = {
            file: resourceResolver.getResource(res,
                helper.constants.screens.NN_MAP + '/' + helper.constants.screens.NN_IMAGE),
            address: props.get(helper.constants.screens.PN_ADDRESS)
        };

        // Set location properties
        locationProperties = {
            position: props.get(helper.constants.screens.PN_IMAGE_PIN_COORDINATES)
                && props.get(helper.constants.screens.PN_IMAGE_PIN_COORDINATES).split(','),
            refId: props.get(helper.constants.screens.PN_SVG_REFERENCE),
            coordinates: props.get(helper.constants.screens.PN_GEO_COORDINATES)
                && props.get(helper.constants.screens.PN_GEO_COORDINATES).split(',')
        };

        // Set device properties
        deviceProperties = {
            id: props.get(helper.constants.screens.PN_ID),
            type: props.get(helper.constants.screens.PN_TYPE),
            brand: props.get(helper.constants.screens.PN_BRAND),
            model: props.get(helper.constants.screens.PN_MODEL),
            platform: props.get(helper.constants.screens.PN_PLATFORM),
            uuid: props.get(helper.constants.screens.PN_UUID),
            version: props.get(helper.constants.screens.PN_VERSION),
            dimensions: props.get(helper.constants.screens.PN_DIMENSIONS)
                && props.get(helper.constants.screens.PN_DIMENSIONS).split('x'),
            resolution: props.get(helper.constants.screens.PN_RESOLUTION),
            width: props.get(helper.constants.screens.PN_WIDTH),
            height: props.get(helper.constants.screens.PN_HEIGHT),
            features: props.get(helper.constants.screens.PN_FEATURES)
        };
    }

    var navigationHref = '';
    if (isChannel) {
        navigationHref = '/screens/dashboard/channel.html' + path;
    }
    else if (isDisplay) {
        navigationHref = '/screens/dashboard/display.html' + path;
    }
    else if (isSchedule) {
        navigationHref = '/screens/dashboard/schedule.html' + path;
    }
    else if (isApp || isScreen) {
        navigationHref = '';
    }
    else if (isDevice) {
        navigationHref = path && '/screens/dashboard/device.html' + path + '?returnPage=/screens/devices.html' + request.requestPathInfo.suffix || '#';
    }
    else if (isDeviceConfig) {
        path = page.parent.path;
        navigationHref = path && '/screens/dashboard/display.html' + path || '#';
    }
    else if (helper.isResourceType(res, ['screens/core/components/devicefolder'])) {
        navigationHref = '/screens/devices.html' + path;
    }

    // apps may have sub pages we do not want to show (content), same goes for channels
    if (isApp || isChannel || isDisplay || isDeviceConfig || isScreen || isDevice || isSchedule) {
        hasChildren = false;
        canDrillDown = false;
    }

    // Folders should be rendered as such
    isFolder = helper.isResourceType(res, [
        helper.constants.screens.RT_APPLICATION_FOLDER,
        helper.constants.screens.RT_CHANNEL_FOLDER,
        helper.constants.screens.RT_DEVICE_FOLDER,
        helper.constants.screens.RT_LOCATION_FOLDER,
        helper.constants.screens.RT_SCHEDULE_FOLDER
    ]);

    // additional flags
    /*eslint-disable */
    var isNew =
        // created less than 24h ago created
        (props['jcr:created'] != null && props['jcr:created'].getTimeInMillis() - ((new Date()).getTime() - 24 * 60 * 60 * 1000) > 0) &&
        // and not yet modified
        (!props['cq:lastModifiedBy']);
    /*eslint-enable */

    var liveRelationshipManager = resourceResolver.adaptTo(com.day.cq.wcm.msm.api.LiveRelationshipManager);
    var isLiveCopy = liveRelationshipManager.hasLiveRelationship(res);

    return {
        image: thumbnailPath,
        path: path,
        resourceType: res.getResourceType(),
        resourceSuperType: res.getResourceSuperType(),
        properties: props,
        isFolder: isFolder,
        isApp: isApp,
        isLocation: isLocation,
        isDisplay: isDisplay,
        isDevice: isDevice,
        isDeviceConfig: isDeviceConfig,
        isScreen: isScreen,
        isChannel: isChannel,
        isSchedule: isSchedule,
        hasChildren: hasChildren,
        canDrillDown: canDrillDown,
        location: locationProperties,
        map: mapProperties,
        device: deviceProperties,
        navigationHref: navigationHref ? request.contextPath + navigationHref : null,
        windowPreference: helper.windowPreference,
        actionRels: actionRels(resource),
        isNew: isNew,
        isLiveCopy: isLiveCopy,
        isOmnisearchRequest: isOmnisearch
    };

});