-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
gridClassFactory.js
164 lines (137 loc) · 5.84 KB
/
gridClassFactory.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
(function () {
'use strict';
/**
* @ngdoc object
* @name ui.grid.service:gridClassFactory
*
* @description factory to return dom specific instances of a grid
*
*/
angular.module('ui.grid').service('gridClassFactory', ['gridUtil', '$q', '$compile', '$templateCache', 'uiGridConstants', '$log', 'Grid', 'GridColumn', 'GridRow',
function (gridUtil, $q, $compile, $templateCache, uiGridConstants, $log, Grid, GridColumn, GridRow) {
var service = {
/**
* @ngdoc method
* @name createGrid
* @methodOf ui.grid.service:gridClassFactory
* @description Creates a new grid instance. Each instance will have a unique id
* @param {object} options An object map of options to pass into the created grid instance.
* @returns {Grid} grid
*/
createGrid : function(options) {
options = (typeof(options) !== 'undefined') ? options: {};
options.id = gridUtil.newId();
var grid = new Grid(options);
// NOTE/TODO: rowTemplate should always be defined...
if (grid.options.rowTemplate) {
var rowTemplateFnPromise = $q.defer();
grid.getRowTemplateFn = rowTemplateFnPromise.promise;
gridUtil.getTemplate(grid.options.rowTemplate)
.then(
function (template) {
var rowTemplateFn = $compile(template);
rowTemplateFnPromise.resolve(rowTemplateFn);
},
function (res) {
// Todo handle response error here?
throw new Error("Couldn't fetch/use row template '" + grid.options.rowTemplate + "'");
});
}
grid.registerColumnBuilder(service.defaultColumnBuilder);
// Reset all rows to visible initially
grid.registerRowsProcessor(function allRowsVisible(rows) {
rows.forEach(function (row) {
row.visible = !row.forceInvisible;
});
return rows;
});
grid.registerColumnsProcessor(function allColumnsVisible(columns) {
columns.forEach(function (column) {
column.visible = true;
});
return columns;
});
grid.registerColumnsProcessor(function(renderableColumns) {
renderableColumns.forEach(function (column) {
if (column.colDef.visible === false) {
column.visible = false;
}
});
return renderableColumns;
});
if (grid.options.enableFiltering) {
grid.registerRowsProcessor(grid.searchRows);
}
// Register the default row processor, it sorts rows by selected columns
if (grid.options.externalSort && angular.isFunction(grid.options.externalSort)) {
grid.registerRowsProcessor(grid.options.externalSort);
}
else {
grid.registerRowsProcessor(grid.sortByColumn);
}
return grid;
},
/**
* @ngdoc function
* @name defaultColumnBuilder
* @methodOf ui.grid.service:gridClassFactory
* @description Processes designTime column definitions and applies them to col for the
* core grid features
* @param {object} colDef reference to column definition
* @param {GridColumn} col reference to gridCol
* @param {object} gridOptions reference to grid options
*/
defaultColumnBuilder: function (colDef, col, gridOptions) {
var templateGetPromises = [];
/**
* @ngdoc property
* @name headerCellTemplate
* @propertyOf ui.grid.class:GridOptions.columnDef
* @description a custom template for the header for this column. The default
* is ui-grid/uiGridHeaderCell
*
*/
if (!colDef.headerCellTemplate) {
colDef.headerCellTemplate = 'ui-grid/uiGridHeaderCell';
}
/**
* @ngdoc property
* @name cellTemplate
* @propertyOf ui.grid.class:GridOptions.columnDef
* @description a custom template for each cell in this column. The default
* is ui-grid/uiGridCell. If you are using the cellNav feature, this template
* must contain a div that can receive focus.
*
*/
if (!colDef.cellTemplate) {
colDef.cellTemplate = 'ui-grid/uiGridCell';
col.cellTemplatePromise = gridUtil.getTemplate(colDef.cellTemplate);
}
col.cellTemplatePromise = gridUtil.getTemplate(colDef.cellTemplate);
templateGetPromises.push(col.cellTemplatePromise
.then(
function (template) {
col.cellTemplate = template.replace(uiGridConstants.CUSTOM_FILTERS, col.cellFilter ? "|" + col.cellFilter : "");
},
function (res) {
throw new Error("Couldn't fetch/use colDef.cellTemplate '" + colDef.cellTemplate + "'");
})
);
templateGetPromises.push(gridUtil.getTemplate(colDef.headerCellTemplate)
.then(
function (template) {
col.headerCellTemplate = template.replace(uiGridConstants.CUSTOM_FILTERS, col.headerCellFilter ? "|" + col.headerCellFilter : "");
},
function (res) {
throw new Error("Couldn't fetch/use colDef.headerCellTemplate '" + colDef.headerCellTemplate + "'");
})
);
// Create a promise for the compiled element function
col.compiledElementFnDefer = $q.defer();
return $q.all(templateGetPromises);
}
};
//class definitions (moved to separate factories)
return service;
}]);
})();