Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | import {IXosConfirmConfig} from './confirm'; |
| 19 | |
| 20 | export interface IXosConfirm { |
Max Chu | 7d32b1e | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 21 | modalInstance: any; |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 22 | open(config: IXosConfirmConfig) : void; |
Max Chu | 7d32b1e | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 23 | close(cb: any) : void; |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 24 | dismiss() : void; |
| 25 | } |
| 26 | |
| 27 | export class XosConfirm implements IXosConfirm { |
| 28 | |
Max Chu | 7d32b1e | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 29 | static $inject = ['$uibModal', '$log']; |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 30 | public modalInstance; |
| 31 | |
| 32 | constructor( |
| 33 | private $uibModal : any, |
Max Chu | 7d32b1e | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 34 | private $log: ng.ILogService, |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 35 | ) { |
| 36 | |
| 37 | } |
| 38 | |
| 39 | public open(config: IXosConfirmConfig) { |
Max Chu | 7d32b1e | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 40 | this.$log.debug('[XosConfirm] called open'); |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 41 | this.modalInstance = this.$uibModal.open({ |
Max Chu | 7d32b1e | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 42 | keyboard: true, |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 43 | component: 'xosConfirm', |
| 44 | backdrop: 'static', |
| 45 | resolve: { |
| 46 | config: () => config |
| 47 | } |
| 48 | }); |
Max Chu | 7d32b1e | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 49 | |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 50 | return this.modalInstance; |
| 51 | } |
| 52 | |
Max Chu | 7d32b1e | 2017-08-25 15:51:18 -0700 | [diff] [blame] | 53 | public close(cb: any) { |
| 54 | // check if model instance exists |
| 55 | if (angular.isUndefined(this.modalInstance)) { |
| 56 | this.$log.debug('[XosConfirm] called close without a modalInstance'); |
| 57 | return; |
| 58 | } |
Max Chu | 886c89f | 2017-08-24 15:44:27 -0700 | [diff] [blame] | 59 | cb() |
| 60 | .then(() => { |
| 61 | this.modalInstance.close(); |
| 62 | }) |
| 63 | .catch((err) => { |
| 64 | this.modalInstance.dismiss(err); |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | public dismiss() { |
| 69 | this.modalInstance.dismiss(); |
| 70 | } |
| 71 | |
| 72 | } |
| 73 | |