1 /** 2 * @fileOverview Contains the code for mock api that responds to a request. 3 * @version 0.1 4 * @author Pulkit Goyal <pulkit110@gmail.com> 5 */ 6 7 /** 8 * Mimics the API and gives response for requests. 9 * 10 * @class MatrixGenerator 11 * See API Details (https://github.com/pulkit110/jMatrixBrowse/wiki/API-Details) 12 * for more details. 13 * 14 * @param {Number} [height] - height of the matrix to answer requests for. optional. default = 100. 15 * @param {Number} [width] - width of the matrix to answer requests for. optional. default = 100. 16 */ 17 function MockApi(height, width) { 18 var matrixGenerator = new MatrixGenerator('sequential'); 19 matrixGenerator.init((height==undefined)?1000:height, (width==undefined)?1000:width); 20 21 /** 22 * Get a response for given request. 23 * @param {Object} request - the request. 24 * @returns {Object} response - the response to the request. 25 */ 26 this.getResponse = function(request) { 27 var mat_testMatrix = matrixGenerator.test_getMatrix(); 28 var arr_rowLabels = matrixGenerator.test_getMatrixRowLabels(); 29 var arr_colLabels = matrixGenerator.test_getMatrixColumnLabels(); 30 31 // TODO: Check for undefined variables 32 var row1 = request.row1; 33 var row2 = request.row2; 34 var col1 = request.col1; 35 var col2 = request.col2; 36 37 var obj_response = { 38 "matrix" : { 39 "height" : mat_testMatrix.length, 40 "width" : mat_testMatrix[0].length // TODO: Check if length is atleast 1 41 }, 42 "row" : { 43 "labels" : [] 44 }, 45 "column" : { 46 "labels" : [] 47 }, 48 "data" : [ 49 ] 50 }; 51 52 for (var i=row1; i <= row2; ++i) { 53 obj_response.row.labels.push(arr_rowLabels[i]); 54 obj_response.data.push([]); 55 for (var j=col1; j <= col2; ++j) { 56 if (i === row1) { 57 obj_response.column.labels.push(arr_colLabels[j]); 58 } 59 obj_response.data[i-row1].push(mat_testMatrix[i][j]); 60 }; 61 }; 62 63 return obj_response; 64 } 65 };