otsdaq_utilities  v2_05_02_indev
resize_columns.js
1 /* Tabulator v4.5.3 (c) Oliver Folkerd */
2 
3 var ResizeColumns = function ResizeColumns(table) {
4  this.table = table; //hold Tabulator object
5  this.startColumn = false;
6  this.startX = false;
7  this.startWidth = false;
8  this.handle = null;
9  this.prevHandle = null;
10 };
11 
12 ResizeColumns.prototype.initializeColumn = function (type, column, element) {
13  var self = this,
14  variableHeight = false,
15  mode = this.table.options.resizableColumns;
16 
17  //set column resize mode
18  if (type === "header") {
19  variableHeight = column.definition.formatter == "textarea" || column.definition.variableHeight;
20  column.modules.resize = { variableHeight: variableHeight };
21  }
22 
23  if (mode === true || mode == type) {
24 
25  var handle = document.createElement('div');
26  handle.className = "tabulator-col-resize-handle";
27 
28  var prevHandle = document.createElement('div');
29  prevHandle.className = "tabulator-col-resize-handle prev";
30 
31  handle.addEventListener("click", function (e) {
32  e.stopPropagation();
33  });
34 
35  var handleDown = function handleDown(e) {
36  var nearestColumn = column.getLastColumn();
37 
38  if (nearestColumn && self._checkResizability(nearestColumn)) {
39  self.startColumn = column;
40  self._mouseDown(e, nearestColumn, handle);
41  }
42  };
43 
44  handle.addEventListener("mousedown", handleDown);
45  handle.addEventListener("touchstart", handleDown, { passive: true });
46 
47  //reszie column on double click
48  handle.addEventListener("dblclick", function (e) {
49  var col = column.getLastColumn();
50 
51  if (col && self._checkResizability(col)) {
52  e.stopPropagation();
53  col.reinitializeWidth(true);
54  }
55  });
56 
57  prevHandle.addEventListener("click", function (e) {
58  e.stopPropagation();
59  });
60 
61  var prevHandleDown = function prevHandleDown(e) {
62  var nearestColumn, colIndex, prevColumn;
63 
64  nearestColumn = column.getFirstColumn();
65 
66  if (nearestColumn) {
67  colIndex = self.table.columnManager.findColumnIndex(nearestColumn);
68  prevColumn = colIndex > 0 ? self.table.columnManager.getColumnByIndex(colIndex - 1) : false;
69 
70  if (prevColumn && self._checkResizability(prevColumn)) {
71  self.startColumn = column;
72  self._mouseDown(e, prevColumn, prevHandle);
73  }
74  }
75  };
76 
77  prevHandle.addEventListener("mousedown", prevHandleDown);
78  prevHandle.addEventListener("touchstart", prevHandleDown, { passive: true });
79 
80  //resize column on double click
81  prevHandle.addEventListener("dblclick", function (e) {
82  var nearestColumn, colIndex, prevColumn;
83 
84  nearestColumn = column.getFirstColumn();
85 
86  if (nearestColumn) {
87  colIndex = self.table.columnManager.findColumnIndex(nearestColumn);
88  prevColumn = colIndex > 0 ? self.table.columnManager.getColumnByIndex(colIndex - 1) : false;
89 
90  if (prevColumn && self._checkResizability(prevColumn)) {
91  e.stopPropagation();
92  prevColumn.reinitializeWidth(true);
93  }
94  }
95  });
96 
97  element.appendChild(handle);
98  element.appendChild(prevHandle);
99  }
100 };
101 
102 ResizeColumns.prototype._checkResizability = function (column) {
103  return typeof column.definition.resizable != "undefined" ? column.definition.resizable : this.table.options.resizableColumns;
104 };
105 
106 ResizeColumns.prototype._mouseDown = function (e, column, handle) {
107  var self = this;
108 
109  self.table.element.classList.add("tabulator-block-select");
110 
111  function mouseMove(e) {
112  // self.table.columnManager.tempScrollBlock();
113 
114  column.setWidth(self.startWidth + ((typeof e.screenX === "undefined" ? e.touches[0].screenX : e.screenX) - self.startX));
115 
116  if (!self.table.browserSlow && column.modules.resize && column.modules.resize.variableHeight) {
117  column.checkCellHeights();
118  }
119  }
120 
121  function mouseUp(e) {
122 
123  //block editor from taking action while resizing is taking place
124  if (self.startColumn.modules.edit) {
125  self.startColumn.modules.edit.blocked = false;
126  }
127 
128  if (self.table.browserSlow && column.modules.resize && column.modules.resize.variableHeight) {
129  column.checkCellHeights();
130  }
131 
132  document.body.removeEventListener("mouseup", mouseUp);
133  document.body.removeEventListener("mousemove", mouseMove);
134 
135  handle.removeEventListener("touchmove", mouseMove);
136  handle.removeEventListener("touchend", mouseUp);
137 
138  self.table.element.classList.remove("tabulator-block-select");
139 
140  if (self.table.options.persistence && self.table.modExists("persistence", true) && self.table.modules.persistence.config.columns) {
141  self.table.modules.persistence.save("columns");
142  }
143 
144  self.table.options.columnResized.call(self.table, column.getComponent());
145  }
146 
147  e.stopPropagation(); //prevent resize from interfereing with movable columns
148 
149  //block editor from taking action while resizing is taking place
150  if (self.startColumn.modules.edit) {
151  self.startColumn.modules.edit.blocked = true;
152  }
153 
154  self.startX = typeof e.screenX === "undefined" ? e.touches[0].screenX : e.screenX;
155  self.startWidth = column.getWidth();
156 
157  document.body.addEventListener("mousemove", mouseMove);
158  document.body.addEventListener("mouseup", mouseUp);
159  handle.addEventListener("touchmove", mouseMove, { passive: true });
160  handle.addEventListener("touchend", mouseUp);
161 };
162 
163 Tabulator.prototype.registerModule("resizeColumns", ResizeColumns);