PageSourceSearch

https://taswerat.com/uploads/js/desertEditor.js?1693017985

js taswerat.com collected 2026-09-24 14:18:57 UTC 20,764 bytes, 577 lines download raw bytes

1/* Mahmoud's Editor
2 * Do not remove this copyright message
3 
4		
5 */
6var desertEditor = {
7	
8	init: function(id,odata=""){
9
10		this.id = id;
11		var textArea = document.getElementById(id);
12		
13		if(!document.getElementById("DesertEditorNo_"+id)){
14			var desertEditorOuter = document.createElement("div");
15			
16			desertEditorOuter.id="DesertEditorNo_"+id;
17			desertEditorOuter.style=textArea.getAttribute("style");
18			desertEditorOuter.style.display="inline-block";
19			
20			
21			desertEditorOuter.classList.add("desertEditorOuter");
22			desertEditorOuter.style.minHeight=(textArea.offsetHeight)+"px";
23			
24			var desertEditorHeader = document.createElement("div");
25			desertEditorHeader.classList.add("desertEditorHeader");
26			desertEditorHeader.setAttribute("aria-hidden","true");
27			
28			for(x in desertEditor.buttons){
29				var btn = desertEditor.buttons[x];
30				var createBtn = document.createElement("span");
31				createBtn.classList.add(btn.icon);
32				createBtn.onclick = btn.exec;
33				desertEditorHeader.appendChild(createBtn);
34			}
35			desertEditorOuter.appendChild(desertEditorHeader);
36			
37			var desertEditorEditable = document.createElement("div");
38			desertEditorEditable.classList.add("desertEditorEditable");
39			desertEditorEditable.id="DesertEditorTextareaNo_"+id;
40			desertEditorEditable.setAttribute("contenteditable","true");
41			desertEditorEditable.style="max-height:"+(window.innerHeight-desertEditorHeader.offsetHeight)+"px;min-height:"+(textArea.offsetHeight)+"px;overflow:auto;";
42			desertEditorEditable.innerHTML = textArea.value;
43			desertEditorOuter.appendChild(desertEditorEditable);
44			this.editableArea = desertEditorEditable;
45			
46			var desertEditorBottomInfo = document.createElement("div");
47			desertEditorBottomInfo.classList.add("desertEditorHeader");
48			desertEditorOuter.appendChild(desertEditorBottomInfo);
49			
50			desertEditorEditable.onkeydown = function(){
51				desertEditor.addSelect();
52				desertEditor.createContainer();
53			}
54			desertEditorEditable.onkeyup = function(){
55				var totalLetters=0;
56				var nodes = this.childNodes,nn;
57				
58				nodes.forEach((x)=>{
59					if(x.nodeName.toString().toLowerCase().indexOf("text")>-1){
60							var np=document.createElement("p");
61							np.innerHTML = x.data;
62							console.log(x.nodeName);
63							x.parentNode.insertBefore(np,x);
64							x.parentNode.removeChild(x);
65					}
66				});
67				
68				/*div to p*/
69				for(var i=0; i<nodes.length; i++){
70					if (nodes[i].nodeName.toLowerCase() == 'div'){
71						nn = document.createElement("p");
72							for(var ii=0;ii<nodes[i].attributes.length;ii++){
73								nn.setAttribute(nodes[i].attributes[ii].name,nodes[i].getAttribute(nodes[i].attributes[ii].name));
74							}
75						nn.innerHTML = nodes[i].innerHTML;
76						nodes[i].parentNode.replaceChild(nn,nodes[i]);
77					 }
78					if(nodes[i].innerText){
79						totalLetters +=nodes[i].innerText.length;
80						desertEditorBottomInfo.innerText="عدد الأحرف:"+(totalLetters);
81					}
82				}
83				/*div to p*/
84				desertEditor.updateTE();
85			};
86			desertEditorEditable.onclick=function(){desertEditor.addSelect();};
87			
88			desertEditorEditable.onmouseout= function(){desertEditor.updateTE();};
89
90			desertEditorEditable.ondragover=function(){desertEditorEditable.style.background="red";};
91			desertEditorEditable.ondragleave=function(){desertEditorEditable.style.background="";};
92			desertEditorEditable.ondrop=function(){
93				desertEditorEditable.style.background="";
94				//desertEditor.manageDrop();
95			};
96			desertEditorEditable.addEventListener("paste",function(){
97				console.log(event.clipboardData.getData("text"));
98			},false);
99			
100			
101			this.textArea=textArea;
102			this.editor=desertEditorOuter;
103			this.editorHeader=desertEditorHeader;
104			this.editorData=desertEditorEditable;
105			this.start();
106		}
107		
108		if(odata!=""){
109			this.editorData.innerHTML = odata;
110			this.textArea.value = odata;
111		}
112			this.editorData.innerHTML = textArea.value;
113		
114		desertEditorEditable.style.maxHeight=(window.innerHeight-desertEditorHeader.offsetHeight)+"px";
115	},
116	ranges:[],
117	
118	buttons: [
119		{name:"list-ol",icon:"fa-list-ol",exec:function(){document.execCommand('insertOrderedList');}},
120		{name:"list-ul",icon:"fa-list-ul",exec:function(){document.execCommand('insertUnorderedList');}},
121		{name:"table",icon:"fa-table",exec:function(){desertEditor.insertHTML('table');}},
122		{name:"link",icon:"fa-link",exec:function(){desertEditor.insertHTML('a');}},
123		{name:"image",icon:"fa-image",exec:function(){desertEditor.clickInputFile();}},
124		{name:"youtube",icon:"fa-youtube",exec:function(){desertEditor.insertHTML('iframe');}},
125		{name:"map-marker",icon:"fa-map-marker",exec:function(){desertEditor.insertHTML('iframe');}},
126		{name:"font",icon:"fa-font",exec:function(){desertEditor.fontsList(this);}},
127		{name:"bold",icon:"fa-bold",exec:function(){document.execCommand('bold');}},
128		{name:"italic",icon:"fa-italic",exec:function(){document.execCommand('italic');}},
129		{name:"underline",icon:"fa-underline",exec:function(){document.execCommand('underline');}},
130		{name:"strikethrough",icon:"fa-strikethrough",exec:function(){document.execCommand('strikethrough');}},
131		{name:"subscript",icon:"fa-subscript",exec:function(){document.execCommand('subscript');}},
132		{name:"superscript",icon:"fa-superscript",exec:function(){document.execCommand('superscript');}},
133		{name:"undo",icon:"fa-undo",exec:function(){document.execCommand('undo');}},
134		{name:"redo",icon:"fa-repeat",exec:function(){document.execCommand('redo');}},
135		{name:"align-left",icon:"fa-align-left",exec:function(){document.execCommand('justifyLeft');}},
136		{name:"align-center",icon:"fa-align-center",exec:function(){document.execCommand('justifyCenter');}},
137		{name:"align-right",icon:"fa-align-right",exec:function(){document.execCommand('justifyRight');}},
138		{name:"align-justify",icon:"fa-align-justify",exec:function(){document.execCommand('justifyFull');}},
139		{name:"outdent",icon:"fa-outdent",exec:function(){document.execCommand('outdent');}},
140		{name:"indent",icon:"fa-indent",exec:function(){document.execCommand('indent');}},
141		{name:"fullscreen",icon:"fa-expand",exec:function(){event.target.parentNode.parentNode.requestFullscreen();if(document.fullscreenElement){document.exitFullscreen();}}},
142		
143		
144	],
145	allowDrop: function(){
146		event.preventDefault();
147	},
148	hideChild: function(elm){
149		$(elm).parent().slideUp();
150	},
151	updateTE: function(){
152		var id = desertEditor.id;
153		const thisEditor=document.getElementById("DesertEditorTextareaNo_"+id);
154		const thisEditorDiv2 = document.querySelector("#DesertEditorTextareaNo_"+id);
155		const thisEditorTa = document.getElementById(id);
156		thisEditorTa.value = thisEditorDiv2.innerHTML;
157		//console.log(thisEditorTa.value);
158	},
159	addSelect: function(){
160		this.seledesertEditorEditabledItem=document.getSelection();
161		this.seledesertEditorEditabledNode=document.getSelection().anchorNode;
162		this.seledesertEditorEditabledRange=document.createRange();
163		this.seledesertEditorEditabledText=document.getSelection().toString();
164		var cnodes = this.editableArea.childNodes;
165		
166		
167		this.editableArea.querySelectorAll("*").forEach(function(x){
168			x.addEventListener("click",function(){
169				desertEditor.lastClicked = (event.target);
170			})
171		});
172		
173		
174		for(var i=0;i<this.seledesertEditorEditabledItem.rangeCount;i++){
175			desertEditor.ranges[i] = this.seledesertEditorEditabledItem.getRangeAt(i);
176		}
177		console.log(desertEditor.ranges);
178	},
179	createContainer: function(){
180		if(this.seledesertEditorEditabledItem.anchorNode.tagName!==undefined){
181			
182				if(this.seledesertEditorEditabledItem.anchorNode.tagName.toLowerCase()==="div" & this.seledesertEditorEditabledItem.anchorNode.childNodes.length===0){
183					np = document.createElement("p");
184					np.innerHTML = " ";
185					this.seledesertEditorEditabledItem.anchorNode.appendChild(np);
186					if(document.body.createTextRange){
187						range = document.body.createTextRange();
188						range.moveToElementText(np);
189						range.select();
190					}else{
191						sel = this.seledesertEditorEditabledItem;
192						range = this.seledesertEditorEditabledRange;
193						range.selectNodeContents(np);
194						sel.removeAllRanges();
195						sel.addRange(range);
196					}
197				}
198			}
199	
200	},
201	insertHTML: async function(el='span',spec=''){
202		if(event){event.preventDefault();}
203		
204		
205		if(el=="table"){
206			var colrow = await desertEditor.prompt("كم عدد الأعمدة والصفوف بالجدول؟<br>",[{title:"عدد الأعمدة",name:"cols",type:"number"},{title:"عدد الصفوف",name:"rows",type:"number"}]);
207			console.log(colrow);
208			if(colrow.cols>0 & colrow.rows>
2080){
209				var tds='';
210				for(n=0;n<colrow.rows;n++){
211						tds+='<tr>';
212						for(nn=0;nn<colrow.cols;nn++){
213							tds+='<td style="border:solid 2px #e0e0e0;">نموذج</td>';
214						}
215						tds+='</tr>';
216				}
217				
218				
219				var s = document.getSelection();
220				s.removeAllRanges();
221				s.addRange(desertEditor.ranges[0]);
222				
223				var r = document.createRange();
224				var e = document.createElement("table");
225				e.innerHTML=tds;
226				e.style='width:100%';
227				s.anchorNode.after(e)
228			}
229			
230				
231		}
232	
233		if(el=="iframe"){
234			var aurl = await desertEditor.prompt("فضلاً أدخل عنوان الرابط<br>",[{title:"رابط الخريطة او الفيديو",name:"url",type:"link"}]);
235			if(aurl.url.length>0){
236				
237				var s = document.getSelection();
238				s.removeAllRanges();
239				s.addRange(desertEditor.ranges[0]);
240				
241				var r = document.createRange();
242				var e = document.createElement("iframe");
243				e.src=aurl.url;
244				e.style='width:100%;min-width:min-height:300px;border:soild 2px #e0e0e0;';
245				s.anchorNode.parentNode.after(e);
246			}
247		}
248		if(el=="a"){
249			var aurl = await desertEditor.prompt("أدخل رابط الصفحة<br>",[{title:"https://example.com/*",name:"url",type:"link"}]);
250			if(aurl.url.length>0){
251				
252				var s = document.getSelection();
253				s.removeAllRanges();
254				s.addRange(desertEditor.ranges[0]);
255				
256				var r = document.createRange();
257				
258				var e = document.createElement("a");
259				e.href=aurl.url;
260				e.innerHTML=s.toString();
261				e.target="blank";
262				//s.anchorNode.after(e);
263				desertEditor.ranges[0].deleteContents();
264				desertEditor.ranges[0].insertNode(e);
265			}
266		}
267		if(el=="img"){
268			var aurl = await desertEditor.prompt("فضلاً أدخل رابط الصورة<br>",[{title:"رابط مباشر للصورة",name:"url",type:"link"}]);
269			if(aurl.url.length>0){
270				
271				var s = document.getSelection();
272				s.removeAllRanges();
273				s.addRange(desertEditor.ranges[0]);
274				
275				var r = document.createRange();
276				var e = document.createElement("img");
277				e.src=aurl.url;
278				e.alt="فشل تحميل الصورة";
279				e.style='width:100%;border:soild 2px #e0e0e0;';
280				s.anchorNode.after(e);
281			}
282		}
283	},
284	insertHTMLOld: function(el='span',spec=''){
285		if(el=="table"){
286			var num=prompt("عدد الأعمدة؟");
287			if(num>0){
288					var tds='';
289				for(n=0;n<num;n++){
290					tds+='<td style="background:#eee;"></td>';
291				}
292					tds = '<tr>'+tds+'</tr>';
293				document.execCommand("insertHTML",false,"<br><table style='width:100%;'>"+tds+"</table><br>");
294			}	
295		}
296	},
297	fontsList: function(t){
298		event.preventDefault();
299		var oe=t.outerHTML;
300		var noe = '<select style="display:inline;width:auto;" onchange="desertEditor.fontChange(\'fontName\',this.value);">';
301		noe += '<option>Arial</option>';
302		noe += '<option>Calibri</option>';
303		noe += '</select>';
304		noe += '<select style="display:inline;width:auto;" onchange="document.execCommand(\'fontSize\',false,this.value);">';
305		noe += '<option>1</option>';
306		noe += '<option>2</option>';
307		noe += '<option>3</option>';
308		noe += '<option>4</option>';
309		noe += '<option>5</option>';
310		noe += '<option>6</option>';
311		noe += '<option>7</option>';
312		noe += '</select>';
313		t.outerHTML=noe;
314	},
315	fontChange: function(d,t){
316		event.preventDefault();
317		//document.execCommand('formatblock', false, 'p');
318		document.execCommand('fontName', false, t);
319		window.getSelection().focusNode.parentNode.style.fontFamily=t;
320
321	},
322	align: function(d){
323		this.lastClicked.style.textAlign=d;
324	},
325	start: function(){
326		//this.textArea.parentNode.replaceChild(this.editor,this.textArea);
327		this.textArea.setAttribute("style","display:none");
328		//this.textArea.setAttribute("style","height:0px;width:0px;");
329		this.textArea.after(this.editor);
330	},
331	restart: function(){
332	
333		this.textArea.style.display="none";
334		this.editorData.innerHTML=this.textArea.value;
335		this.editor.style.display="block";
336	},
337	toggle: function(id){
338		
339		const thisEditor=document.getElementById("DesertEditorTextareaNo_"+id);
340		const thisEditorDiv2 = document.querySelector("#DesertEditorTextareaNo_"+id);
341		const thisEditorTa = document.getElementById(id);
342		thisEditorTa.value = thisEditorDiv2.innerHTML;
343		
344		thisEditorTa.style.display="block";
345		thisEditorDiv2.parentNode.style.display="none";
346	},
347	clickInputFile: function(){
348			var cnI=document.createElement("input");
349			cnI.name="images";
350			cnI.type="file";
351			cnI.accept="image/*";
352			
353			cnI.onchange = function(i){
354						for(var i=0;i<this.files.length;i++){
355						if(desertEditor.valideTypes(this.files[i])===true){
356								const nimg = document.createElement("img");
357								nimg.src="uploads/images/style/loading.gif";
358								nimg.style.maxWidth="100%";
359								nimg.appendChild(document.createElement("br")).prepend(document.createElement("br"));
360								desertEditor.editableArea.appendChild(nimg);
361							alert(1);	desertEditor.handleFiles(this.files[i],nimg);
362alert(2);
363							}
364						}	
365			};
366			cnI.click();
367	},
368	manageDrop :function(){
369			const targetedNode=(event.target);
370			const edt=event.dataTransfer;
371			event.preventDefault();
372			if(edt.files.length){
373				for(var i=0;i<edt.files.length;i++){
374						if(desertEditor.valideTypes(edt.files[i])===true){
375							const nimg = document.createElement("img");
376							nimg.src="img/loading.gif";
377							nimg.style.maxWidth="100%";
378							targetedNode.appendChild(nimg);
379							desertEditor.handleFiles(edt.files[i],nimg);
380							
381						}
382				}
383			}else{
384				return false;
385				var html = edt.getData('text/html');
386				var virt =  document.createElement("p");
387				virt.innerHTML='<br>'+html+'<br>';
388				let imgz = virt.querySelectorAll("img");
389				let elmz = virt.querySelectorAll("*");
390				for(var a=0;a<elmz.length;a++){
391					if(elmz[a].tagName.toLowerCase()=="script"){elmz[a].outerHTML='';continue;} /*users*/
392					var ea=elmz[a].attributes;
393					for(var b=0;b<ea.length;b++){
394						if(ea[b].name.indexOf("on")===0 | ea[b].name.indexOf("id")===0){
395							elmz[a].removeAttribute(ea[b].name);/*users*/
396							continue;
397						}
398					}
399					
400				}
401				for(var a=0;a<imgz.length;a++){
402							console.log(imgz[a]);
403							const nimg = document.createElement("img");
404							nimg.src="uploads/images/style/loading.gif";
405							nimg.style.maxWidth="100%";
406							targetedNode.appendChild(nimg);
407							desertEditor.handleFiles(imgz[a],nimg);							
408							continue;
409				}
410				
411				console.log(virt.innerHTML);
412				targetedNode.appendChild(virt);
413				
414			}
415		desertEditor.updateTE();
416	},
417	valideTypes: function(file){
418	    // check the type
419			var validTypes = ['image/jpeg', 'image/png', 'image/gif'];
420			if (validTypes.indexOf( file.type ) === -1) {
421				alert("صيغة الصورةغير مدعومة");
422				return false;
423				
424			}
425			// check the size
426			var maxSizeInBytes = 10e6; // 10MB
427			if (file.size > maxSizeInBytes) {
428				alert("حجم الصورة كبير");
429				return false;
430			}
431			return true;
432	
433	},
434	handleFiles: function(image,loading){
435 
436	 
437		// create FormData
438		var formData = new FormData();
439		formData.append('image[]', image);
440		formData.append('key', 'a63d9975c05452160e15e93a4c766119');
441	 
442		// upload the image
443		var uploadLocation = 'https://api.imgbb.com/1/upload';
444		//var uploadLocation = 'uploads/uploader.php';
445	 
446		var ajax = new XMLHttpRequest();
447		ajax.open("POST", uploadLocation, true);
448	 
449		ajax.onreadystatechange = function(e) {
450			if (ajax.readyState === 4) {
451				if (ajax.status === 200) {
452alert(ajax.responseText);
453					console.log(ajax.responseText);
454					res = JSON.parse(ajax.responseText);
455					loading.src=(res.images[0].url);
456					loading.setAttribute("thumb_url",res.images[0].thumb);
457					loading.setAttribute("mob_url",res.images[0].mob);
458					loading.setAttribute("web_url",res.images[0].url);
459					
460				} else {
461					loading.outerHTML='';
462				}
463			}
464			desertEditor.updateTE();
465		}
466		ajax.setRequestHeader("Content-type", "multipart/form-data");
467		ajax.send(formData);	 
468	},
469toast: function(data,tOut=3000,ops={}){
470		
471		document.querySelector("#toast").classList.replace("toastHidden","toastShown");
472		document.querySelector("#toast").innerHTML=data;
473		if(typeof(ops.type)!="undefined"){
474			
475			
476		}		
477		if(typeof(autoHideToastInterval)!="undefined"){	clearTimeout(autoHideToastInterval);}
478		
479		if(tOut!==false){
480			autoHideToastInterval=setTimeout(function(){
481				if(document.querySelector("#toast").classList.contains("toastShown")){			
482						document.querySelector("#toast").classList.replace("toastShown","toastHidden");
483				}else{
484					document.querySelector("#toast").innerHTML="";
485				}
486			},tOut);				
487		}
488	},
489	confirm: async function(data,buttonz=[],tOut=null){		
490		document.querySelector("#toast").classList.replace("toastHidden","toastShown");
491		document.querySelector("#toast").innerHTML =data;
492		var btn=[],btnsH="",buttonz = buttonz;
493		return new Promise(function(complete){
494			for(var i in buttonz){
495				btn[i]=document.createElement("button");			
496				btn[i].addEventListener("click",async function(){desert.toastHide(); return complete(event.target.value);},false);
497				btn[i].value=buttonz[i].data;
498				btn[i].innerText=buttonz[i].title;
499				btnsH+=btn[i].outerHTML;
500				document.querySelector("#toast").appendChild(btn[i]);
501			}
502			
503		
504		if(typeof(autoHideToastInterval)!="undefined"){	clearTimeout(autoHideToastInterval);}
505		if(tOut!=null){
506			autoHideToastInterval=setTimeout(function(){
507				if(document.querySelector("#toast").classList.contains("toastShown")){			
508						document.querySelector("#toast").classList.replace("toastShown","toastHidden");
509				}else{
510					document.querySelector("#toast").innerHTML="";
511				}
512			},tOut);
513		}
514		});
515	},
516	prompt: async function(data,buttonz=[],tOut=null){		
517		document.querySelector("#toast").classList.replace("toastHidden","toastShown");
518		document.querySelector("#toast").innerHTML =data+"<br>";
519		var nform,btn=[],btnsH="",subBtn,cxlBtn,formJson=[],buttonz = buttonz;
520		return new Promise(function(complete){
521				nform=document.createElement("form");
522				nform.name = "prom";
523				nform.onsubmit = function(){return false;};
524			for(var i in buttonz){
525				btn[i]=document.createElement("input");
526				btn[i].type=buttonz[i].type;
527				btn[i].name=buttonz[i].name;
528				btn[i].setAttribute("placeholder",buttonz[i].title);
529				btnsH+=btn[i].outerHTML;
530				nform.appendChild(btn[i]);
531				nform.innerHTML+='<br>';
532			}
533			nform.addEventListener("keyup",function(x){formJsonUp();},false);
534			var formJsonUp = function(){
535				for(var i in buttonz){
536					formJson[buttonz[i].name] = nform[buttonz[i].name].value;
537				}
538				return formJson;
539			}
540				nform.innerHTML+='<hr>';
541				subBtn=document.createElement("button");			
542				subBtn.addEventListener("click",async function(){desert.toastHide(); return complete(formJson);},false);
543				subBtn.innerText="تأكيد";
544				nform.appendChild(subBtn);
545				cxlBtn=document.createElement("button");			
546				cxlBtn.addEventListener("click",async function(){desert.toastHide();},false);
547				cxlBtn.innerText="الغاء";
548				nform.appendChild(cxlBtn);
549				
550				document.querySelector("#toast").appendChild(nform);
551			
552			
553		
554		if(typeof(autoHideToastInterval)!="undefined"){	clearTimeout(autoHideToastInterval);}
555		if(tOut!=null){
556			autoHideToastInterval=setTimeout(function(){
557				if(document.querySelector("#toast").classList.contains("toastShown")){			
558						document.querySelector("#toast").classList.replace("toastShown","toastHidden");
559				}else{
560					document.querySelector("#toast").innerHTML="";
561				}
562			},tOut);
563		}
564		});
565	},
566	toastHide:function(){
567		document.querySelector("#toast").classList.replace("toastShown","toastHidden");
568		document.querySelector("#toast").innerHTML="";
569	},
570
571}	
572
573
574
575
576
577

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.