datalist.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. "use strict";
  2. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  3. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  4. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  5. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  6. return c > 3 && r && Object.defineProperty(target, key, r), r;
  7. };
  8. var __metadata = (this && this.__metadata) || function (k, v) {
  9. if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. var core_1 = require("@angular/core");
  13. var common_1 = require("@angular/common");
  14. var shared_1 = require("../common/shared");
  15. var paginator_1 = require("../paginator/paginator");
  16. var DataList = /** @class */ (function () {
  17. function DataList(el, differs) {
  18. this.el = el;
  19. this.differs = differs;
  20. this.pageLinks = 5;
  21. this.onLazyLoad = new core_1.EventEmitter();
  22. this.paginatorPosition = 'bottom';
  23. this.emptyMessage = 'No records found';
  24. this.alwaysShowPaginator = true;
  25. this.trackBy = function (index, item) { return item; };
  26. this.immutable = true;
  27. this.onPage = new core_1.EventEmitter();
  28. this.first = 0;
  29. this.page = 0;
  30. this.differ = differs.find([]).create(null);
  31. }
  32. DataList.prototype.ngAfterContentInit = function () {
  33. var _this = this;
  34. this.templates.forEach(function (item) {
  35. switch (item.getType()) {
  36. case 'item':
  37. _this.itemTemplate = item.template;
  38. break;
  39. default:
  40. _this.itemTemplate = item.template;
  41. break;
  42. }
  43. });
  44. };
  45. DataList.prototype.ngAfterViewInit = function () {
  46. if (this.lazy) {
  47. this.onLazyLoad.emit({
  48. first: this.first,
  49. rows: this.rows
  50. });
  51. }
  52. };
  53. Object.defineProperty(DataList.prototype, "value", {
  54. get: function () {
  55. return this._value;
  56. },
  57. set: function (val) {
  58. this._value = val;
  59. if (this.immutable) {
  60. this.handleDataChange();
  61. }
  62. },
  63. enumerable: true,
  64. configurable: true
  65. });
  66. DataList.prototype.handleDataChange = function () {
  67. if (this.paginator) {
  68. this.updatePaginator();
  69. }
  70. this.updateDataToRender(this.value);
  71. };
  72. DataList.prototype.ngDoCheck = function () {
  73. if (!this.immutable) {
  74. var changes = this.differ.diff(this.value);
  75. if (changes) {
  76. this.handleDataChange();
  77. }
  78. }
  79. };
  80. DataList.prototype.updatePaginator = function () {
  81. //total records
  82. this.totalRecords = this.lazy ? this.totalRecords : (this.value ? this.value.length : 0);
  83. //first
  84. if (this.totalRecords && this.first >= this.totalRecords) {
  85. var numberOfPages = Math.ceil(this.totalRecords / this.rows);
  86. this.first = Math.max((numberOfPages - 1) * this.rows, 0);
  87. }
  88. };
  89. DataList.prototype.paginate = function (event) {
  90. this.first = event.first;
  91. this.rows = event.rows;
  92. if (this.lazy) {
  93. this.onLazyLoad.emit(this.createLazyLoadMetadata());
  94. }
  95. else {
  96. this.updateDataToRender(this.value);
  97. }
  98. this.onPage.emit({
  99. first: this.first,
  100. rows: this.rows
  101. });
  102. };
  103. DataList.prototype.updateDataToRender = function (datasource) {
  104. if (this.paginator && datasource) {
  105. this.dataToRender = [];
  106. var startIndex = this.lazy ? 0 : this.first;
  107. for (var i = startIndex; i < (startIndex + this.rows); i++) {
  108. if (i >= datasource.length) {
  109. break;
  110. }
  111. this.dataToRender.push(datasource[i]);
  112. }
  113. }
  114. else {
  115. this.dataToRender = datasource;
  116. }
  117. };
  118. DataList.prototype.isEmpty = function () {
  119. return !this.dataToRender || (this.dataToRender.length == 0);
  120. };
  121. DataList.prototype.createLazyLoadMetadata = function () {
  122. return {
  123. first: this.first,
  124. rows: this.rows
  125. };
  126. };
  127. DataList.prototype.getBlockableElement = function () {
  128. return this.el.nativeElement.children[0];
  129. };
  130. __decorate([
  131. core_1.Input(),
  132. __metadata("design:type", Boolean)
  133. ], DataList.prototype, "paginator", void 0);
  134. __decorate([
  135. core_1.Input(),
  136. __metadata("design:type", Number)
  137. ], DataList.prototype, "rows", void 0);
  138. __decorate([
  139. core_1.Input(),
  140. __metadata("design:type", Number)
  141. ], DataList.prototype, "totalRecords", void 0);
  142. __decorate([
  143. core_1.Input(),
  144. __metadata("design:type", Number)
  145. ], DataList.prototype, "pageLinks", void 0);
  146. __decorate([
  147. core_1.Input(),
  148. __metadata("design:type", Array)
  149. ], DataList.prototype, "rowsPerPageOptions", void 0);
  150. __decorate([
  151. core_1.Input(),
  152. __metadata("design:type", Boolean)
  153. ], DataList.prototype, "lazy", void 0);
  154. __decorate([
  155. core_1.Output(),
  156. __metadata("design:type", core_1.EventEmitter)
  157. ], DataList.prototype, "onLazyLoad", void 0);
  158. __decorate([
  159. core_1.Input(),
  160. __metadata("design:type", Object)
  161. ], DataList.prototype, "style", void 0);
  162. __decorate([
  163. core_1.Input(),
  164. __metadata("design:type", String)
  165. ], DataList.prototype, "styleClass", void 0);
  166. __decorate([
  167. core_1.Input(),
  168. __metadata("design:type", String)
  169. ], DataList.prototype, "paginatorPosition", void 0);
  170. __decorate([
  171. core_1.Input(),
  172. __metadata("design:type", String)
  173. ], DataList.prototype, "emptyMessage", void 0);
  174. __decorate([
  175. core_1.Input(),
  176. __metadata("design:type", Boolean)
  177. ], DataList.prototype, "alwaysShowPaginator", void 0);
  178. __decorate([
  179. core_1.Input(),
  180. __metadata("design:type", Function)
  181. ], DataList.prototype, "trackBy", void 0);
  182. __decorate([
  183. core_1.Input(),
  184. __metadata("design:type", Boolean)
  185. ], DataList.prototype, "immutable", void 0);
  186. __decorate([
  187. core_1.Input(),
  188. __metadata("design:type", Boolean)
  189. ], DataList.prototype, "scrollable", void 0);
  190. __decorate([
  191. core_1.Input(),
  192. __metadata("design:type", String)
  193. ], DataList.prototype, "scrollHeight", void 0);
  194. __decorate([
  195. core_1.Input(),
  196. __metadata("design:type", Object)
  197. ], DataList.prototype, "paginatorDropdownAppendTo", void 0);
  198. __decorate([
  199. core_1.Output(),
  200. __metadata("design:type", core_1.EventEmitter)
  201. ], DataList.prototype, "onPage", void 0);
  202. __decorate([
  203. core_1.ContentChild(shared_1.Header, { static: false }),
  204. __metadata("design:type", Object)
  205. ], DataList.prototype, "header", void 0);
  206. __decorate([
  207. core_1.ContentChild(shared_1.Footer, { static: false }),
  208. __metadata("design:type", Object)
  209. ], DataList.prototype, "footer", void 0);
  210. __decorate([
  211. core_1.ContentChildren(shared_1.PrimeTemplate),
  212. __metadata("design:type", core_1.QueryList)
  213. ], DataList.prototype, "templates", void 0);
  214. __decorate([
  215. core_1.Input(),
  216. __metadata("design:type", Array),
  217. __metadata("design:paramtypes", [Array])
  218. ], DataList.prototype, "value", null);
  219. DataList = __decorate([
  220. core_1.Component({
  221. selector: 'p-dataList',
  222. template: "\n <div [ngClass]=\"{'ui-datalist ui-widget': true, 'ui-datalist-scrollable': scrollable}\" [ngStyle]=\"style\" [class]=\"styleClass\">\n <div class=\"ui-datalist-header ui-widget-header ui-corner-top\" *ngIf=\"header\">\n <ng-content select=\"p-header\"></ng-content>\n </div>\n <p-paginator [rows]=\"rows\" [first]=\"first\" [totalRecords]=\"totalRecords\" [pageLinkSize]=\"pageLinks\" [alwaysShow]=\"alwaysShowPaginator\"\n (onPageChange)=\"paginate($event)\" styleClass=\"ui-paginator-top\" [rowsPerPageOptions]=\"rowsPerPageOptions\" *ngIf=\"paginator && (paginatorPosition === 'top' || paginatorPosition =='both')\"\n [dropdownAppendTo]=\"paginatorDropdownAppendTo\"></p-paginator>\n <div class=\"ui-datalist-content ui-widget-content\" [ngStyle]=\"{'max-height': scrollHeight}\">\n <div *ngIf=\"isEmpty()\" class=\"ui-datalist-emptymessage\">{{emptyMessage}}</div>\n <ul class=\"ui-datalist-data\">\n <li *ngFor=\"let item of dataToRender;let i = index;trackBy: trackBy\">\n <ng-container *ngTemplateOutlet=\"itemTemplate; context: {$implicit: item, index: (i + first)}\"></ng-container>\n </li>\n </ul>\n </div>\n <p-paginator [rows]=\"rows\" [first]=\"first\" [totalRecords]=\"totalRecords\" [pageLinkSize]=\"pageLinks\" [alwaysShow]=\"alwaysShowPaginator\"\n (onPageChange)=\"paginate($event)\" styleClass=\"ui-paginator-bottom\" [rowsPerPageOptions]=\"rowsPerPageOptions\" *ngIf=\"paginator && (paginatorPosition === 'bottom' || paginatorPosition =='both')\"\n [dropdownAppendTo]=\"paginatorDropdownAppendTo\"></p-paginator>\n <div class=\"ui-datalist-footer ui-widget-header ui-corner-bottom\" *ngIf=\"footer\">\n <ng-content select=\"p-footer\"></ng-content>\n </div>\n </div>\n "
  223. }),
  224. __metadata("design:paramtypes", [core_1.ElementRef, core_1.IterableDiffers])
  225. ], DataList);
  226. return DataList;
  227. }());
  228. exports.DataList = DataList;
  229. var DataListModule = /** @class */ (function () {
  230. function DataListModule() {
  231. }
  232. DataListModule = __decorate([
  233. core_1.NgModule({
  234. imports: [common_1.CommonModule, paginator_1.PaginatorModule],
  235. exports: [DataList, shared_1.SharedModule],
  236. declarations: [DataList]
  237. })
  238. ], DataListModule);
  239. return DataListModule;
  240. }());
  241. exports.DataListModule = DataListModule;
  242. //# sourceMappingURL=datalist.js.map