1 /** 2 * @class ColumnCalc 3 * @description Calculator ojbect for performing column-based calculations 4 * @constructor 5 * @param {Object} config (Required) Configuration object, see slikcalc.BaseCalc for options 6 */ 7 slikcalc.ColumnCalc = function(config) { 8 this.parent.constructor.call(this, config); 9 this.rows = []; 10 }; 11 slikcalc.extend(slikcalc.ColumnCalc, slikcalc.BaseCalc); 12 13 /** 14 * @description {Array} Array of row objects to use in calculator 15 */ 16 slikcalc.ColumnCalc.prototype.rows = null; 17 18 /** 19 * @description Runs on page load. Sets up event listeners if registerEventListeners is set to true. 20 */ 21 slikcalc.ColumnCalc.prototype.initialize = function() { 22 if(this.registerListeners === true) { 23 this.setupEventListeners(); 24 } 25 }; 26 27 /** 28 * @description Processes the rows and applies the totalOperator upon each value, placing the total in the totalId element 29 */ 30 slikcalc.ColumnCalc.prototype.calculate = function() { 31 var total = null; 32 for(var idx in this.rows) { 33 if(this.rows.hasOwnProperty(idx)) { 34 var includeRow = true; 35 if(this.rows[idx].checkbox !== undefined) { 36 var checkbox = this.rows[idx].checkbox; 37 includeRow = (checkbox.invert !== slikcalc.get(checkbox.id).checked); 38 } 39 if(includeRow === true) { 40 total = this.calculateTotal(total, slikcalc.getAmount(this.rows[idx].id)); 41 } 42 } 43 } 44 slikcalc.setAmount(this.totalId, total); 45 }; 46 47 /** 48 * @description Adds event listeners for row checkboxes and inputs 49 */ 50 slikcalc.ColumnCalc.prototype.setupEventListeners = function() { 51 for(var idx in this.rows) { 52 if(this.rows.hasOwnProperty(idx)) { 53 var rowConfig = this.rows[idx]; 54 if(rowConfig.checkbox !== undefined) { 55 slikcalc.addListener(rowConfig.checkbox.id, 'click', this.processCalculation, this); 56 } 57 slikcalc.addListener(rowConfig.id, 'keyup', this.keyupEvent, this); 58 } 59 } 60 }; 61 62 /** 63 * @description Adds a row to the calculator to be included with calculations. 64 * @param {Object} config Configuration object for row definitions, with the following options: 65 * <ul> 66 * <li>id : Element id that holds the value to calculate</li> 67 * <li>checkbox.id : (Optional) Element id of checkbox</li> 68 * <li>checkbox.invert : Defaults to false. If true, row is included in total calculcation when un-checked, and omitted when checked</li> 69 * </ul> 70 */ 71 slikcalc.ColumnCalc.prototype.addRow = function(rowConfig) { 72 rowConfig = rowConfig || {}; 73 if(rowConfig.checkbox !== undefined) { 74 rowConfig.checkbox.invert = rowConfig.checkbox.invert || false; 75 } 76 this.rows.push(rowConfig); 77 };