/**
 * Created by amr on 11/6/15.
 */

"use strict";

// 1 - define module.
var loaderModule = angular.module('loader-component', []);

// 2- define provider
loaderModule.factory('loaderHttpInterceptor', ['$q', '$rootScope', function($q, $rooScope) {
    return {
        // optional method
        'request': function(config) {
            $rooScope.$broadcast('loading:start');
            return config;
        },

        // optional method
        'requestError': function(rejection) {

            return $q.reject(rejection);
        },



        // optional method
        'response': function(response) {
            // do something on success
            $rooScope.$broadcast('loading:finish');
            return response;
        },

        // optional method
        'responseError': function(rejection) {
            // do something on error
            $rooScope.$broadcast('loading:finish');
            return $q.reject(rejection);
        }
    };
}]);

loaderModule.config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('loaderHttpInterceptor');
}]);

@@ -1095,12 +1095,742 @@
 tor');%0A%7D%5D);%0A
+%0A// 3- define directive.%0AloaderModule.directive('loaderElement', %5Bfunction()%7B%0A%0A    var Loader = function()%7B%7D;%0A    Loader.prototype.show = function(element)%7B%0A        element%5B0%5D.style.display = 'block';%0A    %7D;%0A    Loader.prototype.hide = function(element)%7B%0A        element%5B0%5D.style.display = 'none';%0A    %7D;%0A    var _loader = new Loader();%0A%0A    return %7B%0A        restrict: 'A',%0A        scope: %7B%7D,%0A        link: function($scope, $element) %7B%0A            _loader.hide(angular.element($element));%0A            $scope.$on('loading:start', function()%7B%0A                _loader.show($element);%0A            %7D);%0A%0A            $scope.$on('loading:finish', function()%7B%0A                _loader.hide($element);%0A            %7D);%0A        %7D%0A    %7D%0A%7D%5D);%0A%0A%0A

define loaderElement directive to listen to http event and show and hide the loader based on http activities