[Phpfreechat-svn] SF.net SVN: phpfreechat: [1118] trunk/data/public/js/pfcclient.js
Status: Beta
Brought to you by:
kerphi
From: <gpi...@us...> - 2007-08-13 15:06:13
|
Revision: 1118 http://phpfreechat.svn.sourceforge.net/phpfreechat/?rev=1118&view=rev Author: gpinzone Date: 2007-08-13 08:06:14 -0700 (Mon, 13 Aug 2007) Log Message: ----------- Added text selection tracking to fix annoying IE loss of selection and caret position. Will look to extend this fix to other areas like bbcode insertion. Tested on IE 6.0 and IE 7.0. Modified Paths: -------------- trunk/data/public/js/pfcclient.js Modified: trunk/data/public/js/pfcclient.js =================================================================== --- trunk/data/public/js/pfcclient.js 2007-08-12 16:22:48 UTC (rev 1117) +++ trunk/data/public/js/pfcclient.js 2007-08-13 15:06:14 UTC (rev 1118) @@ -81,6 +81,8 @@ // don't use this line because when doing completeNick the "return false" doesn't work (focus is lost) // Event.observe(this.el_words, 'keypress', this.callbackWords_OnKeypress.bindAsEventListener(this), false); Event.observe(this.el_words, 'keydown', this.callbackWords_OnKeydown.bindAsEventListener(this), false); + Event.observe(this.el_words, 'keyup', this.callbackWords_OnKeyup.bindAsEventListener(this), false); + Event.observe(this.el_words, 'mouseup', this.callbackWords_OnMouseup.bindAsEventListener(this), false); Event.observe(this.el_words, 'focus', this.callbackWords_OnFocus.bindAsEventListener(this), false); Event.observe(this.el_handle, 'keydown', this.callbackHandle_OnKeydown.bindAsEventListener(this), false); Event.observe(this.el_handle, 'change', this.callbackHandle_OnChange.bindAsEventListener(this), false); @@ -780,6 +782,16 @@ return true; } }, + callbackWords_OnKeyup: function(evt) + { + // Needed for IE since the text box loses caret position on blur + this.storeSelectionPos(); + }, + callbackWords_OnMouseup: function(evt) + { + // Needed for IE since the text box loses caret position on blur + this.storeSelectionPos(); + }, callbackWords_OnFocus: function(evt) { // if (this.el_handle && this.el_handle.value == '' && !this.minmax_status) @@ -1027,6 +1039,35 @@ }, /** + * Stores the caret position for IE 6.x and 7.x + * Code based on: http://www.bazon.net/mishoo/articles.epl?art_id=1292 + */ + storeSelectionPos: function() + { + var w = this.el_words; + + // IE + if (w.createTextRange && document.selection) + { + // Determine current selection start position. + var range = document.selection.createRange(); + var isCollapsed = range.compareEndPoints("StartToEnd", range) == 0; + if (!isCollapsed) + range.collapse(true); + var b = range.getBookmark(); + w.selStart = b.charCodeAt(2) - b.charCodeAt(0) - 1; + + // Determine current selection end position. + range = document.selection.createRange(); + isCollapsed = range.compareEndPoints("StartToEnd", range) == 0; + if (!isCollapsed) + range.collapse(false); + b = range.getBookmark(); + w.selEnd = b.charCodeAt(2) - b.charCodeAt(0) - 1; + } + }, + + /** * insert a smiley */ insertSmiley: function(smiley) @@ -1045,7 +1086,25 @@ { // IE w.focus(); - document.selection.createRange().text = smiley; + + // Set range based on stored values. + var range = w.createTextRange(); + range.collapse(true); + range.moveStart("character", w.selStart); + range.moveEnd("character", w.selEnd - w.selStart); + range.select(); + + //document.selection.createRange().text = smiley; + range.text = smiley; + + // Check if internally kept values for selection are initialized. + if (w.selStart && w.selEnd) + { + w.selStart += smiley.length; + w.selEnd = w.selStart; + } + else + this.storeSelectionPos(); } else { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |