selectbutton.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 objectutils_1 = require("../utils/objectutils");
  15. var forms_1 = require("@angular/forms");
  16. exports.SELECTBUTTON_VALUE_ACCESSOR = {
  17. provide: forms_1.NG_VALUE_ACCESSOR,
  18. useExisting: core_1.forwardRef(function () { return SelectButton; }),
  19. multi: true
  20. };
  21. var SelectButton = /** @class */ (function () {
  22. function SelectButton(cd) {
  23. this.cd = cd;
  24. this.tabindex = 0;
  25. this.onOptionClick = new core_1.EventEmitter();
  26. this.onChange = new core_1.EventEmitter();
  27. this.onModelChange = function () { };
  28. this.onModelTouched = function () { };
  29. }
  30. Object.defineProperty(SelectButton.prototype, "options", {
  31. get: function () {
  32. return this._options;
  33. },
  34. set: function (val) {
  35. var opts = this.optionLabel ? objectutils_1.ObjectUtils.generateSelectItems(val, this.optionLabel) : val;
  36. this._options = opts;
  37. },
  38. enumerable: true,
  39. configurable: true
  40. });
  41. SelectButton.prototype.writeValue = function (value) {
  42. this.value = value;
  43. this.cd.markForCheck();
  44. };
  45. SelectButton.prototype.registerOnChange = function (fn) {
  46. this.onModelChange = fn;
  47. };
  48. SelectButton.prototype.registerOnTouched = function (fn) {
  49. this.onModelTouched = fn;
  50. };
  51. SelectButton.prototype.setDisabledState = function (val) {
  52. this.disabled = val;
  53. };
  54. SelectButton.prototype.onItemClick = function (event, option, index) {
  55. if (this.disabled || option.disabled) {
  56. return;
  57. }
  58. if (this.multiple) {
  59. var itemIndex_1 = this.findItemIndex(option);
  60. if (itemIndex_1 != -1)
  61. this.value = this.value.filter(function (val, i) { return i != itemIndex_1; });
  62. else
  63. this.value = (this.value || []).concat([option.value]);
  64. }
  65. else {
  66. this.value = option.value;
  67. }
  68. this.onOptionClick.emit({
  69. originalEvent: event,
  70. option: option,
  71. index: index
  72. });
  73. this.onModelChange(this.value);
  74. this.onChange.emit({
  75. originalEvent: event,
  76. value: this.value
  77. });
  78. };
  79. SelectButton.prototype.onFocus = function (event) {
  80. this.focusedItem = event.target;
  81. };
  82. SelectButton.prototype.onBlur = function (event) {
  83. this.focusedItem = null;
  84. this.onModelTouched();
  85. };
  86. SelectButton.prototype.isSelected = function (option) {
  87. if (this.multiple)
  88. return this.findItemIndex(option) != -1;
  89. else
  90. return objectutils_1.ObjectUtils.equals(option.value, this.value, this.dataKey);
  91. };
  92. SelectButton.prototype.findItemIndex = function (option) {
  93. var index = -1;
  94. if (this.value) {
  95. for (var i = 0; i < this.value.length; i++) {
  96. if (this.value[i] == option.value) {
  97. index = i;
  98. break;
  99. }
  100. }
  101. }
  102. return index;
  103. };
  104. __decorate([
  105. core_1.Input(),
  106. __metadata("design:type", Number)
  107. ], SelectButton.prototype, "tabindex", void 0);
  108. __decorate([
  109. core_1.Input(),
  110. __metadata("design:type", Boolean)
  111. ], SelectButton.prototype, "multiple", void 0);
  112. __decorate([
  113. core_1.Input(),
  114. __metadata("design:type", Object)
  115. ], SelectButton.prototype, "style", void 0);
  116. __decorate([
  117. core_1.Input(),
  118. __metadata("design:type", String)
  119. ], SelectButton.prototype, "styleClass", void 0);
  120. __decorate([
  121. core_1.Input(),
  122. __metadata("design:type", Boolean)
  123. ], SelectButton.prototype, "disabled", void 0);
  124. __decorate([
  125. core_1.Input(),
  126. __metadata("design:type", String)
  127. ], SelectButton.prototype, "dataKey", void 0);
  128. __decorate([
  129. core_1.Input(),
  130. __metadata("design:type", String)
  131. ], SelectButton.prototype, "optionLabel", void 0);
  132. __decorate([
  133. core_1.Output(),
  134. __metadata("design:type", core_1.EventEmitter)
  135. ], SelectButton.prototype, "onOptionClick", void 0);
  136. __decorate([
  137. core_1.Output(),
  138. __metadata("design:type", core_1.EventEmitter)
  139. ], SelectButton.prototype, "onChange", void 0);
  140. __decorate([
  141. core_1.ContentChild(core_1.TemplateRef, { static: false }),
  142. __metadata("design:type", Object)
  143. ], SelectButton.prototype, "itemTemplate", void 0);
  144. __decorate([
  145. core_1.Input(),
  146. __metadata("design:type", Array),
  147. __metadata("design:paramtypes", [Array])
  148. ], SelectButton.prototype, "options", null);
  149. SelectButton = __decorate([
  150. core_1.Component({
  151. selector: 'p-selectButton',
  152. template: "\n <div [ngClass]=\"'ui-selectbutton ui-buttonset ui-widget ui-corner-all ui-buttonset-' + (options ? options.length : 0)\" [ngStyle]=\"style\" [class]=\"styleClass\">\n <div *ngFor=\"let option of options; let i = index\" #btn class=\"ui-button ui-widget ui-state-default ui-button-text-only {{option.styleClass}}\"\n [ngClass]=\"{'ui-state-active':isSelected(option), 'ui-state-disabled': disabled || option.disabled, 'ui-state-focus': btn == focusedItem, \n 'ui-button-text-icon-left': (option.icon != null), 'ui-button-icon-only': (option.icon && !option.label)}\" (click)=\"onItemClick($event,option,i)\" (keydown.enter)=\"onItemClick($event,option,i)\"\n [attr.title]=\"option.title\" [attr.aria-label]=\"option.label\" (focus)=\"onFocus($event)\" (blur)=\"onBlur($event)\" [attr.tabindex]=\"tabindex\">\n <ng-container *ngIf=\"!itemTemplate else customcontent\">\n <span [ngClass]=\"['ui-clickable', 'ui-button-icon-left']\" [class]=\"option.icon\" *ngIf=\"option.icon\"></span>\n <span class=\"ui-button-text ui-clickable\">{{option.label||'ui-btn'}}</span>\n </ng-container>\n <ng-template #customcontent>\n <ng-container *ngTemplateOutlet=\"itemTemplate; context: {$implicit: option, index: i}\"></ng-container>\n </ng-template>\n </div>\n </div>\n ",
  153. providers: [exports.SELECTBUTTON_VALUE_ACCESSOR]
  154. }),
  155. __metadata("design:paramtypes", [core_1.ChangeDetectorRef])
  156. ], SelectButton);
  157. return SelectButton;
  158. }());
  159. exports.SelectButton = SelectButton;
  160. var SelectButtonModule = /** @class */ (function () {
  161. function SelectButtonModule() {
  162. }
  163. SelectButtonModule = __decorate([
  164. core_1.NgModule({
  165. imports: [common_1.CommonModule],
  166. exports: [SelectButton],
  167. declarations: [SelectButton]
  168. })
  169. ], SelectButtonModule);
  170. return SelectButtonModule;
  171. }());
  172. exports.SelectButtonModule = SelectButtonModule;
  173. //# sourceMappingURL=selectbutton.js.map