forked from Knockout-Contrib/KoGrid
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathko-grid.js
47 lines (47 loc) · 2.21 KB
/
ko-grid.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
ko.bindingHandlers['koGrid'] = (function () {
return {
'init': function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var options = valueAccessor();
var elem = $(element);
options.gridDim = new window.kg.Dimension({ outerHeight: ko.observable(elem.height()), outerWidth: ko.observable(elem.width()) });
var grid = new window.kg.Grid(options);
var gridElem = $(window.kg.defaultGridTemplate());
// if it is a string we can watch for data changes. otherwise you won't be able to update the grid data
options.data.subscribe(function () {
if (grid.$$selectionPhase) {
return;
}
grid.searchProvider.evalFilter();
grid.refreshDomSizes();
});
// if columndefs are observable watch for changes and rebuild columns.
if (ko.isObservable(options.columnDefs)) {
options.columnDefs.subscribe(function (newDefs) {
grid.columns([]);
grid.config.columnDefs = newDefs;
grid.buildColumns();
grid.configureColumnWidths();
});
}
//set the right styling on the container
elem.addClass("koGrid").addClass(grid.gridId.toString());
elem.append(gridElem);
grid.$userViewModel = bindingContext.$data;
ko.applyBindings(grid, gridElem[0]);
//walk the element's graph and the correct properties on the grid
window.kg.domUtilityService.AssignGridContainers(elem, grid);
grid.configureColumnWidths();
grid.refreshDomSizes();
//now use the manager to assign the event handlers
grid.eventProvider = new window.kg.EventProvider(grid);
//initialize plugins.
$.each(grid.config.plugins, function (i, p) {
if (typeof p.onGridInit === 'function') {
p.onGridInit(grid);
}
});
window.kg.domUtilityService.BuildStyles(grid);
return { controlsDescendantBindings: true };
}
};
}());