Giter VIP home page Giter VIP logo

ocmodal's Introduction

ocModal

An angularJS modal directive & service

Key features

  • Easy to use modal
  • Dependencies free (well except angular off course)
  • Load via the service or the directive
  • Style yourself or use the bootstrap's theme

Demo on Plunker

Usage

  • Download the lib (you can use bower install ocModal)
  • Put ocModal.js into you project
  • Add the css file to your project: if you don't have bootstrap 3, include dist/css/ocModal.full.min.css. If you already have bootstrap 3, use dist/css/ocModal.light.min.css
  • Add the module oc.modal to your application
  • Load on demand using the service or the directive :

Service:

$ocModal.open('partials/modal.html');

or

$ocModal.open('<div>My content</div>');

Directive:

<div oc-modal-open="'partials/modal.html'"></div>

or

<div oc-modal-open="'<div>My content</div>'"></div>

See the example in the 'example' folder to know how to integrate ocLazyLoad with your router.

Parameters

You can also pass parameters when you open a modal via the service or the directive. The previous examples are equivalent to :

Service:

$ocModal.open({
	url: 'partials/modal.html'
});

or

$ocModal.open({
	template: '<div>My content</div>'
});

Directive:

<div oc-modal-open="{url: 'partials/modal.html'}"></div>

or

<div oc-modal-open="{template: '<div>My content</div>'}"></div>

The complete list of parameters is :

  • id: you can specify an id for your modal, it is usefull if you want to open more than one modal at the same time
$ocModal.open({
	id: 'modal1',
	url: 'partials/modal.html'
});

By default the id is set to '_default'.

  • url: a template url loaded via ng-include, so you can use an ng-script template or the url of an external html file

  • template: if you prefer to write the template in line, you can use the template parameter instead of url.

  • controller: you can pass a controller for the new content

$ocModal.open({
	url: 'partials/modal.html',
	controller: 'MyController'
});
  • cls: You can specify one or more (space separated) classes to be added to the modal
$ocModal.open({
	url: 'partials/modal.html',
	cls: 'my-class1 my-class2'
});
  • onOpen: you can add a callback that will be called when the modal is opened
$ocModal.open({
	url: 'partials/modal.html',
	onOpen: function() {
		console.log('Just opened !');
	}
});
  • onClose: you can add a callback that will be called when the modal is closed
$ocModal.open({
	url: 'partials/modal.html',
	onClose: function() {
		console.log('Just closed !');
	}
});
  • init: use this to populate the modal scope. If you use a controller you will also be able to access this via $init
$ocModal.open({
	template: '<div>{{param1}}</div>',
	controller: 'MyController',
	init: {
		param1: 'test'
	}
});

And in your controller :

angular.module('app').controller('MyController', ['$scope', '$init', function($scope, $init) {
	console.log($scope.param1, $init.param1);
}]);
  • $ocModalParams: Access the modal params in your controller
angular.module('app').controller('MyController', ['$scope', '$ocModalParams', function($scope, $ocModalParams) {
	console.log($ocModalParams);
}]);
  • isolate: by default your modal's scope will inherit the variables from the init parameter. If you don't want that and you prefer to access these variables via the $init in your controller, you can use isolate=true
$ocModal.open({
	url: 'partials/modal.html',
	controller: 'MyController',
	isolate: true,
	init: {
		param1: 'test'
	}
});

And use $init in your controller :

angular.module('app').controller('MyController', ['$scope', '$init', function($scope, $init) {
	console.log($init.param1);
}]);

But $scope.param1 will be undefined.

  • closeOnEsc: by default you will be able to close the modal with the "ESC" key. If you want to disable this behaviour, use closeOnEsc: false
$ocModal.open({
	url: 'partials/modal.html',
	closeOnEsc: false
});

Functions & attributes

  • open(url/template/object): use this to open the modal
$ocModal.open({
	url: 'partials/modal.html'
});
  • close([id][, param1][, param2][, ...]): use this to close the modal, it will return a promise that resolves at the end of the closing animation (if any)
$ocModal.close();

With no parameter it will close the last opened modal. If you want to close a specific modal, use the id.

$ocModal.close('modal1');

You can also pass what you want to the onClose callback (if you have one) :

$ocModal.open({
	url: 'partials/modal.html',
	onClose: function(a, b, c) {
		console.log(a); // arg1
		b(); // whatever
		console.log(c); // {p1: 'test'}
	}
});

$ocModal.close('arg1', function() { console.log('whatever') }, {p1: 'test'});
  • $scope.closeModal([id][, param1][, param2][, ...]): this is an alias for $ocModal.close() that you can also use in your template
<button ng-click="closeModal()"></button>
  • getOpenedModals(): if you need to get the ids of the opened modals

  • waitingForOpen: check this property if you need to know if another modal will be opened once this one is closed

$ocModal.open({
	url: "partials/login.html",
	controller: 'LoginCtrl',
	onClose: function() {
		if(!$ocModal.waitingForOpen) {
			$state.transitionTo('welcome');
		}
	}
});

Directives

  • oc-modal-open: this is an alias for $ocModal.open() that you can also use in your template.
<div oc-modal-open="{url: 'partials/modal.html'}"></div>
  • oc-modal-close: this is an alias for $ocModal.close() that you can also use in your template.
<button oc-modal-close="'Some text '+testVar"></button>

Animations

You can use a set of animations by including the file ocModal.animations.css and by adding one of those classes with the cls parameter :

  • fade-in
  • slide-down
  • scale
  • fall
  • flip-horizontal
  • flip-vertical
  • super-scaled
  • slit
oc-modal-open="{url: 'partials/modal.html', cls: 'fade-in'}"

You can add your own animations by adding new styles to .modal .modal-dialog .modal-content and .modal .modal-dialog .modal-content.opened.

ocmodal's People

Contributors

ocombe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ocmodal's Issues

Modal position off the top of the screen

Hi,

I'm seeing 2 issues when using ocModal.full.css and the content of the modal > the visible height of the screen.
a) The backdrop is smaller than the page (So you can scroll down and see the modal, but run out of backdrop behind it)
b) The modal itself is positioned vertically off the top of the screen by quite a bit, this might be what causes a) - since the offset seems about the same amount

Updated plunkr to demo the issue

Be really great if you could suggest a fix - as I can't seem to work out what's going on.

How do I pass data back from modal's controller on close?

I have a MainController that has list of customers called vm.customers

MainController

(function() {
'use strict';

    angular
        .module('app')
        .controller('MainController', MainController);

    MainController.inject = ['$scope', '$q', 'dataservice', 'logger', '$ocModal', 'CUSTOMERS'];
    function MainController($scope, $q, dataservice, logger, $ocModal, CUSTOMERS) {
        var vm = this;
        vm.customers = [];

        vm.populateCustomers = populateCustomers;
        vm.openAddCustomerModal = openAddCustomerModal;

        activate();

        ////////////////

        function activate() {
            var promises = [populateCustomers()];
            return $q.all(promises)
                .then(function () {
                    logger.info('Activated Customer List View');
                    return vm;
                });
        }

        function populateCustomers() {
            // CUSTOMERS is just an angular constant

            if (!vm.customers.length) {
                vm.customers = _.chain(CUSTOMERS).sortBy('name').value();
            }
            return vm.customers;
        }

        function openAddCustomerModal() {
            $ocModal.open({
                url: 'js/app/customer-add.html',
                controller: 'CustomerAddController',
                closeOnEsc: false,
                init: {
                    customer: {}
                }
            });
        }
    }
})();

The MainController uses $ocModal to open up an add new customer modal which is an input form with SAVE and CANCEL buttons.

The SAVE action calls dataservice.createCustomer($scope.customer) followed by $ocModal.close();

CustomerAddController

(function() {
'use strict';

    angular
        .module('app')
        .controller('CustomerAddController', CustomerAddController);

    CustomerAddController.inject = ['$scope', '$q', 'dataservice', 'logger', '$ocModal'];
    function CustomerAddController($scope, $q, dataservice, logger, $ocModal) {
        $scope.addCustomer = addCustomer;

        activate();

        ////////////////

        function activate() { 
            logger.info('Add Customer Modal Open');
        }

        function addCustomer() {
            return dataservice.createCustomer($scope.customer)
                .then(function (newCustomer) { 
                    $ocModal.close();
                })
                .catch(function () {
                    $ocModal.close();
                });
        }
    }
})();

How do I pass newCustomer back to MainController on $ocModal.close so that a function in MainController like shown below can push the received newCustomer to vm.customers?

...
        vm.addNewCustomer = addNewCustomer;
...

        function addNewCustomer(newCustomer) {
                vm.customers.push(newCustomer);
        }

Modal closes on enter

I have a form inside modal and am expecting the form to be submitted first and then on the basis of response from the form submission would like to close the modal but currently the modal is closed directly when I press enter and after the modal is closed the form is submitted. Is there any way I can prevent the modal to be closed on enter key.

How to link with router?

Hi,

What would be your recommended approach so that each modal has a unique URL and works together with browser history?

Backdrop height with multiple modals

When we open multiple modals at the same time, if the second one is higher than the first one, the first backdrop height is incorrect (it doesn't go all the way to the bottom)

Missing license

The project currently has no license, so it's protected and all rights are reserved (source).

Without a license, the default copyright laws apply, meaning that you retain all rights to your source code and no one may reproduce, distribute, or create derivative works from your work.

Is this intentional or are you planning to add an open source license anytime in the future?

controller $scope

Not sure if this is a bug, me misunderstanding a feature, or an implementation issue on my part, but if I set the controller of a modal to a controller that already exists and then within the modal call a method on ng-click then this method, if in the specified controller, will run but any changes to the $scope will not be reflected.

Example.

I set the modal controller to testCtrl. testCtrl already has $scope.testArray with 2 items in it and $scope.testDelete that removes the first item from the $scope.testArray. From the modal I call method testDelete on ng-click which also closes the modal. testDelete gets run, but the page behind the modal (which uses testCtrl and shows the values within $scope.testArray) does not reflect that deletion.

Custom backdrop class

Can we have backdrop customisable based on the custom class given to modal?

I have modals which need different backdrop settings for modals. There is no way to target backdrop with custom class.

Open Close animation

Hi ,
Any Chance you could include or give pointers on how to include open close animations to this really awesome plugin. That will be the clincher i think. Thumbs up

controllerAs syntax

is the controllerAs syntax is this supported at all ?

I have this

 $ocModal.open({
                    url: "someurl",
                    controller: "myController as controller",
                    init: {foo: "bar"},
                });

but my controller doesn't have any properties defined

Is this project still being maintained?

Hey @ocombe, just wondering if this project is still being actively maintained? I personally love it since 90% of angular modal libraries depend on Bootstrap which I do not use.

I'm currently using this in several projects but noticed that there hasn't been an update this year. Is ocModal still on your radar? I would be willing to help out if needed as I definitely have an interest in seeing this module stay up to date. I just don't want to introduce it to newer projects if it won't be maintained.

Either way, thanks for creating this library to begin with!

scope clear

    ocModal.directive('ocModalOpen', ['$ocModal', function($ocModal) {
        return {
            restrict: 'A',
            require: '?modal',
            link: function($scope, $element, $attrs) {
                $element.on('click touchstart', function(e) {
                    e.preventDefault();
                    e.stopPropagation();
                    var newScope = $scope.$new();
                    var params = newScope.$eval($attrs.ocModalOpen);
                    if(params) {
                        if(typeof params === "number") {
                            params = { url: $attrs.ocModalOpen };
                        } else if(typeof params === "string") {
                            params = { url: params };
                        }
                        if(!params.url) {
                            throw "You need to set the modal url";
                        }
                        $scope.$apply(function() {
                            $ocModal.open(params);
                        });
                    }
                });
            }
        };
    }]);

Should the newScope here destroyed on $scope $destroy?

And would it be better to set reference of modals to null instead of deleting them?

remove: function(id) {
  modals[id || '_default'] = null;
  // delete modals[id || '_default'];
},

CSS classes are too generic

ocModal is using generic css classes like .modal which overrides the default rules for all modals. Prepending oc- on the classes would avoid class conflicts.

ocModal closes on "enter" key when an input box is focused on.

    <script type="text/ng-template" id="partials/modal.html">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true" ng-click="closeModal()">&times;</button>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <div class="modal-body">
                <p>One fine body&hellip;</p>
                <input type="text" ng-model="hello">
                {{hello}}
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal" ng-click="closeModal()">Close</button>
                <button type="button" class="btn btn-success">Save changes</button>
            </div>
        </script>

Now hit "enter" inside the input element....

Everything goes to shit.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.