1 /** 2 * @fileOverview Handles requests to the API. 3 * 4 * Handles the requests to API. All the requests to the API are sent through 5 * this class rather than directly making requests to the API. To manage data 6 * of different formats, the user can implement his own API class that 7 * implements the public methods for the module. 8 * 9 * @version 0.1 10 * @author Pulkit Goyal <pulkit110@gmail.com> 11 */ 12 13 var jMatrixBrowseNs = jMatrixBrowseNs || {}; 14 15 (function (jQuery, jMatrixBrowseNs) { 16 17 /** 18 * Manages requests to the api. 19 * 20 * @param {jQuery Object} type - type of API to use. 21 * @class APIHandler 22 * @memberOf jMatrixBrowseNs 23 */ 24 jMatrixBrowseNs.APIHandler = function(type) { 25 var that = this; 26 27 // Initialize the API 28 initApi(type); 29 30 /** 31 * Get matrix size from api. 32 * @returns {Object} size - size of the matrix. 33 * @returns {Number} size.width - width of the matrix. 34 * @returns {Number} size.height - height of the matrix. 35 */ 36 this.getMatrixSize = function() { 37 var response = _api.getResponse({ 38 'row1': 0, 39 'col1': 0, 40 'row2': 0, 41 'col2': 0 42 }); 43 if (response) 44 return response.matrix; 45 }; 46 47 // TODO: Not yet implemented 48 /** 49 * Get the row headers for the current window from top row index. 50 * @param {Number} topRowIndex - index of the top row 51 * @returns {Array} rowHeaders - Array of row headers 52 */ 53 this.getRowHeadersFromTopRow = function(topRowIndex) { 54 55 var height = _renderer.getCellElements().length; 56 var rowHeaders = []; 57 // TODO: Load from API 58 for (var i = 0; i < height; ++i) { 59 rowHeaders.push('row: ' + (topRowIndex+i)); 60 } 61 return rowHeaders; 62 }; 63 64 /** 65 * Get the column headers for the current window from left column index. 66 * @param {Number} leftColIndex - index of the left column 67 * @returns {Array} colHeaders - Array of column headers 68 */ 69 this.getColHeadersFromLeftCol = function(leftColIndex) { 70 var width = _renderer.getCellElements()[0].length; 71 var colHeaders = []; 72 // TODO: Load from API 73 for (var i = 0; i < width; ++i) { 74 colHeaders.push('col: ' + (leftColIndex+i)); 75 } 76 return colHeaders; 77 }; 78 79 /** 80 * Get the row data for the row index for current window. 81 * @param {Object} cell - row and column index of cell 82 * @returns {Array} rowData - Array of row data 83 * @deprecated You need to pass in the full window now. See getResponseData. 84 */ 85 this.getRowDataForCell = function(cell) { 86 var nBackgroundCells = jMatrixBrowseNs.Constants.N_BACKGROUND_CELLS; 87 var rowData = []; 88 var response = _api.getResponse({ 89 row1: cell.row, 90 col1: cell.col - nBackgroundCells, 91 row2: cell.row, 92 col2: cell.col - nBackgroundCells + _renderer.getCellElements()[0].length 93 }); 94 if (response && response.data && response.data.length == 1) { 95 for (var j = 0; j < response.data[0].length; ++j) { 96 var cellData = response.data[0][j]; 97 rowData.push(cellData); 98 } 99 } 100 return rowData; 101 }; 102 103 /** 104 * Get the col data for the col index for current window. 105 * @param {Object} cell - row and column index of cell. 106 * @returns {Array} colData - Array of col data. 107 * @deprecated You need to pass in the full window now. See getResponseData. 108 */ 109 this.getColDataForCell = function(cell) { 110 var nBackgroundCells = jMatrixBrowseNs.Constants.N_BACKGROUND_CELLS; 111 var colData = []; 112 var response = _api.getResponse({ 113 row1: cell.row - nBackgroundCells, 114 col1: cell.col, 115 row2: cell.row - nBackgroundCells + _renderer.getCellElements().length, 116 col2: cell.col 117 }); 118 if (response && response.data) { 119 for (var i = 0; i < response.data.length; ++i) { 120 var cellData = response.data[i][0]; 121 colData.push(cellData); 122 } 123 } 124 return colData; 125 }; 126 127 /** 128 * Sets the renderer. 129 * @param {Object} renderer 130 */ 131 this.setRenderer = function(renderer) { 132 _renderer = renderer; 133 }; 134 135 /** 136 * Gets the response for a request. No checks are performed. 137 * @param {Object} request - request to send to server. See (https://github.com/pulkit110/jMatrixBrowse/wiki/API-Details) 138 * @returns response received from the server. 139 */ 140 this.getResponse = function(request) { 141 return _api.getResponse(request); 142 }; 143 144 /** 145 * Gets the response data for a request. No checks are performed. 146 * @param {Object} request - request to send to server. See (https://github.com/pulkit110/jMatrixBrowse/wiki/API-Details) 147 * @returns response.data received from the server. 148 */ 149 this.getResponseData = function(request) { 150 var response = _api.getResponse(request); 151 return response.data; 152 }; 153 154 /** 155 * Gets the response data for a request. No checks are performed. 156 * callback is called with the data received from api as a parameter. 157 * This method will eventually replace the getResponseData the older method is not very useful with a live API. 158 * @param {Object} request - request to send to server. See (https://github.com/pulkit110/jMatrixBrowse/wiki/API-Details) 159 */ 160 that.getResponseDataAsync = function(request, callback) { 161 var response = _api.getResponse(request); 162 (function(response) { 163 setTimeout(function() { 164 callback.call(this, response.data); 165 }, 1000); 166 })(response); 167 168 }; 169 170 // Private methods 171 var _api; // API object 172 var _renderer; // Reference to the renderer 173 174 /** 175 * Initialize the API 176 * @param {string} type type of api: 'test' initializes the mockAPI 177 */ 178 function initApi(type) { 179 if (type === 'test') { 180 _api = new MockApi(); 181 } else { 182 console.error('API ' + type + 'not yet supported.'); 183 } 184 }; 185 186 return that; 187 }; 188 189 })(jQuery, jMatrixBrowseNs); 190