listbox.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 domhandler_1 = require("../dom/domhandler");
  16. var objectutils_1 = require("../utils/objectutils");
  17. var forms_1 = require("@angular/forms");
  18. var filterutils_1 = require("../utils/filterutils");
  19. exports.LISTBOX_VALUE_ACCESSOR = {
  20. provide: forms_1.NG_VALUE_ACCESSOR,
  21. useExisting: core_1.forwardRef(function () { return Listbox; }),
  22. multi: true
  23. };
  24. var Listbox = /** @class */ (function () {
  25. function Listbox(el, cd) {
  26. this.el = el;
  27. this.cd = cd;
  28. this.checkbox = false;
  29. this.filter = false;
  30. this.filterMode = 'contains';
  31. this.metaKeySelection = true;
  32. this.showToggleAll = true;
  33. this.onChange = new core_1.EventEmitter();
  34. this.onClick = new core_1.EventEmitter();
  35. this.onDblClick = new core_1.EventEmitter();
  36. this.onModelChange = function () { };
  37. this.onModelTouched = function () { };
  38. this.disabledSelectedOptions = [];
  39. }
  40. Object.defineProperty(Listbox.prototype, "options", {
  41. get: function () {
  42. return this._options;
  43. },
  44. set: function (val) {
  45. var opts = this.optionLabel ? objectutils_1.ObjectUtils.generateSelectItems(val, this.optionLabel) : val;
  46. this._options = opts;
  47. },
  48. enumerable: true,
  49. configurable: true
  50. });
  51. Object.defineProperty(Listbox.prototype, "filterValue", {
  52. get: function () {
  53. return this._filterValue;
  54. },
  55. set: function (val) {
  56. this._filterValue = val;
  57. },
  58. enumerable: true,
  59. configurable: true
  60. });
  61. Listbox.prototype.ngAfterContentInit = function () {
  62. var _this = this;
  63. this.templates.forEach(function (item) {
  64. switch (item.getType()) {
  65. case 'item':
  66. _this.itemTemplate = item.template;
  67. break;
  68. default:
  69. _this.itemTemplate = item.template;
  70. break;
  71. }
  72. });
  73. };
  74. Listbox.prototype.writeValue = function (value) {
  75. this.value = value;
  76. this.setDisabledSelectedOptions();
  77. this.cd.markForCheck();
  78. };
  79. Listbox.prototype.registerOnChange = function (fn) {
  80. this.onModelChange = fn;
  81. };
  82. Listbox.prototype.registerOnTouched = function (fn) {
  83. this.onModelTouched = fn;
  84. };
  85. Listbox.prototype.setDisabledState = function (val) {
  86. this.disabled = val;
  87. };
  88. Listbox.prototype.onOptionClick = function (event, option) {
  89. if (this.disabled || option.disabled || this.readonly) {
  90. return;
  91. }
  92. if (this.multiple) {
  93. if (this.checkbox)
  94. this.onOptionClickCheckbox(event, option);
  95. else
  96. this.onOptionClickMultiple(event, option);
  97. }
  98. else {
  99. this.onOptionClickSingle(event, option);
  100. }
  101. this.onClick.emit({
  102. originalEvent: event,
  103. option: option,
  104. value: this.value
  105. });
  106. this.optionTouched = false;
  107. };
  108. Listbox.prototype.onOptionTouchEnd = function (event, option) {
  109. if (this.disabled || option.disabled || this.readonly) {
  110. return;
  111. }
  112. this.optionTouched = true;
  113. };
  114. Listbox.prototype.onOptionDoubleClick = function (event, option) {
  115. if (this.disabled || option.disabled || this.readonly) {
  116. return;
  117. }
  118. this.onDblClick.emit({
  119. originalEvent: event,
  120. option: option,
  121. value: this.value
  122. });
  123. };
  124. Listbox.prototype.onOptionClickSingle = function (event, option) {
  125. var selected = this.isSelected(option);
  126. var valueChanged = false;
  127. var metaSelection = this.optionTouched ? false : this.metaKeySelection;
  128. if (metaSelection) {
  129. var metaKey = (event.metaKey || event.ctrlKey);
  130. if (selected) {
  131. if (metaKey) {
  132. this.value = null;
  133. valueChanged = true;
  134. }
  135. }
  136. else {
  137. this.value = option.value;
  138. valueChanged = true;
  139. }
  140. }
  141. else {
  142. this.value = selected ? null : option.value;
  143. valueChanged = true;
  144. }
  145. if (valueChanged) {
  146. this.onModelChange(this.value);
  147. this.onChange.emit({
  148. originalEvent: event,
  149. value: this.value
  150. });
  151. }
  152. };
  153. Listbox.prototype.onOptionClickMultiple = function (event, option) {
  154. var selected = this.isSelected(option);
  155. var valueChanged = false;
  156. var metaSelection = this.optionTouched ? false : this.metaKeySelection;
  157. if (metaSelection) {
  158. var metaKey = (event.metaKey || event.ctrlKey);
  159. if (selected) {
  160. if (metaKey) {
  161. this.removeOption(option);
  162. }
  163. else {
  164. this.value = [option.value];
  165. }
  166. valueChanged = true;
  167. }
  168. else {
  169. this.value = (metaKey) ? this.value || [] : [];
  170. this.value = this.value.concat([option.value]);
  171. valueChanged = true;
  172. }
  173. }
  174. else {
  175. if (selected) {
  176. this.removeOption(option);
  177. }
  178. else {
  179. this.value = (this.value || []).concat([option.value]);
  180. }
  181. valueChanged = true;
  182. }
  183. if (valueChanged) {
  184. this.onModelChange(this.value);
  185. this.onChange.emit({
  186. originalEvent: event,
  187. value: this.value
  188. });
  189. }
  190. };
  191. Listbox.prototype.onOptionClickCheckbox = function (event, option) {
  192. if (this.disabled || this.readonly) {
  193. return;
  194. }
  195. var selected = this.isSelected(option);
  196. if (selected) {
  197. this.removeOption(option);
  198. }
  199. else {
  200. this.value = this.value ? this.value : [];
  201. this.value = this.value.concat([option.value]);
  202. }
  203. this.onModelChange(this.value);
  204. this.onChange.emit({
  205. originalEvent: event,
  206. value: this.value
  207. });
  208. };
  209. Listbox.prototype.removeOption = function (option) {
  210. var _this = this;
  211. this.value = this.value.filter(function (val) { return !objectutils_1.ObjectUtils.equals(val, option.value, _this.dataKey); });
  212. };
  213. Listbox.prototype.isSelected = function (option) {
  214. var selected = false;
  215. if (this.multiple) {
  216. if (this.value) {
  217. for (var _i = 0, _a = this.value; _i < _a.length; _i++) {
  218. var val = _a[_i];
  219. if (objectutils_1.ObjectUtils.equals(val, option.value, this.dataKey)) {
  220. selected = true;
  221. break;
  222. }
  223. }
  224. }
  225. }
  226. else {
  227. selected = objectutils_1.ObjectUtils.equals(this.value, option.value, this.dataKey);
  228. }
  229. return selected;
  230. };
  231. Object.defineProperty(Listbox.prototype, "allChecked", {
  232. get: function () {
  233. if (this.filterValue) {
  234. return this.allFilteredSelected();
  235. }
  236. else {
  237. var optionCount = this.getEnabledOptionCount();
  238. var disabledSelectedOptionCount = this.disabledSelectedOptions.length;
  239. return this.value && this.options && (this.value.length > 0 && this.value.length == optionCount + disabledSelectedOptionCount);
  240. }
  241. },
  242. enumerable: true,
  243. configurable: true
  244. });
  245. Listbox.prototype.getEnabledOptionCount = function () {
  246. if (this.options) {
  247. var count = 0;
  248. for (var _i = 0, _a = this.options; _i < _a.length; _i++) {
  249. var opt = _a[_i];
  250. if (!opt.disabled) {
  251. count++;
  252. }
  253. }
  254. return count;
  255. }
  256. else {
  257. return 0;
  258. }
  259. };
  260. Listbox.prototype.allFilteredSelected = function () {
  261. var allSelected;
  262. var options = this.filterValue ? this.getFilteredOptions() : this.options;
  263. if (this.value && options && options.length) {
  264. allSelected = true;
  265. for (var _i = 0, _a = this.options; _i < _a.length; _i++) {
  266. var opt = _a[_i];
  267. if (this.isItemVisible(opt)) {
  268. if (!this.isSelected(opt)) {
  269. allSelected = false;
  270. break;
  271. }
  272. }
  273. }
  274. }
  275. return allSelected;
  276. };
  277. Listbox.prototype.onFilter = function (event) {
  278. this._filterValue = event.target.value;
  279. };
  280. Listbox.prototype.toggleAll = function (event) {
  281. if (this.disabled || this.readonly || !this.options || this.options.length === 0) {
  282. return;
  283. }
  284. if (this.allChecked) {
  285. if (this.disabledSelectedOptions && this.disabledSelectedOptions.length > 0) {
  286. var value = [];
  287. value = this.disabledSelectedOptions.slice();
  288. this.value = value;
  289. }
  290. else {
  291. this.value = [];
  292. }
  293. }
  294. else {
  295. if (this.options) {
  296. this.value = [];
  297. if (this.disabledSelectedOptions && this.disabledSelectedOptions.length > 0) {
  298. this.value = this.disabledSelectedOptions.slice();
  299. }
  300. for (var i = 0; i < this.options.length; i++) {
  301. var opt = this.options[i];
  302. if (this.isItemVisible(opt) && !opt.disabled) {
  303. this.value.push(opt.value);
  304. }
  305. }
  306. }
  307. }
  308. this.onModelChange(this.value);
  309. this.onChange.emit({ originalEvent: event, value: this.value });
  310. event.preventDefault();
  311. };
  312. Listbox.prototype.isItemVisible = function (option) {
  313. if (this.filterValue) {
  314. var visible = void 0;
  315. var filterText = objectutils_1.ObjectUtils.removeAccents(this.filterValue).toLowerCase();
  316. if (this.filterMode) {
  317. visible = filterutils_1.FilterUtils[this.filterMode](option.label, this.filterValue);
  318. }
  319. else {
  320. visible = true;
  321. }
  322. return visible;
  323. }
  324. else {
  325. return true;
  326. }
  327. };
  328. Listbox.prototype.onInputFocus = function (event) {
  329. this.focus = true;
  330. };
  331. Listbox.prototype.onInputBlur = function (event) {
  332. this.focus = false;
  333. };
  334. Listbox.prototype.onOptionKeyDown = function (event, option) {
  335. if (this.readonly) {
  336. return;
  337. }
  338. var item = event.currentTarget;
  339. switch (event.which) {
  340. //down
  341. case 40:
  342. var nextItem = this.findNextItem(item);
  343. if (nextItem) {
  344. nextItem.focus();
  345. }
  346. event.preventDefault();
  347. break;
  348. //up
  349. case 38:
  350. var prevItem = this.findPrevItem(item);
  351. if (prevItem) {
  352. prevItem.focus();
  353. }
  354. event.preventDefault();
  355. break;
  356. //enter
  357. case 13:
  358. this.onOptionClick(event, option);
  359. event.preventDefault();
  360. break;
  361. }
  362. };
  363. Listbox.prototype.findNextItem = function (item) {
  364. var nextItem = item.nextElementSibling;
  365. if (nextItem)
  366. return domhandler_1.DomHandler.hasClass(nextItem, 'ui-state-disabled') || domhandler_1.DomHandler.isHidden(nextItem) ? this.findNextItem(nextItem) : nextItem;
  367. else
  368. return null;
  369. };
  370. Listbox.prototype.findPrevItem = function (item) {
  371. var prevItem = item.previousElementSibling;
  372. if (prevItem)
  373. return domhandler_1.DomHandler.hasClass(prevItem, 'ui-state-disabled') || domhandler_1.DomHandler.isHidden(prevItem) ? this.findPrevItem(prevItem) : prevItem;
  374. else
  375. return null;
  376. };
  377. Listbox.prototype.getFilteredOptions = function () {
  378. var filteredOptions = [];
  379. if (this.filterValue) {
  380. for (var i = 0; i < this.options.length; i++) {
  381. var opt = this.options[i];
  382. if (this.isItemVisible(opt) && !opt.disabled) {
  383. filteredOptions.push(opt);
  384. }
  385. }
  386. return filteredOptions;
  387. }
  388. else {
  389. return this.options;
  390. }
  391. };
  392. Listbox.prototype.onHeaderCheckboxFocus = function () {
  393. this.headerCheckboxFocus = true;
  394. };
  395. Listbox.prototype.onHeaderCheckboxBlur = function () {
  396. this.headerCheckboxFocus = false;
  397. };
  398. Listbox.prototype.setDisabledSelectedOptions = function () {
  399. if (this.options) {
  400. this.disabledSelectedOptions = [];
  401. if (this.value) {
  402. for (var _i = 0, _a = this.options; _i < _a.length; _i++) {
  403. var opt = _a[_i];
  404. if (opt.disabled && this.isSelected(opt)) {
  405. this.disabledSelectedOptions.push(opt.value);
  406. }
  407. }
  408. }
  409. }
  410. };
  411. __decorate([
  412. core_1.Input(),
  413. __metadata("design:type", Boolean)
  414. ], Listbox.prototype, "multiple", void 0);
  415. __decorate([
  416. core_1.Input(),
  417. __metadata("design:type", Object)
  418. ], Listbox.prototype, "style", void 0);
  419. __decorate([
  420. core_1.Input(),
  421. __metadata("design:type", String)
  422. ], Listbox.prototype, "styleClass", void 0);
  423. __decorate([
  424. core_1.Input(),
  425. __metadata("design:type", Object)
  426. ], Listbox.prototype, "listStyle", void 0);
  427. __decorate([
  428. core_1.Input(),
  429. __metadata("design:type", Boolean)
  430. ], Listbox.prototype, "readonly", void 0);
  431. __decorate([
  432. core_1.Input(),
  433. __metadata("design:type", Boolean)
  434. ], Listbox.prototype, "disabled", void 0);
  435. __decorate([
  436. core_1.Input(),
  437. __metadata("design:type", Boolean)
  438. ], Listbox.prototype, "checkbox", void 0);
  439. __decorate([
  440. core_1.Input(),
  441. __metadata("design:type", Boolean)
  442. ], Listbox.prototype, "filter", void 0);
  443. __decorate([
  444. core_1.Input(),
  445. __metadata("design:type", String)
  446. ], Listbox.prototype, "filterMode", void 0);
  447. __decorate([
  448. core_1.Input(),
  449. __metadata("design:type", Boolean)
  450. ], Listbox.prototype, "metaKeySelection", void 0);
  451. __decorate([
  452. core_1.Input(),
  453. __metadata("design:type", String)
  454. ], Listbox.prototype, "dataKey", void 0);
  455. __decorate([
  456. core_1.Input(),
  457. __metadata("design:type", Boolean)
  458. ], Listbox.prototype, "showToggleAll", void 0);
  459. __decorate([
  460. core_1.Input(),
  461. __metadata("design:type", String)
  462. ], Listbox.prototype, "optionLabel", void 0);
  463. __decorate([
  464. core_1.Input(),
  465. __metadata("design:type", String)
  466. ], Listbox.prototype, "ariaFilterLabel", void 0);
  467. __decorate([
  468. core_1.Output(),
  469. __metadata("design:type", core_1.EventEmitter)
  470. ], Listbox.prototype, "onChange", void 0);
  471. __decorate([
  472. core_1.Output(),
  473. __metadata("design:type", core_1.EventEmitter)
  474. ], Listbox.prototype, "onClick", void 0);
  475. __decorate([
  476. core_1.Output(),
  477. __metadata("design:type", core_1.EventEmitter)
  478. ], Listbox.prototype, "onDblClick", void 0);
  479. __decorate([
  480. core_1.ViewChild('headerchkbox', { static: false }),
  481. __metadata("design:type", core_1.ElementRef)
  482. ], Listbox.prototype, "headerCheckboxViewChild", void 0);
  483. __decorate([
  484. core_1.ContentChild(shared_1.Header, { static: false }),
  485. __metadata("design:type", Object)
  486. ], Listbox.prototype, "headerFacet", void 0);
  487. __decorate([
  488. core_1.ContentChild(shared_1.Footer, { static: false }),
  489. __metadata("design:type", Object)
  490. ], Listbox.prototype, "footerFacet", void 0);
  491. __decorate([
  492. core_1.ContentChildren(shared_1.PrimeTemplate),
  493. __metadata("design:type", core_1.QueryList)
  494. ], Listbox.prototype, "templates", void 0);
  495. __decorate([
  496. core_1.Input(),
  497. __metadata("design:type", Array),
  498. __metadata("design:paramtypes", [Array])
  499. ], Listbox.prototype, "options", null);
  500. __decorate([
  501. core_1.Input(),
  502. __metadata("design:type", String),
  503. __metadata("design:paramtypes", [String])
  504. ], Listbox.prototype, "filterValue", null);
  505. Listbox = __decorate([
  506. core_1.Component({
  507. selector: 'p-listbox',
  508. template: "\n <div [ngClass]=\"{'ui-listbox ui-inputtext ui-widget ui-widget-content ui-corner-all':true,'ui-state-disabled':disabled,'ui-state-focus':focus}\" [ngStyle]=\"style\" [class]=\"styleClass\">\n <div class=\"ui-helper-hidden-accessible\">\n <input type=\"text\" readonly=\"readonly\" (focus)=\"onInputFocus($event)\" (blur)=\"onInputBlur($event)\">\n </div>\n <div class=\"ui-widget-header ui-corner-all ui-listbox-header ui-helper-clearfix\" *ngIf=\"headerFacet\">\n <ng-content select=\"p-header\"></ng-content>\n </div>\n <div class=\"ui-widget-header ui-corner-all ui-listbox-header ui-helper-clearfix\" *ngIf=\"(checkbox && multiple && showToggleAll) || filter\" [ngClass]=\"{'ui-listbox-header-w-checkbox': checkbox}\">\n <div class=\"ui-chkbox ui-widget\" *ngIf=\"checkbox && multiple && showToggleAll\">\n <div class=\"ui-helper-hidden-accessible\">\n <input type=\"checkbox\" readonly=\"readonly\" [checked]=\"allChecked\" (focus)=\"onHeaderCheckboxFocus()\" (blur)=\"onHeaderCheckboxBlur()\" (keydown.space)=\"toggleAll($event)\">\n </div>\n <div #headerchkbox class=\"ui-chkbox-box ui-widget ui-corner-all ui-state-default\" [ngClass]=\"{'ui-state-active': allChecked, 'ui-state-focus': headerCheckboxFocus}\" (click)=\"toggleAll($event)\">\n <span class=\"ui-chkbox-icon ui-clickable\" [ngClass]=\"{'pi pi-check':allChecked}\"></span>\n </div>\n </div>\n <div class=\"ui-listbox-filter-container\" *ngIf=\"filter\">\n <input type=\"text\" role=\"textbox\" [value]=\"filterValue||''\" (input)=\"onFilter($event)\" class=\"ui-inputtext ui-widget ui-state-default ui-corner-all\" [disabled]=\"disabled\" [attr.aria-label]=\"ariaFilterLabel\">\n <span class=\"ui-listbox-filter-icon pi pi-search\"></span>\n </div>\n </div>\n <div class=\"ui-listbox-list-wrapper\" [ngStyle]=\"listStyle\">\n <ul class=\"ui-listbox-list\">\n <li *ngFor=\"let option of options; let i = index;\" [style.display]=\"isItemVisible(option) ? 'block' : 'none'\" [attr.tabindex]=\"option.disabled ? null : '0'\"\n [ngClass]=\"{'ui-listbox-item ui-corner-all':true,'ui-state-highlight':isSelected(option), 'ui-state-disabled': option.disabled}\" [attr.aria-label]=\"option.label\"\n (click)=\"onOptionClick($event,option)\" (dblclick)=\"onOptionDoubleClick($event,option)\" (touchend)=\"onOptionTouchEnd($event,option)\" (keydown)=\"onOptionKeyDown($event,option)\">\n <div class=\"ui-chkbox ui-widget\" *ngIf=\"checkbox && multiple\">\n <div class=\"ui-chkbox-box ui-widget ui-corner-all ui-state-default\" [ngClass]=\"{'ui-state-active':isSelected(option)}\">\n <span class=\"ui-chkbox-icon ui-clickable\" [ngClass]=\"{'pi pi-check':isSelected(option)}\"></span>\n </div>\n </div>\n <span *ngIf=\"!itemTemplate\">{{option.label}}</span>\n <ng-container *ngTemplateOutlet=\"itemTemplate; context: {$implicit: option, index: i}\"></ng-container>\n </li>\n </ul>\n </div>\n <div class=\"ui-listbox-footer ui-widget-header ui-corner-all\" *ngIf=\"footerFacet\">\n <ng-content select=\"p-footer\"></ng-content>\n </div>\n </div>\n ",
  509. providers: [exports.LISTBOX_VALUE_ACCESSOR]
  510. }),
  511. __metadata("design:paramtypes", [core_1.ElementRef, core_1.ChangeDetectorRef])
  512. ], Listbox);
  513. return Listbox;
  514. }());
  515. exports.Listbox = Listbox;
  516. var ListboxModule = /** @class */ (function () {
  517. function ListboxModule() {
  518. }
  519. ListboxModule = __decorate([
  520. core_1.NgModule({
  521. imports: [common_1.CommonModule, shared_1.SharedModule],
  522. exports: [Listbox, shared_1.SharedModule],
  523. declarations: [Listbox]
  524. })
  525. ], ListboxModule);
  526. return ListboxModule;
  527. }());
  528. exports.ListboxModule = ListboxModule;
  529. //# sourceMappingURL=listbox.js.map