dragula.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import {
  2. Directive,
  3. Input,
  4. ElementRef,
  5. OnInit,
  6. OnChanges,
  7. SimpleChange
  8. } from '@angular/core';
  9. import {DragulaService} from '../providers/dragula';
  10. import * as dragula from 'dragula';
  11. @Directive({
  12. selector: '[dragula]'
  13. })
  14. export class Dragula implements OnInit, OnChanges {
  15. @Input('dragula') bag: string;
  16. @Input() dragulaModel: any;
  17. private container: any;
  18. private drake: any;
  19. constructor(private el: ElementRef, private dragulaService: DragulaService) {
  20. this.container = el.nativeElement;
  21. }
  22. ngOnInit() {
  23. // console.log(this.bag);
  24. let bag = this.dragulaService.find(this.bag);
  25. let checkModel = () => {
  26. if (this.dragulaModel) {
  27. if (this.drake.models) {
  28. this.drake.models.push(this.dragulaModel);
  29. } else {
  30. this.drake.models = [this.dragulaModel];
  31. }
  32. }
  33. };
  34. if (bag) {
  35. this.drake = bag.drake;
  36. checkModel();
  37. this.drake.containers.push(this.container);
  38. } else {
  39. this.drake = dragula({
  40. containers: [this.container]
  41. });
  42. checkModel();
  43. this.dragulaService.add(this.bag, this.drake);
  44. }
  45. }
  46. ngOnChanges(changes: {[propName: string]: SimpleChange}) {
  47. // console.log('dragula.directive: ngOnChanges');
  48. // console.log(changes);
  49. if (changes && changes['dragulaModel']) {
  50. if (this.drake) {
  51. if (this.drake.models) {
  52. let modelIndex = this.drake.models.indexOf(changes['dragulaModel'].previousValue);
  53. this.drake.models.splice(modelIndex, 1, changes['dragulaModel'].currentValue);
  54. } else {
  55. this.drake.models = [changes['dragulaModel'].currentValue];
  56. }
  57. }
  58. }
  59. }
  60. }