Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle escape function (working TypeScript code included) #16

Open
atsu85 opened this issue Jul 8, 2015 · 1 comment
Open

handle escape function (working TypeScript code included) #16

atsu85 opened this issue Jul 8, 2015 · 1 comment

Comments

@atsu85
Copy link

atsu85 commented Jul 8, 2015

i'd like to get a callback when escape function is called. I'll provide my implementation bellow:

  1. I'm using it as fowollows
    <modal showing.bind="showing" escape-callback.call="escapeCallback()">

  2. i changed modal.js so that it

  • accepts bindable escapeCallback
  • registers a keyboard handler when modal is shown ((attached and showing) or state changes to showing = true):
  • unregisters keyboard handler when modal is hidden or detached

My implementation is following (My source is in TypeScript instead of ES6 that is used in this project - but it is quite strait-forward to port it back to ES6):

import {inject, customElement, bindable} from 'aurelia-framework';
import * as $ from 'jquery';

@customElement('modal')
@inject(Element)
export class Modal {
  @bindable showing = false;
  @bindable escapeCallback: () => void;
  private keydownHandler;
  private modal: Element;
  constructor(private element: Element) {
  }

  attached(){
    $(this.modal).modal({show: false});
    if(this.showing) {
      this.addEscHandler();
    }
  }

  detached() {
    this.removeEscHandler();
  }

  showingChanged(newValue){
    if (newValue) {
      this.addEscHandler();
      $(this.modal).modal('show')
    } else {
      $(this.modal).modal('hide')
      this.removeEscHandler();
    }
  }

  addEscHandler() {
    if(this.escapeCallback && !this.keydownHandler) {
      this.keydownHandler = (e: JQueryEventObject) => {
        if(e.which == 27) { // escape was pressed
          this.escapeCallback();
        }
      };
      $(this.modal).on("keydown.dismiss.bs.modal", this.keydownHandler);
    }
  }

  removeEscHandler() {
    if(this.keydownHandler) {
      $(this.modal).off("keydown.dismiss.bs.modal", this.escapeCallback);
      this.keydownHandler = null;
    }
  }
}
@atsu85 atsu85 changed the title handle escape function handle escape function (working TypeScript code included) Jul 9, 2015
atsu85 pushed a commit to atsu85/aurelia-bs-modal that referenced this issue Oct 14, 2015
…the modal using `<modal escape-callback.call="customEscapeCallback()">`
atsu85 pushed a commit to atsu85/aurelia-bs-modal that referenced this issue Oct 18, 2015
…the modal using `<modal escape-callback.call="customEscapeCallback()">`
@atsu85
Copy link
Author

atsu85 commented Oct 18, 2015

to resolve this issue, please merge my pull request #34 and run gulp build to update dist folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant