All files / wdclib NativeDispatcher.js

100% Statements 39/39
90.91% Branches 10/11
100% Functions 14/14
100% Lines 39/39
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                            14x 14x 14x               14x   14x   14x                                   1x                   1x                   1x                   1x                   2x 1x 1x     1x 1x             14x   14x 14x   14x                               2x 1x 1x     1x 1x                   2x 1x 1x     1x 1x                         2x   2x     1x     1x                     1x                       2x   2x   1x     1x               1x          
 
/**
 * Used for communicating between Tableau desktop/server and the WDC's
 * Javascript. is predominantly a pass-through to the Qt WebBridge methods
 */
class NativeDispatcher {
 
    /**
     *
     * @param {Object} nativeApiRootObj - The root object where the native Api methods are available.
     * For WebKit, 'this' is window.
     *
     */
    constructor (nativeApiRootObj) {
        this.nativeApiRootObj = nativeApiRootObj;
        this._initPublicInterface();
        this._initPrivateInterface();
    }
 
    /**
     * @returns {Undefined}
     */
    _initPublicInterface () {
 
        console.log('Initializing public interface for NativeDispatcher');
 
        this._submitCalled = false;
 
        this.publicInterface = {
            abortForAuth: this._abortForAuth.bind(this),
            abortWithError: this._abortWithError.bind(this),
            addCrossOriginException: this._addCrossOriginException.bind(this),
            log: this._log.bind(this),
            submit: this._submit.bind(this),
            reportProgress: this._reportProgress.bind(this)
        };
 
    }
 
    /**
     * @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.tableau.abortforauth
     *
     * @param {String} msg
     * @returns {Undefined}
     */
    _abortForAuth (msg) {
        this.nativeApiRootObj.WDCBridge_Api_abortForAuth.api(msg);
    }
 
    /**
     * @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.tableau.abortwitherror
     *
     * @param {String} msg
     * @returns {Undefined}
     */
    _abortWithError (msg) {
        this.nativeApiRootObj.WDCBridge_Api_abortWithError.api(msg);
    }
 
    /**
     * Missing documentation online, we need to add one
     *
     * @param {Array} destOriginList
     * @returns {Undefined}
     */
    _addCrossOriginException (destOriginList) {
        this.nativeApiRootObj.WDCBridge_Api_addCrossOriginException.api(destOriginList);
    }
 
    /**
     * @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.tableau.log
     *
     * @param {String} msg
     * @returns {Undefined}
     */
    _log (msg) {
        this.nativeApiRootObj.WDCBridge_Api_log.api(msg);
    }
 
    /**
     * @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.tableau.submit
     *
     * @returns {Undefined}
     */
    _submit () {
 
        if (this._submitCalled) {
            console.log('submit called more than once');
            return;
        }
 
        this._submitCalled = true;
        this.nativeApiRootObj.WDCBridge_Api_submit.api();
    }
 
    /**
     * @returns {Undefined}
     */
    _initPrivateInterface () {
        console.log('Initializing private interface for NativeDispatcher');
 
        this._initCallbackCalled = false;
        this._shutdownCallbackCalled = false;
 
        this.privateInterface = {
            _initCallback: this._initCallback.bind(this),
            _shutdownCallback: this._shutdownCallback.bind(this),
            _schemaCallback: this._schemaCallback.bind(this),
            _tableDataCallback: this._tableDataCallback.bind(this),
            _dataDoneCallback: this._dataDoneCallback.bind(this)
        };
    }
 
    /**
     * @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.initcallback
     *
     * @returns {Undefined}
     */
    _initCallback () {
 
        if (this._initCallbackCalled) {
            console.log('initCallback called more than once');
            return;
        }
 
        this._initCallbackCalled = true;
        this.nativeApiRootObj.WDCBridge_Api_initCallback.api();
    }
 
    /**
     * @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.shutdowncallback
     *
     * @returns {Undefined}
     */
    _shutdownCallback () {
 
        if (this._shutdownCallbackCalled) {
            console.log('shutdownCallback called more than once');
            return;
        }
 
        this._shutdownCallbackCalled = true;
        this.nativeApiRootObj.WDCBridge_Api_shutdownCallback.api();
    }
 
    /**
     * @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.schemacallback
     *
     * @param {Array<TableInfo>} schema  TableInfo @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.tableinfo-1
     * @param {Array<StandardConnection>} standardConnections StandardConnection @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.standardconnection
     * @returns {Undefined}
     */
    _schemaCallback (schema, standardConnections = []) {
 
        // Check to make sure we are using a version of desktop which has the WDCBridge_Api_schemaCallbackEx defined
        let schemaCallbackExAvailable = !!this.nativeApiRootObj.WDCBridge_Api_schemaCallbackEx;
 
        if (schemaCallbackExAvailable) {
 
            // Providing standardConnections is optional but we can't pass undefined back because Qt will choke
            this.nativeApiRootObj.WDCBridge_Api_schemaCallbackEx.api(schema, standardConnections);
 
        } else {
            this.nativeApiRootObj.WDCBridge_Api_schemaCallback.api(schema);
        }
    }
 
    /**
     *
     * @param {String} tableName
     * @param {*} data
     * @returns {Undefined}
     */
    _tableDataCallback (tableName, data) {
        this.nativeApiRootObj.WDCBridge_Api_tableDataCallback.api(tableName, data);
    }
 
    /**
     * @see http://tableau.github.io/webdataconnector/docs/api_ref.html#webdataconnectorapi.tableau.reportProgress
     *
     * @param {String} progressMessage
     * @returns {Undefined}
     */
    _reportProgress (progressMessage) {
 
        // Report progress was added in 2.1 so it may not be available if Tableau only knows 2.0
        let reportProgressAvailable = !!this.nativeApiRootObj.WDCBridge_Api_reportProgress;
 
        if (reportProgressAvailable) {
 
            this.nativeApiRootObj.WDCBridge_Api_reportProgress.api(progressMessage);
 
        } else {
            console.log('reportProgress not available from this Tableau version');
        }
    }
 
    /**
     * @returns {Undefined}
     */
    _dataDoneCallback () {
        this.nativeApiRootObj.WDCBridge_Api_dataDoneCallback.api();
    }
}
 
export default NativeDispatcher;