helper.js 6.87 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, java, org, resource, use */
use(function() {
    'use strict';

    // Define some constants to be reused throughout the project
    var constants = {
        jcr: com.day.cq.commons.jcr.JcrConstants,
        sling: org.apache.sling.jcr.resource.api.JcrResourceConstants,
        cq: com.day.cq.wcm.api.NameConstants,
        screens: com.adobe.cq.screens.binding.ScreensConstants,
        device: com.adobe.cq.screens.binding.DeviceConstants
    };

    /**
     * Compute the attributes for the element from the specified properties and exclusions.
     *
     * @param {ValueMap} properties     The properties to use to populate the attributes
     * @param {Map}      [attributes]   The attribtues map to extend
     * @param {String[]} [excludes]     The properties to exclude from the list, in addition to the default ones.
     *
     * @return {Map} The computed attributes
     */
    var computeDataAttributes = function(properties, attributes, excludes) {
        attributes = attributes || {};
        var propertiesIterator = properties.keySet().iterator();
        var key;
        while (propertiesIterator.hasNext()) {
            key = propertiesIterator.next();
            if (!key.startsWith('jcr:') && !key.startsWith('sling:') && !key.startsWith('cq:') // ignore default properties
                && excludes.indexOf(String(key)) === -1) { // ignore excluded properties
                attributes['data-' + key] = properties.get(key).toString();
            }
        }
        return attributes;
    };

    /**
     * Check if a resource extends any of the specified resource types.
     *
     * @param {Resource} resource The resource to test.
     * @param {String|String[]} types The resource type(s) to check against
     *
     * @return {String|Boolean} Returns the extended resource type, or `false`
     */
    var isResourceType = function(resource, types) {
        if (resource == null) {  // eslint-disable-line
            return false;
        }
        var i;
        types = ([]).concat(types);
        for (i = 0; i < types.length; i++) {
            if (resource.isResourceType(types[i])) {
                return types[i];
            }
        }
        return false;
    };

    /**
     * Check if a resource has children.
     *
     * @param {Resource} resource The resource to test.
     *
     * @return {Boolean} Returns `true` if the resource has any children, and `false` otherwise
     */
    var hasChildren = function(resource) {
        var childrenIterator = resource.resourceResolver.listChildren(resource);
        var childResource;
        while (childrenIterator.hasNext()) {
            childResource = childrenIterator.next();
            if (childResource.name.indexOf('rep:') > -1
                    || childResource.getName().equals('jcr:content')) {
                continue;
            }
            return true;
        }
        return false;
    };

    /**
     * Check if a resource has children.
     *
     * @param {Resource} resource The resource to test.
     * @param {String[]} types    The resource types to check against.
     *
     * @return {Boolean} Returns `true` if the resource has any children, and `false` otherwise
     */
    var hasChildrenWithResourceType = function(resource, types) {
        types = ([]).concat(types);
        var childrenIterator = resource.resourceResolver.listChildren(resource);
        var childResource;
        while (childrenIterator.hasNext()) {
            childResource = childrenIterator.next();
            if (childResource.name !== constants.cq.NN_CONTENT) {
                var contentResource = childResource.getChild(constants.cq.NN_CONTENT);
                if (contentResource && isResourceType(contentResource, types)) {
                    return true;
                }
            }
        }
        return false;
    };

    /**
     * Apply a function on each child resource of the specified parent.
     *
     * @param {Resource} resource The parent resource
     * @param {Function} mapFnc The function to apply
     * @param {String[]} excludes Resource types to ignore
     *
     * @return {Object[]} An array of results returned by the map function
     */
    var mapChildren = function(resource, mapFnc, excludes) {
        if (resource == null) {  // eslint-disable-line
            return [];
        }
        var mapResults = [];
        var childrenIterator = resource.resourceResolver.listChildren(resource);
        var childResource, childContentResource, childProperties;
        while (childrenIterator.hasNext()) {
            childResource = childrenIterator.next();
            if (excludes && isResourceType(childResource, excludes)) {
                continue;
            }
            childContentResource = childResource.getChild(constants.cq.NN_CONTENT);
            childProperties = childContentResource;
            if (childProperties == null) {  // eslint-disable-line
                childProperties = childResource;
            }

            childProperties = childProperties.adaptTo(org.apache.sling.api.resource.ValueMap);
            mapResults.push(mapFnc(childResource, childProperties));
        }
        return mapResults;
    };

    var windowPreference = function() {
        var auth = resource.resourceResolver.adaptTo(org.apache.jackrabbit.api.security.user.Authorizable);
        var upm = resource.resourceResolver.adaptTo(com.adobe.granite.security.user.UserPropertiesManager);
        if (upm) {
            var preferences = upm.getUserProperties(auth.getID(),
                com.adobe.granite.security.user.UserPropertiesService.PREFERENCES_PATH);
            if (preferences) {
                return preferences.getProperty('winMode', 'multi', java.lang.String);
            }
        }
        return 'multi';
    };

    return {
        constants: constants,
        computeDataAttributes: computeDataAttributes,
        isResourceType: isResourceType,
        hasChildren: hasChildren,
        hasChildrenWithResourceType: hasChildrenWithResourceType,
        mapChildren: mapChildren,
        windowPreference: {
            attrs: {
                target: String(windowPreference()) === 'multi' ? '_blank' : ''
            },
            string: String(windowPreference()) === 'multi' ? ',"target":"_blank"' : ''
        }
    };

});