From 4a38512616dbf69982c1393eff2166f990d0ec85 Mon Sep 17 00:00:00 2001 From: DanyaPostfactum Date: Tue, 2 Oct 2012 03:51:24 +1000 Subject: [PATCH] Rename restartTimer to resetTimer in cursor, refactor this method --- lib/ace/layer/cursor.js | 49 ++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/ace/layer/cursor.js b/lib/ace/layer/cursor.js index db208f3d..694ab2a5 100644 --- a/lib/ace/layer/cursor.js +++ b/lib/ace/layer/cursor.js @@ -58,8 +58,7 @@ var Cursor = function(parentEl) { this.setBlinking = function(blinking) { this.isBlinking = blinking; - if (blinking) - this.restartTimer(); + this.resetTimer(); }; this.addCursor = function() { @@ -88,32 +87,41 @@ var Cursor = function(parentEl) { this.isVisible = false; for (var i = this.cursors.length; i--; ) dom.addCssClass(this.cursors[i], "ace_hidden"); - clearInterval(this.blinkId); + this.resetTimer(); }; this.showCursor = function() { this.isVisible = true; for (var i = this.cursors.length; i--; ) dom.removeCssClass(this.cursors[i], "ace_hidden"); - - this.element.style.visibility = ""; - this.restartTimer(); + this.resetTimer(); }; - this.restartTimer = function() { - clearInterval(this.blinkId); - if (!this.isBlinking) - return; - if (!this.isVisible) + this.resetTimer = function() { + clearInterval(this.intervalId); + clearTimeout(this.timeoutId); + for (var i = this.cursors.length; i--; ) + this.cursors[i].style.visibility = ""; + + if (!this.isBlinking || !this.isVisible) return; - var element = this.cursors.length == 1 ? this.cursor : this.element; - this.blinkId = setInterval(function() { - element.style.visibility = "hidden"; - setTimeout(function() { - element.style.visibility = ""; - }, 400); - }, 1000); + var blink = function(){ + this.timeoutId = setTimeout(function() { + for (var i = this.cursors.length; i--; ) { + this.cursors[i].style.visibility = "hidden"; + } + }.bind(this), 0.6 * 1000); + }.bind(this); + + this.intervalId = setInterval(function() { + for (var i = this.cursors.length; i--; ) { + this.cursors[i].style.visibility = ""; + } + blink(); + }.bind(this), 1000); + + blink(); }; this.getPixelPosition = function(position, onScreen) { @@ -170,7 +178,7 @@ var Cursor = function(parentEl) { // cache for textarea and gutter highlight this.$pixelPos = pixelPos; - this.restartTimer(); + this.resetTimer(); }; this.$setOverwrite = function(overwrite) { @@ -186,7 +194,8 @@ var Cursor = function(parentEl) { }; this.destroy = function() { - clearInterval(this.blinkId); + clearInterval(this.intervalId); + clearTimeout(this.timeoutId); } }).call(Cursor.prototype);