forked from OnekO/colresizable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.ui.colresizable.js
165 lines (149 loc) · 4.14 KB
/
jquery.ui.colresizable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*!
* jQuery UI Colresizable @VERSION
* PSA PEUGEOT CITROEN
*
* Copyright 2013 PSA PEUGEOT CITROEN By Karlos Presumido AKA OnekO
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*
* https://github.com/OnekO/colresizable
*
* Depends:
* jquery
* jquery.ui.core.js
* jquery.ui.mouse.js
* jquery.ui.resizable.js
* jquery.ui.widget.js
*/
$.widget( "psa.colresizable", {
/**
* Options
* @param boolean table_resizable Set if the table is resizable
* @param string class Set the class of the table
* @param string dragger_class Set the class of the draggers
*/
options: {
table_resizable : true,
class : 'psa_grid_colresizable',
dragger_class : 'psa_grid_dragger'
},
/**
* Widget constructor
**/
_create: function() {
/**
* Reference to this object
*/
var owner = this;
/**
* Draggers array
*/
var draggers = [];
this.element.addClass( this.options.class );
var height = parseInt(this.element.children('thead').height()) + parseInt(this.element.children('tbody').height());
/**
* This function will add the dragger to the DOM (document.body)
*/
this.element.find('th:not(.actions)').each(function(){
var dragger = $('<div class="' + owner.options.dragger_class + '"> </div>').css({
// 'background-color' : 'red',
'cursor' : 'e-resize',
'position' : 'absolute',
'z-index' : 999999
});
dragger.draggable({
axis: "x",
drag : owner._dragging,
stop : owner._stop
});
dragger.data('parent', $(this));
dragger.data('owner', owner);
owner.element.data('colresizable', owner);
dragger.appendTo(document.body);
draggers.push(dragger);
});
// Set dragger's position
this._relocateDraggers();
$(this).data('draggers', draggers);
// If the table is resizable, we made it posible
if (this.options.table_resizable)
{
this.element.resizable({
// We need to relocate the draggers when the table is resized
stop: function( event, ui ){
owner._relocateDraggers();
}
});
}
// We need to relocate the draggers when the window is resized (and only the window)
window.onresize = function(event){
if (event.target.tagName == undefined)
owner._relocateDraggers();
};
},
/**
* Will relocate the draggers when a dragger is moved
* @param event
* @param ui
*/
_stop: function(event, ui) {
$(ui.helper).data('owner')._relocateDraggers();
},
/**
* Relocate all draggers on DOM
*/
_relocateDraggers: function(){
var owner = this;
$('div.' + this.options.dragger_class).each(function(){
owner._relocateDragger($(this));
});
},
/**
* Relocate a dragger
* @param dragger DOM Element
*/
_relocateDragger: function(dragger){
var parent = dragger.data('parent');
var left = parent.offset().left + parent.width() + parseInt(parent.css('padding-left'));
var height = parseInt(parent.parent().height()) + parseInt(parent.parent().parent().parent().children('tbody').height());
var top = parent.offset().top;
dragger.css({
'top' : top,
'left' : left,
'height' : height + 'px'
});
dragger.data('position', {top: top, left: left, width: parent.width()});
},
/**
* This function will change the width of the column on dragger's move
* @param event
* @param ui
*/
_dragging: function(event, ui) {
var data = $(ui.helper).data('position');
var parent = $(ui.helper).data('parent');
var width = 0;
if (event.clientX < data.left)
{
width = (parent.width() - (data.left - event.clientX));
}
else
{
width = parent.width() + (event.clientX - data.left);
}
parent.css('width', width);
data.width = width;
data.left = event.clientX;
$(ui.helper).data('position', data);
},
/**
* On destroy
*/
_destroy: function() {
// remove generated elements
this.element
.removeClass(this.options.class);
}
});