Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cgross committed Apr 10, 2014
1 parent aab49d7 commit b140053
Show file tree
Hide file tree
Showing 35 changed files with 31,102 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ build/Release
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

temp
28 changes: 28 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"curly": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"sub": true,
"undef": true,
"boss": true,
"eqnull": true,
"browser": true,
"smarttabs": true,
"globals": {
"jQuery": true,
"angular": true,
"console": true,
"$": true,
"_": true,
"moment": true,
"describe": true,
"beforeEach": true,
"module": true,
"inject": true,
"it": true,
"expect": true
}
}
71 changes: 71 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';

module.exports = function (grunt) {

require('load-grunt-tasks')(grunt);

grunt.initConfig({
connect: {
main: {
options: {
port: 9001
}
}
},
watch: {
main: {
options: {
livereload: true,
livereloadOnError: false,
spawn: false
},
files: ['angular-prompt.html','angular-prompt.js','dist/**/*','demo/**/*'],
tasks: ['jshint']
}
},
jshint: {
main: {
options: {
jshintrc: '.jshintrc'
},
src: 'angular-prompt.js'
}
},
jasmine: {
unit: {
src: ['./bower_components/angular/angular.js','./bower_components/angular-bootstrap/ui-bootstrap-tpls.js','./dist/angular-prompt.js','./demo/demo.js'],
options: {
specs: 'test/*.js'
}
}
},
ngtemplates: {
main: {
options: {
module:'cgPrompt',
base:''
},
src:'angular-prompt.html',
dest: 'temp/templates.js'
}
},
concat: {
main: {
src: ['angular-prompt.js', 'temp/templates.js'],
dest: 'dist/angular-prompt.js'
}
},
uglify: {
main: {
files: [
{src:'dist/angular-prompt.js',dest:'dist/angular-prompt.min.js'}
]
}
}
});

grunt.registerTask('serve', ['jshint','connect', 'watch']);
grunt.registerTask('build',['ngtemplates','concat','uglify']);
grunt.registerTask('test',['build','jasmine']);

};
97 changes: 94 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,95 @@
angular-prompt
==============
# angular-prompt

Angular service to easily display prompt and confirmation modals
> Angular service to easily display prompt and confirmation modals.
This library depends on [angular-ui-bootstrap](https://github.com/angular-ui/bootstrap).

## Demo

[Live Demo](http://cgross.github.io/angular-prompt/demo)

## Getting Started

Install with Bower or download the the files directly from the dist folder in the repo.
```bash
bower install angular-prompt --save
```

Add `dist/angular-prompt.js` to your index.html.

Add `cgPrompt` as a module dependency for your module.

```js
angular.module('your_app', ['ui.bootstrap','cgPrompt']);
```

Now you can inject and use the `prompt` service.

```js
function MyCtrl($scope, prompt) {

//simple confirmation
prompt({
title: 'Delete this Thing?',
message: 'Are you sure you want to do that?'
}).then(function(){
//he hit ok and not cancel
});

//ask the user for a string
prompt({
title: 'Give me a name',
message: 'What would you like to name it?',
input: true,
label: 'Name',
value: 'Current name',
values: ['other','possible','names']
}).then(function(name){
//the promise is resolved with the user input
});
}
```

## API

### prompt(options);

- #### options.title
Type: `String`
Default: `''`
The title for the dialog.

- #### options.message
Type: `String`
Default: `''`
The message inside the dialog.

- #### options.input
Type: `Boolean`
Default: `false`
Set to `true` if you wish to prompt the user for a text value.

- #### options.label
Type: `String`
Default: `''`
The label for the input if `input=true`.

- #### options.value
Type: `String`
Default: `''`
The initial value of the input if `input=true`.

- #### options.values
Type: `Array` of `String`
Default: `undefined`
A list of values available in a dropdown for the user to select as the input value.

- #### options.buttons
Type: `Array` of `Object` with properties `label`,`cancel`, and `primary`
Default: `[{ label:'OK', primary: true }, { label:'Cancel', cancel: true }]`
A list of the buttons to display on the dialog.

The function returns a promise. That promise is resolved with either the button that was pressed, or in the case of input prompts, the value the user entered. If the user pressed a button where `cancel=true` or canceled the dialog another way (hit ESC, etc) then the promise is rejected.

## Release History
* v1.0.0 - Initial release.
33 changes: 33 additions & 0 deletions angular-prompt.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div>
<div class="modal-header">
<button type="button" class="close pull-right" ng-click="$dismiss()" aria-hidden="true">×</button>
<h4>{{options.title}}</h4>
</div>
<div class="modal-body">

<p ng-if="options.message">
{{options.message}}
</p>

<form id="cgPromptForm" name="cgPromptForm" ng-if="options.input" ng-submit="submit()">
<div class="form-group" ng-class="{'has-error':cgPromptForm.$invalid && changed}">
<label for="cgPromptInput">{{options.label}}</label>
<input id="cgPromptInput" type="text" class="form-control" placeholder="{{options.label}}" ng-model="input.name" required ng-change="changed=true" ng-if="!options.values || options.values.length === 0"/ autofocus="autofocus">
<div class="input-group" ng-if="options.values">
<input id="cgPromptInput" type="text" class="form-control" placeholder="{{options.label}}" ng-model="input.name" required ng-change="changed=true" autofocus="autofocus"/>

<div class="input-group-btn">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
<ul class="dropdown-menu pull-right">
<li ng-repeat="value in options.values"><a href="" ng-click="input.name = value">{{value}}</a></li>
</ul>
</div>
</div>
</div>
</form>

</div>
<div class="modal-footer">
<button ng-repeat="button in options.buttons track by button.label" class="btn btn-default" ng-class="{'btn-primary':button.primary}" ng-click="buttonClicked(button)">{{button.label}}</button>
</div>
</div>
101 changes: 101 additions & 0 deletions angular-prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
angular.module('cgPrompt',['ui.bootstrap']);

angular.module('cgPrompt').factory('prompt',['$modal','$q',function($modal,$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();

$modal.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.buttonClicked = function(button){
if (button.cancel){
$scope.$dismiss();
return;
}
if (options.input && angular.element(document.querySelector('#cgPromptForm')).scope().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);


}]);

29 changes: 29 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "angular-prompt",
"description": "Angular service to easily display prompt and confirmation modals.",
"version": "1.0.0",
"main": [
"dist/angular-prompt.js"
],
"dependencies": {
"angular": "~1.2.16",
"angular-bootstrap": "~0.10.0"
},
"ignore": [
"**/.*",
"node_modules",
"components",
"test",
"temp",
"demo",
"lib",
"angular-prompt.html",
"angular-prompt.js",
"Gruntfile.js",
"package.json",
"README.md"
],
"devDependencies": {
"underscore": "~1.6.0"
}
}
Loading

0 comments on commit b140053

Please sign in to comment.