All files / wdclib Shared.js

0% Statements 0/62
0% Branches 0/16
0% Functions 0/3
0% Lines 0/62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
 
import { applyEnums } from './Enums';
import Table from './Table';
 
/**
* This class represents the shared parts of the javascript
* library which do not have any dependence on whether we are running in
* the simulator, in Tableau, or anywhere else
*/
class Shared {
 
    /**
     *
     * @param {Object} tableauApiObj - The already created tableau API object (usually window.tableau)
     * @param {Object} privateApiObj - The already created private API object (usually window._tableau)
     * @param {Object} globalObj - The global object to attach things to (usually window)
     */
    constructor (tableauApiObj, privateApiObj, globalObj) {
        this.privateApiObj = privateApiObj;
        this.globalObj = globalObj;
        this._hasAlreadyThrownErrorSoDontThrowAgain = false;
 
        this.changeTableauApiObj(tableauApiObj);
    }
 
    /**
     * @returns {Undefined}
     */
    init () {
        console.log('Initializing shared WDC');
 
        this.globalObj.onerror = this._errorHandler.bind(this);
 
        // Initialize the functions which will be invoked by the native code
        this._initTriggerFunctions();
 
        // Assign the deprecated functions which aren't availible in this version of the API
        this._initDeprecatedFunctions();
    }
 
    /**
     *
     * @param {Object} tableauApiObj
     * @returns {Undefined}
     */
    changeTableauApiObj (tableauApiObj) {
        this.tableauApiObj = tableauApiObj;
 
        // Assign our make & register functions right away because a connector can use
        // them immediately, even before bootstrapping has completed
        this.tableauApiObj.makeConnector = this._makeConnector.bind(this);
        this.tableauApiObj.registerConnector = this._registerConnector.bind(this);
 
        applyEnums(this.tableauApiObj);
    }
 
    /**
     *
     * @param {String} message
     * @param {String} file
     * @param {String} line
     * @param {String} column
     * @param {Object} errorObj
     * @returns {Boolean}
     */
    _errorHandler (message, file, line, column, errorObj) {
        // print error for debugging in the browser
        console.error(errorObj);
 
        if (this._hasAlreadyThrownErrorSoDontThrowAgain) {
            console.log('Error already thrown');
            return true;
        }
 
        let msg = message;
 
        if (errorObj) {
            msg += '   stack:' + errorObj.stack;
        } else {
            msg += '   file: ' + file;
            msg += '   line: ' + line;
        }
 
        if (this.tableauApiObj && this.tableauApiObj.abortWithError) {
            this.tableauApiObj.abortWithError(msg);
        } else {
            throw msg;
        }
 
        this._hasAlreadyThrownErrorSoDontThrowAgain = true;
 
        return true;
    }
 
    /**
     * @returns {Object}
     */
    _makeConnector () {
 
        let defaultImpls = {
            init: function (cb) { cb(); },
            shutdown: function (cb) { cb(); }
        };
 
        return defaultImpls;
    }
 
    /**
     *
     * @param {Object} wdc
     * @returns {Undefined}
     */
    _registerConnector (wdc) {
        // do some error checking on the wdc
        const FUNCTION_NAMES = ['init', 'shutdown', 'getSchema', 'getData'];
 
        for (let ii = FUNCTION_NAMES.length - 1; ii >= 0; ii--) {
            if (typeof (wdc[FUNCTION_NAMES[ii]]) !== 'function') {
                throw new Error(`The connector did not define the required function: ${FUNCTION_NAMES[ii]}`);
            }
        }
 
        console.log('Connector registered');
 
        this.globalObj._wdc = wdc;
        this._wdc = wdc;
    }
 
    /**
     * @returns {Undefined}
     */
    _initTriggerFunctions () {
        this.privateApiObj.triggerInitialization = this._triggerInitialization.bind(this);
        this.privateApiObj.triggerSchemaGathering = this._triggerSchemaGathering.bind(this);
        this.privateApiObj.triggerDataGathering = this._triggerDataGathering.bind(this);
        this.privateApiObj.triggerShutdown = this._triggerShutdown.bind(this);
    }
 
    /**
     * Starts the WDC
     * @returns {Undefined}
     */
    _triggerInitialization () {
        this._wdc.init(this.privateApiObj._initCallback);
    }
 
    /**
     * Starts the schema gathering process
     * @returns {Undefined}
     */
    _triggerSchemaGathering () {
        this._wdc.getSchema(this.privateApiObj._schemaCallback);
    }
 
    /**
     * Starts the data gathering process
     * @param {Array} tablesAndIncrementValues
     * @returns {undefined}
     */
    _triggerDataGathering (tablesAndIncrementValues) {
 
        if (tablesAndIncrementValues.length !== 1) {
            throw new Error(`Unexpected number of tables specified. Expected 1, actual  ${tablesAndIncrementValues.length}`);
        }
 
        let tableAndIncrementValue = tablesAndIncrementValues[0];
        let isJoinFiltered = !!tableAndIncrementValue.filterColumnId;
 
        let table = new Table(
            tableAndIncrementValue.tableInfo,
            tableAndIncrementValue.incrementValue,
            isJoinFiltered,
            tableAndIncrementValue.filterColumnId,
            tableAndIncrementValue.filterValues,
            this.privateApiObj._tableDataCallback
        );
 
        this._wdc.getData(table, this.privateApiObj._dataDoneCallback);
    }
 
    /**
     * Tells the WDC it's time to shut down
     * @returns {Undefined}
     */
    _triggerShutdown () {
        this._wdc.shutdown(this.privateApiObj._shutdownCallback);
    }
 
    /**
     * Initializes a series of global callbacks which have been deprecated in version 2.0.0
     * @returns {Undefined}
     */
    _initDeprecatedFunctions () {
        this.tableauApiObj.initCallback = this._initCallback.bind(this);
        this.tableauApiObj.headersCallback = this._headersCallback.bind(this);
        this.tableauApiObj.dataCallback = this._dataCallback.bind(this);
        this.tableauApiObj.shutdownCallback = this._shutdownCallback.bind(this);
    }
 
    /**
     * @deprecated Since v2.0.0
     *
     * @returns {Undefined}
     */
    _initCallback () {
        this.tableauApiObj.abortWithError('tableau.initCallback has been deprecated in version 2.0.0. Please use the callback function passed to init');
    }
 
    /**
     * @deprecated Since v2.0.0
     *
     * @param {Array} fieldNames
     * @param {Array} types
     * @returns {Undefined}
     */
    _headersCallback (fieldNames, types) { // eslint-disable-line no-unused-vars
        this.tableauApiObj.abortWithError('tableau.headersCallback has been deprecated in version 2.0.0');
    }
 
    /**
     * @deprecated Since v2.0.0
     *
     * @param {*} data
     * @param {*} lastRecordToken
     * @param {*} moreData
     * @returns {Undefined}
     */
    _dataCallback (data, lastRecordToken, moreData = null) { // eslint-disable-line no-unused-vars
        this.tableauApiObj.abortWithError('tableau.dataCallback has been deprecated in version 2.0.0');
    }
 
    /**
     * @deprecated Since v2.0.0
     *
     * @returns {Undefined}
     */
    _shutdownCallback () {
        this.tableauApiObj.abortWithError('tableau.shutdownCallback has been deprecated in version 2.0.0. Please use the callback function passed to shutdown');
    }
}
 
export default Shared;