1 /**
  2  * @fileOverview Contains the jMatrixBrowse configuration.
  3  * 
  4  * This reads the configuration set in the DOM by the user as per the options 
  5  * defined in https://github.com/pulkit110/jMatrixBrowse/wiki/Use
  6  * 
  7  * @version 0.1
  8  * @author Pulkit Goyal <pulkit110@gmail.com>
  9 */
 10 
 11 var jMatrixBrowseNs = jMatrixBrowseNs || {};
 12 
 13 (function (jQuery, jMatrixBrowseNs) {
 14 
 15  /**
 16    * Manages configurations for jMatrixBrowse.
 17    *
 18    * @param {jQuery Object} elem - element that initiated jMatrixBrowse.
 19    * @param {Object} api - api manager for making requests to api.
 20    * @class Configuration
 21    * @memberOf jMatrixBrowseNs
 22    */
 23   jMatrixBrowseNs.Configuration = function(elem, api) {
 24     var that = this;
 25 
 26     var _dataReloadStategy = jMatrixBrowseNs.Constants.DEFAULT_DATA_RELOAD_STRATEGY;
 27 
 28     var _api = api;         // api manager
 29     // Get user options
 30     var options = getUserOptions(elem);
 31     // Extending user options with application defaults
 32     var _settings = extendDefaults(options);
 33 
 34     // Public methods
 35     /**
 36      * Gets settings object.
 37      * @returns {Object} User settings for jMatrixBrowse.
 38      * See (https://github.com/pulkit110/jMatrixBrowse/wiki/Use) for list of available settings.
 39      */
 40     this.getSettings = function() {
 41       return _settings;
 42     };
 43 
 44     /**
 45      * Gets window size from settings.
 46      * @returns {Object} size - size of the window.
 47      * @returns {Number} size.width - width of the window.
 48      * @returns {Number} size.height - height of the window.
 49      */
 50     this.getWindowSize = function() {
 51       if (_settings && _settings.str_initialWindowSize) {
 52         var  position = jMatrixBrowseNs.Utils.parsePosition(_settings.str_initialWindowSize);
 53         return {
 54           height: position.row,
 55           width: position.col
 56         };
 57       }
 58       return null;
 59     };
 60 
 61     /**
 62      * Sets window size.
 63      * @param {Object} size - size of the window.
 64      */
 65     this.setWindowSize = function(size) {
 66       _settings.str_initialWindowSize = size.height + ',' + size.width;
 67     };
 68 
 69     /**
 70      * Gets position of window.
 71      * @returns {Object} position - position of the top-left corner of window.
 72      * @returns {Number} position.row - row index of the position.
 73      * @returns {Number} position.col - column index of the position.
 74      */
 75     this.getWindowPosition = function() {
 76       if (_settings && _settings.str_initialWindowPosition) {
 77         var  position = jMatrixBrowseNs.Utils.parsePosition(_settings.str_initialWindowPosition);
 78         return position;
 79       }
 80       return null;
 81     };
 82 
 83     /**
 84      * Get the window end points which has given point at its top left corner.
 85      * @param {Object} position - position of the cell.
 86      * @param {Number} position.row - row of the cell.
 87      * @param {Number} position.col - column of the cell.
 88      * @returns {Object} window - Object representing the window coordinates.
 89      * @returns {Number} window.row1 - row index of the top left corner.
 90      * @returns {Number} window.col1 - column index of the top left corner.
 91      * @returns {Number} window.row2 - row index of the bottom right corner.
 92      * @returns {Number} window.col2 - column index of the bottom right corner.
 93      */
 94     this.getCellWindow = function(position) {
 95       var size = _api.getMatrixSize();
 96       if (size == undefined) {
 97         throw "Unable to get matrix size";
 98         return null;
 99       }
100 
101       var windowSize = that.getWindowSize();
102       if (windowSize == undefined) {
103         throw "Unable to get window size.";
104         return null;
105       }
106 
107       return {
108         row1: position.row - that.getNumberOfBackgroundCells(),
109         col1: position.col - that.getNumberOfBackgroundCells(),
110         row2: Math.min(position.row + windowSize.height, size.height),
111         col2: Math.min(position.col + windowSize.width, size.width)
112       };
113     };
114 
115     /**
116      * Gets the number of background cells to use.
117      * @returns nBackgroundCells The number of background cells.
118      */
119     this.getNumberOfBackgroundCells = function() {
120       return jMatrixBrowseNs.Constants.N_BACKGROUND_CELLS;
121     };
122 
123     /**
124      * Gets the number ata reload strategy to use.
125      * @returns dataReloadStrategy Reload strategy (possible options defined in Constants)
126      */
127     this.getDataReloadStrategy = function() {
128       return _dataReloadStategy;
129     };
130     
131     this.isSnapEnabled = function() {
132       return _settings.boo_snap;
133     };
134 
135     this.getAnimationDuration = function() {
136       return _settings.animationDuration;
137     };
138 
139     this.getMinVelocityForAnimation = function() {
140       return _settings.minVelocityForAnimation;
141     };
142 
143     this.animateEnabled = function() {
144       return _settings.boo_animate;
145     }
146 
147     // Private methods
148     /**
149     * Validates the options defined by the user.
150     * @param {Object} options - User's options for the plugin.
151     * @returns {Boolean} true if the options are valid.
152     */
153     function validate(options) {   
154       if (options.boo_jMatrixBrowser !== true && options.boo_jMatrixBrowser !== true) {
155         throw "data-jmatrix_browser invalid";
156       }
157       if (options.str_api === undefined || options.str_api === null) {
158         throw "data-api invalid.";
159       }
160       if (options.str_initialWindowSize && !(/\s*\d+\s*,\s*\d+\s*/).test(options.str_initialWindowSize)) {
161         throw "data-initial-window-size invalid."
162       }
163       if (options.str_initialWindowPosition && !(/\s*\d+\s*,\s*\d+\s*/).test(options.str_initialWindowPosition)) {
164         throw "data-initial-window-size invalid."
165       }
166       return true;
167     }
168 
169     /**
170     * Get user defined options from data-* elements.
171     * @param {jQuery object} elem - the element to retrieve the user options from.
172     * @returns {Object} options - User's options for the plugin.
173     * @returns {boolean} options.boo_jMatrixBrowser - Is jMatrixBrowse active for the container.
174     * @returns {string} options.str_api - URI for the API.
175     * @returns {string} options.str_initialWindowSize - comma separated window size as (width, height).
176     * @returns {string} options.str_initialWindowPosition - comma separated window position as (row,col).
177     */
178     function getUserOptions(elem) {
179       var options = {
180         boo_jMatrixBrowser: (elem.attr('data-jmatrix_browser') === 'true'),
181         str_api: elem.attr('data-api'),
182         str_initialWindowSize: elem.attr('data-initial-window-size'),
183         str_initialWindowPosition: elem.attr('data-initial-window-position'),
184         boo_snap: elem.attr('data-snap') === 'true',
185         boo_animate: elem.attr('data-animate') === 'true',
186         animationDuration: elem.attr('data-deceleration-duration'),
187         minVelocityForAnimation: elem.attr('data-min-velocity-for-animation')
188       };
189       var temp = elem.attr('data-animate');
190       
191       if (validate(options))
192         return options;
193       else
194         throw "Unable to get user options.";
195     }
196 
197     /**
198     * Extend the user's settings with defaults.
199     * @returns {Object} options - User's options for the plugin.
200     * @returns {boolean} options.boo_jMatrixBrowser - Is jMatrixBrowse active for the container.
201     * @returns {string} options.str_api - URI for the API.
202     * @returns {string} options.str_initialWindowSize - comma separated window size as (width, height).
203     * @returns {string} options.str_initialWindowPosition - comma separated window position as (row,col).
204     */
205     function extendDefaults(options) {
206       return jQuery.extend(jMatrixBrowseNs.Constants.DEFAULT_OPTIONS, options);
207     }
208 
209     /**
210     * Set the settings object.
211     * @param {Object} settings
212     */
213     function setSettings(settings) {
214       _settings = settings;
215     }
216     
217     return that;
218   };
219 
220 })(jQuery, jMatrixBrowseNs);