diff --git a/libraries.txt b/libraries.txt index 2c87ca5..21dfd4f 100644 --- a/libraries.txt +++ b/libraries.txt @@ -30,3 +30,4 @@ backgrid-select-all 1a00053 MIT https://github.com/wyuenho/backgrid dropzone 4e20bd4 MIT https://github.com/enyo/dropzone Filemanager 7e060c2 MIT https://github.com/simogeo/Filemanager Unit (Length) d8e6237 MIT https://github.com/heygrady/Units/blob/master/Length.min.js +backgrid-sizeable-columns 71f0344 MIT https://github.com/WRidder/backgrid-sizeable-columns diff --git a/web/pgadmin/static/css/backgrid/backgrid-sizeable-columns.css b/web/pgadmin/static/css/backgrid/backgrid-sizeable-columns.css new file mode 100644 index 0000000..1b4f130 --- /dev/null +++ b/web/pgadmin/static/css/backgrid/backgrid-sizeable-columns.css @@ -0,0 +1,35 @@ +/* + backgrid-sizeable-columns + https://github.com/WRidder/backgrid-sizeable-columns + + Copyright (c) 2014 Wilbert van de Ridder + Licensed under the MIT @license. + */ +table.backgrid { + overflow: hidden; + position: relative; +} + +.backgrid .resizeHandler { + width: 16px; + height: 100%; + margin-left: -8px; + top: 0; + position: absolute; + cursor: col-resize; + z-index: 2; +} + +.backgrid .resizeHandler.grid-draggable { + opacity: 1; + width: 1px; + margin-left: 0px; + background-color: #000; +} + +.backgrid .resizeHandler .grid-draggable-cursor { + cursor: col-resize; + width: 100px; + margin-left: -50px; + height: 100%; +} \ No newline at end of file diff --git a/web/pgadmin/static/js/backgrid/backgrid-sizeable-columns.js b/web/pgadmin/static/js/backgrid/backgrid-sizeable-columns.js new file mode 100644 index 0000000..fa1ca09 --- /dev/null +++ b/web/pgadmin/static/js/backgrid/backgrid-sizeable-columns.js @@ -0,0 +1,269 @@ +/* + backgrid-sizeable-columns + https://github.com/WRidder/backgrid-sizeable-columns + + Copyright (c) 2014 Wilbert van de Ridder + Licensed under the MIT @license. + */ +(function (root, factory) { + + // CommonJS + if (typeof exports == "object") { + module.exports = factory(require("underscore"), require("backgrid")); + } + // Browser + else factory(root._, root.Backgrid, root.moment); + +}(this, function (_, Backgrid) { + "use strict"; + + // Adds width support to columns + Backgrid.Extension.SizeAbleColumns = Backbone.View.extend({ + /** @property */ + tagName: "colgroup", + + /** + Initializer. + + @param {Object} options + @param {Backbone.Collection.|Array.|Array.} options.columns Column metadata. + */ + initialize: function (options) { + this.columns = options.columns; + this.collection = options.collection; + + // Attach event listeners once on render + this.listenTo(this.collection, "backgrid:header:rendered", this.render); + }, + + /** + Renders a column group. + */ + render: function () { + var view = this; + view.$el.empty(); + + this.columns.each(function(col) { + if (typeof col.get("renderable") == "undefined" || col.get("renderable")) { + var $colEl = $("").appendTo(view.$el).attr("data-column-id", col.cid); + var colWidth = col.get("width"); + var colMinWidth = col.get("minWidth"); + var colMaxWidth = col.get("maxWidth"); + + if (colWidth) { + if (colMinWidth && colWidth < colMinWidth) { + colWidth = colMinWidth; + } + if (colMaxWidth && colWidth > colMaxWidth) { + colWidth = colMaxWidth; + } + $colEl.width(colWidth); + } + } + }); + + // Trigger event + view.collection.trigger("backgrid:colgroup:updated"); + + return this; + } + }); + + // Makes column resizable; requires Backgrid.Extension.sizeAbleColumns + Backgrid.Extension.SizeAbleColumnsHandlers = Backbone.View.extend({ + /** + Initializer. + + @param {Object} options + @param {Backbone.Collection.|Array.|Array.} options.columns Column metadata. + */ + initialize: function (options) { + this.grid = options.grid; + this.columns = this.grid.columns; + this.header = this.grid.header; + this.sizeAbleColumns = options.sizeAbleColumns; + this.saveModelWidth = options.saveModelWidth; + this.setHeaderElements(); + this.attachEvents(); + }, + + /** + Renders a column group. + */ + render: function () { + var view = this; + view.$el.empty(); + + // Event helper function + var stopEvent = function(e) { + if(e.stopPropagation) e.stopPropagation(); + if(e.preventDefault) e.preventDefault(); + e.cancelBubble=true; + e.returnValue=false; + }; + + // For now, loop tds in first row + _.each(view.headerElements, function(columnEl, index) { + // Get matching col element + var $column = $(columnEl); + var $col = view.sizeAbleColumns.$el.find("col").eq(index); + var columnModel = view.columns.get($col.data("column-id")); + + if (columnModel.get("resizeAble") && + (typeof columnModel.get("renderable") == "undefined" || columnModel.get("renderable"))) { + + // Create helper elements + var $resizeHandler = $("
") + .addClass("resizeHandler") + .attr("data-column-index", index) + .appendTo(view.$el); + var $resizeHandlerHelper = $("
") + .hide() + .addClass("grid-draggable-cursor") + .appendTo($resizeHandler); + + // Make draggable + $resizeHandler.on("mousedown", function(e) { + stopEvent(e); + var startX = $resizeHandler.offset().left; + var $doc = $(document); + var handlerNonDragSize = $resizeHandler.outerWidth(); + + // Set class + $resizeHandler.addClass("grid-draggable"); + $resizeHandlerHelper.show(); + + // Follow the mouse + var mouseMoveHandler = function(evt) { + stopEvent(evt); + + // Check for constraints + var minWidth = columnModel.get("minWidth"); + if (!minWidth || minWidth < 20) { + minWidth = 20; + } + var maxWidth = columnModel.get("maxWidth"); + var newLeftPos = evt.pageX; + var currentWidth = columnModel.get("width"); + var newWidth = currentWidth + (newLeftPos - startX) - handlerNonDragSize / 2; + + if (minWidth && newWidth <= minWidth) { + newLeftPos = startX - (currentWidth - minWidth) + handlerNonDragSize / 2; + } + if (maxWidth && newWidth >= maxWidth) { + newLeftPos = startX + maxWidth - currentWidth + handlerNonDragSize / 2; + } + + // Apply mouse change to handler + $resizeHandler.offset({ + left: newLeftPos + }); + }; + $doc.on("mousemove", mouseMoveHandler); + + // Add handler to listen for mouseup + var mouseUpHandler = function(evt) { + // Cleanup + stopEvent(evt); + $resizeHandler.removeClass("grid-draggable"); + $resizeHandlerHelper.hide(); + $doc.off("mouseup", mouseUpHandler); + $doc.off("mousemove", mouseMoveHandler); + + // Adjust column size + var stopX = Math.round($resizeHandler.offset().left); + var offset = (startX - stopX); + var oldWidth = $column.outerWidth(); + var newWidth = oldWidth - offset; + + // Set column width + $col.width(newWidth); + newWidth = $column.outerWidth(); + $col.width(newWidth); + view.updateHandlerPosition(); + + if (newWidth != oldWidth) { + if (view.saveModelWidth) { + // Save updated width + columnModel.set("width", newWidth, { silent: true}); + } + + } + + // Trigger event + view.columns.trigger("resize", columnModel, newWidth, oldWidth, offset); + }; + $doc.on("mouseup", mouseUpHandler); + }); + } + }); + + // Position drag handlers + view.updateHandlerPosition(); + + return this; + }, + attachEvents: function() { + var view = this; + view.listenTo(view.columns, "change:resizeAble", view.render); + view.listenTo(view.columns, "change:width", view.updateHandlerPosition); + view.listenTo(view.grid.collection, "backgrid:refresh", view.render); + view.listenTo(view.grid.collection, "backgrid:colgroup:updated", function() { + // Wait for callstack to be cleared + // TODO: see if we can do without this delay function + _.delay(function() { + view.setHeaderElements(); + view.render(); + }, 0); + }); + }, + updateHandlerPosition: function() { + var view = this; + _.each(view.headerElements, function(columnEl, index) { + var $column = $(columnEl); + + // Get handler for current column and update position + view.$el.children().filter("[data-column-index='" + index + "']") + .css("left", $column.position().left + $column.outerWidth()); + }); + }, + setHeaderElements: function() { + var view = this; + var $headerEl = view.grid.header.$el; + var $rows = $headerEl.children("tr"); + view.headerElements = []; + + // Loop rows to find header cells; currently this method does not support header columns with a colspan > 1. + if ($rows.length < 2) { + view.headerElements = $rows.children(); + } + else { + // Get all rows in the header + var rowAmount = $rows.length; + $rows.each(function(index, row) { + // Loop all cells + $(row).children("th").each(function(ind, cell) { + var $cell = $(cell); + if (($cell.attr("colspan") == 1 || typeof $cell.attr("colspan") == "undefined") && + ($cell.attr("rowspan") == rowAmount - index || + (index + 1 === rowAmount && typeof $cell.attr("rowspan") == "undefined"))) { + view.headerElements.push(cell); + } + }); + }); + + // Sort array + view.headerElements.sort(function(lhs, rhs){ + return parseInt($(lhs).offset().left,10) - parseInt($(rhs).offset().left,10); + }); + } + + // Verification + var unrenderableColCnt = view.columns.where({ renderable: false}).length; + if (view.headerElements.length != (view.columns.size() - unrenderableColCnt)) { + throw new RangeError("cannot determine header elements for every column."); + } + } + }); + +})); diff --git a/web/pgadmin/templates/base.html b/web/pgadmin/templates/base.html index 6e77019..e744aab 100755 --- a/web/pgadmin/templates/base.html +++ b/web/pgadmin/templates/base.html @@ -27,6 +27,7 @@ + @@ -73,6 +74,9 @@ "backgrid.filter": { "deps": ['backgrid'] }, + "backgrid.sizeable.columns": { + "deps": ['backgrid'] + }, "bootstrap.switch": { "deps": ['jquery', 'bootstrap'], "exports": 'jQuery.fn.bootstrapSwitch' @@ -122,6 +126,7 @@ "backgrid.select.all": "{{ url_for('static', filename='js/backgrid/' + ('backgrid-select-all' if config.DEBUG else 'backgrid-select-all.min')) }}", "backgrid.paginator": "{{ url_for('static', filename='js/backgrid/' + ('backgrid-paginator' if config.DEBUG else 'backgrid-paginator.min')) }}", "backgrid.filter": "{{ url_for('static', filename='js/backgrid/' + ('backgrid-filter' if config.DEBUG else 'backgrid-filter.min')) }}", + "backgrid.sizeable.columns": "{{ url_for('static', filename='js/backgrid/backgrid-sizeable-columns') }}", "backbone.undo": "{{ url_for('static', filename='js/' + ('backbone.undo' if config.DEBUG else 'backbone.undo.min')) }}", "pgadmin.backgrid": "{{ url_for('static', filename='js/backgrid/backgrid.pgadmin') }}", 'pgadmin.backform': "{{ url_for('static', filename='js/backform.pgadmin') }}", diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js index b6ae5fe..76d1d6a 100644 --- a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js +++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js @@ -8,7 +8,7 @@ define( 'codemirror/addon/fold/foldgutter', 'codemirror/addon/fold/foldcode', 'codemirror/addon/hint/show-hint', 'codemirror/addon/hint/sql-hint', 'codemirror/addon/fold/pgadmin-sqlfoldcode', 'backgrid.paginator', - 'wcdocker', 'pgadmin.file_manager' + 'backgrid.sizeable.columns', 'wcdocker', 'pgadmin.file_manager' ], function( $, _, S, alertify, pgAdmin, Backbone, Backgrid, CodeMirror, pgExplain @@ -400,6 +400,7 @@ define( }); }, + /* This function is responsible to create and render the * new backgrid and paginator. */ @@ -427,8 +428,10 @@ define( filter_array.push(c.name); }); + var columnsColl = new Backgrid.Columns(columns); + var grid = self.grid = new Backgrid.Grid({ - columns: columns, + columns: columnsColl, collection: collection, className: "backgrid table-bordered", row: SqlEditorCustomRow @@ -446,8 +449,9 @@ define( wait: 150 }); + var $data_grid = self.$el.find('#datagrid'); // Render the grid - self.$el.find('#datagrid').append(self.grid.render().$el); + $data_grid.append(self.grid.render().$el); // Render the paginator self.$el.find('#datagrid-paginator').append(paginator.render().el); @@ -455,6 +459,32 @@ define( // Render the client side filter self.$el.find('.pg-prop-btn-group').append(clientSideFilter.render().el); + var sizeAbleCol = new Backgrid.Extension.SizeAbleColumns({ + collection: collection, + columns: columnsColl, + }); + $data_grid.find('thead').before(sizeAbleCol.render().el); + + // Add resize handlers + var sizeHandler = new Backgrid.Extension.SizeAbleColumnsHandlers({ + sizeAbleColumns: sizeAbleCol, + grid: self.grid, + saveModelWidth: true + }); + $data_grid.find('thead').before(sizeHandler.render().el); + + // Listen to resize events + columnsColl.on('resize', function(columnModel, newWidth, oldWidth, offset) { + var $grid_el = $data_grid.find('table'), + tbl_orig_width = $grid_el.width(), + tbl_new_width = tbl_orig_width - offset; + + // Table new width cannot be less than original width + if (tbl_new_width >= tbl_orig_width) { + $($grid_el).css('width', tbl_new_width + 'px'); + } + }); + // Forcefully sorting by the first column. if (columns.length > 1) { collection.setSorting(columns[1].name); @@ -1346,7 +1376,9 @@ define( label: col_label, cell: col_cell, can_edit: self.can_edit, - editable: self.is_editable + editable: self.is_editable, + resizeAble: true, + renderable: true }; columns.push(col);