derlin / semantic-modals

AngularJS ModalService for the semantic-UI framework.
http://derlin.github.io/semantic-modals/
Apache License 2.0
8 stars 1 forks source link

semantic-modals

AngularJS ModalService for the semantic-UI framework. Have a look at the demo.

Quick start

  1. load the script to your page

  2. reference the module as a dependency to your angular app:

    angular.module('app',
      [
          'semantic.modals',
          // other dependencies
      ]);
  3. In your controller, use the ModalService :

    angular
        .module( 'app' )
        .controller( 'MainCtrl', function(ModalService){
          var self = this;
    
          self.confirmModal = function(){
            ModalService.showModal({
              title : "confirm deletion",
              text: "are you sure ? The action cannot be undone",
              positive: "yep",
              negative: "cancel",
              basic: true
            }).then( function( result ){
                if(result.status){
                  console.log("deleting item...");
                }
            });
          };
    
        } );

    The result is :

    result

With bower

bower install semantic-modals

Usage

Available options

showModals accepts the following options:

Promise

The showModal function returns a promise, which is resolved upon modal close and rejected in case the modal could not be instanciated. The resolve callback takes one argument: {status: true/false, inputs: yourInputs}. The status is true if the use pressed the positive button, false otherwise. The inputs argument is passed back and can have been modified. It is useful for dialogs with forms for example.

Examples

A confirm modal with html and inputs binding, no cancel button:

ModalService.showModal( {
      title: "confirm",
      html: "are you sure you want to delete {{inputs.sensor.name}} ({{inputs.sensor.id}} ) ?",
      positive: "yes",
      basic: false,
      inputs     : {
          sensor: {id: 3, name: "merde"}
      },
      cancelable : true

  } ).then( function( result ){
      console.log( result );

  }, function(){
      console.log( "error" );

  } );

confirm

A modal using a partial:

 ModalService.showModal( {
    title: "edit",
    htmlInclude: "html/_editModalContent.html",
    positive: "yes",
    negative: "nevermind",
    basic: false,
    cancelable : false

} ).then( function( result ){
    console.log( result );
}, function(){
    console.log( "error" );
} );
// html/_editModalContent.html
<form name="editform" class="ui form" novalidate>
    <div class="ui labeled input">
        <!--name-->
        <div class="ui label">
            name
        </div>
        <input type="text" ng-model="inputs.name">
    </div>

    <!--description-->
    <div class="field">
        <label>Description</label>
        <textarea ng-model="inputs.description" rows="2"></textarea>
    </div>

    <!--location-->
    <div class="field">
        <label>Location</label>
        <textarea ng-model="inputs.location" rows="2"></textarea>
    </div>
</form>

html-include

A completely custom template:

<script type="text/ng-template" id="semanticModal.html">
    <div class="ui modal small">

        <i class="close icon" ng-click="close(false)"></i>
        <div class="header">
            edit {{inputs.sensor.id}}
        </div>
        <div class="image content">
            <div class="description">
                <form name="editform" class="ui form" novalidate>
                    <div class="ui labeled input">
                        <!--name-->
                        <div class="ui label">
                            name
                        </div>
                        <input type="text" ng-model="inputs.sensors.name">
                    </div>

                    <!--description-->
                    <div class="field">
                        <label>Description</label>
                        <textarea ng-model="inputs.sensor.description" rows="2"></textarea>
                    </div>

                    <!--location-->
                    <div class="field">
                        <label>Location</label>
                        <textarea ng-model="inputs.sensor.description" rows="2"></textarea>
                    </div>

                </form>
            </div>
        </div>
        <div class="actions">
            <div class="ui black deny button" ng-click="close(false)">
                Nope
            </div>
            <div class="ui positive right labeled icon button" ng-click="close(true)">
                Yep, that's me
                <i class="checkmark icon"></i>
            </div>
        </div>
    </div>
</script>

If the template is located in a file with the path identical to the template id (semanticModal.html):

ModalService.showModal( {
      templateId: "semanticModal.html",
      cancelable : false
  } );

If the template is in a file different from the id, say partial/templates.html:

ModalService.showModal( {
      templateId: "semanticModal.html",
      templateUrl: "partial/templates.html",
      cancelable : false
  } );