add ability to draw show cursors

This commit is contained in:
nightwing 2012-03-13 22:04:45 +04:00
commit c431f6a550

View file

@ -47,11 +47,10 @@ var Cursor = function(parentEl) {
this.element.className = "ace_layer ace_cursor-layer";
parentEl.appendChild(this.element);
this.cursor = dom.createElement("div");
this.cursor.className = "ace_cursor ace_hidden";
this.element.appendChild(this.cursor);
this.isVisible = false;
this.cursors = [];
this.cursor = this.addCursor();
};
(function() {
@ -65,30 +64,54 @@ var Cursor = function(parentEl) {
this.session = session;
};
this.addCursor = function() {
var el = dom.createElement("div");
var className = "ace_cursor"
if (!this.isVisible)
className += " ace_hidden";
if (this.overwrite)
className += " ace_overwrite";
el.className = className;
this.element.appendChild(el);
this.cursors.push(el);
return el;
};
this.removeCursor = function() {
if (this.cursors.length > 1) {
var el = this.cursors.pop();
el.parentNode.removeChild(el)
return el
}
};
this.hideCursor = function() {
this.isVisible = false;
dom.addCssClass(this.cursor, "ace_hidden");
for (var i = this.cursors.length; i--; )
dom.addCssClass(this.cursors[i], "ace_hidden");
clearInterval(this.blinkId);
};
this.showCursor = function() {
this.isVisible = true;
dom.removeCssClass(this.cursor, "ace_hidden");
this.cursor.style.visibility = "visible";
for (var i = this.cursors.length; i--; )
dom.removeCssClass(this.cursors[i], "ace_hidden");
this.element.style.visibility = "";
this.restartTimer();
};
this.restartTimer = function() {
clearInterval(this.blinkId);
if (!this.isVisible) {
if (!this.isVisible)
return;
}
var cursor = this.cursor;
var element = this.element;
this.blinkId = setInterval(function() {
cursor.style.visibility = "hidden";
element.style.visibility = "hidden";
setTimeout(function() {
cursor.style.visibility = "visible";
element.style.visibility = "visible";
}, 400);
}, 1000);
};
@ -118,25 +141,56 @@ var Cursor = function(parentEl) {
this.update = function(config) {
this.config = config;
this.pixelPos = this.getPixelPosition(null, true);
if (!this.session.selection.multiSelectMode) {
this.pixelPos = this.getPixelPosition(null, true);
var style = this.cursor.style;
style.left = this.pixelPos.left + "px";
style.top = this.pixelPos.top + "px";
style.width = config.characterWidth + "px";
style.height = config.lineHeight + "px";
this.cursor.style.left = this.pixelPos.left + "px";
this.cursor.style.top = this.pixelPos.top + "px";
this.cursor.style.width = config.characterWidth + "px";
this.cursor.style.height = config.lineHeight + "px";
while (this.cursors.length > 1)
this.removeCursor();
} else {
var selections = this.session.selection.allRanges
var i = 0, sel, cursorIndex = 0;
var overwrite = this.session.getOverwrite()
if (overwrite != this.overwrite) {
this.overwrite = overwrite;
if (overwrite)
dom.addCssClass(this.cursor, "ace_overwrite");
else
dom.removeCssClass(this.cursor, "ace_overwrite");
while ((sel = selections[i]) && sel.cursor.row < config.firstRow)
i++;
while ((sel = selections[i]) && sel.cursor.row < config.lastRow) {
this.pixelPos = this.getPixelPosition(sel.cursor, true);
var style = (this.cursors[cursorIndex++] || this.addCursor()).style;
style.left = this.pixelPos.left + "px";
style.top = this.pixelPos.top + "px";
style.width = config.characterWidth + "px";
style.height = config.lineHeight + "px";
i++;
}
while (cursorIndex < this.cursors.length)
this.removeCursor();
}
var overwrite = this.session.getOverwrite();
if (overwrite != this.overwrite)
this.$setOverite(overwrite);
this.restartTimer();
};
this.$setOverite = function(overwrite) {
this.overwrite = overwrite;
for (var i = this.cursors.length; i--; ) {
if (overwrite)
dom.addCssClass(this.cursors[i], "ace_overwrite");
else
dom.removeCssClass(this.cursors[i], "ace_overwrite");
}
};
this.destroy = function() {
clearInterval(this.blinkId);
}