├── .gitignore ├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── angular-cli.json ├── bower.json ├── karma.conf.js ├── package.json ├── src ├── confirm │ ├── angular-confirm.ts │ ├── confirm.component.spec.ts │ ├── confirm.component.ts │ └── confirm.service.ts ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── index.html ├── main.ts ├── polyfills.ts ├── test.ts ├── tsconfig.json └── typings.d.ts └── tslint.json /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | 7 | # dependencies 8 | /node_modules 9 | /bower_components 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | *.launch 16 | .settings/ 17 | 18 | # misc 19 | /.sass-cache 20 | /connect.lock 21 | /coverage/* 22 | /libpeerconnection.log 23 | npm-debug.log 24 | testem.log 25 | /typings 26 | 27 | # e2e 28 | /e2e/*.js 29 | /e2e/*.map 30 | 31 | #System Files 32 | .DS_Store 33 | Thumbs.db 34 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4.5.0" 4 | before_script: 5 | - npm install -g angular-cli@1.0.0-beta.15 6 | before_install: 7 | - if [[ `npm -v` != 3* ]]; then npm i -g npm@3; fi 8 | - export CHROME_BIN=chromium-browser 9 | - export DISPLAY=:99.0 10 | - sh -e /etc/init.d/xvfb start -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2015 James Kleeh 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | angular-confirm 2 | =============== 3 | [![Build Status](https://travis-ci.org/jameskleeh/angular-confirm.svg?branch=master)](https://travis-ci.org/jameskleeh/angular-confirm) 4 | 5 | Confirm modal dialog for Angular 2! 6 | 7 | The AngularJS version is maintained in the [1.2.x branch](https://github.com/jameskleeh/angular-confirm/tree/1.2.x) 8 | 9 | See [demo](http://jameskleeh.github.io/angular-confirm/angular2) 10 | 11 | ## Install 12 | This module requires the Ng-Bootstrap [NgbModal](https://ng-bootstrap.github.io/#/components/modal). The default template uses styles from [Bootstrap](http://v4-alpha.getbootstrap.com/) 4.x. 13 | 14 | To install with [npm](https://www.npmjs.com/package/angular-confirm): 15 | ``` 16 | npm install --save angular-confirm 17 | ``` 18 | 19 | Then simply add a dependency to the `angular-confirm` module: 20 | 21 | ```javascript 22 | import {ConfirmModule} from 'angular-confirm'; 23 | 24 | @NgModule({ 25 | ... 26 | imports: [ConfirmModule, ...], 27 | ... 28 | }) 29 | export class AppModule { 30 | } 31 | ``` 32 | -------------------------------------------------------------------------------- /angular-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "project": { 3 | "version": "1.0.0-beta.14", 4 | "name": "angular-confirm" 5 | }, 6 | "apps": [ 7 | { 8 | "root": "src", 9 | "outDir": "dist", 10 | "main": "main.ts", 11 | "test": "test.ts", 12 | "assets": "assets", 13 | "index": "index.html", 14 | "tsconfig": "tsconfig.json", 15 | "prefix": "confirm", 16 | "mobile": false, 17 | "styles": [], 18 | "scripts": [], 19 | "environments": { 20 | "source": "environments/environment.ts", 21 | "dev": "environments/environment.ts", 22 | "prod": "environments/environment.prod.ts" 23 | } 24 | } 25 | ], 26 | "addons": [], 27 | "packages": [], 28 | "test": { 29 | "karma": { 30 | "config": "./karma.conf.js" 31 | } 32 | }, 33 | "defaults": { 34 | "styleExt": "css", 35 | "prefixInterfaces": false 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-confirm-modal", 3 | "main": "angular-confirm.js", 4 | "version": "1.2.6", 5 | "ignore": [ 6 | "test", 7 | "package.json", 8 | "LICENSE", 9 | ".gitignore", 10 | ".npmignore", 11 | "*.md", 12 | ".travis.yml", 13 | "karma.conf.js", 14 | "angular-cli.json" 15 | ], 16 | "homepage": "https://github.com/Schlogen/angular-confirm", 17 | "authors": [ 18 | "James Kleeh" 19 | ], 20 | "description": "Confirmation modal dialog for AngularJS", 21 | "license": "Apache-2.0" 22 | } 23 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/0.13/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', 'angular-cli'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-remap-istanbul'), 12 | require('angular-cli/plugins/karma') 13 | ], 14 | files: [ 15 | { pattern: './src/test.ts', watched: false } 16 | ], 17 | preprocessors: { 18 | './src/test.ts': ['angular-cli'] 19 | }, 20 | remapIstanbulReporter: { 21 | reports: { 22 | html: 'coverage', 23 | lcovonly: './coverage/coverage.lcov' 24 | } 25 | }, 26 | angularCli: { 27 | config: './angular-cli.json', 28 | environment: 'dev' 29 | }, 30 | reporters: ['progress', 'karma-remap-istanbul'], 31 | port: 9876, 32 | colors: true, 33 | logLevel: config.LOG_INFO, 34 | autoWatch: true, 35 | browsers: ['Chrome'], 36 | singleRun: false 37 | }); 38 | }; 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-confirm", 3 | "version": "2.0.0-alpha.1", 4 | "description": "Angular Confirm Modal", 5 | "main": "dist/main.bundle.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/Schlogen/angular-confirm.git" 9 | }, 10 | "keywords": [ 11 | "angular", 12 | "confirm", 13 | "angular-ui", 14 | "$modal" 15 | ], 16 | "author": "James Kleeh", 17 | "license": "Apache-2.0", 18 | "bugs": { 19 | "url": "https://github.com/Schlogen/angular-confirm/issues" 20 | }, 21 | "homepage": "https://github.com/Schlogen/angular-confirm", 22 | "angular-cli": {}, 23 | "scripts": { 24 | "lint": "tslint \"src/**/*.ts\"", 25 | "test": "ng test" 26 | }, 27 | "dependencies": { 28 | "@angular/common": "2.0.0", 29 | "@angular/compiler": "2.0.0", 30 | "@angular/core": "2.0.0", 31 | "@angular/forms": "2.0.0", 32 | "@angular/http": "2.0.0", 33 | "@angular/platform-browser": "2.0.0", 34 | "@angular/platform-browser-dynamic": "2.0.0", 35 | "@angular/router": "3.0.0", 36 | "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.4", 37 | "core-js": "^2.4.1", 38 | "rxjs": "5.0.0-beta.12", 39 | "ts-helpers": "^1.1.1", 40 | "zone.js": "^0.6.23" 41 | }, 42 | "devDependencies": { 43 | "@types/jasmine": "^2.2.30", 44 | "angular-cli": "^1.0.0-beta.15", 45 | "codelyzer": "~0.0.26", 46 | "jasmine-core": "2.4.1", 47 | "jasmine-spec-reporter": "2.5.0", 48 | "karma": "1.2.0", 49 | "karma-chrome-launcher": "^2.0.0", 50 | "karma-cli": "^1.0.1", 51 | "karma-jasmine": "^1.0.2", 52 | "karma-remap-istanbul": "^0.2.1", 53 | "ts-node": "1.2.1", 54 | "tslint": "3.13.0", 55 | "typescript": "2.0.2" 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/confirm/angular-confirm.ts: -------------------------------------------------------------------------------- 1 | import {NgModule} from '@angular/core'; 2 | import {NgbModule} from '@ng-bootstrap/ng-bootstrap'; 3 | import {ConfirmService} from "./confirm.service"; 4 | import {ConfirmComponent} from "./confirm.component"; 5 | 6 | /* 7 | * angular-confirm 8 | * https://github.com/Schlogen/angular-confirm 9 | * @version v2.0.0-beta.1 - 2016-09-20 10 | * @license Apache 11 | */ 12 | @NgModule({ 13 | declarations: [ConfirmComponent], 14 | providers: [ConfirmService], 15 | imports: [NgbModule] 16 | }) 17 | export class AngularConfirm { 18 | 19 | } -------------------------------------------------------------------------------- /src/confirm/confirm.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | import { ConfirmComponent } from './confirm.component'; 3 | import {NgbModalOptions} from '@ng-bootstrap/ng-bootstrap'; 4 | import {ConfirmService} from './confirm.service'; 5 | import {TemplateRef} from '@angular/core'; 6 | 7 | describe('Component: ConfirmComponent', () => { 8 | 9 | let component: ConfirmComponent; 10 | 11 | const confirmService = { 12 | confirm: (template: string | TemplateRef, options ?: NgbModalOptions): Promise => { 13 | return new Promise((resolve, reject) => { 14 | 15 | }); 16 | } 17 | }; 18 | 19 | beforeEach(() => { 20 | TestBed.configureTestingModule({ 21 | declarations: [ 22 | ConfirmComponent 23 | ], 24 | providers: [ 25 | {provide: ConfirmService, useValue: confirmService}, 26 | ], 27 | }); 28 | 29 | let fixture = TestBed.createComponent(ConfirmComponent); 30 | component = fixture.debugElement.componentInstance; 31 | }); 32 | 33 | it('should create the component', async(() => { 34 | expect(component).toBeTruthy(); 35 | })); 36 | 37 | 38 | 39 | }); 40 | 41 | /* 42 | 43 | 44 | describe('angular-confirm', function() { 45 | 46 | var $rootScope, $uibModal; 47 | 48 | beforeEach(angular.mock.module('angular-confirm', function ($provide) { 49 | 50 | $provide.decorator('$uibModal', function($delegate) { 51 | $uibModal = { 52 | open: jasmine.createSpy('$uibModal.open', function(settings) { 53 | return {result: settings}; 54 | }) 55 | }; 56 | return $uibModal; 57 | }); 58 | 59 | $provide.decorator('$confirm', function($delegate) { 60 | return jasmine.createSpy('$confirm', $delegate); 61 | }); 62 | 63 | })); 64 | 65 | beforeEach(angular.mock.inject(function (_$rootScope_) { 66 | $rootScope = _$rootScope_; 67 | })); 68 | 69 | describe('ConfirmModalController', function() { 70 | var $scope, controller, data = {testVal: 1}, $uibModalInstance; 71 | 72 | beforeEach(angular.mock.inject(function($controller) { 73 | $scope = $rootScope.$new(); 74 | $uibModalInstance = { 75 | close: jasmine.createSpy('$uibModalInstance.close'), 76 | dismiss: jasmine.createSpy('$uibModalInstance.dismiss'), 77 | result: { 78 | then: jasmine.createSpy('$uibModalInstance.result.then') 79 | } 80 | }; 81 | controller = $controller('ConfirmModalController', {"$scope": $scope, "$uibModalInstance": $uibModalInstance, "data": data}); 82 | })); 83 | 84 | it("should copy the data, not use it as a reference", function() { 85 | data.testVal = 2; 86 | expect($scope.data.testVal).toEqual(1); 87 | }); 88 | 89 | it("should call close when $scope.ok is invoked", function() { 90 | $scope.ok(); 91 | expect($uibModalInstance.close).toHaveBeenCalled(); 92 | }); 93 | 94 | it("should call dismiss when $scope.cancel is invoked", function() { 95 | $scope.cancel(); 96 | expect($uibModalInstance.dismiss).toHaveBeenCalledWith('cancel'); 97 | }); 98 | 99 | }); 100 | 101 | describe('$confirm factory', function() { 102 | 103 | var $confirm, $confirmModalDefaults; 104 | 105 | beforeEach(angular.mock.inject(function(_$confirm_, _$confirmModalDefaults_) { 106 | $confirm = _$confirm_; 107 | $confirm.and.callThrough(); 108 | $confirmModalDefaults = _$confirmModalDefaults_; 109 | $uibModal.open.and.callThrough(); 110 | })); 111 | 112 | it("should call $uibModal.open", function() { 113 | $confirm(); 114 | expect($uibModal.open).toHaveBeenCalled(); 115 | }); 116 | 117 | it("should override the defaults with settings passed in", function() { 118 | var settings = $confirm({}, {"template": "hello"}); 119 | expect(settings.template).toEqual("hello"); 120 | }); 121 | 122 | it("should not change the defaults", function() { 123 | var settings = $confirm({}, {"templateUrl": "hello"}); 124 | expect(settings.templateUrl).toEqual("hello"); 125 | expect(settings.template).not.toBeDefined(); 126 | expect($confirmModalDefaults.template).toBeDefined(); 127 | expect($confirmModalDefaults.templateUrl).not.toBeDefined(); 128 | }); 129 | 130 | it("should override the default labels with the data passed in", function() { 131 | var settings = $confirm({title: "Title"}); 132 | var data = settings.resolve.data(); 133 | expect(data.title).toEqual("Title"); 134 | expect(data.ok).toEqual('OK'); 135 | }); 136 | 137 | it("should remove template if templateUrl is passed in", function() { 138 | var settings = $confirm({}, {templateUrl: "abc.txt"}); 139 | expect(settings.template).not.toBeDefined(); 140 | }); 141 | 142 | it("should use default template", function(){ 143 | var settings = $confirm({}, {}); 144 | expect(settings.templateUrl).toEqual($confirmModalDefaults.templateUrl) 145 | }); 146 | 147 | it("should use override template", function(){ 148 | var settings = $confirm({}, {templateUrl: 'something'}); 149 | expect(settings.templateUrl).not.toEqual($confirmModalDefaults.templateUrl) 150 | expect(settings.templateUrl).toEqual('something'); 151 | }); 152 | 153 | it("should use custom template if found", function(){ 154 | $confirmModalDefaults.additionalTemplates.danger = {templateUrl: "something"} 155 | var settings = $confirm({templateName: 'random'}, {}); 156 | expect(settings.templateUrl).toEqual($confirmModalDefaults.templateUrl); 157 | 158 | settings = $confirm({templateName: 'danger'}, {}); 159 | expect(settings.templateUrl).not.toEqual($confirmModalDefaults.templateUrl); 160 | expect(settings.templateUrl).toEqual('something'); 161 | 162 | settings = $confirm({templateName: 'danger'}, {templateUrl: 'thiswillbeignored'}); 163 | expect(settings.templateUrl).not.toEqual($confirmModalDefaults.templateUrl); 164 | expect(settings.templateUrl).toEqual('something'); 165 | }) 166 | 167 | }); 168 | 169 | describe('confirm directive', function() { 170 | var $scope, element, $confirm, data, $timeout; 171 | 172 | beforeEach(angular.mock.inject(function (_$confirm_, _$timeout_) { 173 | $confirm = _$confirm_; 174 | $timeout = _$timeout_; 175 | 176 | $confirm.and.callFake(function(d) { 177 | data = d; 178 | return {then: function() {}} 179 | }); 180 | 181 | $scope = $rootScope.$new(); 182 | $scope.click = jasmine.createSpy('$scope.click'); 183 | })); 184 | 185 | describe('resolve properties in title', function() { 186 | beforeEach(angular.mock.inject(function($compile) { 187 | $scope.name = 'Joe'; 188 | element = angular.element(''); 189 | $compile(element)($scope); 190 | $scope.$digest(); 191 | })); 192 | 193 | it("should resolve the name to the text property", function() { 194 | element.triggerHandler('click'); 195 | $timeout.flush(); 196 | expect(data.text).toEqual('Are you sure, Joe?'); 197 | }); 198 | }); 199 | 200 | describe('without confirmIf', function() { 201 | 202 | beforeEach(angular.mock.inject(function($compile) { 203 | element = angular.element(''); 204 | $compile(element)($scope); 205 | $scope.$digest(); 206 | })); 207 | 208 | it("should call confirm on click and not call the function", function() { 209 | element.triggerHandler('click'); 210 | $timeout.flush(); 211 | expect($scope.click).not.toHaveBeenCalled(); 212 | expect($confirm).toHaveBeenCalled(); 213 | }); 214 | 215 | }); 216 | 217 | describe('with confirmIf option', function() { 218 | 219 | beforeEach(angular.mock.inject(function($compile) { 220 | element = angular.element(''); 221 | $compile(element)($scope); 222 | $scope.$digest(); 223 | })); 224 | 225 | it("should call confirm on click and not call the function", function() { 226 | $scope.truthy = true; 227 | $scope.$apply(); 228 | element.triggerHandler('click'); 229 | $timeout.flush(); 230 | expect($scope.click).not.toHaveBeenCalled(); 231 | expect($confirm).toHaveBeenCalled(); 232 | }); 233 | 234 | it("should call the function", function() { 235 | $scope.truthy = false; 236 | $scope.$apply(); 237 | element.triggerHandler('click'); 238 | $timeout.flush(); 239 | expect($scope.click).toHaveBeenCalled(); 240 | expect($confirm).not.toHaveBeenCalled(); 241 | }); 242 | 243 | }); 244 | 245 | describe('with confirmTitle option', function() { 246 | beforeEach(angular.mock.inject(function($compile) { 247 | $scope.name = 'Joe'; 248 | element = angular.element(''); 249 | $compile(element)($scope); 250 | $scope.$digest(); 251 | })); 252 | 253 | it("should resolve the confirmTitle to the title property", function() { 254 | element.triggerHandler('click'); 255 | $timeout.flush(); 256 | expect(data.title).toEqual('Hello, Joe!'); 257 | }); 258 | 259 | }); 260 | 261 | describe('with confirmOk option', function() { 262 | beforeEach(angular.mock.inject(function($compile) { 263 | $scope.name = 'Joe'; 264 | element = angular.element(''); 265 | $compile(element)($scope); 266 | $scope.$digest(); 267 | })); 268 | 269 | it("should resolve the confirmTitle to the title property", function() { 270 | element.triggerHandler('click'); 271 | $timeout.flush(); 272 | expect(data.ok).toEqual('Okie Dokie, Joe!'); 273 | }); 274 | }); 275 | 276 | describe('with confirmCancel option', function() { 277 | beforeEach(angular.mock.inject(function($compile) { 278 | $scope.name = 'Joe'; 279 | element = angular.element(''); 280 | $compile(element)($scope); 281 | $scope.$digest(); 282 | })); 283 | 284 | it("should resolve the confirmTitle to the title property", function() { 285 | element.triggerHandler('click'); 286 | $timeout.flush(); 287 | expect(data.cancel).toEqual('No Way, Joe!'); 288 | }); 289 | }); 290 | 291 | describe('with confirmSettings option', function() { 292 | beforeEach(angular.mock.inject(function($compile) { 293 | $scope.settings = {name: 'Joe'}; 294 | element = angular.element(''); 295 | $compile(element)($scope); 296 | $scope.$digest(); 297 | })); 298 | 299 | it("should pass the settings to $confirm", function() { 300 | element.triggerHandler('click'); 301 | $timeout.flush(); 302 | expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, $scope.settings) 303 | }); 304 | }); 305 | 306 | describe('with confirmSettings option direct entry', function() { 307 | beforeEach(angular.mock.inject(function($compile) { 308 | element = angular.element(''); 309 | $compile(element)($scope); 310 | $scope.$digest(); 311 | })); 312 | 313 | it("should pass the settings to $confirm", function() { 314 | element.triggerHandler('click'); 315 | $timeout.flush(); 316 | expect($confirm).toHaveBeenCalledWith({text: "Are you sure?"}, {name: "Joe"}) 317 | }); 318 | }); 319 | 320 | describe('with confirmTemplateName option', function() { 321 | beforeEach(angular.mock.inject(function($compile) { 322 | element = angular.element(''); 323 | $compile(element)($scope); 324 | $scope.$digest(); 325 | })); 326 | 327 | it("should use custom template", function() { 328 | element.triggerHandler('click'); 329 | $timeout.flush(); 330 | expect($confirm).toHaveBeenCalledWith({text: "Are you sure?", templateName: "danger"}, {}) 331 | }); 332 | }); 333 | 334 | 335 | describe('with checkbox', function() { 336 | 337 | beforeEach(angular.mock.inject(function($compile) { 338 | element = angular.element(''); 339 | $compile(element)($scope); 340 | $scope.$digest(); 341 | })); 342 | 343 | it("should call confirm on click and not call the function", function() { 344 | element.triggerHandler('click'); 345 | $timeout.flush(); 346 | expect($scope.click).not.toHaveBeenCalled(); 347 | expect($confirm).toHaveBeenCalled(); 348 | expect(element[0].checked).toBe(false); 349 | }); 350 | 351 | }); 352 | 353 | describe('with checkbox and confirm if false', function() { 354 | 355 | beforeEach(angular.mock.inject(function($compile) { 356 | element = angular.element(''); 357 | $compile(element)($scope); 358 | $scope.$digest(); 359 | })); 360 | 361 | it("should set the checkbox to checked", function() { 362 | expect($scope.checked).not.toBeDefined(); 363 | expect(element[0].checked).toBe(false); 364 | $scope.truthy = false; 365 | $scope.$apply(); 366 | element.triggerHandler('click'); 367 | $timeout.flush(); 368 | expect($scope.click).toHaveBeenCalled(); 369 | expect($confirm).not.toHaveBeenCalled(); 370 | expect(element[0].checked).toBe(true); 371 | expect($scope.checked).toBe(true); 372 | }); 373 | }); 374 | 375 | describe('with checkbox already checked and confirm if false', function() { 376 | 377 | beforeEach(angular.mock.inject(function($compile) { 378 | $scope.checked = true; 379 | element = angular.element(''); 380 | $compile(element)($scope); 381 | $scope.$digest(); 382 | })); 383 | 384 | it("should set the checkbox to checked", function() { 385 | expect($scope.checked).toBe(true); 386 | expect(element[0].checked).toBe(true); 387 | $scope.truthy = false; 388 | $scope.$apply(); 389 | element.triggerHandler('click'); 390 | $timeout.flush(); 391 | expect($scope.click).toHaveBeenCalled(); 392 | expect($confirm).not.toHaveBeenCalled(); 393 | expect(element[0].checked).toBe(false); 394 | expect($scope.checked).toBe(false); 395 | }); 396 | }); 397 | 398 | describe('with checkbox and confirm if false and ng-true-value/ng-false-value', function() { 399 | 400 | beforeEach(angular.mock.inject(function($compile) { 401 | element = angular.element(''); 402 | $compile(element)($scope); 403 | $scope.$digest(); 404 | })); 405 | 406 | it("should set the checkbox to checked", function() { 407 | expect($scope.checked).not.toBeDefined(); 408 | expect(element[0].checked).toBe(false); 409 | $scope.truthy = false; 410 | $scope.$apply(); 411 | element.triggerHandler('click'); 412 | $timeout.flush(); 413 | expect($scope.click).toHaveBeenCalled(); 414 | expect($confirm).not.toHaveBeenCalled(); 415 | expect(element[0].checked).toBe(true); 416 | expect($scope.checked).toBe("YES"); 417 | }); 418 | }); 419 | 420 | describe('with checkbox without ngModel and confirm if false', function() { 421 | 422 | beforeEach(angular.mock.inject(function($compile) { 423 | element = angular.element(''); 424 | $compile(element)($scope); 425 | $scope.$digest(); 426 | })); 427 | 428 | it("should set the checkbox to checked", function() { 429 | expect(element[0].checked).toBe(false); 430 | $scope.truthy = false; 431 | $scope.$apply(); 432 | element.triggerHandler('click'); 433 | $timeout.flush(); 434 | expect($scope.click).toHaveBeenCalled(); 435 | expect($confirm).not.toHaveBeenCalled(); 436 | expect(element[0].checked).toBe(true); 437 | }); 438 | }); 439 | 440 | describe('with checkbox without ngModel already checked and confirm if false', function() { 441 | 442 | beforeEach(angular.mock.inject(function($compile) { 443 | element = angular.element(''); 444 | $compile(element)($scope); 445 | $scope.$digest(); 446 | })); 447 | 448 | it("should set the checkbox to checked", function() { 449 | expect(element[0].checked).toBe(true); 450 | $scope.truthy = false; 451 | $scope.$apply(); 452 | element.triggerHandler('click'); 453 | $timeout.flush(); 454 | expect($scope.click).toHaveBeenCalled(); 455 | expect($confirm).not.toHaveBeenCalled(); 456 | expect(element[0].checked).toBe(false); 457 | }); 458 | }); 459 | 460 | }); 461 | 462 | 463 | 464 | }); 465 | 466 | */ 467 | -------------------------------------------------------------------------------- /src/confirm/confirm.component.ts: -------------------------------------------------------------------------------- 1 | import {ElementRef, Component, TemplateRef, ViewChild} from '@angular/core'; 2 | import {ConfirmService} from './confirm.service'; 3 | import {Input} from '@angular/core/src/metadata/directives'; 4 | 5 | @Component({ 6 | selector: '[confirm]', 7 | template: ` 8 | 23 | 24 | `, 25 | }) 26 | export class ConfirmComponent { 27 | 28 | @Input() confirmOk: string = 'Ok'; 29 | @Input() confirmCancel: string = 'Cancel'; 30 | @Input() confirmTitle: string = 'Confirm'; 31 | @Input() confirm: string; 32 | @Input() confirmIf: boolean = true; 33 | @Input() confirmSettings: Object = {}; 34 | @Input() confirmTemplate: string | TemplateRef = null; 35 | 36 | @ViewChild(TemplateRef) template: TemplateRef; 37 | 38 | constructor(private el: ElementRef, private confirmService: ConfirmService) { 39 | let element: HTMLElement = el.nativeElement; 40 | let oldAddEventListener: Function = element.addEventListener; 41 | let events: Object[] = []; 42 | 43 | function success(clickEvent) { 44 | events.forEach((evt: any) => { 45 | evt.listener(clickEvent); 46 | }); 47 | } 48 | 49 | element.addEventListener('click', (event) => { 50 | if (this.confirmIf) { 51 | confirmService.confirm(this.confirmTemplate || this.template, this.confirmSettings).then(() => { 52 | success(event); 53 | }); 54 | } else { 55 | success(event); 56 | } 57 | }); 58 | 59 | element.addEventListener = function(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean) { 60 | if (type === 'click') { 61 | events.push({type: type, listener: listener, useCapture: useCapture}); 62 | } else { 63 | oldAddEventListener(type, listener, useCapture); 64 | } 65 | }; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/confirm/confirm.service.ts: -------------------------------------------------------------------------------- 1 | import {Injectable, TemplateRef} from '@angular/core'; 2 | import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap'; 3 | 4 | @Injectable() 5 | export class ConfirmService { 6 | 7 | constructor(private modalService: NgbModal) {} 8 | 9 | confirm(template: string | TemplateRef, options ?: NgbModalOptions): Promise { 10 | return this.modalService.open(template, options).result; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // The file contents for the current environment will overwrite these during build. 2 | // The build system defaults to the dev environment which uses `environment.ts`, but if you do 3 | // `ng build --env=prod` then `environment.prod.ts` will be used instead. 4 | // The list of which env maps to which file can be found in `angular-cli.json`. 5 | 6 | export const environment = { 7 | production: false 8 | }; 9 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jameskleeh/angular-confirm/9638b0e5bc7755a996ba3eab77d241c383dd2ae1/src/index.html -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { ConfirmComponent } from './confirm/confirm.component'; 2 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | // This file includes polyfills needed by Angular 2 and is loaded before 2 | // the app. You can add your own extra polyfills to this file. 3 | import 'core-js/es6/symbol'; 4 | import 'core-js/es6/object'; 5 | import 'core-js/es6/function'; 6 | import 'core-js/es6/parse-int'; 7 | import 'core-js/es6/parse-float'; 8 | import 'core-js/es6/number'; 9 | import 'core-js/es6/math'; 10 | import 'core-js/es6/string'; 11 | import 'core-js/es6/date'; 12 | import 'core-js/es6/array'; 13 | import 'core-js/es6/regexp'; 14 | import 'core-js/es6/map'; 15 | import 'core-js/es6/set'; 16 | import 'core-js/es6/reflect'; 17 | 18 | import 'core-js/es7/reflect'; 19 | import 'zone.js/dist/zone'; 20 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | import './polyfills.ts'; 2 | 3 | import 'zone.js/dist/long-stack-trace-zone'; 4 | import 'zone.js/dist/proxy.js'; 5 | import 'zone.js/dist/sync-test'; 6 | import 'zone.js/dist/jasmine-patch'; 7 | import 'zone.js/dist/async-test'; 8 | import 'zone.js/dist/fake-async-test'; 9 | 10 | // Unfortunately there's no typing for the `__karma__` variable. Just declare it as any. 11 | declare var __karma__: any; 12 | declare var require: any; 13 | 14 | // Prevent Karma from running prematurely. 15 | __karma__.loaded = function () {}; 16 | 17 | 18 | Promise.all([ 19 | System.import('@angular/core/testing'), 20 | System.import('@angular/platform-browser-dynamic/testing') 21 | ]) 22 | // First, initialize the Angular testing environment. 23 | .then(([testing, testingBrowser]) => { 24 | testing.getTestBed().initTestEnvironment( 25 | testingBrowser.BrowserDynamicTestingModule, 26 | testingBrowser.platformBrowserDynamicTesting() 27 | ); 28 | }) 29 | // Then we find all the tests. 30 | .then(() => require.context('./', true, /\.spec\.ts/)) 31 | // And load the modules. 32 | .then(context => context.keys().map(context)) 33 | // Finally, start Karma to run the tests. 34 | .then(__karma__.start, __karma__.error); 35 | -------------------------------------------------------------------------------- /src/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "declaration": false, 4 | "emitDecoratorMetadata": true, 5 | "experimentalDecorators": true, 6 | "lib": ["es6", "dom"], 7 | "mapRoot": "./", 8 | "module": "es6", 9 | "moduleResolution": "node", 10 | "outDir": "../dist/out-tsc", 11 | "sourceMap": true, 12 | "target": "es5", 13 | "typeRoots": [ 14 | "../node_modules/@types" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/typings.d.ts: -------------------------------------------------------------------------------- 1 | // Typings reference file, see links for more information 2 | // https://github.com/typings/typings 3 | // https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html 4 | 5 | declare var System: any; 6 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "node_modules/codelyzer" 4 | ], 5 | "rules": { 6 | "class-name": true, 7 | "comment-format": [ 8 | true, 9 | "check-space" 10 | ], 11 | "curly": true, 12 | "eofline": true, 13 | "forin": true, 14 | "indent": [ 15 | true, 16 | "spaces" 17 | ], 18 | "label-position": true, 19 | "label-undefined": true, 20 | "max-line-length": [ 21 | true, 22 | 140 23 | ], 24 | "member-access": false, 25 | "member-ordering": [ 26 | true, 27 | "static-before-instance", 28 | "variables-before-functions" 29 | ], 30 | "no-arg": true, 31 | "no-bitwise": true, 32 | "no-console": [ 33 | true, 34 | "debug", 35 | "info", 36 | "time", 37 | "timeEnd", 38 | "trace" 39 | ], 40 | "no-construct": true, 41 | "no-debugger": true, 42 | "no-duplicate-key": true, 43 | "no-duplicate-variable": true, 44 | "no-empty": false, 45 | "no-eval": true, 46 | "no-inferrable-types": true, 47 | "no-shadowed-variable": true, 48 | "no-string-literal": false, 49 | "no-switch-case-fall-through": true, 50 | "no-trailing-whitespace": true, 51 | "no-unused-expression": true, 52 | "no-unused-variable": true, 53 | "no-unreachable": true, 54 | "no-use-before-declare": true, 55 | "no-var-keyword": true, 56 | "object-literal-sort-keys": false, 57 | "one-line": [ 58 | true, 59 | "check-open-brace", 60 | "check-catch", 61 | "check-else", 62 | "check-whitespace" 63 | ], 64 | "quotemark": [ 65 | true, 66 | "single" 67 | ], 68 | "radix": true, 69 | "semicolon": [ 70 | "always" 71 | ], 72 | "triple-equals": [ 73 | true, 74 | "allow-null-check" 75 | ], 76 | "typedef-whitespace": [ 77 | true, 78 | { 79 | "call-signature": "nospace", 80 | "index-signature": "nospace", 81 | "parameter": "nospace", 82 | "property-declaration": "nospace", 83 | "variable-declaration": "nospace" 84 | } 85 | ], 86 | "variable-name": false, 87 | "whitespace": [ 88 | true, 89 | "check-branch", 90 | "check-decl", 91 | "check-operator", 92 | "check-separator", 93 | "check-type" 94 | ], 95 | 96 | "directive-selector-prefix": [false, "app"], 97 | "component-selector-prefix": [false, "app"], 98 | "directive-selector-name": [true, "camelCase"], 99 | "component-selector-name": [true, "kebab-case"], 100 | "directive-selector-type": [true, "attribute"], 101 | "component-selector-type": [true, "element"], 102 | "use-input-property-decorator": true, 103 | "use-output-property-decorator": true, 104 | "use-host-property-decorator": true, 105 | "no-input-rename": true, 106 | "no-output-rename": true, 107 | "use-life-cycle-interface": true, 108 | "use-pipe-transform-interface": true, 109 | "component-class-suffix": true, 110 | "directive-class-suffix": true 111 | } 112 | } 113 | --------------------------------------------------------------------------------