-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathgridClassFactory.js
More file actions
220 lines (185 loc) · 8.72 KB
/
Copy pathgridClassFactory.js
File metadata and controls
220 lines (185 loc) · 8.72 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
(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', 'Grid', 'GridColumn', 'GridRow',
function (gridUtil, $q, $compile, $templateCache, uiGridConstants, 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 () {
// Todo handle response error here?
throw new Error("Couldn't fetch/use row template '" + grid.options.rowTemplate + "'");
}).catch(function(err) { $log.error(err); });
}
grid.registerColumnBuilder(service.defaultColumnBuilder);
// Row builder for custom row templates
grid.registerRowBuilder(service.rowTemplateAssigner);
// Reset all rows to visible initially
grid.registerRowsProcessor(function allRowsVisible(rows) {
rows.forEach(function (row) {
row.evaluateRowVisibility( true );
});
return rows;
}, 50);
grid.registerColumnsProcessor(function applyColumnVisibility(columns) {
columns.forEach(function (column) {
column.visible = angular.isDefined(column.colDef.visible) ? column.colDef.visible : true;
});
return columns;
}, 50);
grid.registerRowsProcessor(grid.searchRows, 100);
// 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, 200);
}
else {
grid.registerRowsProcessor(grid.sortByColumn, 200);
}
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 = [];
// Abstracts the standard template processing we do for every template type.
var processTemplate = function( templateType, providedType, defaultTemplate, filterType, tooltipType ) {
if ( !colDef[templateType] ) {
col[providedType] = defaultTemplate;
} else {
col[providedType] = colDef[templateType];
}
var templatePromise = gridUtil.getTemplate(col[providedType])
.then(
function (template) {
if ( angular.isFunction(template) ) { template = template(); }
var tooltipCall = ( tooltipType === 'cellTooltip' )
? 'col.cellTooltip(row,col)'
: 'col.headerTooltip(col)';
if ( tooltipType && col[tooltipType] === false ) {
template = template.replace(uiGridConstants.TOOLTIP, '');
} else if ( tooltipType && col[tooltipType] ) {
template = template.replace(uiGridConstants.TOOLTIP, 'title="{{' + tooltipCall + ' CUSTOM_FILTERS }}"');
}
if ( filterType ) {
template = template.replace(uiGridConstants.CUSTOM_FILTERS, function() {
return col[filterType] ? "|" + col[filterType] : "";
});
}
return gridUtil.postProcessTemplate(template).then(function(template) {
col[templateType] = template;
});
},
function () {
throw new Error("Couldn't fetch/use colDef." + templateType + " '" + colDef[templateType] + "'");
}).catch(function(err) { $log.error(err); });
templateGetPromises.push(templatePromise);
return templatePromise;
};
/**
* @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.
*
*/
col.cellTemplatePromise = processTemplate( 'cellTemplate', 'providedCellTemplate', 'ui-grid/uiGridCell', 'cellFilter', 'cellTooltip' );
/**
* @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
*
*/
col.headerCellTemplatePromise = processTemplate( 'headerCellTemplate', 'providedHeaderCellTemplate', 'ui-grid/uiGridHeaderCell', 'headerCellFilter', 'headerTooltip' );
/**
* @ngdoc property
* @name footerCellTemplate
* @propertyOf ui.grid.class:GridOptions.columnDef
* @description a custom template for the footer for this column. The default
* is ui-grid/uiGridFooterCell
*
*/
col.footerCellTemplatePromise = processTemplate( 'footerCellTemplate', 'providedFooterCellTemplate', 'ui-grid/uiGridFooterCell', 'footerCellFilter' );
/**
* @ngdoc property
* @name filterHeaderTemplate
* @propertyOf ui.grid.class:GridOptions.columnDef
* @description a custom template for the filter input. The default is ui-grid/ui-grid-filter
*
*/
col.filterHeaderTemplatePromise = processTemplate( 'filterHeaderTemplate', 'providedFilterHeaderTemplate', 'ui-grid/ui-grid-filter' );
// Create a promise for the compiled element function
col.compiledElementFnDefer = $q.defer();
return $q.all(templateGetPromises);
},
rowTemplateAssigner: function rowTemplateAssigner(row) {
var grid = this;
// Row has no template assigned to it
if (!row.rowTemplate) {
// Use the default row template from the grid
row.rowTemplate = grid.options.rowTemplate;
// Use the grid's function for fetching the compiled row template function
row.getRowTemplateFn = grid.getRowTemplateFn;
}
// Row has its own template assigned
else {
// Create a promise for the compiled row template function
var perRowTemplateFnPromise = $q.defer();
row.getRowTemplateFn = perRowTemplateFnPromise.promise;
// Get the row template
gridUtil.getTemplate(row.rowTemplate)
.then(function (template) {
// Compile the template
var rowTemplateFn = $compile(template);
// Resolve the compiled template function promise
perRowTemplateFnPromise.resolve(rowTemplateFn);
},
function () {
// Todo handle response error here?
throw new Error("Couldn't fetch/use row template '" + row.rowTemplate + "'");
});
}
return row.getRowTemplateFn;
}
};
// class definitions (moved to separate factories)
return service;
}]);
})();