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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
<script src="{{ settings.MEDIA_URL }}admin/js/vendor/jquery/jquery.js" type="text/javascript"></script>
<script src={{ settings.MEDIA_URL }}codemirror/codemirror.js></script>
<script src={{ settings.MEDIA_URL }}codemirror/xml.js></script>
<script src={{ settings.MEDIA_URL }}codemirror/javascript.js></script>
<script src={{ settings.MEDIA_URL }}codemirror/css.js></script>
<script src={{ settings.MEDIA_URL }}codemirror/htmlmixed.js></script>
<link rel=stylesheet href={{ settings.MEDIA_URL }}codemirror/codemirror.css>
<style type=text/css>
html {
font-family: "Helvetica Neue", sans-serif;
width: 100%;
color: #666666;
text-align: center;
}
.popup-overlay {
/*Hides pop-up when there is no "active" class*/
visibility: hidden;
position: fixed;
top: 10%;
left: 10%;
background: #ffffff;
border: 3px solid #666666;
width: 80%;
height: 80%;
overflow: scroll;
left: 5%;
z-index: 20;
}
.HTMLarea {
width:100%; # width:130%;
# margin-left: -235px
}
.popup-overlay.active {
/*displays pop-up when "active" class is present*/
visibility: visible;
text-align: center;
}
.popup-content {
/*Hides pop-up content when there is no "active" class */
visibility: hidden;
}
.popup-content.active {
/*Shows pop-up content when "active" class is present */
visibility: visible;
}
button {
display: inline-block;
vertical-align: middle;
border-radius: 30px;
margin: .20rem;
font-size: 1rem;
color: #666666;
background: #ffffff;
border: 1px solid #666666;
}
button:hover {
border: 1px solid #666666;
background: #666666;
color: #ffffff;
}
</style>
<style type=text/css>
.CodeMirror {
float: left;
width: 85%;
border: 1px solid black;
height: 70%;
}
.CodeMirror-scroll{
height: 99%
}
iframe {
width: 49%;
height: 70%;
float: left;
border: 1px solid black;
border-left: 0px;
}
body{max-width: none;
margin-left: 275px;
margin-right: 50px;
text-align: left;
}
#id_change_message{
height: 5%;
}
</style>
<script>
$(function() {
$(".HTMLarea").each(function(){
var HTMLarea = $(this).children("textarea")
var preview = $(this).children('.HTMLpreview')
if ( preview.length )
{
HTMLarea.data("editor",
CodeMirror.fromTextArea(HTMLarea[0], {
lineWrapping: true,
mode: 'text/html',
tabMode: 'indent',
onChange: function() {
clearTimeout(HTMLarea.data("delay"));
HTMLarea.data("delay", setTimeout(updatePreview, 300, HTMLarea));
}
})
);
HTMLarea.data("preview", preview);
updatePreview(HTMLarea);
} else {
HTMLarea.data("editor",
CodeMirror.fromTextArea(HTMLarea[0], {
lineWrapping: true,
mode: 'text/html',
tabMode: 'indent'
})
);
}
});
$(".addTag").click(function(){
addTag($(this).parents(".HTMLarea").children("textarea").data("editor"),
$(this).attr("data-tag"),
$(this).attr("data-attr") || ""
);
})
$(".addStr").click(function(){
addStr($(this).parents(".HTMLarea").children("textarea").data("editor"),
$(this).attr("data-str")
);
})
$(".addImage").click(function(){
add_image_popup($(this).parents(".HTMLarea").children("textarea").data("editor"));
})
});
function updatePreview(HTMLarea) {
var preview = HTMLarea.data("preview")[0].contentDocument || $('#preview')[0].contentWindow.document;
preview.open();
preview.write("<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><link rel='stylesheet' type='text/css' href='/css/main2.css' /> <style type=text/css>body{max-width: none;margin-left: 15px;margin-right: 15px;}</style></head><body>");
preview.write(HTMLarea.data("editor").getValue());
preview.write("</body></html>");
preview.close();
}
function addTag(editor, tag, attr){
var from = editor.getCursor(true);
var to = editor.getCursor(false);
editor.replaceRange("</"+tag+">", to);
if (attr.length > 0) {attr = " " + attr;}
editor.replaceRange("<"+tag+attr+">", from);
editor.focus();
editor.setCursor({line: to.line , ch : to.ch + 2 + tag.length + attr.length });
}
function addStr(editor, x){
var to = editor.getCursor(false);
editor.replaceRange(x, to);
editor.focus();
editor.setCursor({line: to.line , ch : to.ch + x.length });
}
function add_image_popup(editor) {
$('.add-image-popup').addClass('active');
window.current_editor = editor;
var path = ""; //Can we get rid of this variable?
/*
{% if not path %}
var path = $("#id_url").val();
{% else %}
var path = "";
{% endif %}
*/
$('#image_popup_content').load("{% url 'image_selector' path %}" + path, function() {
$('.thumbnail').click(function(){
$(".add-image-popup").removeClass("active");
addStr(window.current_editor, $( this ).attr("data-html"))
});
})
}
function new_image_popup() {
$('.add-image-popup').removeClass('active');
$('.new-image-popup').addClass('active');
$.ajax({
type : "GET",
dataType: "json",
url: "{% url 'new_image_form' path %}",
success: function(data){handle_new_image(data)}
});
}
function handle_new_image(data) {
if (data.hasOwnProperty('form')) {
$('#new_image_popup_content').html(data.form);
$('#new_image_form').on('submit', function(e){
e.preventDefault();
data = $('#new_image_form').serialize();
$.ajax({
type : "POST",
dataType: "json",
url: "{% url 'new_image_form' path %}",
data: new FormData($('#new_image_form')[0]),
processData: false,
contentType: false,
success: function(data){
handle_new_image(data);
}
});
});
}
else if (data.hasOwnProperty('html')) {
$('.new-image-popup').removeClass('active');
addStr(window.current_editor, data.html);
}
else {
alert(data.error);
}
}
</script>
|