angular2-draggable.js 49 KB

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