angular2-draggable.js 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. import { __values, __assign } from 'tslib';
  2. import { fromEvent } from 'rxjs';
  3. import { Directive, ElementRef, Renderer2, Input, Output, HostListener, EventEmitter, NgModule } from '@angular/core';
  4. /**
  5. * @fileoverview added by tsickle
  6. * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  7. */
  8. var Position = /** @class */ (function () {
  9. function Position(x, y) {
  10. this.x = x;
  11. this.y = y;
  12. }
  13. /**
  14. * @param {?} e
  15. * @param {?=} el
  16. * @return {?}
  17. */
  18. Position.fromEvent = /**
  19. * @param {?} e
  20. * @param {?=} el
  21. * @return {?}
  22. */
  23. function (e, el) {
  24. if (el === void 0) { el = null; }
  25. /**
  26. * Fix issue: Resize doesn't work on Windows10 IE11 (and on some windows 7 IE11)
  27. * https://github.com/xieziyu/angular2-draggable/issues/164
  28. * e instanceof MouseEvent check returns false on IE11
  29. */
  30. if (this.isMouseEvent(e)) {
  31. return new Position(e.clientX, e.clientY);
  32. }
  33. else {
  34. if (el === null || e.changedTouches.length === 1) {
  35. return new Position(e.changedTouches[0].clientX, e.changedTouches[0].clientY);
  36. }
  37. /**
  38. * Fix issue: Multiple phone draggables at the same time
  39. * https://github.com/xieziyu/angular2-draggable/issues/128
  40. */
  41. for (var i = 0; i < e.changedTouches.length; i++) {
  42. if (e.changedTouches[i].target === el) {
  43. return new Position(e.changedTouches[i].clientX, e.changedTouches[i].clientY);
  44. }
  45. }
  46. }
  47. };
  48. /**
  49. * @param {?} e
  50. * @return {?}
  51. */
  52. Position.isMouseEvent = /**
  53. * @param {?} e
  54. * @return {?}
  55. */
  56. function (e) {
  57. return Object.prototype.toString.apply(e).indexOf('MouseEvent') === 8;
  58. };
  59. /**
  60. * @param {?} obj
  61. * @return {?}
  62. */
  63. Position.isIPosition = /**
  64. * @param {?} obj
  65. * @return {?}
  66. */
  67. function (obj) {
  68. return !!obj && ('x' in obj) && ('y' in obj);
  69. };
  70. /**
  71. * @param {?} el
  72. * @return {?}
  73. */
  74. Position.getCurrent = /**
  75. * @param {?} el
  76. * @return {?}
  77. */
  78. function (el) {
  79. /** @type {?} */
  80. var pos = new Position(0, 0);
  81. if (window) {
  82. /** @type {?} */
  83. var computed = window.getComputedStyle(el);
  84. if (computed) {
  85. /** @type {?} */
  86. var x = parseInt(computed.getPropertyValue('left'), 10);
  87. /** @type {?} */
  88. var y = parseInt(computed.getPropertyValue('top'), 10);
  89. pos.x = isNaN(x) ? 0 : x;
  90. pos.y = isNaN(y) ? 0 : y;
  91. }
  92. return pos;
  93. }
  94. else {
  95. console.error('Not Supported!');
  96. return null;
  97. }
  98. };
  99. /**
  100. * @param {?} p
  101. * @return {?}
  102. */
  103. Position.copy = /**
  104. * @param {?} p
  105. * @return {?}
  106. */
  107. function (p) {
  108. return new Position(0, 0).set(p);
  109. };
  110. Object.defineProperty(Position.prototype, "value", {
  111. get: /**
  112. * @return {?}
  113. */
  114. function () {
  115. return { x: this.x, y: this.y };
  116. },
  117. enumerable: true,
  118. configurable: true
  119. });
  120. /**
  121. * @template THIS
  122. * @this {THIS}
  123. * @param {?} p
  124. * @return {THIS}
  125. */
  126. Position.prototype.add = /**
  127. * @template THIS
  128. * @this {THIS}
  129. * @param {?} p
  130. * @return {THIS}
  131. */
  132. function (p) {
  133. (/** @type {?} */ (this)).x += p.x;
  134. (/** @type {?} */ (this)).y += p.y;
  135. return (/** @type {?} */ (this));
  136. };
  137. /**
  138. * @template THIS
  139. * @this {THIS}
  140. * @param {?} p
  141. * @return {THIS}
  142. */
  143. Position.prototype.subtract = /**
  144. * @template THIS
  145. * @this {THIS}
  146. * @param {?} p
  147. * @return {THIS}
  148. */
  149. function (p) {
  150. (/** @type {?} */ (this)).x -= p.x;
  151. (/** @type {?} */ (this)).y -= p.y;
  152. return (/** @type {?} */ (this));
  153. };
  154. /**
  155. * @param {?} n
  156. * @return {?}
  157. */
  158. Position.prototype.multiply = /**
  159. * @param {?} n
  160. * @return {?}
  161. */
  162. function (n) {
  163. this.x *= n;
  164. this.y *= n;
  165. };
  166. /**
  167. * @param {?} n
  168. * @return {?}
  169. */
  170. Position.prototype.divide = /**
  171. * @param {?} n
  172. * @return {?}
  173. */
  174. function (n) {
  175. this.x /= n;
  176. this.y /= n;
  177. };
  178. /**
  179. * @template THIS
  180. * @this {THIS}
  181. * @return {THIS}
  182. */
  183. Position.prototype.reset = /**
  184. * @template THIS
  185. * @this {THIS}
  186. * @return {THIS}
  187. */
  188. function () {
  189. (/** @type {?} */ (this)).x = 0;
  190. (/** @type {?} */ (this)).y = 0;
  191. return (/** @type {?} */ (this));
  192. };
  193. /**
  194. * @template THIS
  195. * @this {THIS}
  196. * @param {?} p
  197. * @return {THIS}
  198. */
  199. Position.prototype.set = /**
  200. * @template THIS
  201. * @this {THIS}
  202. * @param {?} p
  203. * @return {THIS}
  204. */
  205. function (p) {
  206. (/** @type {?} */ (this)).x = p.x;
  207. (/** @type {?} */ (this)).y = p.y;
  208. return (/** @type {?} */ (this));
  209. };
  210. return Position;
  211. }());
  212. /**
  213. * @fileoverview added by tsickle
  214. * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  215. */
  216. var HelperBlock = /** @class */ (function () {
  217. function HelperBlock(parent, renderer) {
  218. this.parent = parent;
  219. this.renderer = renderer;
  220. this._added = false;
  221. // generate helper div
  222. /** @type {?} */
  223. var helper = renderer.createElement('div');
  224. renderer.setStyle(helper, 'position', 'absolute');
  225. renderer.setStyle(helper, 'width', '100%');
  226. renderer.setStyle(helper, 'height', '100%');
  227. renderer.setStyle(helper, 'background-color', 'transparent');
  228. renderer.setStyle(helper, 'top', '0');
  229. renderer.setStyle(helper, 'left', '0');
  230. // done
  231. this._helper = helper;
  232. }
  233. /**
  234. * @return {?}
  235. */
  236. HelperBlock.prototype.add = /**
  237. * @return {?}
  238. */
  239. function () {
  240. // append div to parent
  241. if (this.parent && !this._added) {
  242. this.parent.appendChild(this._helper);
  243. this._added = true;
  244. }
  245. };
  246. /**
  247. * @return {?}
  248. */
  249. HelperBlock.prototype.remove = /**
  250. * @return {?}
  251. */
  252. function () {
  253. if (this.parent && this._added) {
  254. this.parent.removeChild(this._helper);
  255. this._added = false;
  256. }
  257. };
  258. /**
  259. * @return {?}
  260. */
  261. HelperBlock.prototype.dispose = /**
  262. * @return {?}
  263. */
  264. function () {
  265. this._helper = null;
  266. this._added = false;
  267. };
  268. Object.defineProperty(HelperBlock.prototype, "el", {
  269. get: /**
  270. * @return {?}
  271. */
  272. function () {
  273. return this._helper;
  274. },
  275. enumerable: true,
  276. configurable: true
  277. });
  278. return HelperBlock;
  279. }());
  280. /**
  281. * @fileoverview added by tsickle
  282. * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  283. */
  284. var AngularDraggableDirective = /** @class */ (function () {
  285. function AngularDraggableDirective(el, renderer) {
  286. this.el = el;
  287. this.renderer = renderer;
  288. this.allowDrag = true;
  289. this.moving = false;
  290. this.orignal = null;
  291. this.oldTrans = new Position(0, 0);
  292. this.tempTrans = new Position(0, 0);
  293. this.currTrans = new Position(0, 0);
  294. this.oldZIndex = '';
  295. this._zIndex = '';
  296. this.needTransform = false;
  297. this.draggingSub = null;
  298. /**
  299. * Bugfix: iFrames, and context unrelated elements block all events, and are unusable
  300. * https://github.com/xieziyu/angular2-draggable/issues/84
  301. */
  302. this._helperBlock = null;
  303. this.started = new EventEmitter();
  304. this.stopped = new EventEmitter();
  305. this.edge = new EventEmitter();
  306. /**
  307. * List of allowed out of bounds edges *
  308. */
  309. this.outOfBounds = {
  310. top: false,
  311. right: false,
  312. bottom: false,
  313. left: false
  314. };
  315. /**
  316. * Round the position to nearest grid
  317. */
  318. this.gridSize = 1;
  319. /**
  320. * Whether to limit the element stay in the bounds
  321. */
  322. this.inBounds = false;
  323. /**
  324. * Whether the element should use it's previous drag position on a new drag event.
  325. */
  326. this.trackPosition = true;
  327. /**
  328. * Input css scale transform of element so translations are correct
  329. */
  330. this.scale = 1;
  331. /**
  332. * Whether to prevent default event
  333. */
  334. this.preventDefaultEvent = false;
  335. /**
  336. * Set initial position by offsets
  337. */
  338. this.position = { x: 0, y: 0 };
  339. /**
  340. * Lock axis: 'x' or 'y'
  341. */
  342. this.lockAxis = null;
  343. /**
  344. * Emit position offsets when moving
  345. */
  346. this.movingOffset = new EventEmitter();
  347. /**
  348. * Emit position offsets when put back
  349. */
  350. this.endOffset = new EventEmitter();
  351. this._helperBlock = new HelperBlock(el.nativeElement, renderer);
  352. }
  353. Object.defineProperty(AngularDraggableDirective.prototype, "zIndex", {
  354. /** Set z-index when not dragging */
  355. set: /**
  356. * Set z-index when not dragging
  357. * @param {?} setting
  358. * @return {?}
  359. */
  360. function (setting) {
  361. this.renderer.setStyle(this.el.nativeElement, 'z-index', setting);
  362. this._zIndex = setting;
  363. },
  364. enumerable: true,
  365. configurable: true
  366. });
  367. Object.defineProperty(AngularDraggableDirective.prototype, "ngDraggable", {
  368. set: /**
  369. * @param {?} setting
  370. * @return {?}
  371. */
  372. function (setting) {
  373. if (setting !== undefined && setting !== null && setting !== '') {
  374. this.allowDrag = !!setting;
  375. /** @type {?} */
  376. var element = this.getDragEl();
  377. if (this.allowDrag) {
  378. this.renderer.addClass(element, 'ng-draggable');
  379. }
  380. else {
  381. this.putBack();
  382. this.renderer.removeClass(element, 'ng-draggable');
  383. }
  384. }
  385. },
  386. enumerable: true,
  387. configurable: true
  388. });
  389. /**
  390. * @return {?}
  391. */
  392. AngularDraggableDirective.prototype.ngOnInit = /**
  393. * @return {?}
  394. */
  395. function () {
  396. if (this.allowDrag) {
  397. /** @type {?} */
  398. var element = this.getDragEl();
  399. this.renderer.addClass(element, 'ng-draggable');
  400. }
  401. this.resetPosition();
  402. };
  403. /**
  404. * @return {?}
  405. */
  406. AngularDraggableDirective.prototype.ngOnDestroy = /**
  407. * @return {?}
  408. */
  409. function () {
  410. this.bounds = null;
  411. this.handle = null;
  412. this.orignal = null;
  413. this.oldTrans = null;
  414. this.tempTrans = null;
  415. this.currTrans = null;
  416. this._helperBlock.dispose();
  417. this._helperBlock = null;
  418. if (this.draggingSub) {
  419. this.draggingSub.unsubscribe();
  420. }
  421. };
  422. /**
  423. * @param {?} changes
  424. * @return {?}
  425. */
  426. AngularDraggableDirective.prototype.ngOnChanges = /**
  427. * @param {?} changes
  428. * @return {?}
  429. */
  430. function (changes) {
  431. if (changes['position'] && !changes['position'].isFirstChange()) {
  432. /** @type {?} */
  433. var p = changes['position'].currentValue;
  434. if (!this.moving) {
  435. if (Position.isIPosition(p)) {
  436. this.oldTrans.set(p);
  437. }
  438. else {
  439. this.oldTrans.reset();
  440. }
  441. this.transform();
  442. }
  443. else {
  444. this.needTransform = true;
  445. }
  446. }
  447. };
  448. /**
  449. * @return {?}
  450. */
  451. AngularDraggableDirective.prototype.ngAfterViewInit = /**
  452. * @return {?}
  453. */
  454. function () {
  455. if (this.inBounds) {
  456. this.boundsCheck();
  457. this.oldTrans.add(this.tempTrans);
  458. this.tempTrans.reset();
  459. }
  460. };
  461. /**
  462. * @private
  463. * @return {?}
  464. */
  465. AngularDraggableDirective.prototype.getDragEl = /**
  466. * @private
  467. * @return {?}
  468. */
  469. function () {
  470. return this.handle ? this.handle : this.el.nativeElement;
  471. };
  472. /**
  473. * @return {?}
  474. */
  475. AngularDraggableDirective.prototype.resetPosition = /**
  476. * @return {?}
  477. */
  478. function () {
  479. if (Position.isIPosition(this.position)) {
  480. this.oldTrans.set(this.position);
  481. }
  482. else {
  483. this.oldTrans.reset();
  484. }
  485. this.tempTrans.reset();
  486. this.transform();
  487. };
  488. /**
  489. * @private
  490. * @param {?} p
  491. * @return {?}
  492. */
  493. AngularDraggableDirective.prototype.moveTo = /**
  494. * @private
  495. * @param {?} p
  496. * @return {?}
  497. */
  498. function (p) {
  499. if (this.orignal) {
  500. p.subtract(this.orignal);
  501. this.tempTrans.set(p);
  502. this.tempTrans.divide(this.scale);
  503. this.transform();
  504. if (this.bounds) {
  505. this.edge.emit(this.boundsCheck());
  506. }
  507. this.movingOffset.emit(this.currTrans.value);
  508. }
  509. };
  510. /**
  511. * @private
  512. * @return {?}
  513. */
  514. AngularDraggableDirective.prototype.transform = /**
  515. * @private
  516. * @return {?}
  517. */
  518. function () {
  519. /** @type {?} */
  520. var translateX = this.tempTrans.x + this.oldTrans.x;
  521. /** @type {?} */
  522. var translateY = this.tempTrans.y + this.oldTrans.y;
  523. if (this.lockAxis === 'x') {
  524. translateX = this.oldTrans.x;
  525. this.tempTrans.x = 0;
  526. }
  527. else if (this.lockAxis === 'y') {
  528. translateY = this.oldTrans.y;
  529. this.tempTrans.y = 0;
  530. }
  531. // Snap to grid: by grid size
  532. if (this.gridSize > 1) {
  533. translateX = Math.round(translateX / this.gridSize) * this.gridSize;
  534. translateY = Math.round(translateY / this.gridSize) * this.gridSize;
  535. }
  536. /** @type {?} */
  537. var value = "translate(" + Math.round(translateX) + "px, " + Math.round(translateY) + "px)";
  538. this.renderer.setStyle(this.el.nativeElement, 'transform', value);
  539. this.renderer.setStyle(this.el.nativeElement, '-webkit-transform', value);
  540. this.renderer.setStyle(this.el.nativeElement, '-ms-transform', value);
  541. this.renderer.setStyle(this.el.nativeElement, '-moz-transform', value);
  542. this.renderer.setStyle(this.el.nativeElement, '-o-transform', value);
  543. // save current position
  544. this.currTrans.x = translateX;
  545. this.currTrans.y = translateY;
  546. };
  547. /**
  548. * @private
  549. * @return {?}
  550. */
  551. AngularDraggableDirective.prototype.pickUp = /**
  552. * @private
  553. * @return {?}
  554. */
  555. function () {
  556. // get old z-index:
  557. this.oldZIndex = this.el.nativeElement.style.zIndex ? this.el.nativeElement.style.zIndex : '';
  558. if (window) {
  559. this.oldZIndex = window.getComputedStyle(this.el.nativeElement, null).getPropertyValue('z-index');
  560. }
  561. if (this.zIndexMoving) {
  562. this.renderer.setStyle(this.el.nativeElement, 'z-index', this.zIndexMoving);
  563. }
  564. if (!this.moving) {
  565. this.started.emit(this.el.nativeElement);
  566. this.moving = true;
  567. /** @type {?} */
  568. var element = this.getDragEl();
  569. this.renderer.addClass(element, 'ng-dragging');
  570. /**
  571. * Fix performance issue:
  572. * https://github.com/xieziyu/angular2-draggable/issues/112
  573. */
  574. this.subscribeEvents();
  575. }
  576. };
  577. /**
  578. * @private
  579. * @return {?}
  580. */
  581. AngularDraggableDirective.prototype.subscribeEvents = /**
  582. * @private
  583. * @return {?}
  584. */
  585. function () {
  586. var _this = this;
  587. this.draggingSub = fromEvent(document, 'mousemove', { passive: false }).subscribe(function (event) { return _this.onMouseMove((/** @type {?} */ (event))); });
  588. this.draggingSub.add(fromEvent(document, 'touchmove', { passive: false }).subscribe(function (event) { return _this.onMouseMove((/** @type {?} */ (event))); }));
  589. this.draggingSub.add(fromEvent(document, 'mouseup', { passive: false }).subscribe(function () { return _this.putBack(); }));
  590. // checking if browser is IE or Edge - https://github.com/xieziyu/angular2-draggable/issues/153
  591. /** @type {?} */
  592. var isIEOrEdge = /msie\s|trident\//i.test(window.navigator.userAgent);
  593. if (!isIEOrEdge) {
  594. this.draggingSub.add(fromEvent(document, 'mouseleave', { passive: false }).subscribe(function () { return _this.putBack(); }));
  595. }
  596. this.draggingSub.add(fromEvent(document, 'touchend', { passive: false }).subscribe(function () { return _this.putBack(); }));
  597. this.draggingSub.add(fromEvent(document, 'touchcancel', { passive: false }).subscribe(function () { return _this.putBack(); }));
  598. };
  599. /**
  600. * @private
  601. * @return {?}
  602. */
  603. AngularDraggableDirective.prototype.unsubscribeEvents = /**
  604. * @private
  605. * @return {?}
  606. */
  607. function () {
  608. this.draggingSub.unsubscribe();
  609. this.draggingSub = null;
  610. };
  611. /**
  612. * @return {?}
  613. */
  614. AngularDraggableDirective.prototype.boundsCheck = /**
  615. * @return {?}
  616. */
  617. function () {
  618. if (this.bounds) {
  619. /** @type {?} */
  620. var boundary = this.bounds.getBoundingClientRect();
  621. /** @type {?} */
  622. var elem = this.el.nativeElement.getBoundingClientRect();
  623. /** @type {?} */
  624. var result = {
  625. 'top': this.outOfBounds.top ? true : boundary.top < elem.top,
  626. 'right': this.outOfBounds.right ? true : boundary.right > elem.right,
  627. 'bottom': this.outOfBounds.bottom ? true : boundary.bottom > elem.bottom,
  628. 'left': this.outOfBounds.left ? true : boundary.left < elem.left
  629. };
  630. if (this.inBounds) {
  631. if (!result.top) {
  632. this.tempTrans.y -= (elem.top - boundary.top) / this.scale;
  633. }
  634. if (!result.bottom) {
  635. this.tempTrans.y -= (elem.bottom - boundary.bottom) / this.scale;
  636. }
  637. if (!result.right) {
  638. this.tempTrans.x -= (elem.right - boundary.right) / this.scale;
  639. }
  640. if (!result.left) {
  641. this.tempTrans.x -= (elem.left - boundary.left) / this.scale;
  642. }
  643. this.transform();
  644. }
  645. return result;
  646. }
  647. };
  648. /** Get current offset */
  649. /**
  650. * Get current offset
  651. * @return {?}
  652. */
  653. AngularDraggableDirective.prototype.getCurrentOffset = /**
  654. * Get current offset
  655. * @return {?}
  656. */
  657. function () {
  658. return this.currTrans.value;
  659. };
  660. /**
  661. * @private
  662. * @return {?}
  663. */
  664. AngularDraggableDirective.prototype.putBack = /**
  665. * @private
  666. * @return {?}
  667. */
  668. function () {
  669. if (this._zIndex) {
  670. this.renderer.setStyle(this.el.nativeElement, 'z-index', this._zIndex);
  671. }
  672. else if (this.zIndexMoving) {
  673. if (this.oldZIndex) {
  674. this.renderer.setStyle(this.el.nativeElement, 'z-index', this.oldZIndex);
  675. }
  676. else {
  677. this.el.nativeElement.style.removeProperty('z-index');
  678. }
  679. }
  680. if (this.moving) {
  681. this.stopped.emit(this.el.nativeElement);
  682. // Remove the helper div:
  683. this._helperBlock.remove();
  684. if (this.needTransform) {
  685. if (Position.isIPosition(this.position)) {
  686. this.oldTrans.set(this.position);
  687. }
  688. else {
  689. this.oldTrans.reset();
  690. }
  691. this.transform();
  692. this.needTransform = false;
  693. }
  694. if (this.bounds) {
  695. this.edge.emit(this.boundsCheck());
  696. }
  697. this.moving = false;
  698. this.endOffset.emit(this.currTrans.value);
  699. if (this.trackPosition) {
  700. this.oldTrans.add(this.tempTrans);
  701. }
  702. this.tempTrans.reset();
  703. if (!this.trackPosition) {
  704. this.transform();
  705. }
  706. /** @type {?} */
  707. var element = this.getDragEl();
  708. this.renderer.removeClass(element, 'ng-dragging');
  709. /**
  710. * Fix performance issue:
  711. * https://github.com/xieziyu/angular2-draggable/issues/112
  712. */
  713. this.unsubscribeEvents();
  714. }
  715. };
  716. /**
  717. * @param {?} target
  718. * @param {?} element
  719. * @return {?}
  720. */
  721. AngularDraggableDirective.prototype.checkHandleTarget = /**
  722. * @param {?} target
  723. * @param {?} element
  724. * @return {?}
  725. */
  726. function (target, element) {
  727. // Checks if the target is the element clicked, then checks each child element of element as well
  728. // Ignores button clicks
  729. // Ignore elements of type button
  730. if (element.tagName === 'BUTTON') {
  731. return false;
  732. }
  733. // If the target was found, return true (handle was found)
  734. if (element === target) {
  735. return true;
  736. }
  737. // Recursively iterate this elements children
  738. for (var child in element.children) {
  739. if (element.children.hasOwnProperty(child)) {
  740. if (this.checkHandleTarget(target, element.children[child])) {
  741. return true;
  742. }
  743. }
  744. }
  745. // Handle was not found in this lineage
  746. // Note: return false is ignore unless it is the parent element
  747. return false;
  748. };
  749. /**
  750. * @param {?} event
  751. * @return {?}
  752. */
  753. AngularDraggableDirective.prototype.onMouseDown = /**
  754. * @param {?} event
  755. * @return {?}
  756. */
  757. function (event) {
  758. // 1. skip right click;
  759. if (event instanceof MouseEvent && event.button === 2) {
  760. return;
  761. }
  762. // 2. if handle is set, the element can only be moved by handle
  763. /** @type {?} */
  764. var target = event.target || event.srcElement;
  765. if (this.handle !== undefined && !this.checkHandleTarget(target, this.handle)) {
  766. return;
  767. }
  768. // 3. if allow drag is set to false, ignore the mousedown
  769. if (this.allowDrag === false) {
  770. return;
  771. }
  772. if (this.preventDefaultEvent) {
  773. event.stopPropagation();
  774. event.preventDefault();
  775. }
  776. this.orignal = Position.fromEvent(event, this.getDragEl());
  777. this.pickUp();
  778. };
  779. /**
  780. * @param {?} event
  781. * @return {?}
  782. */
  783. AngularDraggableDirective.prototype.onMouseMove = /**
  784. * @param {?} event
  785. * @return {?}
  786. */
  787. function (event) {
  788. if (this.moving && this.allowDrag) {
  789. if (this.preventDefaultEvent) {
  790. event.stopPropagation();
  791. event.preventDefault();
  792. }
  793. // Add a transparent helper div:
  794. this._helperBlock.add();
  795. this.moveTo(Position.fromEvent(event, this.getDragEl()));
  796. }
  797. };
  798. AngularDraggableDirective.decorators = [
  799. { type: Directive, args: [{
  800. selector: '[ngDraggable]',
  801. exportAs: 'ngDraggable'
  802. },] }
  803. ];
  804. /** @nocollapse */
  805. AngularDraggableDirective.ctorParameters = function () { return [
  806. { type: ElementRef },
  807. { type: Renderer2 }
  808. ]; };
  809. AngularDraggableDirective.propDecorators = {
  810. started: [{ type: Output }],
  811. stopped: [{ type: Output }],
  812. edge: [{ type: Output }],
  813. handle: [{ type: Input }],
  814. bounds: [{ type: Input }],
  815. outOfBounds: [{ type: Input }],
  816. gridSize: [{ type: Input }],
  817. zIndexMoving: [{ type: Input }],
  818. zIndex: [{ type: Input }],
  819. inBounds: [{ type: Input }],
  820. trackPosition: [{ type: Input }],
  821. scale: [{ type: Input }],
  822. preventDefaultEvent: [{ type: Input }],
  823. position: [{ type: Input }],
  824. lockAxis: [{ type: Input }],
  825. movingOffset: [{ type: Output }],
  826. endOffset: [{ type: Output }],
  827. ngDraggable: [{ type: Input }],
  828. onMouseDown: [{ type: HostListener, args: ['mousedown', ['$event'],] }, { type: HostListener, args: ['touchstart', ['$event'],] }]
  829. };
  830. return AngularDraggableDirective;
  831. }());
  832. /**
  833. * @fileoverview added by tsickle
  834. * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  835. */
  836. var ResizeHandle = /** @class */ (function () {
  837. function ResizeHandle(parent, renderer, type, css, onMouseDown) {
  838. var _this = this;
  839. this.parent = parent;
  840. this.renderer = renderer;
  841. this.type = type;
  842. this.css = css;
  843. this.onMouseDown = onMouseDown;
  844. // generate handle div
  845. /** @type {?} */
  846. var handle = renderer.createElement('div');
  847. renderer.addClass(handle, 'ng-resizable-handle');
  848. renderer.addClass(handle, css);
  849. // add default diagonal for se handle
  850. if (type === 'se') {
  851. renderer.addClass(handle, 'ng-resizable-diagonal');
  852. }
  853. // append div to parent
  854. if (this.parent) {
  855. parent.appendChild(handle);
  856. }
  857. // create and register event listener
  858. this._onResize = function (event) { onMouseDown(event, _this); };
  859. handle.addEventListener('mousedown', this._onResize, { passive: false });
  860. handle.addEventListener('touchstart', this._onResize, { passive: false });
  861. // done
  862. this._handle = handle;
  863. }
  864. /**
  865. * @return {?}
  866. */
  867. ResizeHandle.prototype.dispose = /**
  868. * @return {?}
  869. */
  870. function () {
  871. this._handle.removeEventListener('mousedown', this._onResize);
  872. this._handle.removeEventListener('touchstart', this._onResize);
  873. if (this.parent) {
  874. this.parent.removeChild(this._handle);
  875. }
  876. this._handle = null;
  877. this._onResize = null;
  878. };
  879. Object.defineProperty(ResizeHandle.prototype, "el", {
  880. get: /**
  881. * @return {?}
  882. */
  883. function () {
  884. return this._handle;
  885. },
  886. enumerable: true,
  887. configurable: true
  888. });
  889. return ResizeHandle;
  890. }());
  891. /**
  892. * @fileoverview added by tsickle
  893. * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  894. */
  895. var Size = /** @class */ (function () {
  896. function Size(width, height) {
  897. this.width = width;
  898. this.height = height;
  899. }
  900. /**
  901. * @param {?} el
  902. * @return {?}
  903. */
  904. Size.getCurrent = /**
  905. * @param {?} el
  906. * @return {?}
  907. */
  908. function (el) {
  909. /** @type {?} */
  910. var size = new Size(0, 0);
  911. if (window) {
  912. /** @type {?} */
  913. var computed = window.getComputedStyle(el);
  914. if (computed) {
  915. size.width = parseInt(computed.getPropertyValue('width'), 10);
  916. size.height = parseInt(computed.getPropertyValue('height'), 10);
  917. }
  918. return size;
  919. }
  920. else {
  921. console.error('Not Supported!');
  922. return null;
  923. }
  924. };
  925. /**
  926. * @param {?} s
  927. * @return {?}
  928. */
  929. Size.copy = /**
  930. * @param {?} s
  931. * @return {?}
  932. */
  933. function (s) {
  934. return new Size(0, 0).set(s);
  935. };
  936. /**
  937. * @template THIS
  938. * @this {THIS}
  939. * @param {?} s
  940. * @return {THIS}
  941. */
  942. Size.prototype.set = /**
  943. * @template THIS
  944. * @this {THIS}
  945. * @param {?} s
  946. * @return {THIS}
  947. */
  948. function (s) {
  949. (/** @type {?} */ (this)).width = s.width;
  950. (/** @type {?} */ (this)).height = s.height;
  951. return (/** @type {?} */ (this));
  952. };
  953. return Size;
  954. }());
  955. /**
  956. * @fileoverview added by tsickle
  957. * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  958. */
  959. var AngularResizableDirective = /** @class */ (function () {
  960. function AngularResizableDirective(el, renderer) {
  961. this.el = el;
  962. this.renderer = renderer;
  963. this._resizable = true;
  964. this._handles = {};
  965. this._handleType = [];
  966. this._handleResizing = null;
  967. this._direction = null;
  968. this._directionChanged = null;
  969. this._aspectRatio = 0;
  970. this._containment = null;
  971. this._origMousePos = null;
  972. /**
  973. * Original Size and Position
  974. */
  975. this._origSize = null;
  976. this._origPos = null;
  977. /**
  978. * Current Size and Position
  979. */
  980. this._currSize = null;
  981. this._currPos = null;
  982. /**
  983. * Initial Size and Position
  984. */
  985. this._initSize = null;
  986. this._initPos = null;
  987. /**
  988. * Snap to gird
  989. */
  990. this._gridSize = null;
  991. this._bounding = null;
  992. /**
  993. * Bugfix: iFrames, and context unrelated elements block all events, and are unusable
  994. * https://github.com/xieziyu/angular2-draggable/issues/84
  995. */
  996. this._helperBlock = null;
  997. this.draggingSub = null;
  998. this._adjusted = false;
  999. /**
  1000. * Which handles can be used for resizing.
  1001. * \@example
  1002. * [rzHandles] = "'n,e,s,w,se,ne,sw,nw'"
  1003. * equals to: [rzHandles] = "'all'"
  1004. *
  1005. *
  1006. */
  1007. this.rzHandles = 'e,s,se';
  1008. /**
  1009. * Whether the element should be constrained to a specific aspect ratio.
  1010. * Multiple types supported:
  1011. * boolean: When set to true, the element will maintain its original aspect ratio.
  1012. * number: Force the element to maintain a specific aspect ratio during resizing.
  1013. */
  1014. this.rzAspectRatio = false;
  1015. /**
  1016. * Constrains resizing to within the bounds of the specified element or region.
  1017. * Multiple types supported:
  1018. * Selector: The resizable element will be contained to the bounding box of the first element found by the selector.
  1019. * If no element is found, no containment will be set.
  1020. * Element: The resizable element will be contained to the bounding box of this element.
  1021. * String: Possible values: "parent".
  1022. */
  1023. this.rzContainment = null;
  1024. /**
  1025. * Snaps the resizing element to a grid, every x and y pixels.
  1026. * A number for both width and height or an array values like [ x, y ]
  1027. */
  1028. this.rzGrid = null;
  1029. /**
  1030. * The minimum width the resizable should be allowed to resize to.
  1031. */
  1032. this.rzMinWidth = null;
  1033. /**
  1034. * The minimum height the resizable should be allowed to resize to.
  1035. */
  1036. this.rzMinHeight = null;
  1037. /**
  1038. * The maximum width the resizable should be allowed to resize to.
  1039. */
  1040. this.rzMaxWidth = null;
  1041. /**
  1042. * The maximum height the resizable should be allowed to resize to.
  1043. */
  1044. this.rzMaxHeight = null;
  1045. /**
  1046. * Whether to prevent default event
  1047. */
  1048. this.preventDefaultEvent = true;
  1049. /**
  1050. * emitted when start resizing
  1051. */
  1052. this.rzStart = new EventEmitter();
  1053. /**
  1054. * emitted when start resizing
  1055. */
  1056. this.rzResizing = new EventEmitter();
  1057. /**
  1058. * emitted when stop resizing
  1059. */
  1060. this.rzStop = new EventEmitter();
  1061. this._helperBlock = new HelperBlock(el.nativeElement, renderer);
  1062. }
  1063. Object.defineProperty(AngularResizableDirective.prototype, "ngResizable", {
  1064. /** Disables the resizable if set to false. */
  1065. set: /**
  1066. * Disables the resizable if set to false.
  1067. * @param {?} v
  1068. * @return {?}
  1069. */
  1070. function (v) {
  1071. if (v !== undefined && v !== null && v !== '') {
  1072. this._resizable = !!v;
  1073. this.updateResizable();
  1074. }
  1075. },
  1076. enumerable: true,
  1077. configurable: true
  1078. });
  1079. /**
  1080. * @param {?} changes
  1081. * @return {?}
  1082. */
  1083. AngularResizableDirective.prototype.ngOnChanges = /**
  1084. * @param {?} changes
  1085. * @return {?}
  1086. */
  1087. function (changes) {
  1088. if (changes['rzHandles'] && !changes['rzHandles'].isFirstChange()) {
  1089. this.updateResizable();
  1090. }
  1091. if (changes['rzAspectRatio'] && !changes['rzAspectRatio'].isFirstChange()) {
  1092. this.updateAspectRatio();
  1093. }
  1094. if (changes['rzContainment'] && !changes['rzContainment'].isFirstChange()) {
  1095. this.updateContainment();
  1096. }
  1097. };
  1098. /**
  1099. * @return {?}
  1100. */
  1101. AngularResizableDirective.prototype.ngOnInit = /**
  1102. * @return {?}
  1103. */
  1104. function () {
  1105. this.updateResizable();
  1106. };
  1107. /**
  1108. * @return {?}
  1109. */
  1110. AngularResizableDirective.prototype.ngOnDestroy = /**
  1111. * @return {?}
  1112. */
  1113. function () {
  1114. this.removeHandles();
  1115. this._containment = null;
  1116. this._helperBlock.dispose();
  1117. this._helperBlock = null;
  1118. };
  1119. /**
  1120. * @return {?}
  1121. */
  1122. AngularResizableDirective.prototype.ngAfterViewInit = /**
  1123. * @return {?}
  1124. */
  1125. function () {
  1126. /** @type {?} */
  1127. var elm = this.el.nativeElement;
  1128. this._initSize = Size.getCurrent(elm);
  1129. this._initPos = Position.getCurrent(elm);
  1130. this._currSize = Size.copy(this._initSize);
  1131. this._currPos = Position.copy(this._initPos);
  1132. this.updateAspectRatio();
  1133. this.updateContainment();
  1134. };
  1135. /** A method to reset size */
  1136. /**
  1137. * A method to reset size
  1138. * @return {?}
  1139. */
  1140. AngularResizableDirective.prototype.resetSize = /**
  1141. * A method to reset size
  1142. * @return {?}
  1143. */
  1144. function () {
  1145. this._currSize = Size.copy(this._initSize);
  1146. this._currPos = Position.copy(this._initPos);
  1147. this.doResize();
  1148. };
  1149. /** A method to get current status */
  1150. /**
  1151. * A method to get current status
  1152. * @return {?}
  1153. */
  1154. AngularResizableDirective.prototype.getStatus = /**
  1155. * A method to get current status
  1156. * @return {?}
  1157. */
  1158. function () {
  1159. if (!this._currPos || !this._currSize) {
  1160. return null;
  1161. }
  1162. return {
  1163. size: {
  1164. width: this._currSize.width,
  1165. height: this._currSize.height
  1166. },
  1167. position: {
  1168. top: this._currPos.y,
  1169. left: this._currPos.x
  1170. }
  1171. };
  1172. };
  1173. /**
  1174. * @private
  1175. * @return {?}
  1176. */
  1177. AngularResizableDirective.prototype.updateResizable = /**
  1178. * @private
  1179. * @return {?}
  1180. */
  1181. function () {
  1182. /** @type {?} */
  1183. var element = this.el.nativeElement;
  1184. // clear handles:
  1185. this.renderer.removeClass(element, 'ng-resizable');
  1186. this.removeHandles();
  1187. // create new ones:
  1188. if (this._resizable) {
  1189. this.renderer.addClass(element, 'ng-resizable');
  1190. this.createHandles();
  1191. }
  1192. };
  1193. /** Use it to update aspect */
  1194. /**
  1195. * Use it to update aspect
  1196. * @private
  1197. * @return {?}
  1198. */
  1199. AngularResizableDirective.prototype.updateAspectRatio = /**
  1200. * Use it to update aspect
  1201. * @private
  1202. * @return {?}
  1203. */
  1204. function () {
  1205. if (typeof this.rzAspectRatio === 'boolean') {
  1206. if (this.rzAspectRatio && this._currSize.height) {
  1207. this._aspectRatio = (this._currSize.width / this._currSize.height);
  1208. }
  1209. else {
  1210. this._aspectRatio = 0;
  1211. }
  1212. }
  1213. else {
  1214. /** @type {?} */
  1215. var r = Number(this.rzAspectRatio);
  1216. this._aspectRatio = isNaN(r) ? 0 : r;
  1217. }
  1218. };
  1219. /** Use it to update containment */
  1220. /**
  1221. * Use it to update containment
  1222. * @private
  1223. * @return {?}
  1224. */
  1225. AngularResizableDirective.prototype.updateContainment = /**
  1226. * Use it to update containment
  1227. * @private
  1228. * @return {?}
  1229. */
  1230. function () {
  1231. if (!this.rzContainment) {
  1232. this._containment = null;
  1233. return;
  1234. }
  1235. if (typeof this.rzContainment === 'string') {
  1236. if (this.rzContainment === 'parent') {
  1237. this._containment = this.el.nativeElement.parentElement;
  1238. }
  1239. else {
  1240. this._containment = document.querySelector(this.rzContainment);
  1241. }
  1242. }
  1243. else {
  1244. this._containment = this.rzContainment;
  1245. }
  1246. };
  1247. /** Use it to create handle divs */
  1248. /**
  1249. * Use it to create handle divs
  1250. * @private
  1251. * @return {?}
  1252. */
  1253. AngularResizableDirective.prototype.createHandles = /**
  1254. * Use it to create handle divs
  1255. * @private
  1256. * @return {?}
  1257. */
  1258. function () {
  1259. var e_1, _a, e_2, _b;
  1260. if (!this.rzHandles) {
  1261. return;
  1262. }
  1263. /** @type {?} */
  1264. var tmpHandleTypes;
  1265. if (typeof this.rzHandles === 'string') {
  1266. if (this.rzHandles === 'all') {
  1267. tmpHandleTypes = ['n', 'e', 's', 'w', 'ne', 'se', 'nw', 'sw'];
  1268. }
  1269. else {
  1270. tmpHandleTypes = this.rzHandles.replace(/ /g, '').toLowerCase().split(',');
  1271. }
  1272. try {
  1273. for (var tmpHandleTypes_1 = __values(tmpHandleTypes), tmpHandleTypes_1_1 = tmpHandleTypes_1.next(); !tmpHandleTypes_1_1.done; tmpHandleTypes_1_1 = tmpHandleTypes_1.next()) {
  1274. var type = tmpHandleTypes_1_1.value;
  1275. // default handle theme: ng-resizable-$type.
  1276. /** @type {?} */
  1277. var handle = this.createHandleByType(type, "ng-resizable-" + type);
  1278. if (handle) {
  1279. this._handleType.push(type);
  1280. this._handles[type] = handle;
  1281. }
  1282. }
  1283. }
  1284. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  1285. finally {
  1286. try {
  1287. if (tmpHandleTypes_1_1 && !tmpHandleTypes_1_1.done && (_a = tmpHandleTypes_1.return)) _a.call(tmpHandleTypes_1);
  1288. }
  1289. finally { if (e_1) throw e_1.error; }
  1290. }
  1291. }
  1292. else {
  1293. tmpHandleTypes = Object.keys(this.rzHandles);
  1294. try {
  1295. for (var tmpHandleTypes_2 = __values(tmpHandleTypes), tmpHandleTypes_2_1 = tmpHandleTypes_2.next(); !tmpHandleTypes_2_1.done; tmpHandleTypes_2_1 = tmpHandleTypes_2.next()) {
  1296. var type = tmpHandleTypes_2_1.value;
  1297. // custom handle theme.
  1298. /** @type {?} */
  1299. var handle = this.createHandleByType(type, this.rzHandles[type]);
  1300. if (handle) {
  1301. this._handleType.push(type);
  1302. this._handles[type] = handle;
  1303. }
  1304. }
  1305. }
  1306. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  1307. finally {
  1308. try {
  1309. if (tmpHandleTypes_2_1 && !tmpHandleTypes_2_1.done && (_b = tmpHandleTypes_2.return)) _b.call(tmpHandleTypes_2);
  1310. }
  1311. finally { if (e_2) throw e_2.error; }
  1312. }
  1313. }
  1314. };
  1315. /** Use it to create a handle */
  1316. /**
  1317. * Use it to create a handle
  1318. * @private
  1319. * @param {?} type
  1320. * @param {?} css
  1321. * @return {?}
  1322. */
  1323. AngularResizableDirective.prototype.createHandleByType = /**
  1324. * Use it to create a handle
  1325. * @private
  1326. * @param {?} type
  1327. * @param {?} css
  1328. * @return {?}
  1329. */
  1330. function (type, css) {
  1331. /** @type {?} */
  1332. var _el = this.el.nativeElement;
  1333. if (!type.match(/^(se|sw|ne|nw|n|e|s|w)$/)) {
  1334. console.error('Invalid handle type:', type);
  1335. return null;
  1336. }
  1337. return new ResizeHandle(_el, this.renderer, type, css, this.onMouseDown.bind(this));
  1338. };
  1339. /**
  1340. * @private
  1341. * @return {?}
  1342. */
  1343. AngularResizableDirective.prototype.removeHandles = /**
  1344. * @private
  1345. * @return {?}
  1346. */
  1347. function () {
  1348. var e_3, _a;
  1349. try {
  1350. for (var _b = __values(this._handleType), _c = _b.next(); !_c.done; _c = _b.next()) {
  1351. var type = _c.value;
  1352. this._handles[type].dispose();
  1353. }
  1354. }
  1355. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  1356. finally {
  1357. try {
  1358. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  1359. }
  1360. finally { if (e_3) throw e_3.error; }
  1361. }
  1362. this._handleType = [];
  1363. this._handles = {};
  1364. };
  1365. /**
  1366. * @param {?} event
  1367. * @param {?} handle
  1368. * @return {?}
  1369. */
  1370. AngularResizableDirective.prototype.onMouseDown = /**
  1371. * @param {?} event
  1372. * @param {?} handle
  1373. * @return {?}
  1374. */
  1375. function (event, handle) {
  1376. // skip right click;
  1377. if (event instanceof MouseEvent && event.button === 2) {
  1378. return;
  1379. }
  1380. if (this.preventDefaultEvent) {
  1381. // prevent default events
  1382. event.stopPropagation();
  1383. event.preventDefault();
  1384. }
  1385. if (!this._handleResizing) {
  1386. this._origMousePos = Position.fromEvent(event);
  1387. this.startResize(handle);
  1388. this.subscribeEvents();
  1389. }
  1390. };
  1391. /**
  1392. * @private
  1393. * @return {?}
  1394. */
  1395. AngularResizableDirective.prototype.subscribeEvents = /**
  1396. * @private
  1397. * @return {?}
  1398. */
  1399. function () {
  1400. var _this = this;
  1401. this.draggingSub = fromEvent(document, 'mousemove', { passive: false }).subscribe(function (event) { return _this.onMouseMove((/** @type {?} */ (event))); });
  1402. this.draggingSub.add(fromEvent(document, 'touchmove', { passive: false }).subscribe(function (event) { return _this.onMouseMove((/** @type {?} */ (event))); }));
  1403. this.draggingSub.add(fromEvent(document, 'mouseup', { passive: false }).subscribe(function () { return _this.onMouseLeave(); }));
  1404. // fix for issue #164
  1405. /** @type {?} */
  1406. var isIEOrEdge = /msie\s|trident\//i.test(window.navigator.userAgent);
  1407. if (!isIEOrEdge) {
  1408. this.draggingSub.add(fromEvent(document, 'mouseleave', { passive: false }).subscribe(function () { return _this.onMouseLeave(); }));
  1409. }
  1410. this.draggingSub.add(fromEvent(document, 'touchend', { passive: false }).subscribe(function () { return _this.onMouseLeave(); }));
  1411. this.draggingSub.add(fromEvent(document, 'touchcancel', { passive: false }).subscribe(function () { return _this.onMouseLeave(); }));
  1412. };
  1413. /**
  1414. * @private
  1415. * @return {?}
  1416. */
  1417. AngularResizableDirective.prototype.unsubscribeEvents = /**
  1418. * @private
  1419. * @return {?}
  1420. */
  1421. function () {
  1422. this.draggingSub.unsubscribe();
  1423. this.draggingSub = null;
  1424. };
  1425. /**
  1426. * @return {?}
  1427. */
  1428. AngularResizableDirective.prototype.onMouseLeave = /**
  1429. * @return {?}
  1430. */
  1431. function () {
  1432. if (this._handleResizing) {
  1433. this.stopResize();
  1434. this._origMousePos = null;
  1435. this.unsubscribeEvents();
  1436. }
  1437. };
  1438. /**
  1439. * @param {?} event
  1440. * @return {?}
  1441. */
  1442. AngularResizableDirective.prototype.onMouseMove = /**
  1443. * @param {?} event
  1444. * @return {?}
  1445. */
  1446. function (event) {
  1447. if (this._handleResizing && this._resizable && this._origMousePos && this._origPos && this._origSize) {
  1448. this.resizeTo(Position.fromEvent(event));
  1449. this.onResizing();
  1450. }
  1451. };
  1452. /**
  1453. * @private
  1454. * @param {?} handle
  1455. * @return {?}
  1456. */
  1457. AngularResizableDirective.prototype.startResize = /**
  1458. * @private
  1459. * @param {?} handle
  1460. * @return {?}
  1461. */
  1462. function (handle) {
  1463. /** @type {?} */
  1464. var elm = this.el.nativeElement;
  1465. this._origSize = Size.getCurrent(elm);
  1466. this._origPos = Position.getCurrent(elm); // x: left, y: top
  1467. this._currSize = Size.copy(this._origSize);
  1468. this._currPos = Position.copy(this._origPos);
  1469. if (this._containment) {
  1470. this.getBounding();
  1471. }
  1472. this.getGridSize();
  1473. // Add a transparent helper div:
  1474. this._helperBlock.add();
  1475. this._handleResizing = handle;
  1476. this.updateDirection();
  1477. this.rzStart.emit(this.getResizingEvent());
  1478. };
  1479. /**
  1480. * @private
  1481. * @return {?}
  1482. */
  1483. AngularResizableDirective.prototype.stopResize = /**
  1484. * @private
  1485. * @return {?}
  1486. */
  1487. function () {
  1488. // Remove the helper div:
  1489. this._helperBlock.remove();
  1490. this.rzStop.emit(this.getResizingEvent());
  1491. this._handleResizing = null;
  1492. this._direction = null;
  1493. this._origSize = null;
  1494. this._origPos = null;
  1495. if (this._containment) {
  1496. this.resetBounding();
  1497. }
  1498. };
  1499. /**
  1500. * @private
  1501. * @return {?}
  1502. */
  1503. AngularResizableDirective.prototype.onResizing = /**
  1504. * @private
  1505. * @return {?}
  1506. */
  1507. function () {
  1508. this.rzResizing.emit(this.getResizingEvent());
  1509. };
  1510. /**
  1511. * @private
  1512. * @return {?}
  1513. */
  1514. AngularResizableDirective.prototype.getResizingEvent = /**
  1515. * @private
  1516. * @return {?}
  1517. */
  1518. function () {
  1519. return {
  1520. host: this.el.nativeElement,
  1521. handle: this._handleResizing ? this._handleResizing.el : null,
  1522. size: {
  1523. width: this._currSize.width,
  1524. height: this._currSize.height
  1525. },
  1526. position: {
  1527. top: this._currPos.y,
  1528. left: this._currPos.x
  1529. },
  1530. direction: __assign({}, this._directionChanged),
  1531. };
  1532. };
  1533. /**
  1534. * @private
  1535. * @return {?}
  1536. */
  1537. AngularResizableDirective.prototype.updateDirection = /**
  1538. * @private
  1539. * @return {?}
  1540. */
  1541. function () {
  1542. this._direction = {
  1543. n: !!this._handleResizing.type.match(/n/),
  1544. s: !!this._handleResizing.type.match(/s/),
  1545. w: !!this._handleResizing.type.match(/w/),
  1546. e: !!this._handleResizing.type.match(/e/)
  1547. };
  1548. this._directionChanged = __assign({}, this._direction);
  1549. // if aspect ration should be preserved:
  1550. if (this.rzAspectRatio) {
  1551. // if north then west (unless ne)
  1552. if (this._directionChanged.n && !this._directionChanged.e) {
  1553. this._directionChanged.w = true;
  1554. }
  1555. // if south then east (unless sw)
  1556. if (this._directionChanged.s && !this._directionChanged.w) {
  1557. this._directionChanged.e = true;
  1558. }
  1559. // if east then south (unless ne)
  1560. if (this._directionChanged.e && !this._directionChanged.n) {
  1561. this._directionChanged.s = true;
  1562. }
  1563. // if west then south (unless nw)
  1564. if (this._directionChanged.w && !this._directionChanged.n) {
  1565. this._directionChanged.s = true;
  1566. }
  1567. }
  1568. };
  1569. /**
  1570. * @private
  1571. * @param {?} p
  1572. * @return {?}
  1573. */
  1574. AngularResizableDirective.prototype.resizeTo = /**
  1575. * @private
  1576. * @param {?} p
  1577. * @return {?}
  1578. */
  1579. function (p) {
  1580. p.subtract(this._origMousePos);
  1581. /** @type {?} */
  1582. var tmpX = Math.round(p.x / this._gridSize.x) * this._gridSize.x;
  1583. /** @type {?} */
  1584. var tmpY = Math.round(p.y / this._gridSize.y) * this._gridSize.y;
  1585. if (this._direction.n) {
  1586. // n, ne, nw
  1587. this._currPos.y = this._origPos.y + tmpY;
  1588. this._currSize.height = this._origSize.height - tmpY;
  1589. }
  1590. else if (this._direction.s) {
  1591. // s, se, sw
  1592. this._currSize.height = this._origSize.height + tmpY;
  1593. }
  1594. if (this._direction.e) {
  1595. // e, ne, se
  1596. this._currSize.width = this._origSize.width + tmpX;
  1597. }
  1598. else if (this._direction.w) {
  1599. // w, nw, sw
  1600. this._currSize.width = this._origSize.width - tmpX;
  1601. this._currPos.x = this._origPos.x + tmpX;
  1602. }
  1603. this.checkBounds();
  1604. this.checkSize();
  1605. this.adjustByRatio();
  1606. this.doResize();
  1607. };
  1608. /**
  1609. * @private
  1610. * @return {?}
  1611. */
  1612. AngularResizableDirective.prototype.doResize = /**
  1613. * @private
  1614. * @return {?}
  1615. */
  1616. function () {
  1617. /** @type {?} */
  1618. var container = this.el.nativeElement;
  1619. if (!this._direction || this._direction.n || this._direction.s || this._aspectRatio) {
  1620. this.renderer.setStyle(container, 'height', this._currSize.height + 'px');
  1621. }
  1622. if (!this._direction || this._direction.w || this._direction.e || this._aspectRatio) {
  1623. this.renderer.setStyle(container, 'width', this._currSize.width + 'px');
  1624. }
  1625. this.renderer.setStyle(container, 'left', this._currPos.x + 'px');
  1626. this.renderer.setStyle(container, 'top', this._currPos.y + 'px');
  1627. };
  1628. /**
  1629. * @private
  1630. * @return {?}
  1631. */
  1632. AngularResizableDirective.prototype.adjustByRatio = /**
  1633. * @private
  1634. * @return {?}
  1635. */
  1636. function () {
  1637. if (this._aspectRatio && !this._adjusted) {
  1638. if (this._direction.e || this._direction.w) {
  1639. /** @type {?} */
  1640. var newHeight = Math.floor(this._currSize.width / this._aspectRatio);
  1641. if (this._direction.n) {
  1642. this._currPos.y += this._currSize.height - newHeight;
  1643. }
  1644. this._currSize.height = newHeight;
  1645. }
  1646. else {
  1647. /** @type {?} */
  1648. var newWidth = Math.floor(this._aspectRatio * this._currSize.height);
  1649. if (this._direction.n) {
  1650. this._currPos.x += this._currSize.width - newWidth;
  1651. }
  1652. this._currSize.width = newWidth;
  1653. }
  1654. }
  1655. };
  1656. /**
  1657. * @private
  1658. * @return {?}
  1659. */
  1660. AngularResizableDirective.prototype.checkBounds = /**
  1661. * @private
  1662. * @return {?}
  1663. */
  1664. function () {
  1665. if (this._containment) {
  1666. /** @type {?} */
  1667. var maxWidth = this._bounding.width - this._bounding.pr - this._bounding.deltaL - this._bounding.translateX - this._currPos.x;
  1668. /** @type {?} */
  1669. var maxHeight = this._bounding.height - this._bounding.pb - this._bounding.deltaT - this._bounding.translateY - this._currPos.y;
  1670. if (this._direction.n && (this._currPos.y + this._bounding.translateY < 0)) {
  1671. this._currPos.y = -this._bounding.translateY;
  1672. this._currSize.height = this._origSize.height + this._origPos.y + this._bounding.translateY;
  1673. }
  1674. if (this._direction.w && (this._currPos.x + this._bounding.translateX) < 0) {
  1675. this._currPos.x = -this._bounding.translateX;
  1676. this._currSize.width = this._origSize.width + this._origPos.x + this._bounding.translateX;
  1677. }
  1678. if (this._currSize.width > maxWidth) {
  1679. this._currSize.width = maxWidth;
  1680. }
  1681. if (this._currSize.height > maxHeight) {
  1682. this._currSize.height = maxHeight;
  1683. }
  1684. /**
  1685. * Fix Issue: Additional check for aspect ratio
  1686. * https://github.com/xieziyu/angular2-draggable/issues/132
  1687. */
  1688. if (this._aspectRatio) {
  1689. this._adjusted = false;
  1690. if ((this._direction.w || this._direction.e) &&
  1691. (this._currSize.width / this._aspectRatio) >= maxHeight) {
  1692. /** @type {?} */
  1693. var newWidth = Math.floor(maxHeight * this._aspectRatio);
  1694. if (this._direction.w) {
  1695. this._currPos.x += this._currSize.width - newWidth;
  1696. }
  1697. this._currSize.width = newWidth;
  1698. this._currSize.height = maxHeight;
  1699. this._adjusted = true;
  1700. }
  1701. if ((this._direction.n || this._direction.s) &&
  1702. (this._currSize.height * this._aspectRatio) >= maxWidth) {
  1703. /** @type {?} */
  1704. var newHeight = Math.floor(maxWidth / this._aspectRatio);
  1705. if (this._direction.n) {
  1706. this._currPos.y += this._currSize.height - newHeight;
  1707. }
  1708. this._currSize.width = maxWidth;
  1709. this._currSize.height = newHeight;
  1710. this._adjusted = true;
  1711. }
  1712. }
  1713. }
  1714. };
  1715. /**
  1716. * @private
  1717. * @return {?}
  1718. */
  1719. AngularResizableDirective.prototype.checkSize = /**
  1720. * @private
  1721. * @return {?}
  1722. */
  1723. function () {
  1724. /** @type {?} */
  1725. var minHeight = !this.rzMinHeight ? 1 : this.rzMinHeight;
  1726. /** @type {?} */
  1727. var minWidth = !this.rzMinWidth ? 1 : this.rzMinWidth;
  1728. if (this._currSize.height < minHeight) {
  1729. this._currSize.height = minHeight;
  1730. if (this._direction.n) {
  1731. this._currPos.y = this._origPos.y + (this._origSize.height - minHeight);
  1732. }
  1733. }
  1734. if (this._currSize.width < minWidth) {
  1735. this._currSize.width = minWidth;
  1736. if (this._direction.w) {
  1737. this._currPos.x = this._origPos.x + (this._origSize.width - minWidth);
  1738. }
  1739. }
  1740. if (this.rzMaxHeight && this._currSize.height > this.rzMaxHeight) {
  1741. this._currSize.height = this.rzMaxHeight;
  1742. if (this._direction.n) {
  1743. this._currPos.y = this._origPos.y + (this._origSize.height - this.rzMaxHeight);
  1744. }
  1745. }
  1746. if (this.rzMaxWidth && this._currSize.width > this.rzMaxWidth) {
  1747. this._currSize.width = this.rzMaxWidth;
  1748. if (this._direction.w) {
  1749. this._currPos.x = this._origPos.x + (this._origSize.width - this.rzMaxWidth);
  1750. }
  1751. }
  1752. };
  1753. /**
  1754. * @private
  1755. * @return {?}
  1756. */
  1757. AngularResizableDirective.prototype.getBounding = /**
  1758. * @private
  1759. * @return {?}
  1760. */
  1761. function () {
  1762. /** @type {?} */
  1763. var el = this._containment;
  1764. /** @type {?} */
  1765. var computed = window.getComputedStyle(el);
  1766. if (computed) {
  1767. /** @type {?} */
  1768. var p = computed.getPropertyValue('position');
  1769. /** @type {?} */
  1770. var nativeEl = window.getComputedStyle(this.el.nativeElement);
  1771. /** @type {?} */
  1772. var transforms = nativeEl.getPropertyValue('transform').replace(/[^-\d,]/g, '').split(',');
  1773. this._bounding = {};
  1774. this._bounding.width = el.clientWidth;
  1775. this._bounding.height = el.clientHeight;
  1776. this._bounding.pr = parseInt(computed.getPropertyValue('padding-right'), 10);
  1777. this._bounding.pb = parseInt(computed.getPropertyValue('padding-bottom'), 10);
  1778. this._bounding.deltaL = this.el.nativeElement.offsetLeft - this._currPos.x;
  1779. this._bounding.deltaT = this.el.nativeElement.offsetTop - this._currPos.y;
  1780. if (transforms.length >= 6) {
  1781. this._bounding.translateX = parseInt(transforms[4], 10);
  1782. this._bounding.translateY = parseInt(transforms[5], 10);
  1783. }
  1784. else {
  1785. this._bounding.translateX = 0;
  1786. this._bounding.translateY = 0;
  1787. }
  1788. this._bounding.position = computed.getPropertyValue('position');
  1789. if (p === 'static') {
  1790. this.renderer.setStyle(el, 'position', 'relative');
  1791. }
  1792. }
  1793. };
  1794. /**
  1795. * @private
  1796. * @return {?}
  1797. */
  1798. AngularResizableDirective.prototype.resetBounding = /**
  1799. * @private
  1800. * @return {?}
  1801. */
  1802. function () {
  1803. if (this._bounding && this._bounding.position === 'static') {
  1804. this.renderer.setStyle(this._containment, 'position', 'relative');
  1805. }
  1806. this._bounding = null;
  1807. };
  1808. /**
  1809. * @private
  1810. * @return {?}
  1811. */
  1812. AngularResizableDirective.prototype.getGridSize = /**
  1813. * @private
  1814. * @return {?}
  1815. */
  1816. function () {
  1817. // set default value:
  1818. this._gridSize = { x: 1, y: 1 };
  1819. if (this.rzGrid) {
  1820. if (typeof this.rzGrid === 'number') {
  1821. this._gridSize = { x: this.rzGrid, y: this.rzGrid };
  1822. }
  1823. else if (Array.isArray(this.rzGrid)) {
  1824. this._gridSize = { x: this.rzGrid[0], y: this.rzGrid[1] };
  1825. }
  1826. }
  1827. };
  1828. AngularResizableDirective.decorators = [
  1829. { type: Directive, args: [{
  1830. selector: '[ngResizable]',
  1831. exportAs: 'ngResizable'
  1832. },] }
  1833. ];
  1834. /** @nocollapse */
  1835. AngularResizableDirective.ctorParameters = function () { return [
  1836. { type: ElementRef },
  1837. { type: Renderer2 }
  1838. ]; };
  1839. AngularResizableDirective.propDecorators = {
  1840. ngResizable: [{ type: Input }],
  1841. rzHandles: [{ type: Input }],
  1842. rzAspectRatio: [{ type: Input }],
  1843. rzContainment: [{ type: Input }],
  1844. rzGrid: [{ type: Input }],
  1845. rzMinWidth: [{ type: Input }],
  1846. rzMinHeight: [{ type: Input }],
  1847. rzMaxWidth: [{ type: Input }],
  1848. rzMaxHeight: [{ type: Input }],
  1849. preventDefaultEvent: [{ type: Input }],
  1850. rzStart: [{ type: Output }],
  1851. rzResizing: [{ type: Output }],
  1852. rzStop: [{ type: Output }]
  1853. };
  1854. return AngularResizableDirective;
  1855. }());
  1856. /**
  1857. * @fileoverview added by tsickle
  1858. * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1859. */
  1860. var AngularDraggableModule = /** @class */ (function () {
  1861. function AngularDraggableModule() {
  1862. }
  1863. AngularDraggableModule.decorators = [
  1864. { type: NgModule, args: [{
  1865. imports: [],
  1866. declarations: [
  1867. AngularDraggableDirective,
  1868. AngularResizableDirective
  1869. ],
  1870. exports: [
  1871. AngularDraggableDirective,
  1872. AngularResizableDirective
  1873. ]
  1874. },] }
  1875. ];
  1876. return AngularDraggableModule;
  1877. }());
  1878. /**
  1879. * @fileoverview added by tsickle
  1880. * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1881. */
  1882. /**
  1883. * @fileoverview added by tsickle
  1884. * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  1885. */
  1886. export { AngularDraggableDirective, AngularResizableDirective, AngularDraggableModule, Position };
  1887. //# sourceMappingURL=angular2-draggable.js.map