From bb9e39e4a00ec24aa3373f6dca0ffca321820031 Mon Sep 17 00:00:00 2001 From: Joshua Gould Date: Tue, 10 May 2016 14:09:12 -0400 Subject: [PATCH] table width --- src/ui/checkbox_list.js | 3 +- src/ui/grid.js | 198 +++++++++--------- src/ui/table.js | 444 ++++++++++++++++++++-------------------- 3 files changed, 331 insertions(+), 314 deletions(-) diff --git a/src/ui/checkbox_list.js b/src/ui/checkbox_list.js index 49e0598..abca257 100644 --- a/src/ui/checkbox_list.js +++ b/src/ui/checkbox_list.js @@ -38,7 +38,7 @@ morpheus.CheckBoxList = function(options) { } var idColumn = options.columns[0]; for (var i = 0; i < options.columns.length; i++) { - if (options.columns[i].isId) { + if (options.columns[i].idColumn) { idColumn = options.columns[i]; break; } @@ -247,6 +247,7 @@ morpheus.CheckBoxList.prototype = { }, on : function(evtStr, handler) { this.table.on(evtStr, handler); + return this; }, off : function(evtStr, handler) { this.table.off(evtStr, handler); diff --git a/src/ui/grid.js b/src/ui/grid.js index 181e99c..112538a 100644 --- a/src/ui/grid.js +++ b/src/ui/grid.js @@ -1,4 +1,4 @@ -morpheus.Grid = function(options) { +morpheus.Grid = function (options) { this.options = options; var _this = this; var grid; @@ -15,28 +15,28 @@ morpheus.Grid = function(options) { } var model = { - getLength : function() { + getLength: function () { return _this.viewOrder != null ? _this.viewOrder.length - : _this.items.length; + : _this.items.length; }, - getItem : function(index) { + getItem: function (index) { return _this.items[_this.viewOrder != null ? _this.viewOrder[index] - : index]; + : index]; } }; this.$el = options.$el; var gridOptions = $.extend({}, { - select : true, - headerRowHeight : 0, - showHeaderRow : false, - multiColumnSort : true, - multiSelect : false, - topPanelHeight : 0, - enableTextSelectionOnCells : true, - forceFitColumns : true, - dataItemColumnValueExtractor : getItemColumnValue, - defaultFormatter : function(row, cell, value, columnDef, dataContext) { + select: true, + headerRowHeight: 0, + showHeaderRow: false, + multiColumnSort: true, + multiSelect: false, + topPanelHeight: 0, + enableTextSelectionOnCells: true, + forceFitColumns: true, + dataItemColumnValueExtractor: getItemColumnValue, + defaultFormatter: function (row, cell, value, columnDef, dataContext) { if (_.isNumber(value)) { return morpheus.Util.nf(value); } else if (morpheus.Util.isArray(value)) { @@ -59,46 +59,46 @@ morpheus.Grid = function(options) { this.grid = grid; grid.registerPlugin(new morpheus.AutoTooltips2()); - grid.onCellChange.subscribe(function(e, args) { + grid.onCellChange.subscribe(function (e, args) { _this.trigger('edit', args); }); if (gridOptions.select) { grid.setSelectionModel(new Slick.RowSelectionModel({ - selectActiveRow : true, - multiSelect : gridOptions.multiSelect + selectActiveRow: true, + multiSelect: gridOptions.multiSelect })); - grid.getSelectionModel().onSelectedRangesChanged.subscribe(function(e) { + grid.getSelectionModel().onSelectedRangesChanged.subscribe(function (e) { var nitems = grid.getDataLength(); _this.trigger('selectionChanged', { - selectedRows : grid.getSelectedRows().filter(function(row) { + selectedRows: grid.getSelectedRows().filter(function (row) { return row >= 0 && row <= nitems; }) }); }); } - grid.onSort.subscribe(function(e, args) { + grid.onSort.subscribe(function (e, args) { _this.sortCols = args.sortCols; _this._updateMappings(); grid.invalidate(); }); - options.$el.on('click', function(e) { + options.$el.on('click', function (e) { var cell = grid.getCellFromEvent(e); if (cell) { _this.trigger('click', { - row : cell.row, - target : e.target + row: cell.row, + target: e.target }); } }); - options.$el.on('dblclick', function(e) { + options.$el.on('dblclick', function (e) { var cell = grid.getCellFromEvent(e); if (cell) { _this.trigger('dblclick', { - row : cell.row, - target : e.target + row: cell.row, + target: e.target }); } }); @@ -106,7 +106,7 @@ morpheus.Grid = function(options) { var gridSortColumns = []; var gridColumns = grid.getColumns(); var sortCols = []; - options.sort.forEach(function(c) { + options.sort.forEach(function (c) { var column = null; for (var i = 0; i < gridColumns.length; i++) { if (gridColumns[i].name === c.name) { @@ -116,12 +116,12 @@ morpheus.Grid = function(options) { } if (column != null) { sortCols.push({ - sortCol : column, - sortAsc : c.sortAsc + sortCol: column, + sortAsc: c.sortAsc }); gridSortColumns.push({ - columnId : column.id, - sortAsc : c.sortAsc + columnId: column.id, + sortAsc: c.sortAsc }); } else { console.log(c.name + ' not found.'); @@ -137,22 +137,22 @@ morpheus.Grid = function(options) { }; morpheus.Grid.prototype = { - columnsAutosized : false, - setColumns : function(columns) { + columnsAutosized: false, + setColumns: function (columns) { this.grid.setColumns(columns); this.grid.resizeCanvas(); this.grid.invalidate(); }, - getColumns : function() { + getColumns: function () { return this.grid.getColumns(); }, - getSelectedRows : function() { + getSelectedRows: function () { var nitems = this.grid.getDataLength(); - return this.grid.getSelectedRows().filter(function(row) { + return this.grid.getSelectedRows().filter(function (row) { return row >= 0 && row <= nitems; }); }, - getSelectedItems : function() { + getSelectedItems: function () { var rows = this.grid.getSelectedRows(); var selection = []; for (var i = 0, nrows = rows.length; i < nrows; i++) { @@ -160,7 +160,7 @@ morpheus.Grid.prototype = { } return selection; }, - getSelectedItem : function() { + getSelectedItem: function () { var rows = this.grid.getSelectedRows(); if (rows.length === 1) { return this.items[this.convertViewIndexToModel(rows[0])]; @@ -170,30 +170,30 @@ morpheus.Grid.prototype = { /** * Gets the sorted, visible items */ - getItems : function() { + getItems: function () { var items = []; for (var i = 0, length = this.getFilteredItemCount(); i < length; i++) { items.push(this.items[this.convertViewIndexToModel(i)]); } return items; }, - getAllItemCount : function() { + getAllItemCount: function () { return this.items.length; }, - getAllItems : function() { + getAllItems: function () { return this.items; }, - getFilteredItemCount : function() { + getFilteredItemCount: function () { return this.viewOrder ? this.viewOrder.length : this.items.length; }, - redraw : function() { + redraw: function () { this.grid.invalidate(); }, - redrawRows : function(rows) { + redrawRows: function (rows) { this.grid.invalidateRows(rows); this.grid.render(); }, - setItems : function(items) { + setItems: function (items) { // clear the selection this.items = items; if (this.grid.getSelectionModel()) { @@ -205,26 +205,26 @@ morpheus.Grid.prototype = { } }, - convertModelIndexToView : function(modelIndex) { + convertModelIndexToView: function (modelIndex) { if (this.modelToView !== null) { var index = this.modelToView.get(modelIndex); return index !== undefined ? index : -1; } return modelIndex; }, - convertViewIndexToModel : function(viewIndex) { + convertViewIndexToModel: function (viewIndex) { return this.viewOrder != null ? (viewIndex < this.viewOrder.length - && viewIndex >= 0 ? this.viewOrder[viewIndex] : -1) : viewIndex; + && viewIndex >= 0 ? this.viewOrder[viewIndex] : -1) : viewIndex; }, - _updateMappings : function() { + _updateMappings: function () { var selectedViewIndices = this.grid.getSelectionModel() != null ? this.grid - .getSelectedRows() - : null; + .getSelectedRows() + : null; var selectedModelIndices = []; if (selectedViewIndices) { for (var i = 0, length = selectedViewIndices.length; i < length; i++) { selectedModelIndices.push(this - .convertViewIndexToModel(selectedViewIndices[i])); + .convertViewIndexToModel(selectedViewIndices[i])); } } this.viewOrder = null; @@ -247,7 +247,7 @@ morpheus.Grid.prototype = { } var ncols = cols.length; var items = this.items; - this.viewOrder.sort(function(index1, index2) { + this.viewOrder.sort(function (index1, index2) { for (var i = 0; i < ncols; i++) { var getter = cols[i].sortCol.getter; var sign = cols[i].sortAsc ? 1 : -1; @@ -274,7 +274,7 @@ morpheus.Grid.prototype = { var newSelectedViewIndices = []; for (var i = 0, length = selectedModelIndices.length; i < length; i++) { var index = this - .convertModelIndexToView(selectedModelIndices[i]); + .convertModelIndexToView(selectedModelIndices[i]); if (index !== undefined) { newSelectedViewIndices.push(index); } @@ -282,45 +282,49 @@ morpheus.Grid.prototype = { this.grid.setSelectedRows(newSelectedViewIndices); } }, - setSelectedRows : function(rows) { + setSelectedRows: function (rows) { this.grid.setSelectedRows(rows); }, - setFilter : function(filter) { + setFilter: function (filter) { this.filter = filter; this._updateMappings(); this.grid.invalidate(); this.trigger('filter'); }, - getFilter : function() { + getFilter: function () { return this.filter; }, - autosizeColumns : function() { - this.columnsAutosized = true; + autosizeColumns: function () { + var columns = this.grid.getColumns(); var items = this.getItems(); + if (!items || items.length == 0) { + return; + } if (!columns || columns.length <= 1) { return; } + this.columnsAutosized = true; var div = document.createElement('div'); document.body.appendChild(div); var $d = $(div); $d.css({ - position : 'absolute', - left : -1000, - top : -1000 + position: 'absolute', + left: -1000, + top: -1000 }); var $row = $('
' - + '
' - + '
' - + '
'); + + '
' + + '
' + + ''); var $cell = $row.find('.slick-cell'); var $header = $row.find('.slick-header-column'); $row.appendTo($d); - var gridWidth = this.options.$el.width(); + var gridWidth = this.options.$el.width() - 30; var maxWidth = Math.min(parseInt(gridWidth / 2), 400); - var getColumnWidth = function(column) { + var getColumnWidth = function (column) { var w = $header.html(column.name).outerWidth(); w = Math.max($cell.outerWidth(), w); if (column.prototypeValue) { @@ -328,14 +332,17 @@ morpheus.Grid.prototype = { w = Math.max($cell.outerWidth(), w); } else { for (var i = 0, nrows = Math.min(items.length, 10); i < nrows; i++) { - var text = column.formatter(i, null, column - .getter(items[i]), column, items[i]); - $cell.html(text); + var html = column.formatter(i, null, column + .getter(items[i]), column, items[i]); + var $html = $(html); + $html.find('.slick-cell-wrapper').attr('class', ''); + $cell.html($html); + w = Math.max($cell.outerWidth(), w); } } column.width = parseInt(Math.min(maxWidth, w)); - + }; var totalWidth = 0; for (var i = 0; i < columns.length; i++) { @@ -351,10 +358,11 @@ morpheus.Grid.prototype = { } else if (totalWidth > gridWidth) { // shrink - columns[columns.length - 1].width -= (totalWidth - gridWidth); + //columns[columns.length - 1].width -= (totalWidth - gridWidth); // shrink last column } + $d.remove(); this.grid.resizeCanvas(); } @@ -365,10 +373,10 @@ morpheus.Util.extend(morpheus.Grid, morpheus.Events); /** * AutoTooltips2 plugin to show/hide tooltips when columns are too narrow to fit * content. - * + * * @constructor */ -morpheus.AutoTooltips2 = function(options) { +morpheus.AutoTooltips2 = function (options) { var _grid; var _self = this; var tip; @@ -406,7 +414,7 @@ morpheus.AutoTooltips2 = function(options) { /** * Handle mouse entering grid cell to add/remove tooltip. - * + * * @param {jQuery.Event} * e - The event */ @@ -419,9 +427,10 @@ morpheus.AutoTooltips2 = function(options) { } } } + function hideAll() { $(_grid.getCanvasNode()).find('[data-original-title]').attr( - 'data-original-title', '').tooltip('hide'); + 'data-original-title', '').tooltip('hide'); } @@ -431,6 +440,7 @@ morpheus.AutoTooltips2 = function(options) { $node.tooltip('hide'); } } + function showHeaderToolTip(e) { var show = false; var $node = $(e.target); @@ -440,10 +450,10 @@ morpheus.AutoTooltips2 = function(options) { var $name = $node.find('.slick-column-name'); if (!$node.data('bs.tooltip')) { $node.tooltip({ - placement : 'auto', - html : true, - container : 'body', - trigger : 'manual' + placement: 'auto', + html: true, + container: 'body', + trigger: 'manual' }); } $node.attr('data-original-title', $name.text()); @@ -454,6 +464,7 @@ morpheus.AutoTooltips2 = function(options) { } } } + function showToolTip(e) { var cell = _grid.getCellFromEvent(e); if (cell) { @@ -461,8 +472,9 @@ morpheus.AutoTooltips2 = function(options) { var text = ''; var c = _grid.getColumns()[cell.cell]; var show = false; + var $checkNode = $node.find('.slick-cell-wrapper'); if (c.alwaysShowTooltip - || ($node[0].scrollWidth > $node[0].offsetWidth)) { + || ($checkNode[0].scrollWidth > $checkNode[0].offsetWidth)) { var item = _grid.getDataItem(cell.row); text = c.tooltip(item, c.getter(item)); show = true; @@ -471,10 +483,10 @@ morpheus.AutoTooltips2 = function(options) { var hasTip = $node.data('bs.tooltip'); if (!hasTip) { $node.tooltip({ - placement : 'auto', - html : true, - container : 'body', - trigger : 'manual' + placement: 'auto', + html: true, + container: 'body', + trigger: 'manual' }); } if (show) { @@ -487,7 +499,7 @@ morpheus.AutoTooltips2 = function(options) { /** * Handle mouse entering header cell to add/remove tooltip. - * + * * @param {jQuery.Event} * e - The event * @param {object} @@ -495,18 +507,18 @@ morpheus.AutoTooltips2 = function(options) { */ function handleHeaderMouseEnter(e, args) { var column = args.column, $node = $(e.target).closest( - '.slick-header-column'); + '.slick-header-column'); if (!column.toolTip) { $node.attr('title', - ($node.innerWidth() < $node[0].scrollWidth) ? column.name - : ''); + ($node.innerWidth() < $node[0].scrollWidth) ? column.name + : ''); } } // Public API $.extend(this, { - 'init' : init, - 'destroy' : destroy + 'init': init, + 'destroy': destroy }); -}; \ No newline at end of file +}; diff --git a/src/ui/table.js b/src/ui/table.js index 8766a64..5a64d1b 100644 --- a/src/ui/table.js +++ b/src/ui/table.js @@ -3,7 +3,7 @@ * @param options.items * @param options.columns */ -morpheus.Table = function(options) { +morpheus.Table = function (options) { options = morpheus.Table.createOptions(options); this.options = options; if (!options.width) { @@ -27,30 +27,29 @@ morpheus.Table = function(options) { var height = options.height; var $gridDiv = $('
'); + + (options.tableClass ? (' ' + options.tableClass) : '') + + '" style="width:' + options.fixedWidth + ';height:' + height + + '">'); this.$gridDiv = $gridDiv; $gridDiv.appendTo(options.$el); var columns = options.columns; this.columns = columns; - var visibleColumns = columns.filter(function(c) { + var visibleColumns = columns.filter(function (c) { return c.visible; }); var grid = new morpheus.Grid({ - gridOptions : { - forceFitColumns : true, - select : options.select, - rowHeight : options.rowHeight, - autoEdit : false, - editable : false, - autoHeight : options.height === 'auto', - enableTextSelectionOnCells : true, + gridOptions: { + select: options.select, + rowHeight: options.rowHeight, + autoEdit: false, + editable: false, + autoHeight: options.height === 'auto', + enableTextSelectionOnCells: true, }, - $el : $gridDiv, - items : options.items, - columns : visibleColumns + $el: $gridDiv, + items: options.items, + columns: visibleColumns }); this.grid = grid; if (options.search) { @@ -62,8 +61,8 @@ morpheus.Table = function(options) { if (visibleColumns.length !== this.columns.length) { var select = []; select - .push(''); + this.columns.forEach(function (c, i) { select.push('