Coder Social home page Coder Social logo

angular-block-ui's Introduction

angular-block-ui

A simple AngularJS module that allows you to block user interaction on AJAX requests. Blocking is done automatically for each http request and/or manually via an injectable service.

Dependencies

Besides AngularJS (~1.2.4), none.

Installation

Either copy the contents of the package directory of the Github project or install with bower from the command line (recommended):

bower install angular-block-ui

Include both the JS and CSS file in your html:

<link rel="stylesheet" href="path-to-block-ui/angular-block-ui.min.css"/>
<!-- After AngularJS -->
<script src="path-to-block-ui/angular-block-ui.min.js"></script>

Create a dependency on blockUI in your main Angular module:

angular.module('myApp', ['blockUI'])

Usage

By default the module will block the user interface on each pending request made from the browser. This behaviour can be modified in the configuration.

It's also possible to do the blocking manually. The blockUI module exposes a service by the same name. Access to the service is gained by injecting it into your controller or directive:

angular.module('myApp').controller('MyController', function($scope, blockUI) {
  // A function called from user interface, which performs an async operation.
  $scope.onSave = function(item) {

    // Block the user interface
    blockUI.start();

    // Perform the async operation    
    item.$save(function() {
  
      // Unblock the user interface
      blockUI.stop();
      
    });
  };
});

BlockUI service methods

start

The start method will start the user interface block. Because multiple user interface elements can request a user interface block at the same time, the service keeps track of the number of start calls. Each call to start() will increase the count and every call to stop() will decrease the value. Whenever the count reaches 0 the block will end.

Note: By default the block is immediately active after calling this method, but to prevent trashing the user interface each time a button is pressed, the block is visible after a short delay. This behaviour can be modified in the configuration.

Arguments:

  • message (string) Indicates the message to be shown in the overlay. If none is provided, the default message from the configuration is used.

stop

This will decrease the block count. The block will end if the count is 0.

reset

The reset will force a unblock by setting the block count to 0.

message

Allows the message shown in the overlay to be updated while to block is active.

done

Queues a callback function to be called when the block has finished. This can be useful whenever you wish to redirect the user to a different location while there are still pending AJAX requests.

Arguments:

  • callback (function) The callback function to queue.

BlockUI overlay template

The html and styling of the builtin template is kept bare bone. It consist of two divs (overlay and message):

<div ng-show="blockCount > 0" class="block-ui-overlay" ng-class="{ 'block-ui-visible': blocking }"></div>
<div ng-show="blocking" class="block-ui-message">{{ message }}</div>

A custom template can be specified in the module configuration.

BlockUI module configuration

The configuration of the BlockUI module can be modified via the blockUIConfigProvider during the config phase of your Angular application:

angular.module('myApp').config(function(blockUIConfigProvider) {
  
  // Change the default overlay message
  blockUIConfigProvider.message('Please stop clicking!');
  
  // Change the default delay to 100ms before the blocking is visible
  blockUIConfigProvider.delay(100);
  
});

Methods

message

Changes the default message to be used when no message has been provided to the start method of the service. Default value is 'Loading ...'.

// Change the default overlay message
blockUIConfigProvider.message('Please wait');

delay

Specifies the amount in milliseconds before the block is visible to the user. By delaying a visible block your application will appear more responsive. The default value is 250.

// Change the default delay to 100ms before the blocking is visible ...
blockUIConfigProvider.delay(100);

// ... or completely remove the delay
blockUIConfigProvider.delay(1);

template

Specifies a custom template to use as the overlay.

// Provide a custom template to use
blockUIConfigProvider.template('<div class="block-ui-overlay">{{ message }}</div>');

templateUrl

Specifies a url to retrieve the template from. The current release only works with pre-cached templates, which means that this url should be known in the $templateCache service of Angular. If you're using the grunt with html2js or angular-templates, which I highly recommend, you're already set.

// Provide the custom template via a url
blockUIConfigProvider.templateUrl('my-templates/block-ui-overlay.html');

autoBlock

By default the BlockUI module will start a block whenever the Angular $http service has an pending request. If you don't want this behaviour and want to do all the blocking manually you can change this value to false.

// Disable automatically blocking of the user interface
blockUIConfigProvider.autoBlock(false);

resetOnException

By default the BlockUI module will reset the block count and hide the overlay whenever an exception has occurred. You can set this value to false if you don't want this behaviour.

// Disable clearing block whenever an exception has occurred
blockUIConfigProvider.resetOnException(false);

requestFilter

Allows you to specify a filter function to exclude certain ajax requests from blocking the user interface. The function is passed the Angular request config object. The blockUI service will ignore requests when the function returns false.

// Tell the blockUI service to ignore certain requests
blockUIConfigProvider.requestFilter(function(config) {

  // If the request starts with '/api/quote' ...
  if(config.url.match(/^\/api\/quote($|\/).*/)) {
    return false; // ... don't block it.
  }
});

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.