-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreformator_uploader.js
86 lines (79 loc) · 2.28 KB
/
reformator_uploader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Copyright Art. Lebedev | http://www.artlebedev.ru/
Author: Vladimir Tokmakov | vlalek
Updated: 2017-05-19
*/
/*
Create UPLOADER_SCRIPT which save posted file and return JSON:
{path: 'PATH_TO_FILE'}
Insert into sidebar.html:
<ul class="format_group">
<li class="text">
<form action="UPLOADER_SCRIPT" method="post" enctype="multipart/form-data" target="upload_and_insert_img">
Insert<br/>
<a href="#upload_and_insert_img" class="pseudo">
image...
<input name="file" type="file" onchange="reformator_uploader.submit_upload_form(this.parentNode.parentNode)"/>
</a>
</form>
<iframe id="upload_and_insert_img" name="upload_and_insert_img" onload="reformator_uploader.insert_img(this, parent.reformator)"></iframe>
</li>
</ul>
*/
var reformator_uploader = {
submit_upload_form: function(form_element){
form_element.submit();
form_element.reset();
},
insert_img: function(iframe_element, reformator){
if(reformator.current){
var result = this.get_result(iframe_element);
if(!result){return;}
if(result.error){
alert(result.error);
}else if(result.path){
reformator.current.wysiwyg.insert('<img src="' + result.path + '"/>', null, null, false);
}
}
},
insert_a: function(iframe_element, reformator){
if(reformator.current){
var result = this.get_result(iframe_element);
if(!result){return;}
if(result.error){
alert(result.error);
}else if(result.path){
reformator.current.wysiwyg.insert('<a href="' + result.path + '">', '</a>', null, false);
}
}
},
insert_a_img: function(iframe_element, reformator){
if(reformator.current){
var result = this.get_result(iframe_element);
if(!result){return;}
if(result.error){
alert(result.error);
}else if(result.path){
reformator.current.wysiwyg.insert('<a href="' + result.path + '"><img src="' + result.preview_path + '"/></a>', null, null, false);
}
}
},
get_result: function(iframe_element){
var doc = iframe_element.contentDocument
? iframe_element.contentDocument
: (
iframe_element.contentWindow
? iframe_element.contentWindow.document
: null
);
if(!doc || doc.location.href == 'about:blank'){
return;
}
var result = doc.body.innerHTML;
if(result){
return eval('new Object(' + result + ')');
}else{
return;
}
}
};