Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Step 3.16: Add input directive
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Directive } from 'angular-ecmascript/module-helpers'; | ||
|
||
export default class InputDirective extends Directive { | ||
constructor() { | ||
super(...arguments); | ||
|
||
this.restrict = 'E'; | ||
|
||
this.scope = { | ||
'returnClose': '=', | ||
'onReturn': '&', | ||
'onFocus': '&', | ||
'onBlur': '&' | ||
}; | ||
} | ||
|
||
link(scope, element) { | ||
element.bind('focus', (e) => { | ||
if (!scope.onFocus) return; | ||
|
||
this.$timeout(() => { | ||
scope.onFocus(); | ||
}); | ||
}); | ||
|
||
element.bind('blur', (e) => { | ||
if (!scope.onBlur) return; | ||
|
||
this.$timeout(() => { | ||
scope.onBlur(); | ||
}); | ||
}); | ||
|
||
element.bind('keydown', (e) => { | ||
if (e.which != 13) return; | ||
|
||
if (scope.returnClose) { | ||
element[0].blur(); | ||
} | ||
|
||
if (scope.onReturn) { | ||
this.$timeout(() => { | ||
scope.onReturn(); | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
InputDirective.$name = 'input'; | ||
InputDirective.$inject = ['$timeout']; |