-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathangular-prompt.js
103 lines (84 loc) · 2.36 KB
/
angular-prompt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
angular.module('cgPrompt',['ui.bootstrap']);
angular.module('cgPrompt').factory('prompt',['$uibModal','$q',function($uibModal,$q){
var prompt = function(options){
var defaults = {
title: '',
message: '',
input: false,
label: '',
value: '',
values: false,
buttons: [
{label:'Cancel',cancel:true},
{label:'OK',primary:true}
]
};
if (options === undefined){
options = {};
}
for (var key in defaults) {
if (options[key] === undefined) {
options[key] = defaults[key];
}
}
var defer = $q.defer();
$uibModal.open({
templateUrl:'angular-prompt.html',
controller: 'cgPromptCtrl',
resolve: {
options:function(){
return options;
}
}
}).result.then(function(result){
if (options.input){
defer.resolve(result.input);
} else {
defer.resolve(result.button);
}
}, function(){
defer.reject();
});
return defer.promise;
};
return prompt;
}
]);
angular.module('cgPrompt').controller('cgPromptCtrl',['$scope','options','$timeout',function($scope,options,$timeout){
$scope.input = {name:options.value};
$scope.options = options;
$scope.form = {};
$scope.buttonClicked = function(button){
if (button.cancel){
$scope.$dismiss();
return;
}
if (options.input && $scope.form.cgPromptForm.$invalid){
$scope.changed = true;
return;
}
$scope.$close({button:button,input:$scope.input.name});
};
$scope.submit = function(){
var ok;
angular.forEach($scope.options.buttons,function(button){
if (button.primary){
ok = button;
}
});
if (ok){
$scope.buttonClicked(ok);
}
};
$timeout(function(){
var elem = document.querySelector('#cgPromptInput');
if (elem) {
if (elem.select) {
elem.select();
}
if (elem.focus) {
elem.focus();
}
}
},100);
}]);