media.js
7.0 KB
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
/*******************************************************************************
* KindEditor - WYSIWYG HTML Editor for Internet
* Copyright (C) 2006-2011 kindsoft.net
*
* @author Roddy <luolonghao@gmail.com>
* @site http://www.kindsoft.net/
* @licence http://www.kindsoft.net/license.php
*******************************************************************************/
KindEditor.plugin('media', function(K) {
var self = this, name = 'media', lang = self.lang(name + '.'),
allowMediaUpload = K.undef(self.allowMediaUpload, false),
allowFileManager = K.undef(self.allowFileManager, false),
formatUploadUrl = K.undef(self.formatUploadUrl, true),
extraParams = K.undef(self.extraFileUploadParams, {}),
filePostName = K.undef(self.filePostName, 'imgFile'),
uploadJson = K.undef(self.uploadJson, self.basePath + 'php/upload_json.php');
self.plugin.media = {
edit : function() {
var html = [
'<div class="ke-dialog-content-inner">',
//url
'<div class="ke-dialog-row ke-clearfix">',
'<label for="keUrl" class="row-left">' + lang.url + ':</label>',
'<div class="row-right">',
'<input class="ke-input-text" type="text" id="keUrl" name="url" value="" style="width:180px;" /> ',
'<input type="button" class="ke-upload-button" value="' + lang.upload + '" /> ',
'<span class="ke-button-common ke-button-outer">',
'<input type="button" class="ke-button-common ke-button" name="viewServer" value="' + lang.viewServer + '" />',
'</span>',
'<div>支持优酷、爱奇艺、土豆、腾讯视频、56等视频网站【<span style="color:green">通用代码</span>】',
'</div>',
'</div>',
'</div>',
//width
'<div class="ke-dialog-row ke-clearfix">',
'<label for="keWidth" class="row-left">' + lang.width + ':</label>',
'<div class="row-right">',
'<input type="text" id="keWidth" class="ke-input-text ke-input-number" name="width" value="550" maxlength="4" />',
'</div>',
'</div>',
//height
'<div class="ke-dialog-row ke-clearfix">',
'<label for="keHeight" class="row-left">' + lang.height + ':</label>',
'<div class="row-right">',
'<input type="text" id="keHeight" class="ke-input-text ke-input-number" name="height" value="400" maxlength="4" />',
'</div>',
'</div>',
//autostart
'<div class="ke-dialog-row ke-clearfix">',
'<label for="keAutostart" class="row-left">' + lang.autostart + ':</label>',
'<div class="row-right">',
'<input type="checkbox" id="keAutostart" name="autostart" class="ke-input-checkbox" value="" /> ',
'</div>',
'</div>',
'</div>'
].join('');
var dialog = self.createDialog({
name : name,
width : 450,
height : 260,
title : self.lang(name),
body : html,
yesBtn : {
name : self.lang('yes'),
click : function(e) {
var url = K.trim(urlBox.val()),
width = widthBox.val(),
height = heightBox.val();
var match = url.match(/^<iframe\s(.*?)src=('|")(.*?)('|")/);
if(!match) {
if (url == 'http://' || K.invalidUrl(url)) {
K.options.errorMsgHandler(self.lang('invalidUrl'), "error");
urlBox[0].focus();
return;
}
}
if (!/^\d*$/.test(width)) {
K.options.errorMsgHandler(self.lang('invalidWidth'), "error");
widthBox[0].focus();
return;
}
if (!/^\d*$/.test(height)) {
K.options.errorMsgHandler(self.lang('invalidHeight'), "error");
heightBox[0].focus();
return;
}
if(!match) {
var html = K.mediaImg(self.themesPath + 'common/blank.gif', {
src: url,
type: K.mediaType(url),
width: width,
height: height,
autostart: autostartBox[0].checked ? 'true' : 'false',
loop: 'true'
});
} else {
var html = '<iframe src="' + match[3] + '" frameborder="0" style="width:' + width + 'px;height:' + height + 'px;"></iframe>';
}
self.insertHtml(html).hideDialog().focus();
}
}
}),
div = dialog.div,
urlBox = K('[name="url"]', div),
viewServerBtn = K('[name="viewServer"]', div),
widthBox = K('[name="width"]', div),
heightBox = K('[name="height"]', div),
autostartBox = K('[name="autostart"]', div);
urlBox.val('http://');
if (allowMediaUpload) {
var uploadbutton = K.uploadbutton({
button : K('.ke-upload-button', div)[0],
fieldName : filePostName,
extraParams : extraParams,
url : K.addParam(uploadJson, 'fileType=media'),
afterUpload : function(data) {
dialog.hideLoading();
if (data.code == "000") {
var url = data.data.url;
if (formatUploadUrl) {
url = K.formatUrl(url, 'absolute');
}
urlBox.val(url);
if (self.afterUpload) {
self.afterUpload.call(self, url, data, name);
}
K.options.errorMsgHandler(self.lang('uploadSuccess'), "ok");
} else {
K.options.errorMsgHandler(data.message, "error", "error");
}
},
afterError : function(html) {
dialog.hideLoading();
self.errorDialog(html);
}
});
uploadbutton.fileBox.change(function(e) {
dialog.showLoading(self.lang('uploadLoading'));
uploadbutton.submit();
});
} else {
K('.ke-upload-button', div).hide();
}
if (allowMediaUpload && allowFileManager) {
viewServerBtn.click(function(e) {
self.loadPlugin('filemanager', function() {
self.plugin.filemanagerDialog({
dirName : 'media',
clickFn : function(url) {
K('[name="url"]', div).val(url);
if (self.afterSelectFile) {
self.afterSelectFile.call(self, url);
}
}
});
});
});
} else {
K("#keUrl").css("width", "280px");
viewServerBtn.hide();
}
var img = self.plugin.getSelectedMedia();
if (img) {
var attrs = K.mediaAttrs(img.attr('data-ke-tag'));
urlBox.val(attrs.src);
widthBox.val(K.removeUnit(img.css('width')) || attrs.width || 0);
heightBox.val(K.removeUnit(img.css('height')) || attrs.height || 0);
autostartBox[0].checked = (attrs.autostart === 'true');
}
urlBox[0].focus();
urlBox[0].select();
},
'delete' : function() {
self.plugin.getSelectedMedia().remove();
// [IE] 删除图片后立即点击图片按钮出错
self.addBookmark();
}
};
self.clickToolbar(name, self.plugin.media.edit);
});