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