*/
( function( $ ) {
$.fn.textSelection = function( command, options ) {
+
+/**
+ * Helper function to get an IE TextRange object for an element
+ */
+function rangeForElementIE( e ) {
+ if ( e.nodeName.toLowerCase() == 'input' ) {
+ return e.createTextRange();
+ } else {
+ var sel = document.body.createTextRange();
+ sel.moveToElementText( e );
+ return sel;
+ }
+}
+
var fn = {
/**
* Get the contents of the textarea
*
* Inserts text at the begining and end of a text selection, optionally
* inserting text at the caret when selection is empty.
+ *
+ * @fixme document the options parameters
*/
encapsulateSelection: function( options ) {
return this.each( function() {
// IE
$(this).focus();
if ( context ) {
- context.fn.restoreStuffForIE();
+ context.fn.restoreCursorAndScrollTop();
}
var selText = $(this).textSelection( 'getSelection' );
var scrollTop = this.scrollTop;
*
* Get the position (in resolution of bytes not nessecarily characters)
* in a textarea
+ *
+ * @fixme document the options parameters
*/
getCaretPosition: function( options ) {
function getCaret( e ) {
// Create range containing text in the selection
var periRange = document.selection.createRange().duplicate();
// Create range containing text before the selection
- var preRange = document.body.createTextRange();
- // Select all the text
- preRange.moveToElementText(e);
+ var preRange = rangeForElementIE( e );
// Move the end where we need it
preRange.setEndPoint("EndToStart", periRange);
// Create range containing text after the selection
- var postRange = document.body.createTextRange();
- // Select all the text
- postRange.moveToElementText(e);
+ var postRange = rangeForElementIE( e );
// Move the start where we need it
postRange.setEndPoint("StartToEnd", periRange);
// Load the text values we need to compare
if ( preRange.compareEndPoints( "StartToEnd", preRange ) == 0 ) {
preFinished = true;
} else {
- preRange.moveEnd( "character", -1 )
+ preRange.moveEnd( "character", -1 );
if ( preRange.text == preText ) {
rawPreText += "\r\n";
} else {
}
return getCaret( this.get( 0 ) );
},
+/**
+ * @fixme document the options parameters
+ */
setSelection: function( options ) {
return this.each( function() {
if ( $(this).is( ':hidden' ) ) {
this.selectionEnd = options.end;
}
} else if ( document.body.createTextRange ) {
- var selection = document.body.createTextRange();
- selection.moveToElementText( this );
+ var selection = rangeForElementIE( this );
var length = this.value.length;
// IE doesn't count \n when computing the offset, so we won't either
var newLines = this.value.match( /\n/g );
* position with setSelection()
* @param options boolean Whether to force a scroll even if the caret position
* is already visible. Defaults to false
+ *
+ * @fixme document the options parameters (function body suggests options.force is a boolean, not options itself)
*/
scrollToCaretPosition: function( options ) {
function getLineLength( e ) {
- return Math.floor( e.scrollWidth / ( $.os.name == 'linux' ? 7 : 8 ) );
+ return Math.floor( e.scrollWidth / ( $.client.profile().platform == 'linux' ? 7 : 8 ) );
}
function getCaretScrollPosition( e ) {
// FIXME: This functions sucks and is off by a few lines most
charInLine = caret - lastSpaceInLine;
row++;
}
- return ( $.os.name == 'mac' ? 13 : ( $.os.name == 'linux' ? 15 : 16 ) ) * row;
+ return ( $.client.profile().platform == 'mac' ? 13 : ( $.client.profile().platform == 'linux' ? 15 : 16 ) ) * row;
}
return this.each(function() {
if ( $(this).is( ':hidden' ) ) {
break;
}
var context = $(this).data( 'wikiEditor-context' );
- var hasIframe = context !== undefined && context.$iframe !== undefined;
+ var hasIframe = typeof context !== 'undefined' && context && typeof context.$iframe !== 'undefined';
// IE selection restore voodoo
var needSave = false;
}
return retval;
};
-
} )( jQuery );