From 37641650ebe9bed3d317cff5e7f6e64074df89ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Wed, 20 May 2015 16:10:02 +0200 Subject: [PATCH] Add dynamical rowWidth functionailty to table --- js/components/piece_list.js | 16 +++++++++------- js/components/table_header.js | 8 +++++--- js/components/table_item.js | 11 ++++++----- js/mixins/table_column_mixin.js | 13 ++++++++++--- js/utils/general_utils.js | 9 +++++++++ 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/js/components/piece_list.js b/js/components/piece_list.js index 7b5f5d75..088b6dde 100644 --- a/js/components/piece_list.js +++ b/js/components/piece_list.js @@ -23,19 +23,21 @@ let PieceList = React.createClass({ // Specifiy how a TableItemX should look like let columnMap = { 'thumbnail': { - 'display_name': 'Thumbnail', - 'display_type': TableItemImg + 'displayName': 'Thumbnail', + 'displayType': TableItemImg, + 'rowWidth': 2 }, 'artist_name': { - 'display_name': 'Artist', - 'display_type': TableItemText + 'displayName': 'Artist', + 'displayType': TableItemText, + 'rowWidth': 3 }, 'title': { - 'display_name': 'Title', - 'display_type': TableItemText + 'displayName': 'Title', + 'displayType': TableItemText, + 'rowWidth': 6 } }; - return ( diff --git a/js/components/table_header.js b/js/components/table_header.js index af755290..ef92ea28 100644 --- a/js/components/table_header.js +++ b/js/components/table_header.js @@ -12,15 +12,17 @@ let TableHeader = React.createClass({ render() { let columnMapValuesList = GeneralUtils.valuesOfObject(this.props.columnMap); - let columnClasses = this.calcColumnClasses(this.props.columnMap); return (
{columnMapValuesList.map((val, i) => { + + let columnClass = this.calcColumnClasses(this.props.columnMap, i); + return ( -
- {val.display_name} +
+ {val.displayName}
); })} diff --git a/js/components/table_item.js b/js/components/table_item.js index 44b30b90..c1f06872 100644 --- a/js/components/table_item.js +++ b/js/components/table_item.js @@ -10,8 +10,9 @@ let TableItem = React.createClass({ // ToDo: Specify that every columnMap should look like this: // { // 'name-of-the-data-point': { - // 'display_name': String, - // 'display_type': ReactComponent + // 'displayName': String, + // 'displayType': ReactComponent, + // 'rowWidth': number // } // // } @@ -22,7 +23,6 @@ let TableItem = React.createClass({ render() { let columnContent = this.props.columnContent; - let columnClasses = this.calcColumnClasses(this.props.columnMap); let columnMapKeysList = Object.keys(this.props.columnMap); /** @@ -32,10 +32,11 @@ let TableItem = React.createClass({ let calcColumnElementContent = () => { return columnMapKeysList.map((key, i) => { - let TypeElement = this.props.columnMap[key].display_type; + let TypeElement = this.props.columnMap[key].displayType; + let columnClass = this.calcColumnClasses(this.props.columnMap, i); return ( -
+
); diff --git a/js/mixins/table_column_mixin.js b/js/mixins/table_column_mixin.js index 35af442c..946b8262 100644 --- a/js/mixins/table_column_mixin.js +++ b/js/mixins/table_column_mixin.js @@ -5,11 +5,18 @@ let TableColumnMixin = { * Generates the bootstrap grid column declarations automatically using * the columnMap. */ - calcColumnClasses(obj) { + calcColumnClasses(obj, i) { let bootstrapClasses = ['col-xs-', 'col-sm-', 'col-md-', 'col-lg-']; - let numOfColumns = GeneralUtils.valuesOfObject(obj).length; + + let rowData = GeneralUtils.valuesOfObject(obj); + let listOfRowValues = rowData.map((val) => val.rowWidth ); + let numOfColumns = GeneralUtils.sumNumList(listOfRowValues); - return bootstrapClasses.join( numOfColumns + ' ') + numOfColumns; + if(numOfColumns > 12) { + throw new Error('Bootstrap has only 12 columns to assign. You defined ' + numOfColumns + '. Change this in the columnMap you\'re passing to the table.') + } else { + return bootstrapClasses.join( listOfRowValues[i] + ' ') + listOfRowValues[i]; + } } }; diff --git a/js/utils/general_utils.js b/js/utils/general_utils.js index 7fdc88ca..27a40446 100644 --- a/js/utils/general_utils.js +++ b/js/utils/general_utils.js @@ -25,6 +25,15 @@ let GeneralUtils = { return Object .keys(obj) .map(key => obj[key]); + }, + + /** + * Sums up a list of numbers. Like a Epsilon-math-kinda-sum... + */ + sumNumList(l) { + let sum = 0; + l.forEach((num) => sum += parseFloat(num) || 0); + return sum; } };