TwoToc code
[YouAndWeb_TwoToc] / client / components / modal / modal.service.js
1 'use strict';
2
3 angular.module('dashboardApp')
4   .factory('Modal', function ($rootScope, $modal) {
5     /**
6      * Opens a modal
7      * @param  {Object} scope      - an object to be merged with modal's scope
8      * @param  {String} modalClass - (optional) class(es) to be applied to the modal
9      * @return {Object}            - the instance $modal.open() returns
10      */
11     function openModal(scope, modalClass) {
12       var modalScope = $rootScope.$new();
13       scope = scope || {};
14       modalClass = modalClass || 'modal-default';
15
16       angular.extend(modalScope, scope);
17
18       return $modal.open({
19         templateUrl: 'components/modal/modal.html',
20         windowClass: modalClass,
21         scope: modalScope
22       });
23     }
24
25     // Public API here
26     return {
27
28       /* Confirmation modals */
29       confirm: {
30
31         /**
32          * Create a function to open a delete confirmation modal (ex. ng-click='myModalFn(name, arg1, arg2...)')
33          * @param  {Function} del - callback, ran when delete is confirmed
34          * @return {Function}     - the function to open the modal (ex. myModalFn)
35          */
36         delete: function(del) {
37           del = del || angular.noop;
38
39           /**
40            * Open a delete confirmation modal
41            * @param  {String} name   - name or info to show on modal
42            * @param  {All}           - any additional args are passed straight to del callback
43            */
44           return function() {
45             var args = Array.prototype.slice.call(arguments),
46                 name = args.shift(),
47                 deleteModal;
48
49             deleteModal = openModal({
50               modal: {
51                 dismissable: true,
52                 title: 'Confirm Delete',
53                 html: '<p>Are you sure you want to delete <strong>' + name + '</strong> ?</p>',
54                 buttons: [{
55                   classes: 'btn-danger',
56                   text: 'Delete',
57                   click: function(e) {
58                     deleteModal.close(e);
59                   }
60                 }, {
61                   classes: 'btn-default',
62                   text: 'Cancel',
63                   click: function(e) {
64                     deleteModal.dismiss(e);
65                   }
66                 }]
67               }
68             }, 'modal-danger');
69
70             deleteModal.result.then(function(event) {
71               del.apply(event, args);
72             });
73           };
74         }
75       }
76     };
77   });