Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 1 | export interface IXosAlertConfig { |
| 2 | type: string; |
| 3 | closeBtn?: boolean; |
| 4 | autoHide?: number; |
| 5 | } |
| 6 | |
| 7 | class AlertCtrl { |
| 8 | |
| 9 | static $inject = ['$timeout']; |
| 10 | |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 11 | public config: IXosAlertConfig; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 12 | public show: boolean; |
| 13 | public dismiss: () => void; |
| 14 | |
| 15 | constructor ( |
| 16 | private $timeout: ng.ITimeoutService |
| 17 | ) { |
| 18 | |
| 19 | } |
| 20 | |
| 21 | $onInit() { |
| 22 | if (!angular.isDefined(this.config)) { |
| 23 | throw new Error('[xosAlert] Please provide a configuration via the "config" attribute'); |
| 24 | } |
| 25 | |
| 26 | // default the value to true |
| 27 | this.show = this.show !== false; |
| 28 | |
| 29 | this.dismiss = () => { |
| 30 | this.show = false; |
| 31 | }; |
| 32 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 33 | if (this.config.autoHide) { |
| 34 | |
| 35 | let to = this.$timeout(() => { |
| 36 | this.dismiss(); |
| 37 | this.$timeout.cancel(to); |
| 38 | }, this.config.autoHide); |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | export const xosAlert: angular.IComponentOptions = { |
| 44 | template: require('./alert.html'), |
| 45 | controllerAs: 'vm', |
| 46 | controller: AlertCtrl, |
| 47 | transclude: true, |
| 48 | bindings: { |
| 49 | config: '=', |
| 50 | show: '=', |
| 51 | } |
| 52 | }; |