Source: src/main/javascript/oracle/oj/ojdatagrid-model/CollectionHeaderSet.js

Oracle® JavaScript Extension Toolkit (JET)
1.1.2

E65298-01

/**
 * Copyright (c) 2014, Oracle and/or its affiliates.
 * All rights reserved.
 */
 
/**
 * @export
 * A HeaderSet represents a collection of headers.  The HeaderSet is an object returned by the success callback
 * of the fetchHeaders method on DataGridDataSource.  This is an oj collection specific implementation of the HeaderSet.
 * @param {number} start the start index of header set.
 * @param {number} end the end index of the header set.
 * @param {Array} headers the array of headers
 * @param {string=} rowHeader the id of the row header column.  Required for row headers.
 * @constructor
 */
oj.CollectionHeaderSet = function(start, end, headers, rowHeader)
{
    // assert start/end are number
    oj.Assert.assertNumber(start, null);
    oj.Assert.assertNumber(end, null);
    oj.Assert.assertArrayOrNull(headers);

    this.m_start = start;
    this.m_end = end;
    this.m_headers = headers;
    this.m_rowHeader = rowHeader;
};

/**
 * Sets the models used in this header set.
 * @param {Array} models an array of oj model for the header set
 * @private
 */
oj.CollectionHeaderSet.prototype.setModels = function(models)
{
    oj.Assert.assertArray(models);
    // make sure the array size is valid
    if (models != null && models.length === this.getCount())
    {
        this.m_models = models;
    }
};

/**
 * 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 absolute index of the header in which we want to retrieve the header from.  
 * @param {number=} level the level of the header, 0 is the outermost header and increments by 1 moving inward
 * @return {Object} the data object for the specific index.
 * @export
 */
oj.CollectionHeaderSet.prototype.getData = function(index, level)
{
    var model;

    // make sure index/level are valid
    oj.Assert.assert(index <= this.m_end && index >= this.m_start, 'index out of bounds'); 
    oj.Assert.assert(level == null || level == 0, 'level out of bounds'); 

    // row or column header
    if (this.m_rowHeader != null)
    {
        if (this.m_models == null)
        {
            return null;
        }

        model = this.m_models[index - this.m_start];
        return model.get(this.m_rowHeader);
    }
    else
    {
        return this.m_headers[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 can optionally return are:
 *  1) sortDirection - the initial sort direction of the header.  Valid values are "ascending" and "descending".
 *  2) key - the key of the row/column header.
 * @param {number} index the absolute index of the header in which we want to retrieve the metadata from.  
 * @param {number=} level the level of the header, 0 is the outermost header and increments by 1 moving inward
 * @return {Object} the metadata object for the specific index.
 * @export
 */
oj.CollectionHeaderSet.prototype.getMetadata = function(index, level)
{
    var data, model;

    oj.Assert.assert(index <= this.m_end && index >= this.m_start, 'index out of bounds'); 
    oj.Assert.assert(level == null || level == 0, 'level out of bounds'); 
    
    // row header case
    if (this.m_rowHeader != null)
    {
        if (this.m_models == null)
        {
            return null;
        }

        model = this.m_models[index - this.m_start];        
        return {'key':oj.CollectionDataGridUtils._getModelKey(model)};
    }    
    else
    {
        data = this.getData(index, level);
        return {'key': data};
    }    
    
};

/**
 * Gets the actual number of levels of the result set for the specified axis. The levels
 * are the counted from the outermost header indexed at 0, and moving inwards toward the 
 * databody would increment the level by 1.
 * @return {number} the number of levels of the result set
 * @export
 */
oj.CollectionHeaderSet.prototype.getLevelCount = function()
{
    if (this.getCount() > 0)
    {
        return 1;
    }
    return 0;
};

/**
 * Gets the extent of an index on a particular level within the context of the headerSet. Extent is defined as the 
 * number of indexes covered by the header. If the extent extends beyond the start and end of the requested 
 * range the extent should be trimmed to the edge of the requested range and the object for {'more': {before, after}}
 * should have the value appropriate boolean set. For innermost headers the extent will always be 1.
 * @param {number} index the absolute index along the innermost header of the extent to get, 0 is the first header in the data source
 * @param {number=} level the level of the header, 0 is the outermost header and increments by 1 moving inward
 * @return {Object} an object containing two values
 *              extent: the number of absolute indexes spanned by the header at this index 
 *                      bounded by the edges of the result set for the specified axis. 
 *              more: object with keys 'before'/'after' and boolean values true/false representing whether
 *                       there are more indexes before/after what is in the headerSet
 * @example <caption>In this example the header spans 5 indexes and there are more indexes to cover after the request that
 *              aren't included in this headerSet:</caption>                     
 * {'extent':5, 'more': {'before':false, 'after':true}}
 * @export
 */
oj.CollectionHeaderSet.prototype.getExtent = function(index, level)
{ 
    oj.Assert.assert(index <= this.m_end && index >= this.m_start, 'index out of bounds'); 
    oj.Assert.assert(level == null || level == 0, 'level out of bounds'); 
    return {'extent': 1, 'more':{'before': false, 'after':false}};
};

/**
 * Gets the depth of an index starting at a particular level. The depth is the number 
 * of levels spanned by the header.
 * @param {number} index the absolute index of the depth to get
 * @param {number=} level the level of the header, 0 is the outermost header
 * @return {number} the number of levels spanned by the header at the specified position
 * @export
 */
oj.CollectionHeaderSet.prototype.getDepth = function(index, level)
{
    oj.Assert.assert(index <= this.m_end && index >= this.m_start, 'index out of bounds'); 
    oj.Assert.assert(level == null || level == 0, 'level out of bounds');  
    return 1;
}

/**
 * Gets the actual count of the result set, the total indexes spanned by the headerSet
 * along the innermost header.
 * @return {number} the actual count of the result set.  
 * @export
 */
oj.CollectionHeaderSet.prototype.getCount = function()
{
    return Math.max(0, this.m_end - this.m_start);
};

/**
 * For internal testing purposes. Gets the m_start property
 * @return {number} the start index of the result set.
 * @export
 * @ignore
 */
oj.CollectionHeaderSet.prototype.getStart = function()
{
    return this.m_start;
};

/**
 * For internal testing purposes. Gets the m_end property
 * @export
 * @ignore
 */
oj.CollectionHeaderSet.prototype.getEnd = function()
{
    return this.m_end;
};

/**
 * For internal testing purposes. Gets the m_headers property
 * @export
 * @ignore
 */
oj.CollectionHeaderSet.prototype.getHeaders = function()
{
    return this.m_headers;
};

/**
 * For internal testing purposes. Gets the m_rowHeader property
 * @export
 * @ignore
 */
oj.CollectionHeaderSet.prototype.getRowHeader = function()
{
    return this.m_rowHeader;
};