|
| 1 | +(function ($) { |
| 2 | + function SlickColumnPicker(columns, grid, options) { |
| 3 | + var $menu; |
| 4 | + var columnCheckboxes; |
| 5 | + |
| 6 | + var defaults = { |
| 7 | + fadeSpeed:250 |
| 8 | + }; |
| 9 | + |
| 10 | + function init() { |
| 11 | + grid.onHeaderContextMenu.subscribe(handleHeaderContextMenu); |
| 12 | + grid.onColumnsReordered.subscribe(updateColumnOrder); |
| 13 | + options = $.extend({}, defaults, options); |
| 14 | + |
| 15 | + $menu = $("<span class='slick-columnpicker' style='display:none;position:absolute;z-index:20;' />").appendTo(document.body); |
| 16 | + |
| 17 | + $menu.bind("mouseleave", function (e) { |
| 18 | + $(this).fadeOut(options.fadeSpeed) |
| 19 | + }); |
| 20 | + $menu.bind("click", updateColumn); |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + function destroy() { |
| 25 | + grid.onHeaderContextMenu.unsubscribe(handleHeaderContextMenu); |
| 26 | + grid.onColumnsReordered.unsubscribe(updateColumnOrder); |
| 27 | + $menu.remove(); |
| 28 | + } |
| 29 | + |
| 30 | + function handleHeaderContextMenu(e, args) { |
| 31 | + e.preventDefault(); |
| 32 | + $menu.empty(); |
| 33 | + updateColumnOrder(); |
| 34 | + columnCheckboxes = []; |
| 35 | + |
| 36 | + var $li, $input; |
| 37 | + for (var i = 0; i < columns.length; i++) { |
| 38 | + $li = $("<li />").appendTo($menu); |
| 39 | + $input = $("<input type='checkbox' />").data("column-id", columns[i].id); |
| 40 | + columnCheckboxes.push($input); |
| 41 | + |
| 42 | + if (grid.getColumnIndex(columns[i].id) != null) { |
| 43 | + $input.attr("checked", "checked"); |
| 44 | + } |
| 45 | + |
| 46 | + $("<label />") |
| 47 | + .text(columns[i].name) |
| 48 | + .prepend($input) |
| 49 | + .appendTo($li); |
| 50 | + } |
| 51 | + |
| 52 | + $("<hr/>").appendTo($menu); |
| 53 | + $li = $("<li />").appendTo($menu); |
| 54 | + $input = $("<input type='checkbox' />").data("option", "autoresize"); |
| 55 | + $("<label />") |
| 56 | + .text("Force fit columns") |
| 57 | + .prepend($input) |
| 58 | + .appendTo($li); |
| 59 | + if (grid.getOptions().forceFitColumns) { |
| 60 | + $input.attr("checked", "checked"); |
| 61 | + } |
| 62 | + |
| 63 | + $li = $("<li />").appendTo($menu); |
| 64 | + $input = $("<input type='checkbox' />").data("option", "syncresize"); |
| 65 | + $("<label />") |
| 66 | + .text("Synchronous resize") |
| 67 | + .prepend($input) |
| 68 | + .appendTo($li); |
| 69 | + if (grid.getOptions().syncColumnCellResize) { |
| 70 | + $input.attr("checked", "checked"); |
| 71 | + } |
| 72 | + |
| 73 | + $menu |
| 74 | + .css("top", e.pageY - 10) |
| 75 | + .css("left", e.pageX - 10) |
| 76 | + .fadeIn(options.fadeSpeed); |
| 77 | + } |
| 78 | + |
| 79 | + function updateColumnOrder() { |
| 80 | + // Because columns can be reordered, we have to update the `columns` |
| 81 | + // to reflect the new order, however we can't just take `grid.getColumns()`, |
| 82 | + // as it does not include columns currently hidden by the picker. |
| 83 | + // We create a new `columns` structure by leaving currently-hidden |
| 84 | + // columns in their original ordinal position and interleaving the results |
| 85 | + // of the current column sort. |
| 86 | + var current = grid.getColumns().slice(0); |
| 87 | + var ordered = new Array(columns.length); |
| 88 | + for (var i = 0; i < ordered.length; i++) { |
| 89 | + if ( grid.getColumnIndex(columns[i].id) === undefined ) { |
| 90 | + // If the column doesn't return a value from getColumnIndex, |
| 91 | + // it is hidden. Leave it in this position. |
| 92 | + ordered[i] = columns[i]; |
| 93 | + } else { |
| 94 | + // Otherwise, grab the next visible column. |
| 95 | + ordered[i] = current.shift(); |
| 96 | + } |
| 97 | + } |
| 98 | + columns = ordered; |
| 99 | + } |
| 100 | + |
| 101 | + function updateColumn(e) { |
| 102 | + if ($(e.target).data("option") == "autoresize") { |
| 103 | + if (e.target.checked) { |
| 104 | + grid.setOptions({forceFitColumns:true}); |
| 105 | + grid.autosizeColumns(); |
| 106 | + } else { |
| 107 | + grid.setOptions({forceFitColumns:false}); |
| 108 | + } |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if ($(e.target).data("option") == "syncresize") { |
| 113 | + if (e.target.checked) { |
| 114 | + grid.setOptions({syncColumnCellResize:true}); |
| 115 | + } else { |
| 116 | + grid.setOptions({syncColumnCellResize:false}); |
| 117 | + } |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + if ($(e.target).is(":checkbox")) { |
| 122 | + var visibleColumns = []; |
| 123 | + $.each(columnCheckboxes, function (i, e) { |
| 124 | + if ($(this).is(":checked")) { |
| 125 | + visibleColumns.push(columns[i]); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + if (!visibleColumns.length) { |
| 130 | + $(e.target).attr("checked", "checked"); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + grid.setColumns(visibleColumns); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + function getAllColumns() { |
| 139 | + return columns; |
| 140 | + } |
| 141 | + |
| 142 | + init(); |
| 143 | + |
| 144 | + return { |
| 145 | + "getAllColumns": getAllColumns, |
| 146 | + "destroy": destroy |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + // Slick.Controls.ColumnPicker |
| 151 | + $.extend(true, window, { Slick:{ Controls:{ ColumnPicker:SlickColumnPicker }}}); |
| 152 | +})(jQuery); |
0 commit comments