0

I have 2 slide toggles where I can want to show the status of my panels using split.js.
What I'm trying to achieve is, when I open/close a panel the property checked of my slide toggle must be set to true/false.

I have 2 public variables in my component:

  public isOverviewVisible: boolean = true;
  public isDetailsVisible: boolean = false

Which I use in my html file

  <div class="col-auto align-self-end">
    <mat-slide-toggle ngDefaultControl (change)="showOverviewChange($event)" [checked]="isOverviewVisible">
      Show overview
    </mat-slide-toggle>
  </div>
  ...

In theconstructor of the component I want to my 2 public variables via the function onDragEnd presented by split.js. The debugger steps into this method when I drag the splitter, but the variables this.isOverviewVisible and this.isDetailsVisible are not the ones I declared as public at the top of my component.

    let split = Split(['#overview', '#detail'], {
      onDragEnd: function () {
        let sizes: number[] = split.getSizes();

        if (sizes[0] < 1) {
          this.isOverviewVisible = false;
        } else {
          this.isOverviewVisible = true;
        }
        ...
      }
    });

The question is, how can I set my public variables inside the function onDragEnd?
Or maybe there is a better way?

Update
I changed my code to use the arrow function, but now I get an error on split.getSizes() : Property 'getSizes' does not exist on type '() => any'.

let split = () => Split(['#overview', '#detail'], {
  sizes: [50, 50],
  minSize: [0, 0],
  direction: 'vertical',
  onDragEnd: function () {
    let sizes: number[] = split.getSizes();
  }
});
Hypenate
  • 1,907
  • 3
  • 22
  • 38

2 Answers2

1

Try:

let split = () => Split(['#overview', '#detail'], {
      onDragEnd:  () => {
        let sizes: number[] = split.getSizes();

        if (sizes[0] < 1) {
          this.isOverviewVisible = false;
        } else {
          this.isOverviewVisible = true;
        }
        ...
      }
    });
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
1

You need to use an arrow function in your onDragEnd function so that this is the class instance:

    let split = Split(['#overview', '#detail'], {
    onDragEnd: () => {
      let sizes: number[] = split.getSizes();

      if (sizes[0] < 1) {
        this.isOverviewVisible = false;
      } else {
        this.isOverviewVisible = true;
      }
      ...
    }
});
Alen Genzić
  • 1,398
  • 10
  • 17