cupload-cap.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. (function(window, document) {
  2. //定义上传类
  3. var Cupload = function(options,document) {
  4. //初始化 new对象
  5. if (!(this instanceof Cupload)) {
  6. return new Cupload(options)
  7. }
  8. //设置默认参数
  9. this.localValue = {
  10. ele: '#cupload',
  11. name: 'image',
  12. num: 1,
  13. width: 178,
  14. height: 183,
  15. }
  16. //参数覆盖
  17. this.opt = this.extend(this.localValue, options, true);
  18. this.document = document;
  19. //所需变量
  20. this.i = 0;
  21. this.imageArr = new Array();//图片
  22. this.widthArr = new Array();//图片宽度
  23. this.heightArr = new Array();//图片高度
  24. this.imageBox = new Array();//图片盒子
  25. this.imagePreview = new Array();//图片预览
  26. this.imageInput = new Array();//图片input
  27. this.imageDelete = new Array();//图片删除遮罩
  28. this.deleteBtn = new Array();//图片删除按钮
  29. this.sortLeftBtn = new Array();//图片左排序按钮
  30. this.sortRightBtn = new Array();//图片右排序按钮
  31. this.rolateBtn = new Array();//旋转图片按钮
  32. this.imageRolate = new Array();//图片旋转
  33. if ((typeof options.ele) === "string") {
  34. this.opt.ele = this.document.querySelector(options.ele)
  35. } else {
  36. this.opt.ele = options.ele
  37. }
  38. // 初始化为空
  39. this.opt.ele.innerHTML = "";
  40. this.initDom();
  41. }
  42. Cupload.prototype = {
  43. constructor: this,
  44. //初始化
  45. initDom: function() {
  46. this.cteateImageList()
  47. //查看是不需要上传
  48. if(this.opt.model != "formview"){
  49. this.createUploadBox()
  50. }
  51. this.createOverlay()
  52. if (this.opt.data) {
  53. this.showImagePreview()
  54. }
  55. },
  56. //参数覆盖
  57. extend: function(o, n, override) {
  58. for (var key in n) {
  59. if (n.hasOwnProperty(key) && (!o.hasOwnProperty(key) || override)) {
  60. o[key] = n[key]
  61. }
  62. }
  63. return o
  64. },
  65. //创建图片列表
  66. cteateImageList: function() {
  67. // this.imageList = this.document.createElement('ul')
  68. this.imageList = this.document.createElement('span')
  69. this.imageList.className = 'cupload-image-list'
  70. // this.imageList.style.margin = 0
  71. // this.imageList.style.padding = 0
  72. // this.imageList.style.display = 'inline-block'
  73. this.imageList.style.minHeight = this.opt.height
  74. this.opt.ele.appendChild(this.imageList)
  75. // this.imageList.ondragstart = function(event) {
  76. // console.log('start')
  77. // }
  78. },
  79. //创建上传框
  80. createUploadBox: function() {
  81. this.uploadBox = this.document.createElement('span')
  82. this.uploadBox.className = 'cupload-upload-box'
  83. // this.uploadBox.style.position = 'relative'
  84. // this.uploadBox.style.display = 'inline-block'
  85. // this.uploadBox.style.textAlign = 'center'
  86. // this.uploadBox.style.backgroundColor = '#fbfdff'
  87. // this.uploadBox.style.border = '1px dashed #c0ccda'
  88. // this.uploadBox.style.borderRadius = '6px'
  89. // this.uploadBox.style.WebkitBoxSizing = 'border-box'
  90. // this.uploadBox.style.boxSizing = 'border-box'
  91. this.uploadBox.style.width = this.opt.width + 'px'
  92. this.uploadBox.style.height = this.opt.height + 'px'
  93. this.uploadBox.style.lineHeight = this.opt.height + 'px'
  94. this.opt.ele.appendChild(this.uploadBox)
  95. this.createUploadBtn()
  96. this.createUploadInput()
  97. var _this = this
  98. this.uploadBox.onmouseover = function() {
  99. _this.uploadBox.style.borderColor = '#409eff'
  100. }
  101. this.uploadBox.onmouseout = function() {
  102. _this.uploadBox.style.borderColor = '#c0ccda'
  103. }
  104. },
  105. //创建遮罩
  106. createOverlay: function() {
  107. // this.overlay = this.document.createElement('div')
  108. this.overlay = this.document.createElement('span')
  109. this.overlay.className = 'cupload-overlay'
  110. // this.overlay.style.display = "none"
  111. // this.overlay.style.position = "fixed"
  112. // this.overlay.style.textAlign = "center"
  113. // this.overlay.style.top = 0
  114. // this.overlay.style.right = 0
  115. // this.overlay.style.bottom = 0
  116. // this.overlay.style.left = 0
  117. // this.overlay.style.zIndex = 9115
  118. // this.overlay.style.backgroundColor = "rgba(0,0,0,.3)"
  119. this.document.body.appendChild(this.overlay)
  120. // this.opt.ele.appendChild(this.overlay)
  121. var _this = this
  122. this.overlay.onclick = function() {
  123. _this.zoomOutImage()
  124. }
  125. },
  126. //创建上传按钮
  127. createUploadBtn: function() {
  128. this.uploadBtn = this.document.createElement('span')
  129. this.uploadBtn.className = 'cupload-upload-btn'
  130. // this.uploadBtn.style.position = 'absolute'
  131. this.uploadBtn.style.left = this.opt.width/2 - 14 + 'px'
  132. // this.uploadBtn.style.fontSize = '28px'
  133. // this.uploadBtn.style.color = '#8c939d'
  134. this.uploadBtn.innerHTML = '+'
  135. this.uploadBox.appendChild(this.uploadBtn)
  136. },
  137. //创建上传input
  138. createUploadInput: function() {
  139. this.uploadInput = this.document.createElement('input')
  140. this.uploadInput.className = 'cupload-upload-input'
  141. // this.uploadInput.style.position = 'absolute'
  142. // this.uploadInput.style.top = 0
  143. // this.uploadInput.style.right = 0
  144. // this.uploadInput.style.width = '100%'
  145. // this.uploadInput.style.height = '100%'
  146. // this.uploadInput.style.opacity = 0
  147. // this.uploadInput.style.cursor = 'pointer'
  148. this.uploadInput.type = 'file'
  149. this.uploadInput.multiple = 'multiple'
  150. this.uploadInput.accept = 'image/*'
  151. this.uploadInput.title = ''
  152. this.uploadBox.appendChild(this.uploadInput)
  153. var _this = this
  154. this.uploadInput.onchange = function() {
  155. _this.removeUploadBox()
  156. _this.uploadImage()
  157. }
  158. },
  159. //上传图片
  160. uploadImage: function() {
  161. if(this.uploadInput.files.length + this.imageList.children.length > this.opt.num) {
  162. this.createUploadBox()
  163. alert('图片数量超出限制,请重新选择')
  164. return
  165. }
  166. for(j = 0; j < this.uploadInput.files.length; j++){
  167. var file = this.uploadInput.files[j]
  168. if (!file || this.limitedSize(file)) {
  169. this.createUploadBox()
  170. return false
  171. }
  172. var reader = new FileReader()
  173. var _this = this
  174. reader.filename = file.name;
  175. reader.onload = function(e) {
  176. _this.limitedWidthAndHeight(e.target.result, e.target.filename,file)
  177. }
  178. reader.readAsDataURL(file);
  179. }
  180. if (this.uploadInput.files.length + this.imageList.children.length < this.opt.num) {
  181. this.createUploadBox()
  182. }
  183. },
  184. //检测图片大小限制
  185. limitedSize: function(file) {
  186. if (this.opt.minSize && file.size < this.opt.minSize * 1024) {
  187. alert('图片' + file.name + '大小未到最小限制,请重新选择')
  188. return true
  189. }
  190. if (this.opt.maxSize && file.size > this.opt.maxSize * 1024) {
  191. alert('图片' + file.name + '大小超出最大限制,请重新选择')
  192. return true
  193. }
  194. if (this.opt.limitedSize && file.size > this.opt.limitedSize * 1024) {
  195. alert('图片' + file.name + '大小不符合要求,请重新选择')
  196. return true
  197. }
  198. return false
  199. },
  200. //检测图片像素限制
  201. limitedWidthAndHeight: function(src, name,file) {
  202. var tempImage = new Image()
  203. tempImage.src = src
  204. var _this = this
  205. tempImage.onload = function() {
  206. if (_this.opt.minWidth && this.width < _this.opt.minWidth) {
  207. alert('图片' + name + '宽度未到最小限制,请重新选择')
  208. _this.isCreateUploadBox()
  209. return false
  210. }
  211. if (_this.opt.minHeight && this.height < _this.opt.minHeight) {
  212. alert('图片' + name + '高度未到最小限制,请重新选择')
  213. _this.isCreateUploadBox()
  214. return false
  215. }
  216. if (_this.opt.maxWidth && this.width > _this.opt.maxWidth) {
  217. alert('图片' + name + '宽度超出最大限制,请重新选择')
  218. _this.isCreateUploadBox()
  219. return false
  220. }
  221. if (_this.opt.maxHeight && this.height > _this.opt.maxHeight) {
  222. alert('图片' + name + '高度超出最大限制,请重新选择')
  223. _this.isCreateUploadBox()
  224. return false
  225. }
  226. if (_this.opt.limitedWidth && this.width != _this.opt.limitedWidth) {
  227. alert('图片' + name + '宽度不符合要求,请重新选择')
  228. _this.isCreateUploadBox()
  229. return false
  230. }
  231. if (_this.opt.limitedHeight && this.height != _this.opt.limitedHeight) {
  232. alert('图片' + name + '高度不符合要求,请重新选择')
  233. _this.isCreateUploadBox()
  234. return false
  235. }
  236. _this.foreachNum(src, name, this.width, this.height,file)
  237. }
  238. },
  239. //检测图片数量
  240. foreachNum: function(src, name, width, height,file) {
  241. if(this.opt.url) {
  242. // var key = this.opt.name
  243. // var data = {}
  244. // data[key] = src
  245. var _this = this
  246. this.ajaxUploadImage(file, function(res) {
  247. // console.log(res)
  248. _this.createImageBox(res.data.url, res.data.name, width, height);
  249. })
  250. } else {
  251. this.createImageBox(src, name, width, height)
  252. }
  253. },
  254. //图片异步上传
  255. ajaxUploadImage: function(data,success) {
  256. let _this = this;
  257. let formData = new FormData();
  258. formData.append("file", data);
  259. $.ajax({
  260. url: _this.opt.url,
  261. type: "post",
  262. data: formData,
  263. processData: false,
  264. contentType: false,
  265. cache: false,
  266. success: function(res) {
  267. success(res)
  268. },
  269. error: function(res) {
  270. }
  271. });
  272. // var xhr = null;
  273. // if(window.XMLHttpRequest){
  274. // xhr = new XMLHttpRequest()
  275. // } else {
  276. // xhr = new ActiveXObject('Microsoft.XMLHTTP')
  277. // }
  278. // if(typeof data == 'object'){
  279. // var str = '';
  280. // for(var key in data){
  281. // str += key+'='+data[key]+'&';
  282. // }
  283. // data = str.replace(/&$/, '');
  284. // }
  285. // xhr.open('POST', this.opt.url, true);
  286. // xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  287. // xhr.send(data);
  288. // xhr.onreadystatechange = function(){
  289. // if(xhr.readyState == 4){
  290. // if(xhr.status == 200){
  291. // success(xhr)
  292. // } else {
  293. // alert(((xhr.responseText).split("</p>")[0]).split("<p>")[1])
  294. // return false
  295. // }
  296. // }
  297. // }
  298. },
  299. //创建图片框
  300. createImageBox: function(src, name, width, height, state = true) {
  301. this.imageArr[this.i] = src
  302. this.widthArr[this.i] = width
  303. this.heightArr[this.i] = height
  304. // this.imageBox[this.i] = this.document.createElement('li')
  305. this.imageBox[this.i] = this.document.createElement('span')
  306. this.imageBox[this.i].className = 'cupload-image-box'
  307. this.imageBox[this.i].setAttribute("seq",this.i);
  308. // this.imageBox[this.i].style.position = 'relative'
  309. // this.imageBox[this.i].style.display = 'inline-block'
  310. // this.imageBox[this.i].style.marginRight = 8 + 'px'
  311. // this.imageBox[this.i].style.backgroundColor = '#fbfdff'
  312. // this.imageBox[this.i].style.border = '1px solid #c0ccda'
  313. // this.imageBox[this.i].style.borderRadius = '6px'
  314. // this.imageBox[this.i].style.WebkitBoxSizing = 'border-box'
  315. // this.imageBox[this.i].style.boxSizing = 'border-box'
  316. this.imageBox[this.i].style.width = this.opt.width + 'px'
  317. this.imageBox[this.i].style.height = this.opt.height + 'px'
  318. this.imageList.appendChild(this.imageBox[this.i])
  319. this.createImagePreview(src, width, height)
  320. if (!state) {
  321. // this.setDefaultImage()
  322. }
  323. this.createImageInput(src)
  324. this.createImageDelete(name)
  325. var _this = this
  326. for (var m = 0; m <= this.i; m++) {
  327. this.imageBox[m].index = m
  328. this.imageBox[m].onmouseover = function(n) {
  329. return function() {
  330. _this.showImageDelete(n)
  331. }
  332. }(m)
  333. this.imageBox[m].onmouseout = function(n) {
  334. return function() {
  335. _this.hideImageDelete(n)
  336. }
  337. }(m)
  338. }
  339. this.i++
  340. },
  341. //创建图片预览框
  342. createImagePreview: function(src, width, height) {
  343. this.imagePreview[this.i] = this.document.createElement('img')
  344. this.imagePreview[this.i].className = 'cupload-image-preview'
  345. // this.imagePreview[this.i].style.position = 'absolute'
  346. // this.imagePreview[this.i].style.top = 0
  347. // this.imagePreview[this.i].style.left = 0
  348. // this.imagePreview[this.i].style.right = 0
  349. // this.imagePreview[this.i].style.bottom = 0
  350. // this.imagePreview[this.i].style.margin = 'auto'
  351. this.imagePreview[this.i].src = src
  352. this.setImageAttribute(width, height)
  353. //remove by chenqiwang
  354. //this.imageBox[this.i].appendChild(this.imagePreview[this.i])
  355. //add div
  356. let imgDiv = this.document.createElement('div');
  357. let imgRmk = this.document.createElement('a');
  358. imgRmk.className = "input";
  359. imgRmk.setAttribute("contenteditable","true");
  360. imgDiv.className = "cupload-image-div"
  361. imgDiv.appendChild(this.imagePreview[this.i])
  362. imgDiv.appendChild(imgRmk);
  363. this.imageBox[this.i].appendChild(imgDiv)
  364. },
  365. //创建图片input
  366. createImageInput: function(src) {
  367. this.imageInput[this.i] = this.document.createElement('input')
  368. this.imageInput[this.i].type = 'hidden'
  369. this.imageInput[this.i].name = this.opt.name + '[]'
  370. this.imageInput[this.i].value = src
  371. this.imageBox[this.i].appendChild(this.imageInput[this.i])
  372. },
  373. //创建删除
  374. createImageDelete: function(name) {
  375. this.imageDelete[this.i] = this.document.createElement('span')
  376. this.imageDelete[this.i].className = 'cupload-image-delete'
  377. // this.imageDelete[this.i].style.position = 'absolute'
  378. // this.imageDelete[this.i].style.width = '100%'
  379. // this.imageDelete[this.i].style.height = '100%'
  380. // this.imageDelete[this.i].style.left = 0
  381. // this.imageDelete[this.i].style.top = 0
  382. // this.imageDelete[this.i].style.textAlign = 'center'
  383. // this.imageDelete[this.i].style.color = '#fff'
  384. // this.imageDelete[this.i].style.opacity = 0
  385. // this.imageDelete[this.i].style.cursor = 'zoom-in'
  386. // this.imageDelete[this.i].style.backgroundColor = 'rgba(0,0,0,.5)'
  387. // this.imageDelete[this.i].style.WebkitTransition = '.3s'
  388. // this.imageDelete[this.i].style.transition = '.3s'
  389. this.imageDelete[this.i].title = name
  390. this.imageBox[this.i].appendChild(this.imageDelete[this.i])
  391. // 查看时不需要创建
  392. if(this.opt.model != "formview"){
  393. this.createDeleteBtn()
  394. this.createSortBtn()
  395. this.createRolateBtn();//add by chenqiwang
  396. }
  397. var _this = this
  398. for (var m = 0; m <= this.i; m++) {
  399. this.imageDelete[m].onclick = function(n) {
  400. return function() {
  401. _this.zoomInImage(n)
  402. }
  403. }(m)
  404. }
  405. },
  406. //创建删除按钮
  407. createDeleteBtn: function() {
  408. this.deleteBtn[this.i] = this.document.createElement('span')
  409. this.deleteBtn[this.i].className = 'cupload-delete-btn'
  410. // this.deleteBtn[this.i].style.position = 'absolute'
  411. // this.deleteBtn[this.i].style.top = 0
  412. // this.deleteBtn[this.i].style.right = 0
  413. // this.deleteBtn[this.i].style.margin = 0
  414. // this.deleteBtn[this.i].style.padding = 0
  415. // this.deleteBtn[this.i].style.fontSize = '18px'
  416. // this.deleteBtn[this.i].style.width = '24px'
  417. // this.deleteBtn[this.i].style.height = '24px'
  418. // this.deleteBtn[this.i].style.cursor = 'pointer'
  419. // this.deleteBtn[this.i].style.backgroundImage = "url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAAwUlEQVRYhe2WwQ3CMAxFPxXqOjAAqGwHo3QNCIxSxuBzcaRStcRxcin4XSJHjv2aNkoBRwHJhmSgnhvJpqbAPqN5ZKeprbU8ydhvEgDoJ2uqCHQyXhW5Maf7mrUEyYdhu7WEab+5HXiZzJXPp88Uijsm6tQ7KkZcYF0CckSDNi5i7uudzqXipbkx63oFLuACPymwzcy/4/NKTcV2/Dp2AQBPACB5sBYneRzXyl18qfAXHDlbBFqRGAoaD1KjzRb4G96BvtfyCUSIygAAAABJRU5ErkJggg==')"
  420. // this.deleteBtn[this.i].style.backgroundSize = '18px 18px'
  421. // this.deleteBtn[this.i].style.backgroundRepeat = 'no-repeat'
  422. // this.deleteBtn[this.i].style.backgroundPosition = 'right top'
  423. this.deleteBtn[this.i].innerHTML = ''
  424. this.deleteBtn[this.i].title = '删除'
  425. this.imageDelete[this.i].appendChild(this.deleteBtn[this.i])
  426. var _this = this
  427. for (var m = 0; m <= this.i; m++) {
  428. this.deleteBtn[m].onclick = function(n) {
  429. return function() {
  430. _this.deleteImage(n)
  431. }
  432. }(m)
  433. }
  434. },
  435. //创建删除按钮
  436. createRolateBtn: function() {
  437. this.rolateBtn[this.i] = this.document.createElement('span')
  438. this.rolateBtn[this.i].className = 'cupload-rolate-btn'
  439. this.rolateBtn[this.i].innerHTML = ''
  440. this.rolateBtn[this.i].title = '旋转'
  441. this.imageDelete[this.i].appendChild(this.rolateBtn[this.i])
  442. var _this = this
  443. for (var m = 0; m <= this.i; m++) {
  444. this.rolateBtn[m].onclick = function(n) {
  445. return function() {
  446. _this.rolateImage(event,n)
  447. }
  448. }(m)
  449. }
  450. },
  451. rolateImage: function(event, n){
  452. event.cancelBubble = true;
  453. // event.stopImmediatePropagation();
  454. this.rotateBase64Img(this.imagePreview[n],this.imagePreview[n].src,90);
  455. },
  456. rotateBase64Img : async(imgObj,src, edg) => {
  457. const canvas = document.createElement('canvas')
  458. const ctx = canvas.getContext('2d')
  459. let imgW// 图片宽度
  460. let imgH// 图片高度
  461. let size// canvas初始大小
  462. if (edg % 90 != 0) {
  463. console.error('旋转角度必须是90的倍数!')
  464. }
  465. (edg < 0) && (edg = (edg % 360) + 360)
  466. const quadrant = (edg / 90) % 4 // 旋转象限
  467. const cutCoor = { sx: 0, sy: 0, ex: 0, ey: 0 } // 裁剪坐标
  468. const image = new Image()
  469. image.crossOrigin = 'anonymous'
  470. image.src = src
  471. image.onload = await function() {
  472. debugger;
  473. imgW = image.width
  474. imgH = image.height
  475. size = imgW > imgH ? imgW : imgH
  476. canvas.width = size * 2
  477. canvas.height = size * 2
  478. switch (quadrant) {
  479. case 0:
  480. cutCoor.sx = size
  481. cutCoor.sy = size
  482. cutCoor.ex = size + imgW
  483. cutCoor.ey = size + imgH
  484. break
  485. case 1:
  486. cutCoor.sx = size - imgH
  487. cutCoor.sy = size
  488. cutCoor.ex = size
  489. cutCoor.ey = size + imgW
  490. break
  491. case 2:
  492. cutCoor.sx = size - imgW
  493. cutCoor.sy = size - imgH
  494. cutCoor.ex = size
  495. cutCoor.ey = size
  496. break
  497. case 3:
  498. cutCoor.sx = size
  499. cutCoor.sy = size - imgW
  500. cutCoor.ex = size + imgH
  501. cutCoor.ey = size + imgW
  502. break
  503. }
  504. ctx.translate(size, size)
  505. ctx.rotate(edg * Math.PI / 180)
  506. ctx.drawImage(image, 0, 0)
  507. var imgData = ctx.getImageData(cutCoor.sx, cutCoor.sy, cutCoor.ex, cutCoor.ey)
  508. if (quadrant % 2 == 0) {
  509. canvas.width = imgW
  510. canvas.height = imgH
  511. } else {
  512. canvas.width = imgH
  513. canvas.height = imgW
  514. }
  515. ctx.putImageData(imgData, 0, 0)
  516. imgObj.src = canvas.toDataURL()
  517. // this.imagePreview[n].src = canvas.toDataURL();
  518. // 获取旋转后的base64图片
  519. //state.esignImg = canvas.toDataURL()
  520. //state.showEsign = false
  521. }
  522. },
  523. createSortBtn: function() {
  524. this.sortLeftBtn[this.i] = this.document.createElement('span')
  525. this.sortLeftBtn[this.i].className = 'cupload-sort-left'
  526. // this.sortLeftBtn[this.i].style.position = 'absolute'
  527. // this.sortLeftBtn[this.i].style.bottom = 0
  528. // this.sortLeftBtn[this.i].style.left = 0
  529. // this.sortLeftBtn[this.i].style.margin = 0
  530. // this.sortLeftBtn[this.i].style.padding = 0
  531. // this.sortLeftBtn[this.i].style.fontSize = '18px'
  532. // this.sortLeftBtn[this.i].style.width = '24px'
  533. // this.sortLeftBtn[this.i].style.height = '24px'
  534. // this.sortLeftBtn[this.i].style.cursor = 'pointer'
  535. // this.sortLeftBtn[this.i].style.backgroundImage = "url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAABP0lEQVRYhe2UIUsEURRGv7suGza4sCBoENYgGMwWs/4A/4AWwWRyMfgHRCyKGhWsYtegRYOCCGoUk1kwKLiws8dywRFGcXZ23yDMiW8e75z3YK5UUPBfAQbzlK8BEbCa9axySrFJ2pC0klWcGsCALb7Y8aAg8gHgICZfDyJ2eQU4dnEHCPf8QBU4dXkELIaU14BLl7eB+ZDyOnDt8g9gLqR8BHhw+Rsw00/ft98IaEg6lzTmS2eSbnroiyTtm9lT4ldgm/6z+9sLTPoLDPnSraTk2u5oSdo0s7sfdwATwLPXvgBTPQz4G0ADePSIV2A6j4hh4N4j3oHZPCLqwJVHtAg5D2IRNeDCI8JOxFhEFTjxiA6wnEdEBTiKRTTziCgDh7GhspT1zFKazWbWlrQgac+XRrMGdA0wDqS6QEFBEp/yS6NBq8E1tgAAAABJRU5ErkJggg==')"
  536. // this.sortLeftBtn[this.i].style.backgroundSize = '18px 18px'
  537. // this.sortLeftBtn[this.i].style.backgroundRepeat = 'no-repeat'
  538. // this.sortLeftBtn[this.i].style.backgroundPosition = 'left bottom'
  539. this.sortLeftBtn[this.i].innerHTML = ''
  540. this.sortLeftBtn[this.i].title = '左移'
  541. this.imageDelete[this.i].appendChild(this.sortLeftBtn[this.i])
  542. this.sortRightBtn[this.i] = this.document.createElement('span')
  543. this.sortRightBtn[this.i].className = 'cupload-sort-right'
  544. // this.sortRightBtn[this.i].style.position = 'absolute'
  545. // this.sortRightBtn[this.i].style.bottom = 0
  546. // this.sortRightBtn[this.i].style.right = 0
  547. // this.sortRightBtn[this.i].style.margin = 0
  548. // this.sortRightBtn[this.i].style.padding = 0
  549. // this.sortRightBtn[this.i].style.fontSize = '18px'
  550. // this.sortRightBtn[this.i].style.width = '24px'
  551. // this.sortRightBtn[this.i].style.height = '24px'
  552. // this.sortRightBtn[this.i].style.cursor = 'pointer'
  553. // this.sortRightBtn[this.i].style.backgroundImage = "url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAA4UlEQVRYhe3WMUpDQRCA4XkKWlhIqnQWwc4iKbyBTVohJ/AC2lh4gxxBA7lAqhwgvbWtpYWVnVgI4mfhghKeEPCFSeD93cIu8w+7OzMRLS3bDga4Qy9LYOqbZ5xkCBzjqUi84DRD4giPReIVZxkSXTwUiTcMMyQ6uC8S7xhlSBxgUSQ+cJEhsY95kfjEVYbEHmZ+GNftq2oOHkbETkMeuxExiYjzsh5XVXXz525cWz+Xv2MuZ6qhzFdms66gSVZ9hOsKnvcNZRYimaVYZjOS2Y5lDyQ2YCTr41bWUNrS8l++ABQQn/PCTE8cAAAAAElFTkSuQmCC')"
  554. // this.sortRightBtn[this.i].style.backgroundSize = '18px 18px'
  555. // this.sortRightBtn[this.i].style.backgroundRepeat = 'no-repeat'
  556. // this.sortRightBtn[this.i].style.backgroundPosition = 'right bottom'
  557. this.sortRightBtn[this.i].innerHTML = ''
  558. this.sortRightBtn[this.i].title = '右移'
  559. this.imageDelete[this.i].appendChild(this.sortRightBtn[this.i])
  560. var _this = this
  561. for (var m = 0; m <= this.i; m++) {
  562. this.sortLeftBtn[m].onclick = function(n) {
  563. return function() {
  564. _this.sortLeft(event, n)
  565. }
  566. }(m)
  567. this.sortRightBtn[m].onclick = function(n) {
  568. return function() {
  569. _this.sortRight(event, n)
  570. }
  571. }(m)
  572. }
  573. },
  574. //设置默认图片
  575. setDefaultImage: function() {
  576. this.imageBox[this.i].style.backgroundColor = "#B2B2B2"
  577. this.imageDelete[this.i].title = '图片不存在'
  578. this.imagePreview[this.i].src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAB4CAMAAABCfAldAAAAllBMVEUAAAB/f39XV1fKysrr6+vo6Oj+/v5DQ0OZmZl5eXni4uLGxsbe3t7b29u2trajo6Nubm5mZmbOzs6wsLCGhob8/Pzl5eXU1NS9vb2rq6tSUlJLS0vY2NiRkZFeXl7y8vLw8PBzc3P6+vr39/fBwcH09PTe3t7w8PDv7+/b29vt7e3j4+Pa2trq6urg4ODZ2dnm5ub4+PhWkjdOAAAAJnRSTlMAWCWy29fzDHdP0a7MyJuEQje4k2Dw1b+ijR8XxG0v5OFJ7uun5sxAWrkAAAUySURBVHja7ZsNV9owFIY7TUBBFBD5EEHcNM1N0hb+/59bmsQltSutbUdzdvqefbid5OXxvtzmamvQq1evXr169er1X+qqNQ0r+n0L7+YORy3poF5593EsWYfvbqrzPRygJSV4EUhtR0JAmQ4PVfmelwJIKxJ8rRzXvNwQBH6uCHgbtQQI5G6oDJGo8tlEtxUBf7CkHcAE6Zd8IVBlNftRGVC0EzB+1G9pLCotvzQgkBd9hcFAvARM0EzZjfk/BIzrC5C+bCwOcH5hE8CI1hYLX7QbChk9q6g2IPCQ0rCeKPulD6/HJ3bWQ74Ch/qAjNE6Cmk00CfX/qfkO1tpJgEvXUFK2WmuvVYy4JIKXh6QSqjXrbLaDSLp4BmgjJQNFspp+EplAX0DDEN2etdO6xOjoW8VTK8wSE+p04Gsn28Rq4DNWDIKZQF9q6As4NO9mXsHks+3iGUHhyMz9/6SfL5VMA34aaZtrk9pB3sGmB4hD8ZFXQI9A0w7eBI4Q4JngFTPCHZI8KyCcrGeEaRm6ZDgW8Q0NDOC1IsM2LcKUsroxzYwQ0IasGeAaQcvgj9DQki7BzSjqPzLDIFj4/B+YrTzChq2zw/SI+Qw1AZvKuCOAK2vpjN/mhnBDgm0S0D13/IXi3hMYs4jamYEZ0jotI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oCV9bwZX1fUeDX7HLHmclNtjTdXQa9evXr16tWr17f1G41D8rN+B+2KAAAAAElFTkSuQmCC'
  579. if (130 / this.opt.width > 105 / this.opt.height) {
  580. this.imagePreview[this.i].style.width = this.opt.width - 2 + 'px'
  581. this.imagePreview[this.i].style.height = 105 / (130 / this.opt.width) - 2 + 'px'
  582. } else {
  583. this.imagePreview[this.i].style.width = 130 / (105 / this.opt.height) - 2 + 'px'
  584. this.imagePreview[this.i].style.height = this.opt.height - 2 + 'px'
  585. }
  586. },
  587. //设置图片宽高
  588. setImageAttribute: function(width, height) {
  589. // if (width / this.opt.width > height / this.opt.height) {
  590. // this.imagePreview[this.i].style.width = this.opt.width - 2 + 'px'
  591. // this.imagePreview[this.i].style.height = height / (width / this.opt.width) - 2 + 'px'
  592. // } else {
  593. // this.imagePreview[this.i].style.width = width / (height / this.opt.height) - 2 + 'px'
  594. // this.imagePreview[this.i].style.height = this.opt.height - 2 + 'px'
  595. // }
  596. this.imagePreview[this.i].style.width = (this.opt.width - 10) + 'px'
  597. this.imagePreview[this.i].style.height = (this.opt.height - 10) + 'px'
  598. },
  599. //data图片预览
  600. showImagePreview: function() {
  601. var obj = this.opt.data
  602. if (obj.length >= this.opt.num) {
  603. this.removeUploadBox()
  604. }
  605. var _this = this
  606. var tempImage = new Image()
  607. tempImage.src = obj[this.i]
  608. tempImage.onload = function() {
  609. _this.createImageBox(obj[_this.i], obj[_this.i], this.width, this.height)
  610. setTimeout(function() {
  611. if (obj[_this.i]) {
  612. _this.showImagePreview()
  613. }
  614. }, 0);
  615. }
  616. tempImage.onerror = function() {
  617. _this.createImageBox(obj[_this.i], obj[_this.i], 0, 0, false)
  618. setTimeout(function() {
  619. if (obj[_this.i]) {
  620. _this.showImagePreview()
  621. }
  622. }, 0);
  623. }
  624. },
  625. //图片放大预览
  626. zoomInImage: function(n) {
  627. if(event.target.classList[0] === 'cupload-delete-btn' || event.target.classList[0] === 'cupload-sort-right' || event.target.classList[0] === 'cupload-sort-left')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Xr16tWr17f1G41D8rN+B+2KAAAAAElFTkSuQmCC') {
  631. alert('图片不存在')
  632. return;
  633. }
  634. // console.log(this.document)
  635. // console.log(this.document)
  636. this.zommImage = this.document.createElement('img')
  637. this.zommImage.className = 'cupload-overlay-img';
  638. // this.zommImage.style.display = "inline-block"
  639. // this.zommImage.style.verticalAlign = "middle"
  640. this.zommImage.src = this.imageArr[n]
  641. if (this.widthArr[n] / window.innerWidth > this.heightArr[n] / window.innerHeight) {
  642. this.zommImage.style.width = 0.8 * window.innerWidth + 'px'
  643. this.zommImage.style.height = 0.8 * this.heightArr[n] / (this.widthArr[n] / window.innerWidth) + 'px'
  644. } else {
  645. this.zommImage.style.width = 0.8 * this.widthArr[n] / (this.heightArr[n] / window.innerHeight) + 'px'
  646. this.zommImage.style.height = 0.8 * window.innerHeight + 'px'
  647. }
  648. this.overlay.appendChild(this.zommImage)
  649. this.overlay.style.lineHeight = window.innerHeight + 'px'
  650. this.overlay.style.cursor = "zoom-out"
  651. this.overlay.style.display = "block"
  652. },
  653. //关闭图片放大预览
  654. zoomOutImage: function() {
  655. this.overlay.style.display = "none"
  656. this.zommImage.remove()
  657. },
  658. //检测当前图片数量,判断是否创建上传框
  659. isCreateUploadBox: function() {
  660. this.removeUploadBox()
  661. if (this.imageList.children.length < this.opt.num) {
  662. this.createUploadBox()
  663. }
  664. },
  665. //删除图片
  666. deleteImage: function(n) {
  667. this.imageBox[n].remove()
  668. this.removeUploadBox()
  669. if (this.imageList.children.length < this.opt.num) {
  670. this.createUploadBox()
  671. }
  672. if(this.opt.deleteUrl) {
  673. var xhr = null;
  674. var key = this.opt.name
  675. var data = {}
  676. data[key] = this.imageArr[n]
  677. if(window.XMLHttpRequest){
  678. xhr = new XMLHttpRequest()
  679. } else {
  680. xhr = new ActiveXObject('Microsoft.XMLHTTP')
  681. }
  682. if(typeof data == 'object'){
  683. var str = '';
  684. for(var key in data){
  685. str += key+'='+data[key]+'&';
  686. }
  687. data = str.replace(/&$/, '');
  688. }
  689. xhr.open('POST', this.opt.deleteUrl, true);
  690. xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  691. xhr.send(data);
  692. xhr.onreadystatechange = function(){
  693. if(xhr.readyState == 4){
  694. if(xhr.status == 200){
  695. console.log(xhr.response)
  696. } else {
  697. alert(((xhr.responseText).split("</p>")[0]).split("<p>")[1])
  698. return false
  699. }
  700. }
  701. }
  702. }
  703. },
  704. sortLeft: function(event, n) {
  705. if(this.imageBox[n].previousSibling) {
  706. this.imageList.insertBefore(this.imageBox[n], this.imageBox[n].previousSibling)
  707. }
  708. },
  709. sortRight: function(event, n) {
  710. if(this.imageBox[n].nextSibling) {
  711. this.imageList.insertBefore(this.imageBox[n].nextSibling, this.imageBox[n])
  712. }
  713. },
  714. //移除上传框
  715. removeUploadBox: function() {
  716. this.uploadBox.remove()
  717. },
  718. //显示图片删除
  719. showImageDelete: function(m) {
  720. this.imageDelete[m].style.opacity = 1
  721. },
  722. //隐藏图片删除
  723. hideImageDelete: function(m) {
  724. this.imageDelete[m].style.opacity = 0
  725. },
  726. }
  727. //初始化
  728. Cupload.init = function(ue){
  729. // 初始化上传图片
  730. let imgUploads = ue.document.querySelectorAll("a[datatype='imgUpload']");
  731. // console.log(imgUploads)
  732. if(imgUploads){
  733. imgUploads.forEach((child) => {
  734. // 是否存在批注
  735. let note = child.querySelector(".note");
  736. // 判断是否存在图片,有的话初始化
  737. let imgs = child.querySelectorAll("img.cupload-image-preview");
  738. // console.log(imgs)
  739. let imgSrcs = null;
  740. if(imgs && imgs.length > 0){
  741. imgSrcs = [];
  742. imgs.forEach((img) => {
  743. imgSrcs.push(img.src);
  744. });
  745. }
  746. // console.log(imgSrcs)
  747. let id = child.getAttribute("cupload");
  748. let imgNum = child.getAttribute("imageNum");
  749. AttrA.imgUploads[id] = new Cupload ({
  750. ele: '#'+id,
  751. num: imgNum,
  752. model: ue.options.model,
  753. url : url+"/api/v1/emreditor/imgUpload",
  754. data: imgSrcs
  755. },ue.document);
  756. // 存在批注则添加到元素中
  757. if(note){
  758. child.prepend(note);
  759. }
  760. })
  761. }
  762. // console.log(AttrA.imgUploads)
  763. }
  764. window.Cupload = Cupload;
  765. })(window, document)