Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3425 | obado | 1 | //------------------------------------------------------------- |
2 | // Nom Document : GFCURSOR.JS |
||
3 | // Auteur : G.Ferraz |
||
4 | // Objet : Gestion du curseur dans TEXTAERA et INPUT... |
||
5 | // Création : 18.09.2006 |
||
6 | //------------------------------------------------------------- |
||
7 | // Mise à Jour : 20.11.2006 |
||
8 | // Objet : Vilain BUG avec le retour chariot sous IE (*) |
||
9 | //------------------------------------------------------------- |
||
10 | //-(*)------------------ |
||
11 | function Get_NbrCR(txt_){ |
||
12 | var NbrCR = 0; |
||
13 | var Pos = txt_.indexOf("\r\n"); |
||
14 | while( Pos > -1){ |
||
15 | Pos = txt_.indexOf("\r\n", Pos+2); |
||
16 | NbrCR ++; |
||
17 | } |
||
18 | return( NbrCR); |
||
19 | } |
||
20 | //---------------------------------- |
||
21 | function Cursor_SetPos( where_, pos_){ |
||
22 | //-- Recup l'Objet |
||
23 | var Obj = document.getElementById( where_); |
||
24 | if( Obj){ |
||
25 | Obj.focus(); |
||
26 | if( typeof Obj.selectionStart != "undefined"){ |
||
27 | Obj.setSelectionRange( pos_, pos_); |
||
28 | } |
||
29 | else{ // IE and consort |
||
30 | var Chaine = Obj.createTextRange(); |
||
31 | Chaine.moveStart('character', pos_); |
||
32 | //-- Deplace le curseur |
||
33 | Chaine.collapse(); |
||
34 | Chaine.select(); |
||
35 | } |
||
36 | //-- Retour valeur Reelle placee |
||
37 | return( Cursor_GetPos( where_, pos_)); |
||
38 | } |
||
39 | } |
||
40 | //---------------------------------- |
||
41 | function Cursor_GetPos( where_, pos_){ |
||
42 | //-- Recup l'Objet |
||
43 | var Obj= document.getElementById(where_); |
||
44 | if( Obj){ |
||
45 | //-- Focus sur Objet |
||
46 | Obj.focus(); |
||
47 | if(typeof Obj.selectionStart != "undefined") |
||
48 | return Obj.selectionStart; |
||
49 | else{ // IE and consort |
||
50 | var szMark = "~~"; |
||
51 | var Chaine = Obj.value; |
||
52 | //-- Cree un double et insert la Mark ou est le curseur |
||
53 | var szTmp = document.selection.createRange(); |
||
54 | szTmp.text = szMark; |
||
55 | //-- Recup. la position du curseur |
||
56 | var PosDeb = Obj.value.search(szMark); |
||
57 | //-(*)- Supprime les retours Chariot |
||
58 | var szAvant = Chaine.substring( 0 , PosDeb); |
||
59 | PosDeb -= Get_NbrCR( szAvant); |
||
60 | //-- Restaure valeur initiale |
||
61 | Obj.value = Chaine; |
||
62 | Chaine = Obj.createTextRange(); |
||
63 | //-- Deplace le Debut de la chaine |
||
64 | Chaine.moveStart('character', PosDeb); |
||
65 | //-- Deplace le curseur |
||
66 | Chaine.collapse(); |
||
67 | Chaine.select(); |
||
68 | return( PosDeb); |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | //------------------------------------ |
||
73 | function Cursor_AddTexte(where_, txt_){ |
||
74 | //-- Recup l'Objet |
||
75 | var Obj = document.getElementById( where_); |
||
76 | if( Obj){ |
||
77 | //-- Focus sur Objet |
||
78 | Obj.focus(); |
||
79 | if( typeof Obj.selectionStart != "undefined"){ |
||
80 | //-- Position du curseur |
||
81 | var PosDeb = Obj.selectionStart; |
||
82 | var PosFin = Obj.selectionEnd; |
||
83 | //-- Recup. des Chaines |
||
84 | var Chaine = Obj.value; |
||
85 | var szAvant = Chaine.substring( 0 , PosDeb); |
||
86 | var szApres = Chaine.substring( PosFin, Obj.textLength ); |
||
87 | //-- Recup. texte selectionne |
||
88 | var szSelect = Chaine.substring( PosDeb, PosFin); |
||
89 | //-- Insertion du texte |
||
90 | Obj.value = szAvant + txt_ + szApres; |
||
91 | //-- Replace le curseur |
||
92 | Obj.setSelectionRange( szAvant.length + txt_.length, szAvant.length + txt_.length ); |
||
93 | //-- Replace le Focus |
||
94 | Obj.focus(); |
||
95 | } |
||
96 | else{ // IE and consort |
||
97 | //-- Recup. de la selection |
||
98 | var szSelect = document.selection.createRange().text; |
||
99 | //-- Si du Texte est selectionne on le remplace |
||
100 | if( szSelect.length > 0){ |
||
101 | var Chaine = document.selection.createRange(); |
||
102 | Chaine.text = txt_ ; |
||
103 | Chaine.collapse(); |
||
104 | Chaine.select(); |
||
105 | } |
||
106 | else{ |
||
107 | var Chaine = Obj.value; |
||
108 | var szMark ="~~"; |
||
109 | //-- Cree un double et insert la Mark ou est le curseur |
||
110 | var szTmp = document.selection.createRange().duplicate(); |
||
111 | szTmp.text = szMark; |
||
112 | //-- Recup. la position du curseur |
||
113 | var PosDeb = Obj.value.search(szMark); |
||
114 | //-- Recup. des Chaines |
||
115 | var szAvant = Chaine.substring( 0 , PosDeb); |
||
116 | var szApres = Chaine.substring( PosDeb, Obj.textLength ); |
||
117 | //-- Insertion du texte |
||
118 | Obj.value = szAvant + txt_ + szSelect + szApres; |
||
119 | //-- Repositionne le curseur |
||
120 | PosDeb += txt_.length; |
||
121 | //-(*)- Supprime les retours Chariot |
||
122 | PosDeb -= Get_NbrCR( szAvant); |
||
123 | //-- Recup de la Chaine |
||
124 | Chaine = Obj.createTextRange(); |
||
125 | //-- Deplace le Debut de la chaine |
||
126 | Chaine.moveStart('character', PosDeb); |
||
127 | //-- Deplace le curseur |
||
128 | Chaine.collapse(); |
||
129 | Chaine.select(); |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | //-- EOF ------------------------------------------------------ |