~azzar1/unity/add-show-desktop-key

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