image.js
20.5 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
(function () {
document.domain="1n4j.com";
var utils = UM.utils,
browser = UM.browser,
Base = {
checkURL: function (url) {
if(!url) return false;
url = utils.trim(url);
if (url.length <= 0) {
return false;
}
if (url.search(/http:\/\/|https:\/\//) !== 0) {
url += 'http://';
}
url=url.replace(/\?[\s\S]*$/,"");
if (!/(.gif|.jpg|.jpeg|.png)$/i.test(url)) {
return false;
}
return url;
},
getAllPic: function (sel, $w, editor) {
var me = this,
arr = [],
$imgs = $(sel, $w);
$.each($imgs, function (index, node) {
$(node).removeAttr("width").removeAttr("height");
// if (node.width > editor.options.initialFrameWidth) {
// me.scale(node, editor.options.initialFrameWidth -
// parseInt($(editor.body).css("padding-left")) -
// parseInt($(editor.body).css("padding-right")));
// }
return arr.push({
_src: node.src,
src: node.src
});
});
return arr;
},
scale: function (img, max, oWidth, oHeight) {
var width = 0, height = 0, percent, ow = img.width || oWidth, oh = img.height || oHeight;
if (ow > max || oh > max) {
if (ow >= oh) {
if (width = ow - max) {
percent = (width / ow).toFixed(2);
img.height = oh - oh * percent;
img.width = max;
}
} else {
if (height = oh - max) {
percent = (height / oh).toFixed(2);
img.width = ow - ow * percent;
img.height = max;
}
}
}
return this;
},
close: function ($img) {
$img.css({
top: ($img.parent().height() - $img.height()) / 2,
left: ($img.parent().width()-$img.width())/2
}).prev().on("click",function () {
if ( $(this).parent().remove().hasClass("edui-image-upload-item") ) {
//显示图片计数-1
Upload.showCount--;
Upload.updateView();
}
});
return this;
},
createImgBase64: function (img, file, $w) {
if (browser.webkit) {
//Chrome8+
img.src = window.webkitURL.createObjectURL(file);
} else if (browser.gecko) {
//FF4+
img.src = window.URL.createObjectURL(file);
} else {
//实例化file reader对象
var reader = new FileReader();
reader.onload = function (e) {
img.src = this.result;
$w.append(img);
};
reader.readAsDataURL(file);
}
},
callback: function (editor, $w, url, state) {
if (state == "SUCCESS") {
//显示图片计数+1
Upload.showCount++;
var $img = $("<img src='" + url + "' class='edui-image-pic' />"),
$item = $("<div class='edui-image-item edui-image-upload-item'><div class='edui-image-close'></div></div>").append($img);
if ($(".edui-image-upload2", $w).length < 1) {
$(".edui-image-content", $w).append($item);
Upload.render(".edui-image-content", 2)
.config(".edui-image-upload2");
} else {
$(".edui-image-upload2", $w).before($item).show();
}
$img.on("load", function () {
Base.scale(this, 120);
Base.close($(this));
$(".edui-image-content", $w).focus();
});
} else {
currentDialog.showTip( state );
window.setTimeout( function () {
currentDialog.hideTip();
}, 3000 );
}
Upload.toggleMask();
}
};
/*
* 本地上传
* */
var Upload = {
showCount: 0,
uploadTpl: '<div class="edui-image-upload%%">' +
'<span class="edui-image-icon"></span>' +
'<form class="edui-image-form" method="post" enctype="multipart/form-data" target="up">' +
'<input style=\"filter: alpha(opacity=0);\" class="edui-image-file" type="file" hidefocus name="image" accept="image/gif,image/jpeg,image/png,image/jpg,image/bmp"/>' +
'</form>' +
'</div>',
init: function (editor, $w) {
var me = this;
me.editor = editor;
me.dialog = $w;
me.render(".edui-image-local", 1);
me.config(".edui-image-upload1");
me.submit();
me.drag();
$(".edui-image-upload1").hover(function () {
$(".edui-image-icon", this).toggleClass("hover");
});
if (!(UM.browser.ie && UM.browser.version <= 9)) {
$(".edui-image-dragTip", me.dialog).css("display", "block");
}
return me;
},
render: function (sel, t) {
var me = this;
$(sel, me.dialog).append($(me.uploadTpl.replace(/%%/g, t)));
return me;
},
config: function (sel) {
var me = this,
url=me.editor.options.imageUrl;
url=url + (url.indexOf("?") == -1 ? "?" : "&") + "editorid="+me.editor.id;//初始form提交地址;
$("form", $(sel, me.dialog)).attr("action", url);
return me;
},
uploadComplete: function(r){
var me = this;
try{
var json = eval('('+r+')');
Base.callback(me.editor, me.dialog, json.url, json.state);
}catch (e){
var lang = me.editor.getLang('image');
Base.callback(me.editor, me.dialog, '', (lang && lang.uploadError) || 'Error!');
}
},
submit: function (callback) {
var me = this,
input = $( '<input style="filter: alpha(opacity=0);" class="edui-image-file" type="file" hidefocus="" name="image" accept="image/gif,image/jpeg,image/png,image/jpg,image/bmp">'),
input = input[0];
$(me.dialog).delegate( ".edui-image-file", "change", function ( e ) {
if ( !this.parentNode ) {
return;
}
$('<iframe name="up" style="display: none"></iframe>').insertBefore(me.dialog).on('load', function(){
var r = this.contentWindow.document.body.innerHTML;
if(r == '')return;
r=r.replaceAll("\n","");
me.uploadComplete(r);
$(this).unbind('load');
$(this).remove();
});
$(this).parent()[0].submit();
Upload.updateInput( input );
me.toggleMask("Loading....");
callback && callback();
});
return me;
},
//更新input
updateInput: function ( inputField ) {
$( ".edui-image-file", this.dialog ).each( function ( index, ele ) {
ele.parentNode.replaceChild( inputField.cloneNode( true ), ele );
} );
},
//更新上传框
updateView: function () {
if ( Upload.showCount !== 0 ) {
return;
}
$(".edui-image-upload2", this.dialog).hide();
$(".edui-image-dragTip", this.dialog).show();
$(".edui-image-upload1", this.dialog).show();
},
drag: function () {
var me = this;
//做拽上传的支持
if (!UM.browser.ie9below) {
me.dialog.find('.edui-image-content').on('drop',function (e) {
//获取文件列表
var fileList = e.originalEvent.dataTransfer.files;
var img = document.createElement('img');
var hasImg = false;
$.each(fileList, function (i, f) {
if (/^image/.test(f.type)) {
//创建图片的base64
Base.createImgBase64(img, f, me.dialog);
// var xhr = new XMLHttpRequest();
// xhr.open("post", me.editor.getOpt('imageUrl') + "?type=ajax", true);
// xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
//模拟数据
var fd = new FormData();
fd.append(me.editor.getOpt('imageFieldName'), f);
XMLHttp.sendRequest("POST",me.editor.getOpt('imageUrl') + "?type=ajax",fd,function(e){
var r = e.response, json;
r=$(r)[6].data;
r=r.replaceAll("\n","");
me.uploadComplete(r);
if (i == fileList.length - 1) {
$(img).remove()
}
});
// xhr.send(fd);
// xhr.addEventListener('load', function (e) {
// var r = e.target.response, json;
// r=$(r)[6].data;
// r=r.replaceAll("\n","");
// me.uploadComplete(r);
// if (i == fileList.length - 1) {
// $(img).remove()
// }
// });
// xhr.removeListener("load");
hasImg = true;
}
});
if (hasImg) {
e.preventDefault();
me.toggleMask("Loading....");
}
}).on('dragover', function (e) {
e.preventDefault();
});
}
},
toggleMask: function (html) {
var me = this;
var $mask = $(".edui-image-mask", me.dialog);
if (html) {
if (!(UM.browser.ie && UM.browser.version <= 9)) {
$(".edui-image-dragTip", me.dialog).css( "display", "none" );
}
$(".edui-image-upload1", me.dialog).css( "display", "none" );
$mask.addClass("edui-active").html(html);
} else {
$mask.removeClass("edui-active").html();
if ( Upload.showCount > 0 ) {
return me;
}
if (!(UM.browser.ie && UM.browser.version <= 9) ){
$(".edui-image-dragTip", me.dialog).css("display", "block");
}
$(".edui-image-upload1", me.dialog).css( "display", "block" );
}
return me;
}
};
/*
* 网络图片
* */
var NetWork = {
init: function (editor, $w) {
var me = this;
me.editor = editor;
me.dialog = $w;
me.initEvt();
},
initEvt: function () {
var me = this,
url,
$ele = $(".edui-image-searchTxt", me.dialog);
$(".edui-image-searchAdd", me.dialog).on("click", function () {
url = Base.checkURL($ele.val());
if (url) {
$("<img src='" + url + "' class='edui-image-pic' />").on("load", function () {
var $item = $("<div class='edui-image-item'><div class='edui-image-close'></div></div>").append(this);
$(".edui-image-searchRes", me.dialog).append($item);
Base.scale(this, 120);
$item.width($(this).width());
Base.close($(this));
$ele.val("");
});
}
})
.hover(function () {
$(this).toggleClass("hover");
});
}
};
var $tab = null,
currentDialog = null;
UM.registerWidget('image', {
tpl: "<link rel=\"stylesheet\" type=\"text/css\" href=\"<%=image_url%>image.css\">" +
"<div class=\"edui-image-wrapper\">" +
"<ul class=\"edui-tab-nav\">" +
"<li class=\"edui-tab-item edui-active\"><a data-context=\".edui-image-local\" class=\"edui-tab-text\"><%=lang_tab_local%></a></li>" +
// "<li class=\"edui-tab-item\"><a data-context=\".edui-image-JimgSearch\" class=\"edui-tab-text\"><%=lang_tab_imgSearch%></a></li>" +
"</ul>" +
"<div class=\"edui-tab-content\">" +
"<div class=\"edui-image-local edui-tab-pane edui-active\">" +
"<div class=\"edui-image-content\"></div>" +
"<div class=\"edui-image-mask\"></div>" +
"<div class=\"edui-image-dragTip\"><%=lang_input_dragTip%></div>" +
"</div>" +
"<div class=\"edui-image-JimgSearch edui-tab-pane\">" +
"<div class=\"edui-image-searchBar\">" +
"<table><tr><td><input class=\"edui-image-searchTxt\" type=\"text\"></td>" +
"<td><div class=\"edui-image-searchAdd\"><%=lang_btn_add%></div></td></tr></table>" +
"</div>" +
"<div class=\"edui-image-searchRes\"></div>" +
"</div>" +
"</div>" +
"</div>",
initContent: function (editor, $dialog) {
var lang = editor.getLang('image')["static"],
opt = $.extend({}, lang, {
image_url: UMEDITOR_CONFIG.UMEDITOR_HOME_URL + 'dialogs/image/'
});
Upload.showCount = 0;
if (lang) {
var html = $.parseTmpl(this.tpl, opt);
}
currentDialog = $dialog.edui();
this.root().html(html);
},
initEvent: function (editor, $w) {
$tab = $.eduitab({selector: ".edui-image-wrapper"})
.edui().on("beforeshow", function (e) {
e.stopPropagation();
});
Upload.init(editor, $w);
// NetWork.init(editor, $w);
},
buttons: {
'ok': {
exec: function (editor, $w) {
var sel = "",
index = $tab.activate();
if (index == 0) {
sel = ".edui-image-content .edui-image-pic";
} else if (index == 1) {
sel = ".edui-image-searchRes .edui-image-pic";
}
var list = Base.getAllPic(sel, $w, editor);
if (index != -1) {
editor.execCommand('insertimage', list);
}
}
},
'cancel': {}
},
width: 450,
height: 408
}, function (editor, $w, url, state) {
Base.callback(editor, $w, url, state)
})
//使用literal语法定义一个对象:XMLHttp
var XMLHttp =
{
//定义第一个属性,该属性用于缓存XMLHttpRequest对象的数组
XMLHttpRequestPool: [],
//对象的第一个方法,该方法用于返回一个XMLHttpRequest对象
getInstance:function()
{
// 从XMLHttpRequest对象池中取出一个空闲的XMLHttpRequest
for (var i = 0; i < this.XMLHttpRequestPool.length; i ++)
{
//如果XMLHttpReuqest的readyState为0,或者为4,
//都表示当前的XMLHttpRequest对象为闲置的对象
if (this.XMLHttpRequestPool[i].readyState == 0 ||
this.XMLHttpRequestPool[i].readyState == 4)
{
return this.XMLHttpRequestPool[i];
}
}
//如果没有空闲的,将再次创建一个新的XMLHttpRequest对象
this.XMLHttpRequestPool[this.XMLHttpRequestPool.length]
= this.createXMLHttpRequest();
//返回刚刚创建的XMLHttpRequest对象
return this.XMLHttpRequestPool[this.XMLHttpRequestPool.length - 1];
},
//创建新的XMLHttpRequest对象
createXMLHttpRequest:function()
{
//对于DOM 2 规范的浏览器
if (window.XMLHttpRequest)
{
var objXMLHttp = new XMLHttpRequest();
}
//对于Internet Explorer浏览器
else
{
//将Internet Explorer内置的所有XMLHTTP ActiveX控制设置成数组
var MSXML = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
//依次对Internet Explorer内置的XMLHTTP控件初始化,尝试创建XMLHttpRequest对象
for(var n = 0; n < MSXML.length; n ++)
{
try
{
//如果可以正常创建XMLHttpRequest对象,使用break跳出循环
var objXMLHttp = new ActiveXObject(MSXML[n]);
break;
}
catch(e)
{
}
}
}
//Mozilla某些版本没有readyState属性
if (objXMLHttp.readyState == null)
{
//直接设置其readyState为0
objXMLHttp.readyState = 0;
//对于哪些没有readyState属性的浏览器,将load动作与下面的函数关联起来
objXMLHttp.addEventListener("load", function ()
{
//当从服务器加载数据完成后,将readyState状态设为4
objXMLHttp.readyState = 4;
if (typeof objXMLHttp.onreadystatechange == "function")
{
objXMLHttp.onreadystatechange();
}
}, false);
}
return objXMLHttp;
},
//定义对象的第三个方法: 发送请求(方法[POST,GET], 地址, 数据, 回调函数)
sendRequest: function (method, url, data, callback)
{
var objXMLHttp = this.getInstance();
with(objXMLHttp)
{
try
{
//增加一个额外的randnum请求参数,用于防止IE缓存服务器响应
if (url.indexOf("?") > 0)
{
url += "&randnum=" + Math.random();
}
else
{
url += "?randnum=" + Math.random();
}
//打开与服务器的连接
open(method, url, true);
//对于使用POST请求方式
if (method == "POST")
{
// 设定请求头
setRequestHeader("X-Requested-With", "XMLHttpRequest");
send(data);
}
//对于采用GET请求
if (method == "GET")
{
send(null);
}
//设置状态改变的回调函数
onreadystatechange = function ()
{
//当服务器的相应完成时,以及获得了正常的服务器响应
if (objXMLHttp.readyState == 4 &&
(objXMLHttp.status == 200 ||
objXMLHttp.status == 304))
{
//当响应时机成熟时,调用回调函数处理响应
callback(objXMLHttp);
}
}
}
catch(e)
{
alert(e);
}
}
}
};
})();