From a2179ea178c2e8cdcef9fd0cab517048778383de Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Wed, 18 Nov 2020 11:22:25 +0100 Subject: [PATCH] ClientJS: Funktion zum Setzen der Cursorposition (z.B. in textareas) --- SL/ClientJS.pm | 2 ++ js/client_js.js | 3 ++- js/kivi.js | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/SL/ClientJS.pm b/SL/ClientJS.pm index 336f97144..7d8c362c9 100644 --- a/SL/ClientJS.pm +++ b/SL/ClientJS.pm @@ -123,6 +123,8 @@ my %supported_methods = ( run_once_for => 3, # kivi.run_once_for(, ) scroll_into_view => 1, # $()[0].scrollIntoView() + + set_cursor_position => 2, # kivi.set_cursor_position(, ) ); my %trim_target_for = map { ($_ => 1) } qw(insertAfter insertBefore appendTo prependTo); diff --git a/js/client_js.js b/js/client_js.js index 9fc14b6c6..d231d10ca 100644 --- a/js/client_js.js +++ b/js/client_js.js @@ -113,7 +113,7 @@ ns.eval_json_result = function(data) { // ## jQuery UI dialog plugin ## - // Opening and closing and closing a popup + // Opening and closing a popup else if (action[0] == 'dialog:open') kivi.popup_dialog(action[1]); else if (action[0] == 'dialog:close') $(action[1]).dialog('close'); @@ -159,6 +159,7 @@ ns.eval_json_result = function(data) { else if (action[0] == 'run') kivi.run(action[1], action.slice(2, action.length)); else if (action[0] == 'run_once_for') kivi.run_once_for(action[1], action[2], action[3]); else if (action[0] == 'scroll_into_view') $(action[1])[0].scrollIntoView(); + else if (action[0] == 'set_cursor_position') kivi.set_cursor_position(action[1], action[2]); else console.log('Unknown action: ' + action[0]); diff --git a/js/kivi.js b/js/kivi.js index b2b39e180..77dfe5c63 100644 --- a/js/kivi.js +++ b/js/kivi.js @@ -662,6 +662,15 @@ namespace("kivi", function(ns) { $input.parent().replaceWith($area); $area.focus(); }; + + ns.set_cursor_position = function(selector, position) { + var $input = $(selector); + if (position === 'end') + position = $input.val().length; + + $input.prop('selectionStart', position); + $input.prop('selectionEnd', position); + }; }); kivi = namespace('kivi'); -- 2.20.1