Source: src/main/javascript/oracle/oj/ojcube/CubeDataGridDataSource.js

Oracle® JavaScript Extension Toolkit (JET)
1.1.2

E65298-01

/**
 * Copyright (c) 2015, Oracle and/or its affiliates.
 * All rights reserved.
 */
 
/**
 * A cubic/aggregating DataGridDataSource based on the oj.Cube
 * @param {oj.Cube} cube the cube that will underpin the data source
 * @export
 * @extends oj.DataGridDataSource
 * @constructor
 * @since 1.1.0
 */
oj.CubeDataGridDataSource = function(cube)
{
    oj.CubeDataGridDataSource.superclass.constructor.call(this, cube);
};

// Subclass CubeDataGridDataSource to DataGridDataSource
oj.Object.createSubclass(oj.CubeDataGridDataSource, oj.DataGridDataSource, "oj.CubeDataGridDataSource");

/**
 * Set a new cube on the data source
 * @param {oj.Cube} cube
 * @export
 */
oj.CubeDataGridDataSource.prototype.setCube = function(cube) {
    this.data = cube;
    this._fireRefresh();
};

/**
 * Pin any axes beyond the row and column to specific index values (to allow the idea of "paging" through a cube)
 * @param {Array} indices an array of objects each containing an 'axis' attribute and a zero-based 'index' attribute giving the index to "pin" the axis to.
 * @export
 */
oj.CubeDataGridDataSource.prototype.setPage = function(indices) {
    this.data.setPage(indices);
    this._fireRefresh();
};

oj.CubeDataGridDataSource.prototype._fireRefresh = function() {
    var event = {};
    event['source'] = this;
    event['operation'] = "refresh";
    this.handleEvent("change", event);
};
    
/**
 * Returns the total number of rows or columns.  If the value return is not >= 0 then it is automatically assumed
 * that the total count is unknown.
 * @param {string} axis the axis in which we inquire for the total count.  Valid values are "row" and "column".
 * @return {number} the total number of rows/columns.
 * @export
 */
oj.CubeDataGridDataSource.prototype.getCount = function(axis)
{
    var axisObj = this._getAxis(axis);
    
    return axisObj ? axisObj.getExtent() : 0;
};

/**
 * Returns whether the total count returned in getCount function is an actual or an estimate.
 * @param {string} axis the axis in which we inquire whether the total count is an estimate.  Valid values are 
 *        "row" and "column".
 * @return {string} "exact" if the count returned in getCount function is the actual count, "estimate" if the 
 *         count returned in getCount function is an estimate.  The default value is "exact".
 * @export
 */
oj.CubeDataGridDataSource.prototype.getCountPrecision = function(axis)
{
    return "exact";
};

/**
 * Fetch a range of headers from the data source.
 * @param {Object} headerRange information about the header range, it must contain the following properties:
 *        axis, start, count.
 * @param {string} headerRange.axis the axis of the header that are fetched.  Valid values are "row" and "column".
 * @param {number} headerRange.start the start index of the range in which the header data are fetched.
 * @param {number} headerRange.count the size of the range in which the header data are fetched.  
 * @param {Object} callbacks the callbacks to be invoke when fetch headers operation is completed.  The valid callback
 *        types are "success" and "error".
 * @param {function(oj.HeaderSet)} callbacks.success the callback to invoke when fetch headers completed successfully.
 * @param {function({status: Object})} callbacks.error the callback to invoke when fetch cells failed.
 * @param {Object=} callbackObjects the object in which the callback function is invoked on.  This is optional.  
 *        You can specify the callback object for each callbacks using the "success" and "error" keys.
 * @export
 */
oj.CubeDataGridDataSource.prototype.fetchHeaders = function(headerRange, callbacks, callbackObjects)
{
    
    var cubeheaders = new oj.CubeHeaderSet(this._getAxis(headerRange['axis']), this.data, headerRange['start'], headerRange['count']);
    callbacks['success'].call(callbackObjects ? callbackObjects['success'] : undefined, cubeheaders, headerRange);
};

/**
 * Fetch a range of cells from the data source.
 * @param {Array.<Object>} cellRange Information about the cell range.  A cell range is defined by an array 
 *        of range info for each axis, where each range contains three properties: axis, start, count.
 * @param {string} cellRange.axis the axis associated with this range where cells are fetched.  Valid 
 *        values are "row" and "column".
 * @param {number} cellRange.start the start index of the range for this axis in which the cells are fetched.
 * @param {number} cellRange.count the size of the range for this axis in which the cells are fetched. 
 * @param {Object} callbacks the callbacks to be invoke when fetch cells operation is completed.  The valid callback
 *        types are "success" and "error".
 * @param {function(oj.CellSet)} callbacks.success the callback to invoke when fetch cells completed successfully.
 * @param {function({status: Object})} callbacks.error the callback to invoke when fetch cells failed.
 * @param {Object=} callbackObjects the object in which the callback function is invoked on.  This is optional.  
 *        You can specify the callback object for each callbacks using the "success" and "error" keys.
 * @export
 */
oj.CubeDataGridDataSource.prototype.fetchCells = function(cellRange, callbacks, callbackObjects)
{
    var obj = {};
    for (var i = 0; i < cellRange.length; i++) {
        var start = cellRange[i]['start'] === undefined ? 0 : cellRange[i]['start'];
        if (cellRange[i].axis === 'row') {
            var count = cellRange[i]['count'] === undefined ? this.data.getAxes()[1].getExtent() : cellRange[i]['count'];
            obj.row = {start: start, count: count};
        }
        if (cellRange[i].axis === 'column') {
            var count = cellRange[i]['count'] === undefined ? this.data.getAxes()[0].getExtent() : cellRange[i]['count'];
            obj.column = {start: start, count: count};
        }
    }
    var cubecells = new oj.CubeCellSet(this.data, obj);
    callbacks['success'].call(callbackObjects ? callbackObjects['success'] : undefined, cubecells, cellRange);
};

/**
 * Returns the keys based on the indexes. 
 * @param {Object} indexes the index for each axis
 * @param {Object} indexes.row the index for the row axis
 * @param {Object} indexes.column the index for the column axis
 * @return {Object} a Promise object which when resolved returns an object containing the keys for each axis
 * @export
 */
oj.CubeDataGridDataSource.prototype.keys = function(indexes)
{
    var retObj = {};
    retObj = this._getKey(indexes, 'row', retObj);
    retObj = this._getKey(indexes, 'column', retObj);
    return Promise.resolve(retObj);
};

oj.CubeDataGridDataSource.prototype._getKey = function(indexes, axis, retObj) {
    var axisObj = this._getAxis(axis);
    var item = indexes[axis];
    var keys = new oj.CubeKeys();
    keys = axisObj ? axisObj.GetCubeKeys(item, keys) : "";
    retObj[axis] = keys.GetHashCodes()[0].key;
    return retObj;
};

/**
 * Returns the row and column index based on the keys.
 * @param {Object} keys the key for each axis
 * @param {Object} keys.row the key for the row axis
 * @param {Object} keys.column the key for the column axis
 * @return {Object} a Promise object which when resolved returns an object containing the index for each axis
 * @export
 */
oj.CubeDataGridDataSource.prototype.indexes = function(keys)
{
    var retObj = {};
    retObj = this._getIndex(keys, 'row', retObj);
    retObj = this._getIndex(keys, 'column', retObj);

    return Promise.resolve(retObj);
};

oj.CubeDataGridDataSource.prototype._getIndex = function(keys, axis, retObj) {
    retObj[axis] = this._getAxis(axis).getIndex(keys[axis]);
    return retObj;
};

/**
 * Performs a sort on the data source.
 * @param {Object} criteria the sort criteria.  Specifies null to reset sort state.
 * @param {string} criteria.axis The axis in which the sort is performed, valid values are "row", "column"
 * @param {Object} criteria.key The key that identifies which header to sort
 * @param {string} criteria.direction the sort direction, valid values are "ascending", "descending", "none" (default)
 * @param {Object} callbacks the callbacks to be invoke upon completion of the sort operation.  The callback
 *        properties are "success" and "error".
 * @param {function()} callbacks.success the callback to invoke when the sort completed successfully.  
 * @param {function({status: Object})} callbacks.error the callback to invoke when sort failed.
 * @param {Object=} callbackObjects the object in which the callback function is invoked on.  This is optional.  
 *        You can specify the callback object for each callbacks using the "success" and "error" properties.
 * @export
 */
oj.CubeDataGridDataSource.prototype.sort = function(criteria, callbacks, callbackObjects)
{
    oj.Assert.failedInAbstractFunction();
};

/**
 * Moves a row from one location to another.
 * @param {Object} rowToMove the key of the row to move
 * @param {Object} referenceRow the key of the reference row which combined with position are used to determine 
 *        the destination of where the row should moved to.
 * @param {string} position The position of the moved row relative to the reference row.  
 *        Valid values are: "before", "after" 
 * @param {function()} callbacks.success the callback to invoke when the move completed successfully.  
 * @param {function({status: Object})} callbacks.error the callback to invoke when move failed.
 * @param {Object=} callbackObjects the object in which the callback function is invoked on.  This is optional.  
 *        You can specify the callback object for each callbacks using the "success" and "error" properties.
 * @export
 */ 
oj.CubeDataGridDataSource.prototype.move = function(rowToMove, referenceRow, position, callbacks, callbackObjects)
{
    oj.Assert.failedInAbstractFunction();
};

/**
 * Checks whether a move operation is valid.
 * @param {Object} rowToMove the key of the row to move
 * @param {Object} referenceRow the key of the reference row which combined with position are used to determine 
 *        the destination of where the row should moved to.
 * @param {string} position The position of the moved row relative to the reference row.  
 *        Valid values are: "before", "after".
 * @return {string} returns "valid" if the move is valid, "invalid" otherwise.
 * @export
 */ 
oj.CubeDataGridDataSource.prototype.moveOK = function(rowToMove, referenceRow, position)
{
    return "invalid";
};

/**
 * Determines whether this DataGridDataSource supports certain feature.
 * @param {string} feature the feature in which its capabilities is inquired.  Currently the only valid feature is "sort".
 * @return {string|null} the name of the feature.  For "sort", the valid return values are: "full", "none", "row", "column".  
 *         For "move", the valid return values are: "row", "none".  
 *         Returns null if the feature is not recognized.
 * @export
 */
oj.CubeDataGridDataSource.prototype.getCapability = function(feature)
{
    switch (feature) {
        case "sort":
            return "none";
        case "move":
            return "none";
    }
    return null;
};

oj.CubeDataGridDataSource._convertAxes = function(axis) {
    return axis === "row" ? 1 : 0;
};

oj.CubeDataGridDataSource.prototype._getAxis = function(axis) {
    var axisNum = oj.CubeDataGridDataSource._convertAxes(axis);
    var axes = this.data.getAxes();
    if (axes.length > axisNum) {
        return axes[axisNum];
    }
    return null;
};