This repository has been archived by the owner on Sep 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
tubular-column.js
63 lines (55 loc) · 2.67 KB
/
tubular-column.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
(angular => {
'use strict';
angular.module('tubular.models')
/**
* @ngdoc factory
* @name tubularColumn
* @module tubular.models
*
* @description
* The `tubularColumn` factory is the base to generate a column model to use with `tbGrid`.
*/
.factory('tubularColumn', [
'dataTypes',
'sortDirection',
'aggregateFunctions',
'tubular',
function (dataTypes, sortDirection, aggregateFunctions, tubular) {
return function (columnName, options) {
options = options || {};
options.DataType = options.DataType || dataTypes.STRING;
options.Aggregate = options.Aggregate || aggregateFunctions.NONE;
if (!tubular.isValueInObject(options.DataType, dataTypes)) {
throw `Invalid data type: '${options.DataType}' for column '${columnName}'`;
}
if (!tubular.isValueInObject(options.Aggregate, aggregateFunctions)) {
throw `Invalid aggregate function: '${options.Aggregate}' for column '${columnName}'`;
}
const obj = {
Label: options.Label || (columnName || '').replace(/([a-z])([A-Z])/g, '$1 $2'),
Name: columnName,
Sortable: options.Sortable,
SortOrder: parseInt(options.SortOrder) || -1,
SortDirection: function () {
if (angular.isUndefined(options.SortDirection)) {
return sortDirection.NONE;
}
if (options.SortDirection.toLowerCase().startsWith('asc')) {
return sortDirection.ASCENDING;
}
if (options.SortDirection.toLowerCase().startsWith('desc')) {
return sortDirection.DESCENDING;
}
return sortDirection.NONE;
}(),
IsKey: angular.isDefined(options.IsKey) ? options.IsKey : false,
Searchable: angular.isDefined(options.Searchable) ? options.Searchable : false,
Visible: options.Visible === 'false' ? false : true,
Filter: null,
DataType: options.DataType,
Aggregate: options.Aggregate
};
return obj;
};
}]);
})(angular);