cupload.js 29 KB

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