From 1ea54122659da0ce80c3028785119eeba3fda155 Mon Sep 17 00:00:00 2001 From: Juanfran Date: Mon, 18 Aug 2014 13:20:56 +0200 Subject: [PATCH] fix project loader --- app/coffee/modules/common/loader.coffee | 102 ++++++++++++------------ app/coffee/modules/nav.coffee | 12 ++- 2 files changed, 61 insertions(+), 53 deletions(-) diff --git a/app/coffee/modules/common/loader.coffee b/app/coffee/modules/common/loader.coffee index c6b662c7..8739460a 100644 --- a/app/coffee/modules/common/loader.coffee +++ b/app/coffee/modules/common/loader.coffee @@ -5,15 +5,16 @@ module = angular.module("taigaCommon") LoaderDirective = (tgLoader) -> link = ($scope, $el, $attrs) -> - tgLoader.end () -> + tgLoader.onStart () -> + $(document.body).addClass("loader-active") + $el.addClass("active") + + tgLoader.onEnd () -> $(document.body).removeClass("loader-active") $el.removeClass("active") $scope.$on "$routeChangeSuccess", () -> - tgLoader.start () -> - $(document.body).addClass("loader-active") - $el.addClass("active") - + tgLoader.start() return { link: link @@ -22,10 +23,6 @@ LoaderDirective = (tgLoader) -> module.directive("tgLoader", ["tgLoader", LoaderDirective]) Loader = () -> - interval = null - onLoad = () -> - startLoadTime = 0 - defaultLog = { request: { count: 0, @@ -38,7 +35,7 @@ Loader = () -> } defaultConfig = { - enabled: false, + enabled: true, minTime: 1000, auto: false } @@ -46,55 +43,52 @@ Loader = () -> log = _.merge({}, defaultLog) config = _.merge({}, defaultConfig) - reset = () -> - log = _.merge({}, defaultLog) - config = _.merge({}, defaultConfig) - - pageLoaded = () -> - reset() - - endTime = new Date().getTime() - diff = endTime - startLoadTime - - if diff < config.minTime - timeout = config.minTime - diff - else - timeout = 0 - - setTimeout ( -> - onLoad() - ), timeout - - autoCheckLoad = () -> - interval = setInterval ( -> - currentDate = new Date().getTime() - - if log.request.count == log.response.count && currentDate - log.response.time > 200 - clearInterval(interval) - pageLoaded() - - ), 100 - @.add = (auto = false) -> return () -> - config.enabled = true config.auto = auto - @.$get = () -> + @.$get = ["$rootScope", ($rootscope) -> + interval = null + startLoadTime = 0 + return { - start: (callback) -> + pageLoaded: () -> + if config.enabled + log = _.merge({}, defaultLog) + config = _.merge({}, defaultConfig) + + endTime = new Date().getTime() + diff = endTime - startLoadTime + + if diff < config.minTime + timeout = config.minTime - diff + else + timeout = 0 + + setTimeout ( -> + $rootscope.$broadcast("loader:end"); + ), timeout + + start: () -> if config.enabled if config.auto - autoCheckLoad() + interval = setInterval ( -> + currentDate = new Date().getTime() + + if log.request.count == log.response.count && currentDate - log.response.time > 200 + clearInterval(interval) + pageLoaded() + + ), 100 startLoadTime = new Date().getTime() - callback() + $rootscope.$broadcast("loader:start"); - end: (fn) -> - onLoad = fn + onStart: (fn) -> + $rootscope.$on("loader:start", fn); - pageLoaded: () -> - pageLoaded() + onEnd: (fn) -> + $rootscope.$on("loader:end", fn); logRequest: () -> log.request.count++ @@ -103,11 +97,21 @@ Loader = () -> logResponse: () -> log.response.count++ log.response.time = new Date().getTime() + + isEneabled: () -> + config.enabled == true + + disabled: () -> + config.enabled = false + + enabled: () -> + config.enabled = true } + ] return -module.provider("tgLoader", Loader) +module.provider("tgLoader", [Loader]) loaderInterceptor = (tgLoader) -> return { diff --git a/app/coffee/modules/nav.coffee b/app/coffee/modules/nav.coffee index d3e0a124..fc40170c 100644 --- a/app/coffee/modules/nav.coffee +++ b/app/coffee/modules/nav.coffee @@ -66,7 +66,7 @@ class ProjectsNavigationController extends taiga.Controller module.controller("ProjectsNavigationController", ProjectsNavigationController) -ProjectsNavigationDirective = ($rootscope, animationFrame, $timeout) -> +ProjectsNavigationDirective = ($rootscope, animationFrame, $timeout, tgLoader) -> baseTemplate = _.template("""

Your projects

@@ -111,8 +111,8 @@ ProjectsNavigationDirective = ($rootscope, animationFrame, $timeout) -> difftime = new Date().getTime() - loadingStart timeout = 0 - if (difftime < 3500) - timeout = 3500 - timeout + if (difftime < 1000) + timeout = 1000 - timeout setTimeout ( -> overlay.one 'transitionend', () -> @@ -120,6 +120,8 @@ ProjectsNavigationDirective = ($rootscope, animationFrame, $timeout) -> $(document.body) .removeClass("loading-project open-projects-nav") + + tgLoader.enabled() ), timeout renderProjects = ($el, projects) -> @@ -151,6 +153,8 @@ ProjectsNavigationDirective = ($rootscope, animationFrame, $timeout) -> $(document.body) .addClass('loading-project') + tgLoader.disabled() + loadingStart = new Date().getTime() $el.on "click", ".create-project-button .button", (event) -> @@ -246,7 +250,7 @@ ProjectsNavigationDirective = ($rootscope, animationFrame, $timeout) -> link: link } -module.directive("tgProjectsNav", ["$rootScope", "animationFrame", "$timeout", ProjectsNavigationDirective]) +module.directive("tgProjectsNav", ["$rootScope", "animationFrame", "$timeout", "tgLoader", ProjectsNavigationDirective]) #############################################################################