-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcleditor-imageupload-plugin.js
131 lines (115 loc) · 5.35 KB
/
cleditor-imageupload-plugin.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
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
$(document).ready(function() {
var dlgBody = '';
// Image button
$.cleditor.buttons.image = {
name: 'image',
title: 'Insert/Upload Image',
command: 'insertimage',
popupName: 'image',
popupClass: 'cleditorPrompt',
stripIndex: $.cleditor.buttons.image.stripIndex,
popupContent: 'Please configure imageListUrl',
buttonClick: insertImageClick,
uploadUrl: 'imageUpload',
imageListUrl: 'imagesList'
};
function insertImageClick(e, data) {
createDlg($.cleditor.buttons.image.imageListUrl, $.cleditor.buttons.image.uploadUrl, data);
}
function createDlg(imagesListUrl, uploadUrl, data) {
var editor = data.editor;
var popup = data.popup;
var hiddenFrameName = '__uploadIframe';
$.getJSON(imagesListUrl, function(jsonData) {
// Create dialog with images list
dlgBody = '<div class="tabbable">' +
'<ul class="nav nav-tabs">' +
' <li class="active"><a href="#tab1" data-toggle="tab">Images list</a></li>' +
' <li><a href="#tab2" data-toggle="tab">Upload image</a></li>' +
'</ul>' +
'<div class="tab-content">' +
// Images list panel
' <div class="tab-pane active span7" id="tab1">' +
' <p>Images list</p>' +
' <div style="overflow-y: auto; max-height:200px;" class="well">';
for (var i = 0; i < jsonData.length; i++) {
dlgBody += '<div style="width:15%; padding:5px; float:left;">' +
'<a href="#" id="imgBtn' + i + '" class="btn thumbnail">' +
'<img src="' + jsonData[i] + '" width="100"/>' +
'</a></div>';
}
dlgBody += '</div>' +
'</div>' +
// Image upload panel
'<div class="tab-pane span7" id="tab2">' +
'<iframe style="width:0;height:0;border:0;" name="' + hiddenFrameName + '" />' +
'<table cellpadding="5" cellspacing="0">' +
'<tr><td>Choose a File:</td></tr>' +
'<tr><td> ' +
'<form id="imgUploadForm" method="post" enctype="multipart/form-data" action="" target="' + hiddenFrameName + '">' +
'<input id="imageName" name="imageName" type="file" /></form> </td></tr>' +
'<tr><td>Or enter URL:</td></tr>' +
'<tr><td><input type="text" id="cleditorUrl" size="40" value="" /></td></tr>' +
'</table><input type="button" id="uploadBtn" value="Submit">' +
'</div>' +
'</div>' +
'</div>';
$(popup).html(dlgBody);
// Insert image
$(data.popup).find('.btn').unbind("click").bind("click", function(e) {
e.preventDefault();
// Insert some image into the document
editor.execCommand(data.command, $(this).children("img").attr("src"), null, data.button);
hidePopup(editor);
});
urlTextField = $(data.popup).find(':text'),
url = $.trim(urlTextField.val()),
uploadIframe = $(data.popup).find('iframe'),
loadedFile = $(data.popup).find(':file');
// Cleaning of previously selected file and url
loadedFile.val('');
urlTextField.val('').focus();
// Submit button click event
$(data.popup).find(':button').unbind("click").bind("click", function(e) {
// User downloads the file
var img_url = urlTextField.val();
if (loadedFile.val()) {
uploadIframe.bind('load', function() {
var fileUrl = '';
try {
fileUrl = uploadIframe.get(0).contentWindow.document.getElementById('image').innerHTML;
} catch (e) {
console.log('Error: ' + e);
}
if (fileUrl) {
editor.execCommand(data.command, fileUrl, null, data.button);
} else {
alert('An error occured during upload!');
}
uploadIframe.unbind('load');
hidePopup(editor);
});
$(data.popup).find('form').attr('action', uploadUrl);
$(data.popup).find('form').submit();
// User puts URL
} else if (img_url != '') {
if(checkImgURL(img_url)){
editor.execCommand(data.command, img_url, null, data.button);
hidePopup(editor);
} else {
alert('Please enter valid image URL');
}
} else {
alert('Please uplod image or URL');
}
});
});
}
function hidePopup(editor) {
editor.hidePopups();
editor.focus();
}
function checkImgURL(url) {
return(url.match(/\.(jpeg|jpg|gif|png|bmp)$/) != null);
}
});