UNPKG

5.24 kBJavaScriptView Raw
1/**
2 * ngTable: Table + Angular JS
3 *
4 * @author Vitalii Savchuk <esvit666@gmail.com>
5 * @url https://github.com/esvit/ng-table/
6 * @license New BSD License <http://creativecommons.org/licenses/BSD/>
7 */
8import * as ng1 from 'angular';
9/**
10 * @private
11 */
12function isScopeLike(object) {
13 return object != null && ng1.isFunction(object.$new);
14}
15/**
16 * @private
17 * Service to construct a $column definition used by {@link ngTable ngTable} directive
18 */
19export var NgTableColumn = (function () {
20 function NgTableColumn() {
21 }
22 /**
23 * Creates a $column for use within a header template
24 *
25 * @param column the initial definition for $column to build
26 * @param defaultScope the $scope to supply to the $column getter methods when not supplied by caller
27 * @param columns a reference to the $columns array to make available on the context supplied to the
28 * $column getter methods
29 */
30 NgTableColumn.prototype.buildColumn = function (column, defaultScope, columns) {
31 // note: we're not modifying the original column object. This helps to avoid unintended side affects
32 var extendedCol = Object.create(column);
33 var defaults = this.createDefaults();
34 var _loop_1 = function(prop) {
35 if (extendedCol[prop] === undefined) {
36 extendedCol[prop] = defaults[prop];
37 }
38 if (!ng1.isFunction(extendedCol[prop])) {
39 // wrap raw field values with "getter" functions
40 // - this is to ensure consistency with how ngTable.compile builds columns
41 // - note that the original column object is being "proxied"; this is important
42 // as it ensure that any changes to the original object will be returned by the "getter"
43 var getterSetter = function getterSetter() {
44 if (arguments.length === 1 && !isScopeLike(arguments[0])) {
45 getterSetter.assign(null, arguments[0]);
46 }
47 else {
48 return column[prop];
49 }
50 };
51 getterSetter.assign = function ($scope, value) {
52 column[prop] = value;
53 };
54 extendedCol[prop] = getterSetter;
55 }
56 // satisfy the arguments expected by the function returned by parsedAttribute in the ngTable directive
57 var getterFn = extendedCol[prop];
58 extendedCol[prop] = function () {
59 if (arguments.length === 1 && !isScopeLike(arguments[0])) {
60 getterFn.assign(defaultScope, arguments[0]);
61 }
62 else {
63 var scope = arguments[0] || defaultScope;
64 var context = Object.create(scope);
65 ng1.extend(context, {
66 $column: extendedCol,
67 $columns: columns
68 });
69 return getterFn.call(column, context);
70 }
71 };
72 if (getterFn.assign) {
73 extendedCol[prop].assign = getterFn.assign;
74 }
75 else {
76 var wrappedGetterFn_1 = extendedCol[prop];
77 var localValue_1;
78 var getterSetter = function getterSetter() {
79 if (arguments.length === 1 && !isScopeLike(arguments[0])) {
80 getterSetter.assign(null, arguments[0]);
81 }
82 else {
83 return localValue_1 != undefined ? localValue_1 : wrappedGetterFn_1.apply(extendedCol, arguments);
84 }
85 };
86 getterSetter.assign = function ($scope, value) {
87 localValue_1 = value;
88 };
89 extendedCol[prop] = getterSetter;
90 }
91 };
92 for (var prop in defaults) {
93 _loop_1(prop);
94 }
95 return extendedCol;
96 };
97 NgTableColumn.prototype.createDefaults = function () {
98 return {
99 'class': this.createGetterSetter(''),
100 filter: this.createGetterSetter(false),
101 groupable: this.createGetterSetter(false),
102 filterData: ng1.noop,
103 headerTemplateURL: this.createGetterSetter(false),
104 headerTitle: this.createGetterSetter(''),
105 sortable: this.createGetterSetter(false),
106 show: this.createGetterSetter(true),
107 title: this.createGetterSetter(''),
108 titleAlt: this.createGetterSetter('')
109 };
110 };
111 NgTableColumn.prototype.createGetterSetter = function (initialValue) {
112 var value = initialValue;
113 var getterSetter = function getterSetter() {
114 if (arguments.length === 1 && !isScopeLike(arguments[0])) {
115 getterSetter.assign(null, arguments[0]);
116 }
117 else {
118 return value;
119 }
120 };
121 getterSetter.assign = function ($scope, newValue) {
122 value = newValue;
123 };
124 return getterSetter;
125 };
126 NgTableColumn.$inject = [];
127 return NgTableColumn;
128}());
129//# sourceMappingURL=ngTableColumn.js.map
\No newline at end of file