image.js
19.4 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
var $=require('jquery');
require("../util/jquery-ui.min");
(function () {
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");
// 去除url中的imageview
var orignal_src = node.src;
if (orignal_src.indexOf('?') != -1) {
orignal_src = orignal_src.substring(0, orignal_src.indexOf('?'));
}
return arr.push({
_src: orignal_src,
src: orignal_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) {
var width = $img.width() || $img[0].width,
height = $img.height() || $img[0].height;
// console.log("close: width=" + width + ",height=" + height);
$img.css({
top: ($img.parent().height() - height) / 2,
left: ($img.parent().width() - 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='" + editor.options.imagePath + 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();
}
// 支持拖拽调整顺序
$( ".edui-image-content" ).sortable({
items: ".edui-image-item.edui-image-upload-item"
});
$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-dragTip" style="display: block;text-shadow:0 0 0 #555;">支持多图拖拽上传</div>' +
'<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" multiple hidefocus name="upfile" 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{
r=r.match(/\[.*\]/g)[0];
var json = eval('('+r+')');
for (var i = 0; i < json.length; i++) {
json[i].url = json[i].url + "?imageView2/0/w/120/q/75";
Base.callback(me.editor, me.dialog, json[i].url, json[i].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" multiple hidefocus="" name="upfile" 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;
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);
xhr.send(fd);
xhr.addEventListener('load', function (e) {
var r = e.target.response, json;
me.uploadComplete(r);
if (i == fileList.length - 1) {
$(img).remove()
}
});
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 Product={
init: function (editor, $w) {
var me = this;
me.editor = editor;
me.dialog = $w;
me.initEvt();
},
initEvt: function () {
var me = this,
$ele = $(".edui-image-product .edui-product-content", me.dialog);
if($.trim($('#productSkn').val())){
$.ajax({
url:"/productPhoto/queryProductPhotoList",
data:{
productSkn:$('#productSkn').val()
},
dataType: 'json',
method:'post',
success:function(res){
var data = res;
if (res.code == 200 && data.data.list instanceof Array && data.data.list[0].pictureBoList instanceof Array) {
$.each(data.data.list[0].pictureBoList, function(i, value) {
var url=value.fileName;
if (url) {
url+="?imageView2/0/w/120/q/75";
$("<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-product .edui-product-content", me.dialog).append($item);
// 支持拖拽调整顺序
$(".edui-image-product .edui-product-content", me.dialog).sortable({
items: ".edui-image-item"
});
Base.scale(this, 120);
$item.width(120);
Base.close($(this));
});
}
});
}
}
});
$(document).on("click",".edui-image-product .edui-product-content .edui-image-pic",function(){
//$(".edui-image-product .edui-product-content .edui-image-pic").removeClass("active-product");
if ($(this).parent().hasClass("active-product")) {
$(this).parent().removeClass("active-product");
}
else {
$(this).parent().addClass("active-product");
}
});
}
}
}
var $tab = null,
currentDialog = null;
UM.registerWidget('image', {
tpl:"<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>" +
"<li class=\"edui-tab-item\"><a data-context=\".edui-image-product\" class=\"edui-tab-text\">商品图片</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 class=\"edui-image-product edui-tab-pane\">" +
"<div class=\"edui-product-content\" ></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);
// 禁止滚动
$(document.body).css("overflow", "hidden");
$(".fileinput-button").hide();
},
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);
Product.init(editor, $w);
},
buttons: {
'ok': {
exec: function (editor, $w) {
// 允许滚动
$(document.body).css("overflow", "scroll");
$(".fileinput-button").show();
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";
}else{
sel = ".edui-product-content .edui-image-item.active-product .edui-image-pic ";
}
var list = Base.getAllPic(sel, $w, editor);
/*$.each(list,function (index,item){
item.src = item.src + "imageView2/0/q/75";
item._src = item._src + "imageView2/0/q/75";
})*/
if (index != -1) {
editor.execCommand('insertimage', list);
}
}
},
'cancel': {
exec: function() {
// 允许滚动
$(document.body).css("overflow", "scroll");
$(".fileinput-button").show();
$(document).off("click",".edui-image-product .edui-product-content .edui-image-pic");
}
}
},
width: 700,
height: 408
}, function (editor, $w, url, state) {
Base.callback(editor, $w, url, state)
})
})();