UNPKG

2.63 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 */
8ngTableSelectFilterDs.$inject = [];
9/**
10 * Takes the array returned by $column.filterData and makes it available as `$selectData` on the `$scope`.
11 *
12 * The resulting `$selectData` array will contain an extra item that is suitable to represent the user
13 * "deselecting" an item from a `<select>` tag
14 *
15 * This directive is is focused on providing a datasource to an `ngOptions` directive
16 */
17function ngTableSelectFilterDs() {
18 // note: not using isolated or child scope "by design"
19 // this is to allow this directive to be combined with other directives that do
20 var directive = {
21 restrict: 'A',
22 controller: NgTableSelectFilterDsController
23 };
24 return directive;
25}
26/**
27 * @private
28 */
29export var NgTableSelectFilterDsController = (function () {
30 function NgTableSelectFilterDsController($scope, $parse, $attrs, $q) {
31 var _this = this;
32 this.$scope = $scope;
33 this.$attrs = $attrs;
34 this.$q = $q;
35 this.$column = $parse($attrs.ngTableSelectFilterDs)($scope);
36 $scope.$watch(function () { return _this.$column && _this.$column.data; }, function () { _this.bindDataSource(); });
37 }
38 NgTableSelectFilterDsController.prototype.bindDataSource = function () {
39 var _this = this;
40 this.getSelectListData(this.$column).then(function (data) {
41 if (data && !_this.hasEmptyOption(data)) {
42 data.unshift({ id: '', title: '' });
43 }
44 data = data || [];
45 _this.$scope.$selectData = data;
46 });
47 };
48 NgTableSelectFilterDsController.prototype.hasEmptyOption = function (data) {
49 var isMatch;
50 for (var i = 0; i < data.length; i++) {
51 var item = data[i];
52 if (item && item.id === '') {
53 isMatch = true;
54 break;
55 }
56 }
57 return isMatch;
58 };
59 NgTableSelectFilterDsController.prototype.getSelectListData = function ($column) {
60 var dataInput = $column.data;
61 if (dataInput instanceof Array) {
62 return this.$q.when(dataInput);
63 }
64 else {
65 return this.$q.when(dataInput && dataInput());
66 }
67 };
68 NgTableSelectFilterDsController.$inject = ['$scope', '$parse', '$attrs', '$q'];
69 return NgTableSelectFilterDsController;
70}());
71export { ngTableSelectFilterDs };
72//# sourceMappingURL=ngTableSelectFilterDs.directive.js.map
\No newline at end of file