use()

in src/main/resources/SLING-INF/libs/sling/sightly/js/internal/resource.js [19:166]


use(['helper.js'], function(helper) {
       
    function getParentPath(path) {
        var index = path.lastIndexOf('/');
        if (index == -1) {
            return null;
        }
        return path.substring(0, index);
    }


    function getProperties(nativeResource) {
        var valueMap = nativeResource.adaptTo(Packages.org.apache.sling.api.resource.ValueMap);
        return (valueMap) ? helper.mapToObject(valueMap) : {};
    }

    /**
     * @name Resource
     * @constructor
     * @class The Resource class
     * @param {object} nativeResource The nativeResource resource object
     */
    function Resource(nativeResource, promise) {
        /**
         * The absolute path for this resource
         * @name Resource~path
         * @member
         * @type {string}
         */
        this.path = nativeResource.getPath();

        /**
         * The map of properties for this object
         * @name Resource~properties
         * @member
         * @type {object.<string, object>}
         */
        this.properties = getProperties(nativeResource);

        /** @private */
        this.nativeResource = nativeResource;

        if (!promise) {
            throw new Error('No promise library provided');
        }
        this._promise = promise;
    }

    Resource.prototype = /** @lends Resource.prototype */ {
        constructor: Resource,

        /**
         * Get the parent resource
         * @return {promise.<Resource>} a promise with the parent of this resource, or null if
         * the resource has no parent
         */
        getParent: function() {
            var parentPath = getParentPath(this.path);
            if (!parentPath) {
                return null;
            }
            var resolver = this.nativeResource.getResourceResolver();
            var parent = resolver.resolve(parentPath);
            return this._promise.success(new Resource(parent, this._promise));
        },

        /**
         * Get the children of this resource
         * @return {promise.<array.<Resource>>} a promise with the array of children resource
         */
        getChildren: function() {
            var resolver = this.nativeResource.getResourceResolver();
            var children = [];
            var it = resolver.listChildren(this.nativeResource);
            var promise = this._promise;
            while (it.hasNext()) {
                var childNativeResource = it.next();
                children.push(new Resource(childNativeResource, promise));
            }
            return this._promise.success(children);
        },

        /**
         * Returns the name of this resource. The name of a resource is the last segment of the path.
         * @returns {string} the name of this resource
         */
        getName: function () {
            var index = this.path.lastIndexOf('/');
            if (index == -1) {
                return this.path;
            }
            return this.path.substring(index + 1);
        },

        /**
         * The resource type is meant to point to rendering/processing scripts, editing dialogs, etc.
         * @return {string} the resource type of this resource
         */
        getResourceType: function () {
            return this.nativeResource.resourceType;
        },

        /**
         * Resolve a path to a resource. The path may be relative
         * to this path
         * @param  {string} path the requested path
         * @return {promise.<Resource>} the promise of a resource. If the resource
         * does not exist, the promise will fail
         */
        resolve: function(path) {
            var resolver = this.nativeResource.getResourceResolver();
            var res = resolver.getResource(this.nativeResource, path);
            if (res == null) {
                return this._promise.failure(new Error('No resource found at path: ' + path));
            }
            return this._promise.success(new Resource(res, this._promise));
        }
    };

    Object.defineProperties(Resource.prototype, {
        /**
         * The name of the resource
         * @name Resource~name
         * @member
         * @type {string}
         */
        name: {
            get: function() {
                return this.getName();
            }
        },

        /**
         * The resource type
         * @name Resource~resourceType
         * @member
         * @type {string}
         */
        resourceType: {
            get: function() {
                return this.getResourceType();
            }
        }
    });

    return Resource;

});