Source: src/main/javascript/oracle/oj/ojrowexpander/NodeSetWrapper.js

Oracle® JavaScript Extension Toolkit (JET)
1.1.2

E65298-01

/**
 * Copyright (c) 2014, Oracle and/or its affiliates.
 * All rights reserved.
 */
 
/**
 * Wraps around the NodeSet to provide additional metadata
 * @param {Object} nodeSet the node set to wrap
 * @param {function(Object, Object)} metadataCallback callback to inject additional metadata information
 * @param {Object=} range the requested range
 * @param {Array=} collapsedKeys an array of the collapsedKeys that should be passed in the expandAll case only
 * @constructor
 * @export
 */
oj.NodeSetWrapper = function(nodeSet, metadataCallback, range, collapsedKeys)
{
    this.m_nodeSet = nodeSet;
    this.m_callback = metadataCallback;
    this.m_range = range;
    this.m_collapsedKeys = collapsedKeys;    
};

/**
 * Gets the parent
 * @return {Object} the key of the parent.
 * @export
 */
oj.NodeSetWrapper.prototype.getParent = function()
{
    return this.m_nodeSet.getParent();
};

/**
 * Gets the start index of the result set.  
 * @return {number} the start index of the result set.  
 * @export
 */
oj.NodeSetWrapper.prototype.getStart = function()
{
    // if the requested start is a subset of the result set, adjust
    // accordingly
    if (this.m_range != null)
    {
        return this.m_range['start'];
    }
    else
    {
        return this.m_nodeSet.getStart();
    }
};

/**
 * Gets the actual count of the result set.  
 * @return {number} the actual count of the result set.  
 * @export
 */
oj.NodeSetWrapper.prototype.getCount = function()
{
    var nodeStart, nodeCount;

    nodeStart = this.m_nodeSet.getStart();
    nodeCount = this.m_nodeSet.getCount();

    // if the requested start is a subset of the NodeSet, adjust
    // accordingly
    if (this.m_range != null)
    {
        if (this.m_range['start'] > nodeStart)
        {
            nodeCount = Math.min(0, nodeCount - (this.m_range['start'] - nodeStart));
        }
        else if (this.m_range['start'] < nodeStart)
        {
            // this is an invalid NodeSet, so just return 0
            nodeCount = 0;
        }
    }

    return nodeCount;
};

/**
 * Gets the data of the specified index.  An error is throw when 1) the range is not yet available and
 * 2) the index specified is out of bounds. 
 * @param {number} index the index of the node/row in which we want to retrieve the data from.  
 * @return {Object} the data for the specified index.
 * @export
 */
oj.NodeSetWrapper.prototype.getData = function(index)
{
    return this.m_nodeSet.getData(index);
};

/**
 * Gets the metadata of the specified index.  An error is throw when 1) the range is not yet available and 
 * 2) the index specified is out of bounds. 
 * The metadata that the data source must return are:
 *  1) key - Object, the key of the node/row.
 *  2) state - state of the node, valid values are 'expanded', 'collapsed', 'leaf'. 
 *  3) depth - number, the depth of the node/row. 
 * @param {number} index the index of the node/row in which we want to retrieve the metadata from.  
 * @return {Object} the metadata object for the specific index.
 * @export
 */
oj.NodeSetWrapper.prototype.getMetadata = function(index)
{
    var metadata, rowKey;

    metadata = this.m_nodeSet.getMetadata(index);
    metadata['index'] = index;
    metadata['parentKey'] = this.getParent();
    rowKey = metadata['key'];

    // inject additional metadata
    this.m_callback.call(null, rowKey, metadata);

    return metadata;
};

/**
 * Gets the node set child of the specified index.
 * @param {number} index the index of the node/row in which we want to retrieve the child node set
 * @return {Object|null} the child node set representing the child tree data.
 * @export
 */
oj.NodeSetWrapper.prototype.getChildNodeSet = function(index) 
{
    var result;
    if (this.m_collapsedKeys == null || this.m_collapsedKeys.indexOf(this.m_nodeSet.getMetadata(index)['key']) == -1)
    {
        if (this.m_nodeSet.getChildNodeSet)
        {
            result = this.m_nodeSet.getChildNodeSet(index);
            if (result != null)
            {
                // wraps the child nodeset too
                return new oj.NodeSetWrapper(result, this.m_callback, null, this.m_collapsedKeys);
            }
        }
    }
    return null;
};