diff --git a/src/ui/checkbox_list.js b/src/ui/checkbox_list.js index 49e0598ad06add470f91b5fa1ff1d80254d632ac..abca257eb7542c623e37ea4d197a5270f2f19b25 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 181e99c438e4bbc878f54e55988ec81c453561cf..112538a0a45d4126c5d747804e8d774bfc456ed4 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 = $('