1 |
|
2 | import { bindingBehavior } from 'aurelia-binding';
|
3 |
|
4 | function findOriginalEventTarget(event) {
|
5 | return (event.path && event.path[0]) || (event.deepPath && event.deepPath[0]) || event.target;
|
6 | }
|
7 |
|
8 | function handleSelfEvent(event) {
|
9 | let target = findOriginalEventTarget(event);
|
10 | if (this.target !== target) { return; }
|
11 | this.selfEventCallSource(event);
|
12 | }
|
13 |
|
14 | @bindingBehavior('self')
|
15 | export class SelfBindingBehavior {
|
16 | bind(binding, source) {
|
17 | if (!binding.callSource || !binding.targetEvent) {
|
18 | throw new Error('Self binding behavior only supports event.');
|
19 | }
|
20 | binding.selfEventCallSource = binding.callSource;
|
21 | binding.callSource = handleSelfEvent;
|
22 | }
|
23 |
|
24 | unbind(binding, source) {
|
25 | binding.callSource = binding.selfEventCallSource;
|
26 | binding.selfEventCallSource = null;
|
27 | }
|
28 | }
|