slider.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 domhandler_1 = require("../dom/domhandler");
  15. var forms_1 = require("@angular/forms");
  16. exports.SLIDER_VALUE_ACCESSOR = {
  17. provide: forms_1.NG_VALUE_ACCESSOR,
  18. useExisting: core_1.forwardRef(function () { return Slider; }),
  19. multi: true
  20. };
  21. var Slider = /** @class */ (function () {
  22. function Slider(el, renderer, ngZone, cd) {
  23. this.el = el;
  24. this.renderer = renderer;
  25. this.ngZone = ngZone;
  26. this.cd = cd;
  27. this.min = 0;
  28. this.max = 100;
  29. this.orientation = 'horizontal';
  30. this.tabindex = 0;
  31. this.onChange = new core_1.EventEmitter();
  32. this.onSlideEnd = new core_1.EventEmitter();
  33. this.handleValues = [];
  34. this.onModelChange = function () { };
  35. this.onModelTouched = function () { };
  36. this.handleIndex = 0;
  37. }
  38. Slider.prototype.onMouseDown = function (event, index) {
  39. if (this.disabled) {
  40. return;
  41. }
  42. this.dragging = true;
  43. this.updateDomData();
  44. this.sliderHandleClick = true;
  45. this.handleIndex = index;
  46. this.bindDragListeners();
  47. event.target.focus();
  48. event.preventDefault();
  49. };
  50. Slider.prototype.onTouchStart = function (event, index) {
  51. if (this.disabled) {
  52. return;
  53. }
  54. var touchobj = event.changedTouches[0];
  55. this.startHandleValue = (this.range) ? this.handleValues[index] : this.handleValue;
  56. this.dragging = true;
  57. this.handleIndex = index;
  58. if (this.orientation === 'horizontal') {
  59. this.startx = parseInt(touchobj.clientX, 10);
  60. this.barWidth = this.el.nativeElement.children[0].offsetWidth;
  61. }
  62. else {
  63. this.starty = parseInt(touchobj.clientY, 10);
  64. this.barHeight = this.el.nativeElement.children[0].offsetHeight;
  65. }
  66. event.preventDefault();
  67. };
  68. Slider.prototype.onTouchMove = function (event, index) {
  69. if (this.disabled) {
  70. return;
  71. }
  72. var touchobj = event.changedTouches[0], handleValue = 0;
  73. if (this.orientation === 'horizontal') {
  74. handleValue = Math.floor(((parseInt(touchobj.clientX, 10) - this.startx) * 100) / (this.barWidth)) + this.startHandleValue;
  75. }
  76. else {
  77. handleValue = Math.floor(((this.starty - parseInt(touchobj.clientY, 10)) * 100) / (this.barHeight)) + this.startHandleValue;
  78. }
  79. this.setValueFromHandle(event, handleValue);
  80. event.preventDefault();
  81. };
  82. Slider.prototype.onTouchEnd = function (event, index) {
  83. if (this.disabled) {
  84. return;
  85. }
  86. this.dragging = false;
  87. if (this.range)
  88. this.onSlideEnd.emit({ originalEvent: event, values: this.values });
  89. else
  90. this.onSlideEnd.emit({ originalEvent: event, value: this.value });
  91. event.preventDefault();
  92. };
  93. Slider.prototype.onBarClick = function (event) {
  94. if (this.disabled) {
  95. return;
  96. }
  97. if (!this.sliderHandleClick) {
  98. this.updateDomData();
  99. this.handleChange(event);
  100. }
  101. this.sliderHandleClick = false;
  102. };
  103. Slider.prototype.onHandleKeydown = function (event, handleIndex) {
  104. if (event.which == 38 || event.which == 39) {
  105. this.spin(event, 1, handleIndex);
  106. }
  107. else if (event.which == 37 || event.which == 40) {
  108. this.spin(event, -1, handleIndex);
  109. }
  110. };
  111. Slider.prototype.spin = function (event, dir, handleIndex) {
  112. var step = (this.step || 1) * dir;
  113. if (this.range) {
  114. this.handleIndex = handleIndex;
  115. this.updateValue(this.values[this.handleIndex] + step);
  116. this.updateHandleValue();
  117. }
  118. else {
  119. this.updateValue(this.value + step);
  120. this.updateHandleValue();
  121. }
  122. event.preventDefault();
  123. };
  124. Slider.prototype.handleChange = function (event) {
  125. var handleValue = this.calculateHandleValue(event);
  126. this.setValueFromHandle(event, handleValue);
  127. };
  128. Slider.prototype.bindDragListeners = function () {
  129. var _this = this;
  130. this.ngZone.runOutsideAngular(function () {
  131. if (!_this.dragListener) {
  132. _this.dragListener = _this.renderer.listen('document', 'mousemove', function (event) {
  133. if (_this.dragging) {
  134. _this.ngZone.run(function () {
  135. _this.handleChange(event);
  136. });
  137. }
  138. });
  139. }
  140. if (!_this.mouseupListener) {
  141. _this.mouseupListener = _this.renderer.listen('document', 'mouseup', function (event) {
  142. if (_this.dragging) {
  143. _this.dragging = false;
  144. _this.ngZone.run(function () {
  145. if (_this.range) {
  146. _this.onSlideEnd.emit({ originalEvent: event, values: _this.values });
  147. }
  148. else {
  149. _this.onSlideEnd.emit({ originalEvent: event, value: _this.value });
  150. }
  151. });
  152. }
  153. });
  154. }
  155. });
  156. };
  157. Slider.prototype.unbindDragListeners = function () {
  158. if (this.dragListener) {
  159. this.dragListener();
  160. }
  161. if (this.mouseupListener) {
  162. this.mouseupListener();
  163. }
  164. };
  165. Slider.prototype.setValueFromHandle = function (event, handleValue) {
  166. var newValue = this.getValueFromHandle(handleValue);
  167. if (this.range) {
  168. if (this.step) {
  169. this.handleStepChange(newValue, this.values[this.handleIndex]);
  170. }
  171. else {
  172. this.handleValues[this.handleIndex] = handleValue;
  173. this.updateValue(newValue, event);
  174. }
  175. }
  176. else {
  177. if (this.step) {
  178. this.handleStepChange(newValue, this.value);
  179. }
  180. else {
  181. this.handleValue = handleValue;
  182. this.updateValue(newValue, event);
  183. }
  184. }
  185. };
  186. Slider.prototype.handleStepChange = function (newValue, oldValue) {
  187. var diff = (newValue - oldValue);
  188. var val = oldValue;
  189. if (diff < 0) {
  190. val = oldValue + Math.ceil(newValue / this.step - oldValue / this.step) * this.step;
  191. }
  192. else if (diff > 0) {
  193. val = oldValue + Math.floor(newValue / this.step - oldValue / this.step) * this.step;
  194. }
  195. this.updateValue(val);
  196. this.updateHandleValue();
  197. };
  198. Slider.prototype.writeValue = function (value) {
  199. if (this.range)
  200. this.values = value || [0, 0];
  201. else
  202. this.value = value || 0;
  203. this.updateHandleValue();
  204. this.cd.markForCheck();
  205. };
  206. Slider.prototype.registerOnChange = function (fn) {
  207. this.onModelChange = fn;
  208. };
  209. Slider.prototype.registerOnTouched = function (fn) {
  210. this.onModelTouched = fn;
  211. };
  212. Slider.prototype.setDisabledState = function (val) {
  213. this.disabled = val;
  214. };
  215. Object.defineProperty(Slider.prototype, "rangeStartLeft", {
  216. get: function () {
  217. return this.isVertical() ? 'auto' : this.handleValues[0] + '%';
  218. },
  219. enumerable: true,
  220. configurable: true
  221. });
  222. Object.defineProperty(Slider.prototype, "rangeStartBottom", {
  223. get: function () {
  224. return this.isVertical() ? this.handleValues[0] + '%' : 'auto';
  225. },
  226. enumerable: true,
  227. configurable: true
  228. });
  229. Object.defineProperty(Slider.prototype, "rangeEndLeft", {
  230. get: function () {
  231. return this.isVertical() ? 'auto' : this.handleValues[1] + '%';
  232. },
  233. enumerable: true,
  234. configurable: true
  235. });
  236. Object.defineProperty(Slider.prototype, "rangeEndBottom", {
  237. get: function () {
  238. return this.isVertical() ? this.handleValues[1] + '%' : 'auto';
  239. },
  240. enumerable: true,
  241. configurable: true
  242. });
  243. Slider.prototype.isVertical = function () {
  244. return this.orientation === 'vertical';
  245. };
  246. Slider.prototype.updateDomData = function () {
  247. var rect = this.el.nativeElement.children[0].getBoundingClientRect();
  248. this.initX = rect.left + domhandler_1.DomHandler.getWindowScrollLeft();
  249. this.initY = rect.top + domhandler_1.DomHandler.getWindowScrollTop();
  250. this.barWidth = this.el.nativeElement.children[0].offsetWidth;
  251. this.barHeight = this.el.nativeElement.children[0].offsetHeight;
  252. };
  253. Slider.prototype.calculateHandleValue = function (event) {
  254. if (this.orientation === 'horizontal')
  255. return ((event.pageX - this.initX) * 100) / (this.barWidth);
  256. else
  257. return (((this.initY + this.barHeight) - event.pageY) * 100) / (this.barHeight);
  258. };
  259. Slider.prototype.updateHandleValue = function () {
  260. if (this.range) {
  261. this.handleValues[0] = (this.values[0] < this.min ? 0 : this.values[0] - this.min) * 100 / (this.max - this.min);
  262. this.handleValues[1] = (this.values[1] > this.max ? 100 : this.values[1] - this.min) * 100 / (this.max - this.min);
  263. }
  264. else {
  265. if (this.value < this.min)
  266. this.handleValue = 0;
  267. else if (this.value > this.max)
  268. this.handleValue = 100;
  269. else
  270. this.handleValue = (this.value - this.min) * 100 / (this.max - this.min);
  271. }
  272. };
  273. Slider.prototype.updateValue = function (val, event) {
  274. if (this.range) {
  275. var value = val;
  276. if (this.handleIndex == 0) {
  277. if (value < this.min) {
  278. value = this.min;
  279. this.handleValues[0] = 0;
  280. }
  281. else if (value > this.values[1]) {
  282. value = this.values[1];
  283. this.handleValues[0] = this.handleValues[1];
  284. }
  285. this.sliderHandleStart.nativeElement.focus();
  286. }
  287. else {
  288. if (value > this.max) {
  289. value = this.max;
  290. this.handleValues[1] = 100;
  291. }
  292. else if (value < this.values[0]) {
  293. value = this.values[0];
  294. this.handleValues[1] = this.handleValues[0];
  295. }
  296. this.sliderHandleEnd.nativeElement.focus();
  297. }
  298. this.values[this.handleIndex] = this.getNormalizedValue(value);
  299. this.onModelChange(this.values);
  300. this.onChange.emit({ event: event, values: this.values });
  301. }
  302. else {
  303. if (val < this.min) {
  304. val = this.min;
  305. this.handleValue = 0;
  306. }
  307. else if (val > this.max) {
  308. val = this.max;
  309. this.handleValue = 100;
  310. }
  311. this.value = this.getNormalizedValue(val);
  312. this.onModelChange(this.value);
  313. this.onChange.emit({ event: event, value: this.value });
  314. this.sliderHandle.nativeElement.focus();
  315. }
  316. };
  317. Slider.prototype.getValueFromHandle = function (handleValue) {
  318. return (this.max - this.min) * (handleValue / 100) + this.min;
  319. };
  320. Slider.prototype.getDecimalsCount = function (value) {
  321. if (value && Math.floor(value) !== value)
  322. return value.toString().split(".")[1].length || 0;
  323. return 0;
  324. };
  325. Slider.prototype.getNormalizedValue = function (val) {
  326. var decimalsCount = this.getDecimalsCount(this.step);
  327. if (decimalsCount > 0) {
  328. return +val.toFixed(decimalsCount);
  329. }
  330. else {
  331. return Math.floor(val);
  332. }
  333. };
  334. Slider.prototype.ngOnDestroy = function () {
  335. this.unbindDragListeners();
  336. };
  337. __decorate([
  338. core_1.Input(),
  339. __metadata("design:type", Boolean)
  340. ], Slider.prototype, "animate", void 0);
  341. __decorate([
  342. core_1.Input(),
  343. __metadata("design:type", Boolean)
  344. ], Slider.prototype, "disabled", void 0);
  345. __decorate([
  346. core_1.Input(),
  347. __metadata("design:type", Number)
  348. ], Slider.prototype, "min", void 0);
  349. __decorate([
  350. core_1.Input(),
  351. __metadata("design:type", Number)
  352. ], Slider.prototype, "max", void 0);
  353. __decorate([
  354. core_1.Input(),
  355. __metadata("design:type", String)
  356. ], Slider.prototype, "orientation", void 0);
  357. __decorate([
  358. core_1.Input(),
  359. __metadata("design:type", Number)
  360. ], Slider.prototype, "step", void 0);
  361. __decorate([
  362. core_1.Input(),
  363. __metadata("design:type", Boolean)
  364. ], Slider.prototype, "range", void 0);
  365. __decorate([
  366. core_1.Input(),
  367. __metadata("design:type", Object)
  368. ], Slider.prototype, "style", void 0);
  369. __decorate([
  370. core_1.Input(),
  371. __metadata("design:type", String)
  372. ], Slider.prototype, "styleClass", void 0);
  373. __decorate([
  374. core_1.Input(),
  375. __metadata("design:type", Number)
  376. ], Slider.prototype, "tabindex", void 0);
  377. __decorate([
  378. core_1.Output(),
  379. __metadata("design:type", core_1.EventEmitter)
  380. ], Slider.prototype, "onChange", void 0);
  381. __decorate([
  382. core_1.Output(),
  383. __metadata("design:type", core_1.EventEmitter)
  384. ], Slider.prototype, "onSlideEnd", void 0);
  385. __decorate([
  386. core_1.ViewChild("sliderHandle", { static: false }),
  387. __metadata("design:type", core_1.ElementRef)
  388. ], Slider.prototype, "sliderHandle", void 0);
  389. __decorate([
  390. core_1.ViewChild("sliderHandleStart", { static: false }),
  391. __metadata("design:type", core_1.ElementRef)
  392. ], Slider.prototype, "sliderHandleStart", void 0);
  393. __decorate([
  394. core_1.ViewChild("sliderHandleEnd", { static: false }),
  395. __metadata("design:type", core_1.ElementRef)
  396. ], Slider.prototype, "sliderHandleEnd", void 0);
  397. Slider = __decorate([
  398. core_1.Component({
  399. selector: 'p-slider',
  400. template: "\n <div [ngStyle]=\"style\" [class]=\"styleClass\" [ngClass]=\"{'ui-slider ui-widget ui-widget-content ui-corner-all':true,'ui-state-disabled':disabled,\n 'ui-slider-horizontal':orientation == 'horizontal','ui-slider-vertical':orientation == 'vertical','ui-slider-animate':animate}\"\n (click)=\"onBarClick($event)\">\n <span *ngIf=\"range && orientation == 'horizontal'\" class=\"ui-slider-range ui-widget-header ui-corner-all\" [ngStyle]=\"{'left':handleValues[0] + '%',width: (handleValues[1] - handleValues[0] + '%')}\"></span>\n <span *ngIf=\"range && orientation == 'vertical'\" class=\"ui-slider-range ui-widget-header ui-corner-all\" [ngStyle]=\"{'bottom':handleValues[0] + '%',height: (handleValues[1] - handleValues[0] + '%')}\"></span>\n <span *ngIf=\"!range && orientation=='vertical'\" class=\"ui-slider-range ui-slider-range-min ui-widget-header ui-corner-all\" [ngStyle]=\"{'height': handleValue + '%'}\"></span>\n <span *ngIf=\"!range && orientation=='horizontal'\" class=\"ui-slider-range ui-slider-range-min ui-widget-header ui-corner-all\" [ngStyle]=\"{'width': handleValue + '%'}\"></span>\n <span #sliderHandle *ngIf=\"!range\" [attr.tabindex]=\"tabindex\" (keydown)=\"onHandleKeydown($event)\" class=\"ui-slider-handle ui-state-default ui-corner-all ui-clickable\" (mousedown)=\"onMouseDown($event)\" (touchstart)=\"onTouchStart($event)\" (touchmove)=\"onTouchMove($event)\" (touchend)=\"onTouchEnd($event)\"\n [style.transition]=\"dragging ? 'none': null\" [ngStyle]=\"{'left': orientation == 'horizontal' ? handleValue + '%' : null,'bottom': orientation == 'vertical' ? handleValue + '%' : null}\"></span>\n <span #sliderHandleStart *ngIf=\"range\" [attr.tabindex]=\"tabindex\" (keydown)=\"onHandleKeydown($event,0)\" (mousedown)=\"onMouseDown($event,0)\" (touchstart)=\"onTouchStart($event,0)\" (touchmove)=\"onTouchMove($event,0)\" (touchend)=\"onTouchEnd($event)\" [style.transition]=\"dragging ? 'none': null\" class=\"ui-slider-handle ui-state-default ui-corner-all ui-clickable\" \n [ngStyle]=\"{'left': rangeStartLeft, 'bottom': rangeStartBottom}\" [ngClass]=\"{'ui-slider-handle-active':handleIndex==0}\"></span>\n <span #sliderHandleEnd *ngIf=\"range\" [attr.tabindex]=\"tabindex\" (keydown)=\"onHandleKeydown($event,1)\" (mousedown)=\"onMouseDown($event,1)\" (touchstart)=\"onTouchStart($event,1)\" (touchmove)=\"onTouchMove($event,1)\" (touchend)=\"onTouchEnd($event)\" [style.transition]=\"dragging ? 'none': null\" class=\"ui-slider-handle ui-state-default ui-corner-all ui-clickable\" \n [ngStyle]=\"{'left': rangeEndLeft, 'bottom': rangeEndBottom}\" [ngClass]=\"{'ui-slider-handle-active':handleIndex==1}\"></span>\n </div>\n ",
  401. providers: [exports.SLIDER_VALUE_ACCESSOR]
  402. }),
  403. __metadata("design:paramtypes", [core_1.ElementRef, core_1.Renderer2, core_1.NgZone, core_1.ChangeDetectorRef])
  404. ], Slider);
  405. return Slider;
  406. }());
  407. exports.Slider = Slider;
  408. var SliderModule = /** @class */ (function () {
  409. function SliderModule() {
  410. }
  411. SliderModule = __decorate([
  412. core_1.NgModule({
  413. imports: [common_1.CommonModule],
  414. exports: [Slider],
  415. declarations: [Slider]
  416. })
  417. ], SliderModule);
  418. return SliderModule;
  419. }());
  420. exports.SliderModule = SliderModule;
  421. //# sourceMappingURL=slider.js.map