1 /**
  2  * @fileOverview Contains utility funcitons that several modules of 
  3  * jMatrixBrowse use.
  4  * @version 0.1
  5  * @author Pulkit Goyal <pulkit110@gmail.com> 
  6  */
  7 
  8 var jMatrixBrowseNs = jMatrixBrowseNs || {};
  9 
 10 /**
 11  * Utility functions for jMatrixBrowse.
 12  * 
 13  * @class Utils
 14  * @memberOf jMatrixBrowseNs
 15  */
 16 jMatrixBrowseNs.Utils = {
 17   /** 
 18    * Utility function that parses a position (row,col).
 19    * @returns {Object} position - Object representing the position denoted by the string. 
 20    * @returns {Number} position.row - row index of the position.
 21    * @returns {Number} position.col - column index of the position.
 22    */
 23   parsePosition : function(str_position) {
 24     var arr_position = str_position.split(',');
 25     if (arr_position.length == 2) {
 26       return {
 27         row: parseInt(arr_position[0]),
 28         col: parseInt(arr_position[1])
 29       };
 30     }
 31   },
 32 
 33   /**
 34    * Check if the given cell is overflowing the container dimensions. 
 35    * @param {jQuery Object} element - jQuery object for the element to be checked for overflow.
 36    * @param {jQuery Object} container - The container against which to check the overflow.
 37    * @param {Number} overflow - Type of oevrflow to check.
 38    * @returns true if there is an overflow. false otherwise.
 39    */
 40   isOverflowing : function(element, container, overflow) {
 41     var containerOffset = container.offset();
 42     var elementOffset = element.offset();
 43       
 44     var top = elementOffset.top - containerOffset.top;
 45     var left = elementOffset.left - containerOffset.left;
 46     var width = element.width();
 47     var height = element.height();
 48 
 49     switch (overflow) {
 50       case jMatrixBrowseNs.Constants.OVERFLOW_LEFT:
 51         return (left+width < 0);
 52       case jMatrixBrowseNs.Constants.OVERFLOW_RIGHT:
 53         return (left > container.width());
 54       case jMatrixBrowseNs.Constants.OVERFLOW_TOP:
 55         return (top+height < 0);
 56       case jMatrixBrowseNs.Constants.OVERFLOW_BOTTOM:
 57         return (top > container.height());
 58     }
 59     return false;
 60   },
 61 
 62   /**
 63    * Finds the min and index of min number in the array.
 64    *
 65    * @param {Array} array - the array to look into.
 66    * @returns {Number} obj.min - the minimum number in the array.
 67    * @returns {Number} obj.minIndex - the index of minimum number in the array.
 68    */
 69   findIndexOfMin : function(array) {
 70     if (array && array.length) {
 71       var min = array[0];
 72       var minIndex = 0;
 73       for (var i = 1, l = array.length; i < l; ++i) {
 74         if (array[i] < min) {
 75           min = array[i];
 76           minIndex = i;
 77         }
 78       }
 79       return {
 80         min: min,
 81         minIndex: minIndex
 82       };
 83     }
 84     return null;
 85   }
 86 }
 87 
 88