Page 1 of 1

IWCGJQHTMLEditor1 Set Focus

PostPosted: 16 Jun 2014 23:30
by jandekkers
With TMS' I would set the focus to the HTML Editor as follow (How do I do this with IWCGJQHTMLEditor ?)

>> Please note: I want to set the focus to the 1st line in the text, not at the bottom!

Code: Select all
 function fxSetJavaScriptFocus: string;
 var
   lJS  : TStringList;
 begin
   lJS := TStringList.Create;
   try
      lJS.Add('');
      lJS.Add('function setFocus()');
      lJS.Add('{');
      lJS.Add(' editor = TIWHTMLEDITMESSAGEgetIFrame(''TIWHTMLEDITMESSAGEeditorDiv'');');
      lJS.Add(' editor.focus();');
      lJS.Add('}');
      Result := lJS.Text;
   finally
      lJS.Free;
   end;
 end;

begin
  self.JavaScript.Text := fxSetJavaScriptFocus;
  AddToInitProc('window.onLoad=setFocus();');
end;

Re: IWCGJQHTMLEditor1 Set Focus

PostPosted: 17 Jun 2014 10:17
by Alexander Bulei
Hi jandekkers,

In this case, the setFocus function needs to be more complex.
We will fix/implement it asap.

Best Regards.

Re: IWCGJQHTMLEditor1 Set Focus

PostPosted: 17 Jun 2014 15:06
by Alexander Bulei
Hello,

Fixed/Implemented in V2.1.0.3689.

Thanks.

Best Regards.

Re: IWCGJQHTMLEditor1 Set Focus

PostPosted: 15 Jul 2014 18:51
by jandekkers
I am revisiting this problem. Currently I am using CG version 2.2.1.2 and would like to set the focus to the 1st line of text displayed in the HTML Editor. You said this was possible but how ?

Jan

Re: IWCGJQHTMLEditor1 Set Focus

PostPosted: 16 Jul 2014 09:05
by Alexander Bulei
Hi,

Please check this topic: viewtopic.php?f=3&t=1588

Best Regards.

Re: IWCGJQHTMLEditor1 Set Focus

PostPosted: 16 Jul 2014 18:28
by jandekkers
Thanks, for anyone interested I created this function In formcreate which works great:

Code: Select all
 
procedure TformPortalReplyCreate.IWAppFormCreate(Sender: TObject);

 function fxSetJavaScriptFocus: string;
 var
   lJS  : TStringList;
 begin

   lJS := TStringList.Create;
   try
      lJS.Add('');
      lJS.Add('function setCaretAtStartIframeBody(iframe)');
      lJS.Add('{');
      lJS.Add('  var doc = iframe.contentDocument,');
      lJS.Add('  win = iframe.contentWindow;');
      lJS.Add('  doc.body.focus();');
      lJS.Add('    if (win.getSelection && doc.createRange)');
      lJS.Add('   {');
      lJS.Add('        // IE >=9 and non-IE');
      lJS.Add('        var sel = win.getSelection();');
      lJS.Add('        var range = doc.createRange();');
      lJS.Add('        range.setStart(doc.body, 0);');
      lJS.Add('        range.collapse(true);');
      lJS.Add('        sel.removeAllRanges();');
      lJS.Add('        sel.addRange(range);');
      lJS.Add('    }');
      lJS.Add('    else if (doc.body.createTextRange)');
      lJS.Add('   {');
      lJS.Add('        // IE < 9');
      lJS.Add('        var textRange = doc.body.createTextRange();');
      lJS.Add('        textRange.moveToElementText(doc.body);');
      lJS.Add('        textRange.collapse(true);');
      lJS.Add('        textRange.select();');
      lJS.Add('    }');
      lJS.Add('}');
      lJS.Add('');
      lJS.Add('function setFocus()');
      lJS.Add('{');
      lJS.Add(Format('  var elrteObj = $("#%s").get(0).elrte;',[IWCGJQHTMLEditor1.JQHTMLName]));
      lJS.Add('  $(elrteObj.doc.body).scrollTop(0);');
      lJS.Add('  setCaretAtStartIframeBody(elrteObj.iframe);');
      lJS.Add('}');
      Result := lJS.Text;
   finally
      lJS.Free;
   end;
 end;

BEGIN
  self.JavaScript.Text := fxSetJavaScriptFocus;
  AddToInitProc('window.onLoad=setFocus();');
END;


Re: IWCGJQHTMLEditor1 Set Focus

PostPosted: 18 Jul 2014 09:43
by Jorge Sousa
Hi Jan

Thank you for sharing this code.

if you want, we can go forward to a next level, putting this javascript in a file and be loaded by Intraweb.

This will have the advantage of cutting a little bit in the upstream size, as well as be able to control this part from outside, without the need of recompile.

Re: IWCGJQHTMLEditor1 Set Focus

PostPosted: 21 Jul 2014 18:55
by jandekkers
since I have it implemented and working there is no direct need on my part but if it can help other people, sure that's great.

Jan

Re: IWCGJQHTMLEditor1 Set Focus

PostPosted: 22 Jul 2014 11:22
by Alexander Bulei
Hi jandekkers,

We have implemented the new method to do this ;)

Available since V2.2.1.12.

Best Regards.