forked from andrewchilds/SlickGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
152 lines (131 loc) · 4.83 KB
/
init.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
/**
* See README file for documentation.
*/
var dataGrids = [];
$(function() {
function init(g) {
var defaults = {
// Server-side storage:
// session: {
// loadFrom: '/slickgrid/dummy.json',
// saveTo: '/slickgrid/session/save'
// },
// Client-side storage:
// session: true,
// No session handling:
session: false,
grid: {
defaultMinWidth: 68,
forceFitColumns: (g.columns.length <= 8),
rowHeight: 21,
showTotalsHeader: true,
showTotalsFooter: true,
syncColumnCellResize: true,
autoContainerHeight: false
},
columnPicker: {
fadeSpeed: 150,
showAutoResize: false,
showSyncResize: false
},
view: {
showColumnPresets: true,
hideTotalsOnFilter: true,
updateTotalsOnFilter: false
}
};
// Generate column defaults
for (var i = 0, cols = g.columns.length; i < cols; i++) {
var converted = convertName(g.columns[i]);
g.columns[i].field = g.columns[i].field || converted;
g.columns[i].id = g.columns[i].id || converted;
g.columns[i].sortable = (typeof g.columns[i].sortable == 'boolean') ? g.columns[i].sortable : true;
}
// Merge options and defaults
g.options = $.extend(true, {}, defaults, g.options);
g.options.grid.originalForceFitColumns = g.options.grid.forceFitColumns;
g.container = $(g.container);
var sessionIsActive = g.options.session;
// Init Session
if (sessionIsActive) {
g.Session = new Slick.Session(g.id, g.columns, g.options.session);
g.columns = g.Session.getColumns();
}
// Init DataView, Grid, ColumnPicker
g.View = new Slick.Data.DataView(g.container, g.options.view);
g.Grid = new Slick.Grid(g.container, g.View.rows, g.columns, g.options.grid, g.totals);
g.ColumnPicker = new Slick.Controls.ColumnPicker(g.Grid, g.options.columnPicker);
if (sessionIsActive) {
g.Session.setGrid(g.Grid);
g.Grid.onSetAllColumns = g.Session.saveColumns;
}
g.Grid.onColumnsReordered = function() {
if (sessionIsActive) {
g.Session.saveColumns();
}
if (g.options.view.updateTotalsOnFilter) {
g.View.calculateTotals();
}
};
g.Grid.onSort = function(column, ascending) {
g.View.onSort(column, ascending);
if (sessionIsActive) {
g.Session.saveSort(column, ascending);
}
};
g.View.setGrid(g.Grid);
g.View.setColumnPicker(g.ColumnPicker);
g.View.drawControls();
if (g.data.length) {
g.View.setItems(g.data);
g.View.applyDefaultFilters();
}
var throttle;
var latestTimestamp;
var throttleThreshold = 500;
var resizeTimeout;
// Update autosized column widths on window resize.
$(window).resize(function() {
latestTimestamp = new Date().getTime();
throttle = throttle || latestTimestamp;
clearTimeout(resizeTimeout);
if (throttle > latestTimestamp - throttleThreshold) {
resizeTimeout = setTimeout(g.Grid.resizeGrid, throttleThreshold + 100);
return true;
}
throttle = latestTimestamp;
g.Grid.resizeGrid();
});
setTimeout(g.Grid.resizeGrid, 100);
if (g.data.length && g.options.grid.autoContainerHeight) {
var offset = 0;
$('div.slick-header, div.slick-totals', g.container).each(function() {
offset += $(this).height();
});
var autoHeight = (g.options.grid.rowHeight * g.data.length) + offset;
if (autoHeight < g.container.height()) {
g.container.height(autoHeight);
g.Grid.resizeGrid();
} else {
var options = g.Grid.getOptions();
options.autoContainerHeight = false;
g.Grid.setOptions(options);
}
}
}
for (var i = 0; i < dataGrids.length; i++) {
init(dataGrids[i]);
}
function convertName(column) {
if (!column.name) {
throw new Error('Missing "name" definition in SlickGrid column.');
}
if (column.field) {
return column.field;
}
if (column.id) {
return column.id;
}
return column.name.toLowerCase().replace(/[\s\-]/ig, '_').replace(/[^_a-z0-9]/ig, '');
}
});