bd266b35ad13240705674b5b25c13e1701a09651
[redakcja.git] / project / static / js / search_replace.js
1         EditArea.prototype.show_search = function(){\r
2                 if(_$("area_search_replace").style.visibility=="visible"){\r
3                         this.hidden_search();\r
4                 }else{\r
5                         this.open_inline_popup("area_search_replace");\r
6                         var text= this.area_get_selection();\r
7                         var search= text.split("\n")[0];\r
8                         _$("area_search").value= search;\r
9                         _$("area_search").focus();\r
10                 }\r
11         };\r
12         \r
13         EditArea.prototype.hidden_search= function(){\r
14                 /*_$("area_search_replace").style.visibility="hidden";\r
15                 this.textarea.focus();\r
16                 var icon= _$("search");\r
17                 setAttribute(icon, "class", getAttribute(icon, "class").replace(/ selected/g, "") );*/\r
18                 this.close_inline_popup("area_search_replace");\r
19         };\r
20         \r
21         EditArea.prototype.area_search= function(mode){\r
22                 \r
23                 if(!mode)\r
24                         mode="search";\r
25                 _$("area_search_msg").innerHTML="";             \r
26                 var search=_$("area_search").value;             \r
27                 \r
28                 this.textarea.focus();          \r
29                 this.textarea.textareaFocused=true;\r
30                 \r
31                 var infos= this.get_selection_infos();  \r
32                 var start= infos["selectionStart"];\r
33                 var pos=-1;\r
34                 var pos_begin=-1;\r
35                 var length=search.length;\r
36                 \r
37                 if(_$("area_search_replace").style.visibility!="visible"){\r
38                         this.show_search();\r
39                         return;\r
40                 }\r
41                 if(search.length==0){\r
42                         _$("area_search_msg").innerHTML=this.get_translation("search_field_empty");\r
43                         return;\r
44                 }\r
45                 // advance to the next occurence if no text selected\r
46                 if(mode!="replace" ){\r
47                         if(_$("area_search_reg_exp").checked)\r
48                                 start++;\r
49                         else\r
50                                 start+= search.length;\r
51                 }\r
52                 \r
53                 //search\r
54                 if(_$("area_search_reg_exp").checked){\r
55                         // regexp search\r
56                         var opt="m";\r
57                         if(!_$("area_search_match_case").checked)\r
58                                 opt+="i";\r
59                         var reg= new RegExp(search, opt);\r
60                         pos= infos["full_text"].substr(start).search(reg);\r
61                         pos_begin= infos["full_text"].search(reg);\r
62                         if(pos!=-1){\r
63                                 pos+=start;\r
64                                 length=infos["full_text"].substr(start).match(reg)[0].length;\r
65                         }else if(pos_begin!=-1){\r
66                                 length=infos["full_text"].match(reg)[0].length;\r
67                         }\r
68                 }else{\r
69                         if(_$("area_search_match_case").checked){\r
70                                 pos= infos["full_text"].indexOf(search, start); \r
71                                 pos_begin= infos["full_text"].indexOf(search); \r
72                         }else{\r
73                                 pos= infos["full_text"].toLowerCase().indexOf(search.toLowerCase(), start); \r
74                                 pos_begin= infos["full_text"].toLowerCase().indexOf(search.toLowerCase()); \r
75                         }               \r
76                 }\r
77                 \r
78                 // interpret result\r
79                 if(pos==-1 && pos_begin==-1){\r
80                         _$("area_search_msg").innerHTML="<strong>"+search+"</strong> "+this.get_translation("not_found");\r
81                         return;\r
82                 }else if(pos==-1 && pos_begin != -1){\r
83                         begin= pos_begin;\r
84                         _$("area_search_msg").innerHTML=this.get_translation("restart_search_at_begin");\r
85                 }else\r
86                         begin= pos;\r
87                 \r
88                 //_$("area_search_msg").innerHTML+="<strong>"+search+"</strong> found at "+begin+" strat at "+start+" pos "+pos+" curs"+ infos["indexOfCursor"]+".";\r
89                 if(mode=="replace" && pos==infos["indexOfCursor"]){\r
90                         var replace= _$("area_replace").value;\r
91                         var new_text="";                        \r
92                         if(_$("area_search_reg_exp").checked){\r
93                                 var opt="m";\r
94                                 if(!_$("area_search_match_case").checked)\r
95                                         opt+="i";\r
96                                 var reg= new RegExp(search, opt);\r
97                                 new_text= infos["full_text"].substr(0, begin) + infos["full_text"].substr(start).replace(reg, replace);\r
98                         }else{\r
99                                 new_text= infos["full_text"].substr(0, begin) + replace + infos["full_text"].substr(begin + length);\r
100                         }\r
101                         this.textarea.value=new_text;\r
102                         this.area_select(begin, length);\r
103                         this.area_search();\r
104                 }else\r
105                         this.area_select(begin, length);\r
106         };\r
107         \r
108         \r
109         \r
110         \r
111         EditArea.prototype.area_replace= function(){            \r
112                 this.area_search("replace");\r
113         };\r
114         \r
115         EditArea.prototype.area_replace_all= function(){\r
116         /*      this.area_select(0, 0);\r
117                 _$("area_search_msg").innerHTML="";\r
118                 while(_$("area_search_msg").innerHTML==""){\r
119                         this.area_replace();\r
120                 }*/\r
121         \r
122                 var base_text= this.textarea.value;\r
123                 var search= _$("area_search").value;            \r
124                 var replace= _$("area_replace").value;\r
125                 if(search.length==0){\r
126                         _$("area_search_msg").innerHTML=this.get_translation("search_field_empty");\r
127                         return ;\r
128                 }\r
129                 \r
130                 var new_text="";\r
131                 var nb_change=0;\r
132                 if(_$("area_search_reg_exp").checked){\r
133                         // regExp\r
134                         var opt="mg";\r
135                         if(!_$("area_search_match_case").checked)\r
136                                 opt+="i";\r
137                         var reg= new RegExp(search, opt);\r
138                         nb_change= infos["full_text"].match(reg).length;\r
139                         new_text= infos["full_text"].replace(reg, replace);\r
140                         \r
141                 }else{\r
142                         \r
143                         if(_$("area_search_match_case").checked){\r
144                                 var tmp_tab=base_text.split(search);\r
145                                 nb_change= tmp_tab.length -1 ;\r
146                                 new_text= tmp_tab.join(replace);\r
147                         }else{\r
148                                 // case insensitive\r
149                                 var lower_value=base_text.toLowerCase();\r
150                                 var lower_search=search.toLowerCase();\r
151                                 \r
152                                 var start=0;\r
153                                 var pos= lower_value.indexOf(lower_search);                             \r
154                                 while(pos!=-1){\r
155                                         nb_change++;\r
156                                         new_text+= this.textarea.value.substring(start , pos)+replace;\r
157                                         start=pos+ search.length;\r
158                                         pos= lower_value.indexOf(lower_search, pos+1);\r
159                                 }\r
160                                 new_text+= this.textarea.value.substring(start);                                \r
161                         }\r
162                 }                       \r
163                 if(new_text==base_text){\r
164                         _$("area_search_msg").innerHTML="<strong>"+search+"</strong> "+this.get_translation("not_found");\r
165                 }else{\r
166                         this.textarea.value= new_text;\r
167                         _$("area_search_msg").innerHTML="<strong>"+nb_change+"</strong> "+this.get_translation("occurrence_replaced");\r
168                         // firefox and opera doesn't manage with the focus if it's done directly\r
169                         //editArea.textarea.focus();editArea.textarea.textareaFocused=true;\r
170                         setTimeout("editArea.textarea.focus();editArea.textarea.textareaFocused=true;", 100);\r
171                 }\r
172                 \r
173                 \r
174         };\r