Giter Site home page Giter Site logo

ninshadow's Introduction

NinShadow

2017-11-12

Ninshadow (short for ninja shadow) helps you providing visual insights to the gui user when an ajax action is processing in the background.

Base principles

When an ajax call is made, we want to enhance the user experience by prodiving an insight that the app is doing something in the background.

There are two main types of shadow actions:

  • localized action
  • generic action

The localized action

localized-loader.png

The localized action consist of displaying a small ajax loader, next to the gui control the user just interacted with.

So for instance, if the user clicked a button, an ajax loader shows up next to the button and disappears when the background action completes.

The generic action

overlay-loader.png

The generic action is an overlay spanning the whole page, and the loader is right in the center.

The user can't do nothing else but watch the loader disappear.

Implementation suggestion

Here is a simplified example sketch implementation.

// somewhere in your js api
function ajaxRequest(){

    window.onRequestLoaderPrepare();
    window.onRequestLoaderStart();

    $.post(url, data, function (response) {
        window.onRequestLoaderEnd();
        // do other things
    });
}



// synopsis 1: generic action
$(".add-to-cart-btn").on("click", function(){
    ajaxRequest();
});

// synopsis 2: localized action
$(".add-to-cart-btn").on("click", function(){
    window.ninShadow = $(this).parent().find('.nin-shadow-loader');
    ajaxRequest();
});



The missing piece of this code is the following, somewhere in your js code:

(function () {
    var jNinShadow = null; // the current instance
    window.ninShadow = null; // allows external code to temporarily trigger the localized mode

    window.onRequestLoaderPrepare = function () {
        if (null !== window.ninShadow) { // do we want a localized action? (otherwise assume generic action)
            jNinShadow = window.ninShadow;
        }
    };
    window.onRequestLoaderStart = function () {
        if (null === jNinShadow) { // seems that we want a generic action
            jNinShadow = $("#nin-shadow");
        }
        jNinShadow.addClass('active');
    };
    window.onRequestLoaderEnd = function () {
        // resetting our variables
        jNinShadow.removeClass('active');
        jNinShadow = null;
        window.ninShadow = null;
    };
})();

And here is some example css code:

// localized version
.nin-shadow-loader {
    display: none;
    border: 3px solid #d1d1d1;
    animation: spin 1s linear infinite;
    border-top: 3px solid #211313;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    animation: spin 2s linear infinite;


    &.active {
        display: block;
    }
}


// generic version, don't forget to put
// a div#nin-shadow at the bottom of your page
#nin-shadow {
    display: none;
    position: fixed;
    opacity: 0;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10000;
    background: rgba(236, 236, 236, 0.5);

    &.active {
        display: flex;
    }
}

ninshadow's People

Contributors

lingtalfi avatar

Watchers

 avatar James Cloos avatar

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.