Skip to content

Commit

Permalink
Feature/modal (#24)
Browse files Browse the repository at this point in the history
* initial modal setup

* add modal prompt

* modal fixes
  • Loading branch information
IdanCo authored Jul 29, 2017
1 parent abf6e67 commit 50f76a6
Show file tree
Hide file tree
Showing 13 changed files with 183 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
"indent": [2, 2], // indent with two spaces
"angular/file-name": 0, // not friendly to modular architecture
"no-console": 1, // useful for debugging
"angular/function-type": 0 // not friendly to modular architecture
"angular/function-type": 0, // not friendly to modular architecture
"angular/no-service-method": 0 // what's wrong with services?!
}
};
29 changes: 29 additions & 0 deletions src/docs/demos/modal/modal-demo.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class controller {
constructor ($ngbsModal) {
this.$ngbsModal = $ngbsModal;
}

$onInit() {
console.info('yay');
}

openModalPrompt () {
const options = {
title: 'Modal Prompt Title',
body: 'Here is the prompt text',
buttons: [
{ type: 'primary', text: 'Thanks!' },
{ type: 'secondary', text: 'Close' }
]
};

this.$ngbsModal.openPrompt(options)
.then(function(button) {
console.info('clicked ' + button.text);
}, function() {
console.info('modal prompt closed without button click');
});
}
}

export default controller;
1 change: 1 addition & 0 deletions src/docs/demos/modal/modal-demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button type="button" class="btn btn-outline-success" ng-click="$ctrl.openModalPrompt()">Open Modal Prompt</button>
13 changes: 13 additions & 0 deletions src/docs/demos/modal/modal-demo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## $ngbsModal

This injectable service will help you create and handle bootstrap's modals.

### openPrompt(*options*)

A method to create prompt modals. accepts an options object which should include the following properties:

**title** (*string*, optional) - The title which appears in the modal header.

**body** (*string*, optional) - The modal's body text.

**buttons** (*array*, optional) - An array of objects, each object represents a button and should include two properties: **name** (*string*) which appears on the button; **type** (*string*) which sets the button's style as [offered by bootstrap](http://v4-alpha.getbootstrap.com/components/buttons/#examples)
30 changes: 30 additions & 0 deletions src/docs/demos/modal/modal-demo.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Import Angular Resources
import template from './modal-demo.html';
import controller from './modal-demo.controller.js';

// Import Raw Files
import TemplateRaw from '!raw-loader!./modal-demo.html';
import ControllerRaw from '!raw-loader!./modal-demo.controller.js';
import MarkdownRaw from '!raw-loader!./modal-demo.md';

// manipulate controller to pass raw files up to demo
controller.prototype.$onInit = function() {
this.demo.html = TemplateRaw;
this.demo.js = ControllerRaw;
this.demo.md = MarkdownRaw;
};

// Component definition
const Component = {
require: {
demo: '^demo'
},
template,
controller
};

// Register module, register component and export name
export default angular
.module('ng1bs4.docs.modalDemo', [])
.component('modalDemo', Component)
.name;
4 changes: 4 additions & 0 deletions src/docs/docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ <h1>Alert</h1>
<demo>
<alert-demo></alert-demo>
</demo>
<h1>Modal</h1>
<demo>
<modal-demo></modal-demo>
</demo>
</div>
</div>

Expand Down
4 changes: 3 additions & 1 deletion src/docs/docs.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import TabsDemo from './demos/tabs/tabs-demo.module';
import MenuDemo from './demos/menu/menu-demo.module';
import TooltipDemo from './demos/tooltip/tooltip-demo.module';
import AlertDemo from './demos/alert/alert-demo.module';
import ModalDemo from './demos/modal/modal-demo.module';

// Register module, define components, configure routes and export name
export default angular
Expand All @@ -27,7 +28,8 @@ export default angular
TabsDemo,
MenuDemo,
TooltipDemo,
AlertDemo
AlertDemo,
ModalDemo
])
.component('docs', Docs)
.name;
7 changes: 6 additions & 1 deletion src/library/library.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import TabsModule from './tabs/tabs.module';
import MenuModule from './menu/menu.module';
import TooltipModule from './tooltip/tooltip.module';
import AlertModule from './alert/alert.module';
import ModalModule from './modal/modal.module';

// Register module, inject components and export name
export default angular
Expand All @@ -20,6 +21,10 @@ export default angular
TabsModule,
MenuModule,
TooltipModule,
AlertModule
AlertModule,
ModalModule
])
.config(function ($qProvider) {
$qProvider.errorOnUnhandledRejections(false);
})
.name;
18 changes: 18 additions & 0 deletions src/library/modal/modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="modal fade">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ options.title }}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p>{{ options.body }}</p>
</div>
<div class="modal-footer">
<button type="button" ng-repeat="button in options.buttons" class="btn btn-{{button.type}}" ng-click="closeModal(button)">{{ button.text }}</button>
</div>
</div>
</div>
</div>
9 changes: 9 additions & 0 deletions src/library/modal/modal.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Import Resources
import ModalService from './modal.service';
import './modal.scss';

// Register module, register component and export name
export default angular
.module('ng1bs4.library.modal', [])
.service('$ngbsModal', ModalService)
.name;
Empty file added src/library/modal/modal.scss
Empty file.
67 changes: 67 additions & 0 deletions src/library/modal/modal.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import template from './modal.html';

const DEFAULT_OPTIONS = {
title: 'Modal Title',
body: 'Modal body text goes here.',
buttons: [
{
type: 'primary',
text: 'OK',
},
{
type: 'secondary',
text: 'Cancel',
},
]
};

class ModalService {
constructor ($document, $compile, $rootScope, $q) {
this.$document = $document;
this.$compile = $compile;
this.$rootScope = $rootScope;
this.$q = $q;
}

openPrompt(options) {
// promise object to handle prompt results
const deferred = this.$q.defer();
// new scope for the modal
const modalScope = this.$rootScope.$new(true);
// prepare the modal element
const modalElement = angular.element(template);
// get the document body
const body = this.$document.find('body').eq(0);

let buttonClicked;

// merge user options with defaults and make it available on the modal's scope
modalScope.options = angular.merge({}, DEFAULT_OPTIONS, options);
// compile the modal element with the new scope
this.$compile(modalElement)(modalScope);
// inject the modal element into the dom
body.append(modalElement);
// open the modal
modalElement.modal();

// handle button click
modalScope.closeModal = (button) => {
modalElement.modal('hide');
buttonClicked = button;
};

// on modal close, resolve or reject promise
modalElement.on('hidden.bs.modal', function () {
if (buttonClicked) {
deferred.resolve(buttonClicked);
} else {
deferred.reject();
}
modalScope.$destroy();
});

return deferred.promise;
}
}

export default ModalService;
3 changes: 1 addition & 2 deletions src/library/select/select.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ const DEFAULT_PLACEHOLDER = 'Select an option...';

// Set up controller
class controller {
constructor($element, $timeout) {
constructor($element) {
this.$element = $element;
this.$timeout = $timeout;
}

$onInit() {
Expand Down

0 comments on commit 50f76a6

Please sign in to comment.