Rev 3425 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
11617 | bpr | 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 | // Mise à Jour : 04.06.2017 |
||
11 | // Objet : le texte selectionne etait oublie lorsque text_ contient des accolades (*) |
||
12 | // le curseur est positionne entre les deux accolades si il n'y a pas de texte |
||
13 | // selectionne |
||
14 | //------------------------------------------------------------- |
||
15 | //-(*)------------------ |
||
16 | function Get_NbrCR(txt_){ |
||
17 | var NbrCR = 0; |
||
18 | var Pos = txt_.indexOf("\r\n"); |
||
19 | while( Pos > -1){ |
||
20 | Pos = txt_.indexOf("\r\n", Pos+2); |
||
21 | NbrCR ++; |
||
22 | } |
||
23 | return( NbrCR); |
||
24 | } |
||
25 | //---------------------------------- |
||
26 | function Cursor_SetPos( where_, pos_){ |
||
27 | //-- Recup l'Objet |
||
28 | var Obj = document.getElementById( where_); |
||
29 | if( Obj){ |
||
30 | Obj.focus(); |
||
31 | if( typeof Obj.selectionStart != "undefined"){ |
||
32 | Obj.setSelectionRange( pos_, pos_); |
||
33 | } |
||
34 | else{ // IE and consort |
||
35 | var Chaine = Obj.createTextRange(); |
||
36 | Chaine.moveStart('character', pos_); |
||
37 | //-- Deplace le curseur |
||
38 | Chaine.collapse(); |
||
39 | Chaine.select(); |
||
40 | } |
||
41 | //-- Retour valeur Reelle placee |
||
42 | return( Cursor_GetPos( where_, pos_)); |
||
43 | } |
||
44 | } |
||
45 | //---------------------------------- |
||
46 | function Cursor_GetPos( where_, pos_){ |
||
47 | //-- Recup l'Objet |
||
48 | var Obj= document.getElementById(where_); |
||
49 | if( Obj){ |
||
50 | //-- Focus sur Objet |
||
51 | Obj.focus(); |
||
52 | if(typeof Obj.selectionStart != "undefined") |
||
53 | return Obj.selectionStart; |
||
54 | else{ // IE and consort |
||
55 | var szMark = "~~"; |
||
56 | var Chaine = Obj.value; |
||
57 | //-- Cree un double et insert la Mark ou est le curseur |
||
58 | var szTmp = document.selection.createRange(); |
||
59 | szTmp.text = szMark; |
||
60 | //-- Recup. la position du curseur |
||
61 | var PosDeb = Obj.value.search(szMark); |
||
62 | //-(*)- Supprime les retours Chariot |
||
63 | var szAvant = Chaine.substring( 0 , PosDeb); |
||
64 | PosDeb -= Get_NbrCR( szAvant); |
||
65 | //-- Restaure valeur initiale |
||
66 | Obj.value = Chaine; |
||
67 | Chaine = Obj.createTextRange(); |
||
68 | //-- Deplace le Debut de la chaine |
||
69 | Chaine.moveStart('character', PosDeb); |
||
70 | //-- Deplace le curseur |
||
71 | Chaine.collapse(); |
||
72 | Chaine.select(); |
||
73 | return( PosDeb); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | //------------------------------------ |
||
78 | function Cursor_AddTexte(where_, txt_){ |
||
79 | //-- Recup l'Objet |
||
80 | var Obj = document.getElementById( where_); |
||
81 | if( Obj){ |
||
82 | //-- Focus sur Objet |
||
83 | Obj.focus(); |
||
84 | if( typeof Obj.selectionStart != "undefined"){ |
||
85 | //-- Position du curseur |
||
86 | var PosDeb = Obj.selectionStart; |
||
87 | var PosFin = Obj.selectionEnd; |
||
88 | //-- Recup. des Chaines |
||
89 | var Chaine = Obj.value; |
||
90 | var szAvant = Chaine.substring( 0 , PosDeb); |
||
91 | var szApres = Chaine.substring( PosFin, Obj.textLength ); |
||
92 | //-- Recup. texte selectionne |
||
93 | var szSelect = Chaine.substring( PosDeb, PosFin); |
||
94 | //-- Insertion du texte |
||
95 | if (txt_.indexOf("{") == -1) { |
||
96 | Obj.value = szAvant + szSelect + txt_ + szApres; |
||
97 | //-- Replace le curseur |
||
98 | Obj.setSelectionRange( szAvant.length + txt_.length, szAvant.length + txt_.length); |
||
99 | } else { |
||
100 | Obj.value = szAvant + txt_.substring(0, txt_.length-1) + szSelect + txt_.substring(txt_.length-1,txt_.length) + szApres; |
||
101 | //-- Replace le curseur |
||
102 | if( szSelect.length > 0) { |
||
103 | Obj.setSelectionRange( szAvant.length + szSelect.length + txt_.length+1, szAvant.length + szSelect.length + txt_.length+1); |
||
104 | } |
||
105 | else |
||
106 | { // curseur entre les deux accolades |
||
107 | Obj.setSelectionRange( szAvant.length + txt_.length-2, szAvant.length + szSelect.length + txt_.length-2 ); |
||
108 | } |
||
109 | } |
||
110 | //-- Replace le Focus |
||
111 | Obj.focus(); |
||
112 | } |
||
113 | else{ // IE and consort |
||
114 | //-- Recup. de la selection |
||
115 | var szSelect = document.selection.createRange().text; |
||
116 | //-- Si du Texte est selectionne on le remplace |
||
117 | if( szSelect.length > 0){ |
||
118 | var Chaine = document.selection.createRange(); |
||
119 | Chaine.text = txt_ ; |
||
120 | Chaine.collapse(); |
||
121 | Chaine.select(); |
||
122 | } |
||
123 | else{ |
||
124 | var Chaine = Obj.value; |
||
125 | var szMark ="~~"; |
||
126 | //-- Cree un double et insert la Mark ou est le curseur |
||
127 | var szTmp = document.selection.createRange().duplicate(); |
||
128 | szTmp.text = szMark; |
||
129 | //-- Recup. la position du curseur |
||
130 | var PosDeb = Obj.value.search(szMark); |
||
131 | //-- Recup. des Chaines |
||
132 | var szAvant = Chaine.substring( 0 , PosDeb); |
||
133 | var szApres = Chaine.substring( PosDeb, Obj.textLength ); |
||
134 | //-- Insertion du texte |
||
135 | Obj.value = szAvant + txt_ + szSelect + szApres; |
||
136 | //-- Repositionne le curseur |
||
137 | PosDeb += txt_.length; |
||
138 | //-(*)- Supprime les retours Chariot |
||
139 | PosDeb -= Get_NbrCR( szAvant); |
||
140 | //-- Recup de la Chaine |
||
141 | Chaine = Obj.createTextRange(); |
||
142 | //-- Deplace le Debut de la chaine |
||
143 | Chaine.moveStart('character', PosDeb); |
||
144 | //-- Deplace le curseur |
||
145 | Chaine.collapse(); |
||
146 | Chaine.select(); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | //-- EOF ------------------------------------------------------ |