Esanders has uploaded a new change for review.
https://gerrit.wikimedia.org/r/248315
Change subject: TextInputWidget: Improve selection API
......................................................................
TextInputWidget: Improve selection API
Provide a generic #selectRange methdo and run all existing
selection calls through this (#select and #moveCursorToEnd).
Provide #moveCursorToStart for symmetry.
Change-Id: I4109d5ad6089035cf689e11667e6ec2bb007d6ce
---
M src/widgets/TextInputWidget.js
1 file changed, 58 insertions(+), 14 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/oojs/ui refs/changes/15/248315/1
diff --git a/src/widgets/TextInputWidget.js b/src/widgets/TextInputWidget.js
index e09c78c..2a06162 100644
--- a/src/widgets/TextInputWidget.js
+++ b/src/widgets/TextInputWidget.js
@@ -474,30 +474,74 @@
};
/**
- * Select the entire text of the input.
+ * Focus the input and select a specified range within the text.
*
+ * @param {number} from Select from offset
+ * @param {number} [to] Select to offset, defaults to from
* @chainable
*/
-OO.ui.TextInputWidget.prototype.select = function () {
- this.$input.select();
+OO.ui.TextInputWidget.prototype.selectRange = function ( from, to ) {
+ var textRange, isBackwards, start, end,
+ element = this.$input[ 0 ];
+
+ to = to || from;
+
+ isBackwards = to < from;
+ start = isBackwards ? to : from;
+ end = isBackwards ? from : to;
+
+ this.focus();
+
+ if ( element.setSelectionRange ) {
+ element.setSelectionRange( start, end, isBackwards ? 'backward'
: 'forward' );
+ } else if ( element.createTextRange ) {
+ // IE 8 and below
+ textRange = element.createTextRange();
+ textRange.collapse( true );
+ textRange.moveStart( 'character', start );
+ textRange.moveEnd( 'character', end - start );
+ textRange.select();
+ }
return this;
};
/**
+ * Get the length of the text input value.
+ *
+ * This could differ from the length of #getValue if the
+ * value gets filtered
+ *
+ * @return {number} Input length
+ */
+OO.ui.TextInputWidget.prototype.getInputLength = function () {
+ return this.$input[ 0 ].value.length;
+};
+
+/**
+ * Focus the input and select the entire text.
+ *
+ * @chainable
+ */
+OO.ui.TextInputWidget.prototype.select = function () {
+ return this.selectRange( 0, this.getInputLength() );
+};
+
+/**
+ * Focus the input and move the cursor to the start.
+ *
+ * @chainable
+ */
+OO.ui.TextInputWidget.prototype.moveCursorToStart = function () {
+ return this.selectRange( 0 );
+};
+
+/**
* Focus the input and move the cursor to the end.
+ *
+ * @chainable
*/
OO.ui.TextInputWidget.prototype.moveCursorToEnd = function () {
- var textRange,
- element = this.$input[ 0 ];
- this.focus();
- if ( element.selectionStart !== undefined ) {
- element.selectionStart = element.selectionEnd =
element.value.length;
- } else if ( element.createTextRange ) {
- // IE 8 and below
- textRange = element.createTextRange();
- textRange.collapse( false );
- textRange.select();
- }
+ return this.selectRange( this.getInputLength() );
};
/**
--
To view, visit https://gerrit.wikimedia.org/r/248315
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4109d5ad6089035cf689e11667e6ec2bb007d6ce
Gerrit-PatchSet: 1
Gerrit-Project: oojs/ui
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits