You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2257 lines
64 KiB

  1. /**
  2. * zyupload.drag.js -- HTML5多文件上传的拖拽功能性js代码, 包含拖拽上传等功能
  3. * 当前包含2个具体部分
  4. * 1. zyFile.js -- zyupload调用的数据处理层
  5. * 2. zyUpload.js -- zyupload调用的业务层
  6. */
  7. /**
  8. * jquery.Jcrop.js v0.9.8
  9. * zyupload调用的开源的裁剪图片插件 -- 供zyPopup.js使用
  10. */
  11. (function($) {
  12. $.Jcrop = function(obj,opt)
  13. {
  14. // Initialization {{{
  15. // Sanitize some options {{{
  16. var obj = obj, opt = opt;
  17. if (typeof(obj) !== 'object') obj = $(obj)[0];
  18. if (typeof(opt) !== 'object') opt = { };
  19. // Some on-the-fly fixes for MSIE...sigh
  20. if (!('trackDocument' in opt))
  21. {
  22. opt.trackDocument = $.browser.msie ? false : true;
  23. if ($.browser.msie && $.browser.version.split('.')[0] == '8')
  24. opt.trackDocument = true;
  25. }
  26. if (!('keySupport' in opt))
  27. opt.keySupport = $.browser.msie ? false : true;
  28. // }}}
  29. // Extend the default options {{{
  30. var defaults = {
  31. // Basic Settings
  32. trackDocument: false,
  33. baseClass: 'jcrop',
  34. addClass: null,
  35. // Styling Options
  36. bgColor: 'black',
  37. bgOpacity: .6,
  38. borderOpacity: .4,
  39. handleOpacity: .5,
  40. handlePad: 5,
  41. handleSize: 9,
  42. handleOffset: 5,
  43. edgeMargin: 14,
  44. aspectRatio: 0,
  45. keySupport: true,
  46. cornerHandles: true,
  47. sideHandles: true,
  48. drawBorders: true,
  49. dragEdges: true,
  50. boxWidth: 0,
  51. boxHeight: 0,
  52. boundary: 8,
  53. animationDelay: 20,
  54. swingSpeed: 3,
  55. allowSelect: true,
  56. allowMove: true,
  57. allowResize: true,
  58. minSelect: [ 0, 0 ],
  59. maxSize: [ 0, 0 ],
  60. minSize: [ 0, 0 ],
  61. // Callbacks / Event Handlers
  62. onChange: function() { },
  63. onSelect: function() { }
  64. };
  65. var options = defaults;
  66. setOptions(opt);
  67. // }}}
  68. // Initialize some jQuery objects {{{
  69. var $origimg = $(obj);
  70. var $img = $origimg.clone().removeAttr('id').css({ position: 'absolute' });
  71. $img.width($origimg.width());
  72. $img.height($origimg.height());
  73. $origimg.after($img).hide();
  74. presize($img,options.boxWidth,options.boxHeight);
  75. var boundx = $img.width(),
  76. boundy = $img.height(),
  77. $div = $('<div />')
  78. .width(boundx).height(boundy)
  79. .addClass(cssClass('holder'))
  80. .css({
  81. position: 'relative',
  82. backgroundColor: options.bgColor
  83. }).insertAfter($origimg).append($img);
  84. ;
  85. if (options.addClass) $div.addClass(options.addClass);
  86. //$img.wrap($div);
  87. var $img2 = $('<img />')/*{{{*/
  88. .attr('src',$img.attr('src'))
  89. .css('position','absolute')
  90. .width(boundx).height(boundy)
  91. ;/*}}}*/
  92. var $img_holder = $('<div />')/*{{{*/
  93. .width(pct(100)).height(pct(100))
  94. .css({
  95. zIndex: 310,
  96. position: 'absolute',
  97. overflow: 'hidden'
  98. })
  99. .append($img2)
  100. ;/*}}}*/
  101. var $hdl_holder = $('<div />')/*{{{*/
  102. .width(pct(100)).height(pct(100))
  103. .css('zIndex',320);
  104. /*}}}*/
  105. var $sel = $('<div />')/*{{{*/
  106. .css({
  107. position: 'absolute',
  108. zIndex: 300
  109. })
  110. .insertBefore($img)
  111. .append($img_holder,$hdl_holder)
  112. ;/*}}}*/
  113. var bound = options.boundary;
  114. var $trk = newTracker().width(boundx+(bound*2)).height(boundy+(bound*2))
  115. .css({ position: 'absolute', top: px(-bound), left: px(-bound), zIndex: 290 })
  116. .mousedown(newSelection);
  117. /* }}} */
  118. // Set more variables {{{
  119. var xlimit, ylimit, xmin, ymin;
  120. var xscale, yscale, enabled = true;
  121. var docOffset = getPos($img),
  122. // Internal states
  123. btndown, lastcurs, dimmed, animating,
  124. shift_down;
  125. // }}}
  126. // }}}
  127. // Internal Modules {{{
  128. var Coords = function()/*{{{*/
  129. {
  130. var x1 = 0, y1 = 0, x2 = 0, y2 = 0, ox, oy;
  131. function setPressed(pos)/*{{{*/
  132. {
  133. var pos = rebound(pos);
  134. x2 = x1 = pos[0];
  135. y2 = y1 = pos[1];
  136. };
  137. /*}}}*/
  138. function setCurrent(pos)/*{{{*/
  139. {
  140. var pos = rebound(pos);
  141. ox = pos[0] - x2;
  142. oy = pos[1] - y2;
  143. x2 = pos[0];
  144. y2 = pos[1];
  145. };
  146. /*}}}*/
  147. function getOffset()/*{{{*/
  148. {
  149. return [ ox, oy ];
  150. };
  151. /*}}}*/
  152. function moveOffset(offset)/*{{{*/
  153. {
  154. var ox = offset[0], oy = offset[1];
  155. if (0 > x1 + ox) ox -= ox + x1;
  156. if (0 > y1 + oy) oy -= oy + y1;
  157. if (boundy < y2 + oy) oy += boundy - (y2 + oy);
  158. if (boundx < x2 + ox) ox += boundx - (x2 + ox);
  159. x1 += ox;
  160. x2 += ox;
  161. y1 += oy;
  162. y2 += oy;
  163. };
  164. /*}}}*/
  165. function getCorner(ord)/*{{{*/
  166. {
  167. var c = getFixed();
  168. switch(ord)
  169. {
  170. case 'ne': return [ c.x2, c.y ];
  171. case 'nw': return [ c.x, c.y ];
  172. case 'se': return [ c.x2, c.y2 ];
  173. case 'sw': return [ c.x, c.y2 ];
  174. }
  175. };
  176. /*}}}*/
  177. function getFixed()/*{{{*/
  178. {
  179. if (!options.aspectRatio) return getRect();
  180. // This function could use some optimization I think...
  181. var aspect = options.aspectRatio,
  182. min_x = options.minSize[0]/xscale,
  183. min_y = options.minSize[1]/yscale,
  184. max_x = options.maxSize[0]/xscale,
  185. max_y = options.maxSize[1]/yscale,
  186. rw = x2 - x1,
  187. rh = y2 - y1,
  188. rwa = Math.abs(rw),
  189. rha = Math.abs(rh),
  190. real_ratio = rwa / rha,
  191. xx, yy
  192. ;
  193. if (max_x == 0) { max_x = boundx * 10 }
  194. if (max_y == 0) { max_y = boundy * 10 }
  195. if (real_ratio < aspect)
  196. {
  197. yy = y2;
  198. w = rha * aspect;
  199. xx = rw < 0 ? x1 - w : w + x1;
  200. if (xx < 0)
  201. {
  202. xx = 0;
  203. h = Math.abs((xx - x1) / aspect);
  204. yy = rh < 0 ? y1 - h: h + y1;
  205. }
  206. else if (xx > boundx)
  207. {
  208. xx = boundx;
  209. h = Math.abs((xx - x1) / aspect);
  210. yy = rh < 0 ? y1 - h : h + y1;
  211. }
  212. }
  213. else
  214. {
  215. xx = x2;
  216. h = rwa / aspect;
  217. yy = rh < 0 ? y1 - h : y1 + h;
  218. if (yy < 0)
  219. {
  220. yy = 0;
  221. w = Math.abs((yy - y1) * aspect);
  222. xx = rw < 0 ? x1 - w : w + x1;
  223. }
  224. else if (yy > boundy)
  225. {
  226. yy = boundy;
  227. w = Math.abs(yy - y1) * aspect;
  228. xx = rw < 0 ? x1 - w : w + x1;
  229. }
  230. }
  231. // Magic %-)
  232. if(xx > x1) { // right side
  233. if(xx - x1 < min_x) {
  234. xx = x1 + min_x;
  235. } else if (xx - x1 > max_x) {
  236. xx = x1 + max_x;
  237. }
  238. if(yy > y1) {
  239. yy = y1 + (xx - x1)/aspect;
  240. } else {
  241. yy = y1 - (xx - x1)/aspect;
  242. }
  243. } else if (xx < x1) { // left side
  244. if(x1 - xx < min_x) {
  245. xx = x1 - min_x
  246. } else if (x1 - xx > max_x) {
  247. xx = x1 - max_x;
  248. }
  249. if(yy > y1) {
  250. yy = y1 + (x1 - xx)/aspect;
  251. } else {
  252. yy = y1 - (x1 - xx)/aspect;
  253. }
  254. }
  255. if(xx < 0) {
  256. x1 -= xx;
  257. xx = 0;
  258. } else if (xx > boundx) {
  259. x1 -= xx - boundx;
  260. xx = boundx;
  261. }
  262. if(yy < 0) {
  263. y1 -= yy;
  264. yy = 0;
  265. } else if (yy > boundy) {
  266. y1 -= yy - boundy;
  267. yy = boundy;
  268. }
  269. return last = makeObj(flipCoords(x1,y1,xx,yy));
  270. };
  271. /*}}}*/
  272. function rebound(p)/*{{{*/
  273. {
  274. if (p[0] < 0) p[0] = 0;
  275. if (p[1] < 0) p[1] = 0;
  276. if (p[0] > boundx) p[0] = boundx;
  277. if (p[1] > boundy) p[1] = boundy;
  278. return [ p[0], p[1] ];
  279. };
  280. /*}}}*/
  281. function flipCoords(x1,y1,x2,y2)/*{{{*/
  282. {
  283. var xa = x1, xb = x2, ya = y1, yb = y2;
  284. if (x2 < x1)
  285. {
  286. xa = x2;
  287. xb = x1;
  288. }
  289. if (y2 < y1)
  290. {
  291. ya = y2;
  292. yb = y1;
  293. }
  294. return [ Math.round(xa), Math.round(ya), Math.round(xb), Math.round(yb) ];
  295. };
  296. /*}}}*/
  297. function getRect()/*{{{*/
  298. {
  299. var xsize = x2 - x1;
  300. var ysize = y2 - y1;
  301. if (xlimit && (Math.abs(xsize) > xlimit))
  302. x2 = (xsize > 0) ? (x1 + xlimit) : (x1 - xlimit);
  303. if (ylimit && (Math.abs(ysize) > ylimit))
  304. y2 = (ysize > 0) ? (y1 + ylimit) : (y1 - ylimit);
  305. if (ymin && (Math.abs(ysize) < ymin))
  306. y2 = (ysize > 0) ? (y1 + ymin) : (y1 - ymin);
  307. if (xmin && (Math.abs(xsize) < xmin))
  308. x2 = (xsize > 0) ? (x1 + xmin) : (x1 - xmin);
  309. if (x1 < 0) { x2 -= x1; x1 -= x1; }
  310. if (y1 < 0) { y2 -= y1; y1 -= y1; }
  311. if (x2 < 0) { x1 -= x2; x2 -= x2; }
  312. if (y2 < 0) { y1 -= y2; y2 -= y2; }
  313. if (x2 > boundx) { var delta = x2 - boundx; x1 -= delta; x2 -= delta; }
  314. if (y2 > boundy) { var delta = y2 - boundy; y1 -= delta; y2 -= delta; }
  315. if (x1 > boundx) { var delta = x1 - boundy; y2 -= delta; y1 -= delta; }
  316. if (y1 > boundy) { var delta = y1 - boundy; y2 -= delta; y1 -= delta; }
  317. return makeObj(flipCoords(x1,y1,x2,y2));
  318. };
  319. /*}}}*/
  320. function makeObj(a)/*{{{*/
  321. {
  322. return { x: a[0], y: a[1], x2: a[2], y2: a[3],
  323. w: a[2] - a[0], h: a[3] - a[1] };
  324. };
  325. /*}}}*/
  326. return {
  327. flipCoords: flipCoords,
  328. setPressed: setPressed,
  329. setCurrent: setCurrent,
  330. getOffset: getOffset,
  331. moveOffset: moveOffset,
  332. getCorner: getCorner,
  333. getFixed: getFixed
  334. };
  335. }();
  336. /*}}}*/
  337. var Selection = function()/*{{{*/
  338. {
  339. var start, end, dragmode, awake, hdep = 370;
  340. var borders = { };
  341. var handle = { };
  342. var seehandles = false;
  343. var hhs = options.handleOffset;
  344. /* Insert draggable elements {{{*/
  345. // Insert border divs for outline
  346. if (options.drawBorders) {
  347. borders = {
  348. top: insertBorder('hline')
  349. .css('top',$.browser.msie?px(-1):px(0)),
  350. bottom: insertBorder('hline'),
  351. left: insertBorder('vline'),
  352. right: insertBorder('vline')
  353. };
  354. }
  355. // Insert handles on edges
  356. if (options.dragEdges) {
  357. handle.t = insertDragbar('n');
  358. handle.b = insertDragbar('s');
  359. handle.r = insertDragbar('e');
  360. handle.l = insertDragbar('w');
  361. }
  362. // Insert side handles
  363. options.sideHandles &&
  364. createHandles(['n','s','e','w']);
  365. // Insert corner handles
  366. options.cornerHandles &&
  367. createHandles(['sw','nw','ne','se']);
  368. /*}}}*/
  369. // Private Methods
  370. function insertBorder(type)/*{{{*/
  371. {
  372. var jq = $('<div />')
  373. .css({position: 'absolute', opacity: options.borderOpacity })
  374. .addClass(cssClass(type));
  375. $img_holder.append(jq);
  376. return jq;
  377. };
  378. /*}}}*/
  379. function dragDiv(ord,zi)/*{{{*/
  380. {
  381. var jq = $('<div />')
  382. .mousedown(createDragger(ord))
  383. .css({
  384. cursor: ord+'-resize',
  385. position: 'absolute',
  386. zIndex: zi
  387. })
  388. ;
  389. $hdl_holder.append(jq);
  390. return jq;
  391. };
  392. /*}}}*/
  393. function insertHandle(ord)/*{{{*/
  394. {
  395. return dragDiv(ord,hdep++)
  396. .css({ top: px(-hhs+1), left: px(-hhs+1), opacity: options.handleOpacity })
  397. .addClass(cssClass('handle'));
  398. };
  399. /*}}}*/
  400. function insertDragbar(ord)/*{{{*/
  401. {
  402. var s = options.handleSize,
  403. o = hhs,
  404. h = s, w = s,
  405. t = o, l = o;
  406. switch(ord)
  407. {
  408. case 'n': case 's': w = pct(100); break;
  409. case 'e': case 'w': h = pct(100); break;
  410. }
  411. return dragDiv(ord,hdep++).width(w).height(h)
  412. .css({ top: px(-t+1), left: px(-l+1)});
  413. };
  414. /*}}}*/
  415. function createHandles(li)/*{{{*/
  416. {
  417. for(i in li) handle[li[i]] = insertHandle(li[i]);
  418. };
  419. /*}}}*/
  420. function moveHandles(c)/*{{{*/
  421. {
  422. var midvert = Math.round((c.h / 2) - hhs),
  423. midhoriz = Math.round((c.w / 2) - hhs),
  424. north = west = -hhs+1,
  425. east = c.w - hhs,
  426. south = c.h - hhs,
  427. x, y;
  428. 'e' in handle &&
  429. handle.e.css({ top: px(midvert), left: px(east) }) &&
  430. handle.w.css({ top: px(midvert) }) &&
  431. handle.s.css({ top: px(south), left: px(midhoriz) }) &&
  432. handle.n.css({ left: px(midhoriz) });
  433. 'ne' in handle &&
  434. handle.ne.css({ left: px(east) }) &&
  435. handle.se.css({ top: px(south), left: px(east) }) &&
  436. handle.sw.css({ top: px(south) });
  437. 'b' in handle &&
  438. handle.b.css({ top: px(south) }) &&
  439. handle.r.css({ left: px(east) });
  440. };
  441. /*}}}*/
  442. function moveto(x,y)/*{{{*/
  443. {
  444. $img2.css({ top: px(-y), left: px(-x) });
  445. $sel.css({ top: px(y), left: px(x) });
  446. };
  447. /*}}}*/
  448. function resize(w,h)/*{{{*/
  449. {
  450. $sel.width(w).height(h);
  451. };
  452. /*}}}*/
  453. function refresh()/*{{{*/
  454. {
  455. var c = Coords.getFixed();
  456. Coords.setPressed([c.x,c.y]);
  457. Coords.setCurrent([c.x2,c.y2]);
  458. updateVisible();
  459. };
  460. /*}}}*/
  461. // Internal Methods
  462. function updateVisible()/*{{{*/
  463. { if (awake) return update(); };
  464. /*}}}*/
  465. function update()/*{{{*/
  466. {
  467. var c = Coords.getFixed();
  468. resize(c.w,c.h);
  469. moveto(c.x,c.y);
  470. options.drawBorders &&
  471. borders['right'].css({ left: px(c.w-1) }) &&
  472. borders['bottom'].css({ top: px(c.h-1) });
  473. seehandles && moveHandles(c);
  474. awake || show();
  475. options.onChange(unscale(c));
  476. };
  477. /*}}}*/
  478. function show()/*{{{*/
  479. {
  480. $sel.show();
  481. $img.css('opacity',options.bgOpacity);
  482. awake = true;
  483. };
  484. /*}}}*/
  485. function release()/*{{{*/
  486. {
  487. disableHandles();
  488. $sel.hide();
  489. $img.css('opacity',1);
  490. awake = false;
  491. };
  492. /*}}}*/
  493. function showHandles()//{{{
  494. {
  495. if (seehandles)
  496. {
  497. moveHandles(Coords.getFixed());
  498. $hdl_holder.show();
  499. }
  500. };
  501. //}}}
  502. function enableHandles()/*{{{*/
  503. {
  504. seehandles = true;
  505. if (options.allowResize)
  506. {
  507. moveHandles(Coords.getFixed());
  508. $hdl_holder.show();
  509. return true;
  510. }
  511. };
  512. /*}}}*/
  513. function disableHandles()/*{{{*/
  514. {
  515. seehandles = false;
  516. $hdl_holder.hide();
  517. };
  518. /*}}}*/
  519. function animMode(v)/*{{{*/
  520. {
  521. (animating = v) ? disableHandles(): enableHandles();
  522. };
  523. /*}}}*/
  524. function done()/*{{{*/
  525. {
  526. animMode(false);
  527. refresh();
  528. };
  529. /*}}}*/
  530. var $track = newTracker().mousedown(createDragger('move'))
  531. .css({ cursor: 'move', position: 'absolute', zIndex: 360 })
  532. $img_holder.append($track);
  533. disableHandles();
  534. return {
  535. updateVisible: updateVisible,
  536. update: update,
  537. release: release,
  538. refresh: refresh,
  539. setCursor: function (cursor) { $track.css('cursor',cursor); },
  540. enableHandles: enableHandles,
  541. enableOnly: function() { seehandles = true; },
  542. showHandles: showHandles,
  543. disableHandles: disableHandles,
  544. animMode: animMode,
  545. done: done
  546. };
  547. }();
  548. /*}}}*/
  549. var Tracker = function()/*{{{*/
  550. {
  551. var onMove = function() { },
  552. onDone = function() { },
  553. trackDoc = options.trackDocument;
  554. if (!trackDoc)
  555. {
  556. $trk
  557. .mousemove(trackMove)
  558. .mouseup(trackUp)
  559. .mouseout(trackUp)
  560. ;
  561. }
  562. function toFront()/*{{{*/
  563. {
  564. $trk.css({zIndex:450});
  565. if (trackDoc)
  566. {
  567. $(document)
  568. .mousemove(trackMove)
  569. .mouseup(trackUp)
  570. ;
  571. }
  572. }
  573. /*}}}*/
  574. function toBack()/*{{{*/
  575. {
  576. $trk.css({zIndex:290});
  577. if (trackDoc)
  578. {
  579. $(document)
  580. .unbind('mousemove',trackMove)
  581. .unbind('mouseup',trackUp)
  582. ;
  583. }
  584. }
  585. /*}}}*/
  586. function trackMove(e)/*{{{*/
  587. {
  588. onMove(mouseAbs(e));
  589. };
  590. /*}}}*/
  591. function trackUp(e)/*{{{*/
  592. {
  593. e.preventDefault();
  594. e.stopPropagation();
  595. if (btndown)
  596. {
  597. btndown = false;
  598. onDone(mouseAbs(e));
  599. options.onSelect(unscale(Coords.getFixed()));
  600. toBack();
  601. onMove = function() { };
  602. onDone = function() { };
  603. }
  604. return false;
  605. };
  606. /*}}}*/
  607. function activateHandlers(move,done)/* {{{ */
  608. {
  609. btndown = true;
  610. onMove = move;
  611. onDone = done;
  612. toFront();
  613. return false;
  614. };
  615. /* }}} */
  616. function setCursor(t) { $trk.css('cursor',t); };
  617. $img.before($trk);
  618. return {
  619. activateHandlers: activateHandlers,
  620. setCursor: setCursor
  621. };
  622. }();
  623. /*}}}*/
  624. var KeyManager = function()/*{{{*/
  625. {
  626. var $keymgr = $('<input type="radio" />')
  627. .css({ position: 'absolute', left: '-30px' })
  628. .keypress(parseKey)
  629. .blur(onBlur),
  630. $keywrap = $('<div />')
  631. .css({
  632. position: 'absolute',
  633. overflow: 'hidden'
  634. })
  635. .append($keymgr)
  636. ;
  637. function watchKeys()/*{{{*/
  638. {
  639. if (options.keySupport)
  640. {
  641. $keymgr.show();
  642. $keymgr.focus();
  643. }
  644. };
  645. /*}}}*/
  646. function onBlur(e)/*{{{*/
  647. {
  648. $keymgr.hide();
  649. };
  650. /*}}}*/
  651. function doNudge(e,x,y)/*{{{*/
  652. {
  653. if (options.allowMove) {
  654. Coords.moveOffset([x,y]);
  655. Selection.updateVisible();
  656. };
  657. e.preventDefault();
  658. e.stopPropagation();
  659. };
  660. /*}}}*/
  661. function parseKey(e)/*{{{*/
  662. {
  663. if (e.ctrlKey) return true;
  664. shift_down = e.shiftKey ? true : false;
  665. var nudge = shift_down ? 10 : 1;
  666. switch(e.keyCode)
  667. {
  668. case 37: doNudge(e,-nudge,0); break;
  669. case 39: doNudge(e,nudge,0); break;
  670. case 38: doNudge(e,0,-nudge); break;
  671. case 40: doNudge(e,0,nudge); break;
  672. case 27: Selection.release(); break;
  673. case 9: return true;
  674. }
  675. return nothing(e);
  676. };
  677. /*}}}*/
  678. if (options.keySupport) $keywrap.insertBefore($img);
  679. return {
  680. watchKeys: watchKeys
  681. };
  682. }();
  683. /*}}}*/
  684. // }}}
  685. // Internal Methods {{{
  686. function px(n) { return '' + parseInt(n) + 'px'; };
  687. function pct(n) { return '' + parseInt(n) + '%'; };
  688. function cssClass(cl) { return options.baseClass + '-' + cl; };
  689. function getPos(obj)/*{{{*/
  690. {
  691. // Updated in v0.9.4 to use built-in dimensions plugin
  692. var pos = $(obj).offset();
  693. return [ pos.left, pos.top ];
  694. };
  695. /*}}}*/
  696. function mouseAbs(e)/*{{{*/
  697. {
  698. return [ (e.pageX - docOffset[0]), (e.pageY - docOffset[1]) ];
  699. };
  700. /*}}}*/
  701. function myCursor(type)/*{{{*/
  702. {
  703. if (type != lastcurs)
  704. {
  705. Tracker.setCursor(type);
  706. //Handles.xsetCursor(type);
  707. lastcurs = type;
  708. }
  709. };
  710. /*}}}*/
  711. function startDragMode(mode,pos)/*{{{*/
  712. {
  713. docOffset = getPos($img);
  714. Tracker.setCursor(mode=='move'?mode:mode+'-resize');
  715. if (mode == 'move')
  716. return Tracker.activateHandlers(createMover(pos), doneSelect);
  717. var fc = Coords.getFixed();
  718. var opp = oppLockCorner(mode);
  719. var opc = Coords.getCorner(oppLockCorner(opp));
  720. Coords.setPressed(Coords.getCorner(opp));
  721. Coords.setCurrent(opc);
  722. Tracker.activateHandlers(dragmodeHandler(mode,fc),doneSelect);
  723. };
  724. /*}}}*/
  725. function dragmodeHandler(mode,f)/*{{{*/
  726. {
  727. return function(pos) {
  728. if (!options.aspectRatio) switch(mode)
  729. {
  730. case 'e': pos[1] = f.y2; break;
  731. case 'w': pos[1] = f.y2; break;
  732. case 'n': pos[0] = f.x2; break;
  733. case 's': pos[0] = f.x2; break;
  734. }
  735. else switch(mode)
  736. {
  737. case 'e': pos[1] = f.y+1; break;
  738. case 'w': pos[1] = f.y+1; break;
  739. case 'n': pos[0] = f.x+1; break;
  740. case 's': pos[0] = f.x+1; break;
  741. }
  742. Coords.setCurrent(pos);
  743. Selection.update();
  744. };
  745. };
  746. /*}}}*/
  747. function createMover(pos)/*{{{*/
  748. {
  749. var lloc = pos;
  750. KeyManager.watchKeys();
  751. return function(pos)
  752. {
  753. Coords.moveOffset([pos[0] - lloc[0], pos[1] - lloc[1]]);
  754. lloc = pos;
  755. Selection.update();
  756. };
  757. };
  758. /*}}}*/
  759. function oppLockCorner(ord)/*{{{*/
  760. {
  761. switch(ord)
  762. {
  763. case 'n': return 'sw';
  764. case 's': return 'nw';
  765. case 'e': return 'nw';
  766. case 'w': return 'ne';
  767. case 'ne': return 'sw';
  768. case 'nw': return 'se';
  769. case 'se': return 'nw';
  770. case 'sw': return 'ne';
  771. };
  772. };
  773. /*}}}*/
  774. function createDragger(ord)/*{{{*/
  775. {
  776. return function(e) {
  777. if (options.disabled) return false;
  778. if ((ord == 'move') && !options.allowMove) return false;
  779. btndown = true;
  780. startDragMode(ord,mouseAbs(e));
  781. e.stopPropagation();
  782. e.preventDefault();
  783. return false;
  784. };
  785. };
  786. /*}}}*/
  787. function presize($obj,w,h)/*{{{*/
  788. {
  789. var nw = $obj.width(), nh = $obj.height();
  790. if ((nw > w) && w > 0)
  791. {
  792. nw = w;
  793. nh = (w/$obj.width()) * $obj.height();
  794. }
  795. if ((nh > h) && h > 0)
  796. {
  797. nh = h;
  798. nw = (h/$obj.height()) * $obj.width();
  799. }
  800. xscale = $obj.width() / nw;
  801. yscale = $obj.height() / nh;
  802. $obj.width(nw).height(nh);
  803. };
  804. /*}}}*/
  805. function unscale(c)/*{{{*/
  806. {
  807. return {
  808. x: parseInt(c.x * xscale), y: parseInt(c.y * yscale),
  809. x2: parseInt(c.x2 * xscale), y2: parseInt(c.y2 * yscale),
  810. w: parseInt(c.w * xscale), h: parseInt(c.h * yscale)
  811. };
  812. };
  813. /*}}}*/
  814. function doneSelect(pos)/*{{{*/
  815. {
  816. var c = Coords.getFixed();
  817. if (c.w > options.minSelect[0] && c.h > options.minSelect[1])
  818. {
  819. Selection.enableHandles();
  820. Selection.done();
  821. }
  822. else
  823. {
  824. Selection.release();
  825. }
  826. Tracker.setCursor( options.allowSelect?'crosshair':'default' );
  827. };
  828. /*}}}*/
  829. function newSelection(e)/*{{{*/
  830. {
  831. if (options.disabled) return false;
  832. if (!options.allowSelect) return false;
  833. btndown = true;
  834. docOffset = getPos($img);
  835. Selection.disableHandles();
  836. myCursor('crosshair');
  837. var pos = mouseAbs(e);
  838. Coords.setPressed(pos);
  839. Tracker.activateHandlers(selectDrag,doneSelect);
  840. KeyManager.watchKeys();
  841. Selection.update();
  842. e.stopPropagation();
  843. e.preventDefault();
  844. return false;
  845. };
  846. /*}}}*/
  847. function selectDrag(pos)/*{{{*/
  848. {
  849. Coords.setCurrent(pos);
  850. Selection.update();
  851. };
  852. /*}}}*/
  853. function newTracker()
  854. {
  855. var trk = $('<div></div>').addClass(cssClass('tracker'));
  856. $.browser.msie && trk.css({ opacity: 0, backgroundColor: 'white' });
  857. return trk;
  858. };
  859. // }}}
  860. // API methods {{{
  861. function animateTo(a)/*{{{*/
  862. {
  863. var x1 = a[0] / xscale,
  864. y1 = a[1] / yscale,
  865. x2 = a[2] / xscale,
  866. y2 = a[3] / yscale;
  867. if (animating) return;
  868. var animto = Coords.flipCoords(x1,y1,x2,y2);
  869. var c = Coords.getFixed();
  870. var animat = initcr = [ c.x, c.y, c.x2, c.y2 ];
  871. var interv = options.animationDelay;
  872. var x = animat[0];
  873. var y = animat[1];
  874. var x2 = animat[2];
  875. var y2 = animat[3];
  876. var ix1 = animto[0] - initcr[0];
  877. var iy1 = animto[1] - initcr[1];
  878. var ix2 = animto[2] - initcr[2];
  879. var iy2 = animto[3] - initcr[3];
  880. var pcent = 0;
  881. var velocity = options.swingSpeed;
  882. Selection.animMode(true);
  883. var animator = function()
  884. {
  885. return function()
  886. {
  887. pcent += (100 - pcent) / velocity;
  888. animat[0] = x + ((pcent / 100) * ix1);
  889. animat[1] = y + ((pcent / 100) * iy1);
  890. animat[2] = x2 + ((pcent / 100) * ix2);
  891. animat[3] = y2 + ((pcent / 100) * iy2);
  892. if (pcent < 100) animateStart();
  893. else Selection.done();
  894. if (pcent >= 99.8) pcent = 100;
  895. setSelectRaw(animat);
  896. };
  897. }();
  898. function animateStart()
  899. { window.setTimeout(animator,interv); };
  900. animateStart();
  901. };
  902. /*}}}*/
  903. function setSelect(rect)//{{{
  904. {
  905. setSelectRaw([rect[0]/xscale,rect[1]/yscale,rect[2]/xscale,rect[3]/yscale]);
  906. };
  907. //}}}
  908. function setSelectRaw(l) /*{{{*/
  909. {
  910. Coords.setPressed([l[0],l[1]]);
  911. Coords.setCurrent([l[2],l[3]]);
  912. Selection.update();
  913. };
  914. /*}}}*/
  915. function setOptions(opt)/*{{{*/
  916. {
  917. if (typeof(opt) != 'object') opt = { };
  918. options = $.extend(options,opt);
  919. if (typeof(options.onChange)!=='function')
  920. options.onChange = function() { };
  921. if (typeof(options.onSelect)!=='function')
  922. options.onSelect = function() { };
  923. };
  924. /*}}}*/
  925. function tellSelect()/*{{{*/
  926. {
  927. return unscale(Coords.getFixed());
  928. };
  929. /*}}}*/
  930. function tellScaled()/*{{{*/
  931. {
  932. return Coords.getFixed();
  933. };
  934. /*}}}*/
  935. function setOptionsNew(opt)/*{{{*/
  936. {
  937. setOptions(opt);
  938. interfaceUpdate();
  939. };
  940. /*}}}*/
  941. function disableCrop()//{{{
  942. {
  943. options.disabled = true;
  944. Selection.disableHandles();
  945. Selection.setCursor('default');
  946. Tracker.setCursor('default');
  947. };
  948. //}}}
  949. function enableCrop()//{{{
  950. {
  951. options.disabled = false;
  952. interfaceUpdate();
  953. };
  954. //}}}
  955. function cancelCrop()//{{{
  956. {
  957. Selection.done();
  958. Tracker.activateHandlers(null,null);
  959. };
  960. //}}}
  961. function destroy()//{{{
  962. {
  963. $div.remove();
  964. $origimg.show();
  965. };
  966. //}}}
  967. function interfaceUpdate(alt)//{{{
  968. // This method tweaks the interface based on options object.
  969. // Called when options are changed and at end of initialization.
  970. {
  971. options.allowResize ?
  972. alt?Selection.enableOnly():Selection.enableHandles():
  973. Selection.disableHandles();
  974. Tracker.setCursor( options.allowSelect? 'crosshair': 'default' );
  975. Selection.setCursor( options.allowMove? 'move': 'default' );
  976. $div.css('backgroundColor',options.bgColor);
  977. if ('setSelect' in options) {
  978. setSelect(opt.setSelect);
  979. Selection.done();
  980. delete(options.setSelect);
  981. }
  982. if ('trueSize' in options) {
  983. xscale = options.trueSize[0] / boundx;
  984. yscale = options.trueSize[1] / boundy;
  985. }
  986. xlimit = options.maxSize[0] || 0;
  987. ylimit = options.maxSize[1] || 0;
  988. xmin = options.minSize[0] || 0;
  989. ymin = options.minSize[1] || 0;
  990. if ('outerImage' in options)
  991. {
  992. $img.attr('src',options.outerImage);
  993. delete(options.outerImage);
  994. }
  995. Selection.refresh();
  996. };
  997. //}}}
  998. // }}}
  999. $hdl_holder.hide();
  1000. interfaceUpdate(true);
  1001. var api = {
  1002. animateTo: animateTo,
  1003. setSelect: setSelect,
  1004. setOptions: setOptionsNew,
  1005. tellSelect: tellSelect,
  1006. tellScaled: tellScaled,
  1007. disable: disableCrop,
  1008. enable: enableCrop,
  1009. cancel: cancelCrop,
  1010. focus: KeyManager.watchKeys,
  1011. getBounds: function() { return [ boundx * xscale, boundy * yscale ]; },
  1012. getWidgetSize: function() { return [ boundx, boundy ]; },
  1013. release: Selection.release,
  1014. destroy: destroy
  1015. };
  1016. $origimg.data('Jcrop',api);
  1017. return api;
  1018. };
  1019. $.fn.Jcrop = function(options)/*{{{*/
  1020. {
  1021. function attachWhenDone(from)/*{{{*/
  1022. {
  1023. var loadsrc = options.useImg || from.src;
  1024. var img = new Image();
  1025. img.onload = function() { $.Jcrop(from,options); };
  1026. img.src = loadsrc;
  1027. };
  1028. /*}}}*/
  1029. if (typeof(options) !== 'object') options = { };
  1030. // Iterate over each object, attach Jcrop
  1031. this.each(function()
  1032. {
  1033. // If we've already attached to this object
  1034. if ($(this).data('Jcrop'))
  1035. {
  1036. // The API can be requested this way (undocumented)
  1037. if (options == 'api') return $(this).data('Jcrop');
  1038. // Otherwise, we just reset the options...
  1039. else $(this).data('Jcrop').setOptions(options);
  1040. }
  1041. // If we haven't been attached, preload and attach
  1042. else attachWhenDone(this);
  1043. });
  1044. // Return "this" so we're chainable a la jQuery plugin-style!
  1045. return this;
  1046. };
  1047. /*}}}*/
  1048. })(jQuery);
  1049. /*
  1050. * zyPopup.js 基于HTML5 文件上传的弹出层脚本 http://www.52doit.com
  1051. * by zhangyan 2014-07-12 QQ : 623585268
  1052. */
  1053. (function($,undefined){
  1054. $.fn.zyPopup = function(options,param){
  1055. var otherArgs = Array.prototype.slice.call(arguments, 1);
  1056. if (typeof options == "string") {
  1057. var fn = this[0][options];
  1058. if($.isFunction(fn)){
  1059. return fn.apply(this, otherArgs);
  1060. }else{
  1061. throw ("zyPopup - No such method: " + options);
  1062. }
  1063. }
  1064. return this.each(function(){
  1065. var para = {}; // 保留参数
  1066. var self = this; // 保存组件对象
  1067. var zoom = "",
  1068. zoomContent = "",
  1069. zoomedIn = false,
  1070. openedImage = null,
  1071. windowWidth = "",
  1072. windowHeight = "";
  1073. var tailorVal = {}; // 保留裁剪之后的值
  1074. var defaults = {
  1075. src : "", // 图片的src地址
  1076. index : 0, // 图片在预览区域的索引
  1077. name : "", // 图片的名字
  1078. onTailor : function(val){} // 返回图片裁剪的坐标和宽高的值
  1079. };
  1080. para = $.extend(defaults,options);
  1081. this.init = function(){
  1082. this.createHtml(); // 创建组件html
  1083. this.openPopup(); // 打开弹出层
  1084. this.bindPopupEvent(); // 绑定弹出层事件
  1085. };
  1086. /**
  1087. * 功能:创建弹出层所使用的html
  1088. * 参数: 无
  1089. * 返回: 无
  1090. */
  1091. this.createHtml = function(){
  1092. // 有可能多次创建此插件,所以要先删除
  1093. $("#zoom").remove();
  1094. $("body").append('<div id="zoom"><a class="finish"></a><a class="close"></a><div class="content loading" style="width: 750px; height: 340px;"></div></div>');
  1095. zoom = $("#zoom").hide(),
  1096. zoomContent = $("#zoom .content"),
  1097. zoomedIn = false,
  1098. openedImage = null,
  1099. windowWidth = $(window).width(),
  1100. windowHeight = $(window).height();
  1101. };
  1102. /**
  1103. * 功能:打开弹出层
  1104. * 参数: 无
  1105. * 返回: 无
  1106. */
  1107. this.openPopup = function(){
  1108. var self = this;
  1109. var image = $(new Image()).attr("id","tailorImg").hide();
  1110. $("#zoom .previous, #zoom .next").show();
  1111. if (!zoomedIn) {
  1112. zoomedIn = true;
  1113. zoom.show();
  1114. $("body").addClass("zoomed");
  1115. }
  1116. zoomContent.html(image).delay(500).addClass("loading");
  1117. image.load(render).attr("src", para.src);
  1118. // 渲染
  1119. function render() {
  1120. var image = $(this),
  1121. borderWidth = parseInt(zoomContent.css("borderLeftWidth")),
  1122. maxImageWidth = windowWidth - (borderWidth * 2),
  1123. maxImageHeight = windowHeight - (borderWidth * 2),
  1124. imageWidth = image.width(),
  1125. imageHeight = image.height();
  1126. if (imageWidth == zoomContent.width() && imageWidth <= maxImageWidth && imageHeight == zoomContent.height() && imageHeight <= maxImageHeight) {
  1127. show(image);
  1128. return;
  1129. }
  1130. zoomContent.animate({
  1131. width: imageWidth,
  1132. height: imageHeight,
  1133. marginTop: -(image.height() / 2) - borderWidth,
  1134. marginLeft: -(image.width() / 2) - borderWidth
  1135. }, 200, function() {
  1136. show(image);
  1137. });
  1138. // 显示
  1139. function show(image) {
  1140. image.show();
  1141. zoomContent.removeClass("loading");
  1142. self.createTailorPlug();
  1143. }
  1144. }
  1145. };
  1146. /**
  1147. * 功能: 加载裁剪插件
  1148. * 参数: 无
  1149. * 返回: 无
  1150. */
  1151. this.createTailorPlug = function(){
  1152. // 设置默认选择区域的范围
  1153. var width = $("#tailorImg").width();
  1154. var height = $("#tailorImg").height();
  1155. var x1 = (width/2)-(width/5);
  1156. var y1 = (height/2)-(height/5);
  1157. var x2 = (width/2)+(width/5);
  1158. var y2 = (height/2)+(height/5);
  1159. // 创建插件
  1160. var api = $.Jcrop("#tailorImg",{
  1161. setSelect : [x1,y1,x2,y2], //setSelect是Jcrop插件内部已定义的运动方法
  1162. onChange : setCoords,
  1163. onSelect : setCoords
  1164. });
  1165. // 设置选择区域的值
  1166. function setCoords(obj){
  1167. tailorVal = {"leftX" : obj.x, "leftY" : obj.y, "rightX" : obj.x2, "rightY" : obj.y2, "width" : obj.w, "height" : obj.h};
  1168. }
  1169. };
  1170. /**
  1171. * 功能: 绑定事件
  1172. * 参数: 无
  1173. * 返回: 无
  1174. */
  1175. this.bindPopupEvent = function(){
  1176. var self = this;
  1177. // 弹出层本身的点击事件
  1178. zoom.bind("click", function(event) {
  1179. event.preventDefault();
  1180. if ($(event.target).attr("id") == "zoom"){
  1181. // 关闭弹出层
  1182. self.closePopup(event);
  1183. }
  1184. });
  1185. // 弹出层完成按钮的点击事件
  1186. $("#zoom .finish").bind("click", function(event){
  1187. var quondamImgInfo = new Object();
  1188. quondamImgInfo["width"] = $(".jcrop-holder>div>div>img").width();
  1189. quondamImgInfo["height"] = $(".jcrop-holder>div>div>img").height();
  1190. // 回调方法,将裁减的数据返回
  1191. para.onTailor(tailorVal, quondamImgInfo);
  1192. // 关闭弹出层
  1193. self.closePopup(event);
  1194. });
  1195. // 弹出层关闭按钮的点击事件
  1196. $("#zoom .close").bind("click", function(event){
  1197. // 关闭弹出层
  1198. self.closePopup(event);
  1199. });
  1200. };
  1201. /**
  1202. * 功能: 打开弹出层
  1203. * 参数: event 事件源
  1204. * 返回: 无
  1205. */
  1206. this.closePopup = function(event){
  1207. if (event){
  1208. event.preventDefault();
  1209. }
  1210. zoomedIn = false;
  1211. openedImage = null;
  1212. zoom.hide();
  1213. $("body").removeClass("zoomed");
  1214. zoomContent.empty();
  1215. };
  1216. // 初始化裁剪插件
  1217. this.init();
  1218. });
  1219. };
  1220. })(jQuery);
  1221. /*
  1222. * zyFile.js 基于HTML5 文件上传的核心脚本 http://www.52doit.com
  1223. * by zhangyan 2014-06-21 QQ : 623585268
  1224. */
  1225. var ZYFILE = {
  1226. fileInput : null, // 选择文件按钮dom对象
  1227. uploadInput : null, // 上传文件按钮dom对象
  1228. dragDrop: null, // 拖拽敏感区域
  1229. url : "", // 上传action路径
  1230. uploadFile : [], // 需要上传的文件数组
  1231. lastUploadFile : [], // 上一次选择的文件数组,方便继续上传使用
  1232. perUploadFile : [], // 存放永久的文件数组,方便删除使用
  1233. fileNum : 0, // 代表文件总个数,因为涉及到继续添加,所以下一次添加需要在它的基础上添加索引
  1234. /* 提供给外部的接口 */
  1235. filterFile : function(files){ // 提供给外部的过滤文件格式等的接口,外部需要把过滤后的文件返回
  1236. return files;
  1237. },
  1238. onSelect : function(selectFile, files){ // 提供给外部获取选中的文件,供外部实现预览等功能 selectFile:当前选中的文件 allFiles:还没上传的全部文件
  1239. },
  1240. onDelete : function(file, files){ // 提供给外部获取删除的单个文件,供外部实现删除效果 file:当前删除的文件 files:删除之后的文件
  1241. },
  1242. onProgress : function(file, loaded, total){ // 提供给外部获取单个文件的上传进度,供外部实现上传进度效果
  1243. },
  1244. onSuccess : function(file, responseInfo){ // 提供给外部获取单个文件上传成功,供外部实现成功效果
  1245. },
  1246. onFailure : function(file, responseInfo){ // 提供给外部获取单个文件上传失败,供外部实现失败效果
  1247. },
  1248. onComplete : function(responseInfo){ // 提供给外部获取全部文件上传完成,供外部实现完成效果
  1249. },
  1250. /* 内部实现功能方法 */
  1251. // 获得选中的文件
  1252. //文件拖放
  1253. funDragHover: function(e) {
  1254. e.stopPropagation();
  1255. e.preventDefault();
  1256. this[e.type === "dragover"? "onDragOver": "onDragLeave"].call(e.target);
  1257. return this;
  1258. },
  1259. // 获取文件
  1260. funGetFiles : function(e){
  1261. var self = this;
  1262. // 取消鼠标经过样式
  1263. this.funDragHover(e);
  1264. // 从事件中获取选中的所有文件
  1265. var files = e.target.files || e.dataTransfer.files;
  1266. self.lastUploadFile = this.uploadFile;
  1267. this.uploadFile = this.uploadFile.concat(this.filterFile(files));
  1268. var tmpFiles = [];
  1269. // 因为jquery的inArray方法无法对object数组进行判断是否存在于,所以只能提取名称进行判断
  1270. var lArr = []; // 之前文件的名称数组
  1271. var uArr = []; // 现在文件的名称数组
  1272. $.each(self.lastUploadFile, function(k, v){
  1273. lArr.push(v.name);
  1274. });
  1275. $.each(self.uploadFile, function(k, v){
  1276. uArr.push(v.name);
  1277. });
  1278. $.each(uArr, function(k, v){
  1279. // 获得当前选择的每一个文件 判断当前这一个文件是否存在于之前的文件当中
  1280. if($.inArray(v, lArr) < 0){ // 不存在
  1281. tmpFiles.push(self.uploadFile[k]);
  1282. }
  1283. });
  1284. // 如果tmpFiles进行过过滤上一次选择的文件的操作,需要把过滤后的文件赋值
  1285. //if(tmpFiles.length!=0){
  1286. this.uploadFile = tmpFiles;
  1287. //}
  1288. // 调用对文件处理的方法
  1289. this.funDealtFiles();
  1290. return true;
  1291. },
  1292. // 处理过滤后的文件,给每个文件设置下标
  1293. funDealtFiles : function(){
  1294. var self = this;
  1295. // 目前是遍历所有的文件,给每个文件增加唯一索引值
  1296. $.each(this.uploadFile, function(k, v){
  1297. // 因为涉及到继续添加,所以下一次添加需要在总个数的基础上添加
  1298. v.index = self.fileNum;
  1299. // 添加一个之后自增
  1300. self.fileNum++;
  1301. });
  1302. // 先把当前选中的文件保存备份
  1303. var selectFile = this.uploadFile;
  1304. // 要把全部的文件都保存下来,因为删除所使用的下标是全局的变量
  1305. this.perUploadFile = this.perUploadFile.concat(this.uploadFile);
  1306. // 合并下上传的文件
  1307. this.uploadFile = this.lastUploadFile.concat(this.uploadFile);
  1308. // 执行选择回调
  1309. this.onSelect(selectFile, this.uploadFile);
  1310. console.info("继续选择");
  1311. console.info(this.uploadFile);
  1312. return this;
  1313. },
  1314. // 处理需要删除的文件 isCb代表是否回调onDelete方法
  1315. // 因为上传完成并不希望在页面上删除div,但是单独点击删除的时候需要删除div 所以用isCb做判断
  1316. funDeleteFile : function(delFileIndex, isCb){
  1317. var self = this; // 在each中this指向没个v 所以先将this保留
  1318. var tmpFile = []; // 用来替换的文件数组
  1319. // 合并下上传的文件
  1320. var delFile = this.perUploadFile[delFileIndex];
  1321. //console.info(delFile);
  1322. // 目前是遍历所有的文件,对比每个文件 删除
  1323. $.each(this.uploadFile, function(k, v){
  1324. if(delFile != v){
  1325. // 如果不是删除的那个文件 就放到临时数组中
  1326. tmpFile.push(v);
  1327. }
  1328. });
  1329. this.uploadFile = tmpFile;
  1330. if(isCb){ // 执行回调
  1331. // 回调删除方法,供外部进行删除效果的实现
  1332. self.onDelete(delFile, this.uploadFile);
  1333. }
  1334. console.info("还剩这些文件没有上传:");
  1335. console.info(this.uploadFile);
  1336. return true;
  1337. },
  1338. // 上传多个文件
  1339. funUploadFiles : function(){
  1340. var self = this; // 在each中this指向没个v 所以先将this保留
  1341. // 遍历所有文件 ,在调用单个文件上传的方法
  1342. var formdata = new FormData();
  1343. $.each(this.uploadFile, function(k, file){
  1344. // self.funUploadFile(v);
  1345. formdata.append("file[]", file);
  1346. });
  1347. var xhr = new XMLHttpRequest();
  1348. xhr.upload.addEventListener("progress", function(e){
  1349. // 回调到外部
  1350. self.onProgress(file, e.loaded, e.total);
  1351. }, false);
  1352. // 完成
  1353. xhr.addEventListener("load", function(e){
  1354. // 从文件中删除上传成功的文件 false是不执行onDelete回调方法
  1355. self.funDeleteFile(file.index, false);
  1356. // 回调到外部
  1357. self.onSuccess(file, xhr.responseText);
  1358. if(self.uploadFile.length==0){
  1359. // 回调全部完成方法
  1360. self.onComplete("全部完成");
  1361. }
  1362. }, false);
  1363. xhr.open("POST",self.url, false);
  1364. xhr.send(formdata);
  1365. },
  1366. // 上传单个个文件
  1367. funUploadFile : function(file){
  1368. var self = this; // 在each中this指向没个v 所以先将this保留
  1369. var formdata = new FormData();
  1370. formdata.append("file", file);
  1371. // 添加裁剪的坐标和宽高发送给后台
  1372. if($("#uploadTailor_"+file.index).length>0){
  1373. // 除了这样获取不到zyUpload的值啊啊啊啊啊啊啊啊啊啊啊
  1374. formdata.append("tailor", $("#uploadTailor_"+file.index).attr("tailor"));
  1375. }
  1376. var xhr = new XMLHttpRequest();
  1377. // 绑定上传事件
  1378. // 进度
  1379. xhr.upload.addEventListener("progress", function(e){
  1380. // 回调到外部
  1381. self.onProgress(file, e.loaded, e.total);
  1382. }, false);
  1383. // 完成
  1384. xhr.addEventListener("load", function(e){
  1385. // 从文件中删除上传成功的文件 false是不执行onDelete回调方法
  1386. self.funDeleteFile(file.index, false);
  1387. // 回调到外部
  1388. self.onSuccess(file, xhr.responseText);
  1389. if(self.uploadFile.length==0){
  1390. // 回调全部完成方法
  1391. self.onComplete("全部完成");
  1392. }
  1393. }, false);
  1394. // 错误
  1395. xhr.addEventListener("error", function(e){
  1396. // 回调到外部
  1397. self.onFailure(file, xhr.responseText);
  1398. }, false);
  1399. xhr.open("POST",self.url, false);
  1400. xhr.send(formdata);
  1401. },
  1402. // 返回需要上传的文件
  1403. funReturnNeedFiles : function(){
  1404. return this.uploadFile;
  1405. },
  1406. // 初始化
  1407. init : function(){ // 初始化方法,在此给选择、上传按钮绑定事件
  1408. var self = this; // 克隆一个自身
  1409. if (this.dragDrop) {
  1410. this.dragDrop.addEventListener("dragover", function(e) { self.funDragHover(e); }, false);
  1411. this.dragDrop.addEventListener("dragleave", function(e) { self.funDragHover(e); }, false);
  1412. this.dragDrop.addEventListener("drop", function(e) { self.funGetFiles(e); }, false);
  1413. }
  1414. // 如果选择按钮存在
  1415. if(self.fileInput){
  1416. // 绑定change事件
  1417. this.fileInput.addEventListener("change", function(e) {
  1418. self.funGetFiles(e);
  1419. }, false);
  1420. }
  1421. // 如果上传按钮存在
  1422. if(self.uploadInput){
  1423. // 绑定click事件
  1424. this.uploadInput.addEventListener("click", function(e) {
  1425. self.funUploadFiles(e);
  1426. }, false);
  1427. }
  1428. }
  1429. };
  1430. /*
  1431. * zyUpload.js 基于HTML5 文件上传的血肉脚本 http://www.52doit.com
  1432. * by zhangyan 2014-06-26 QQ : 623585268
  1433. */
  1434. (function($,undefined){
  1435. $.fn.zyUpload = function(options,param){
  1436. var otherArgs = Array.prototype.slice.call(arguments, 1);
  1437. if (typeof options == 'string') {
  1438. var fn = this[0][options];
  1439. if($.isFunction(fn)){
  1440. return fn.apply(this, otherArgs);
  1441. }else{
  1442. throw ("zyUpload - No such method: " + options);
  1443. }
  1444. }
  1445. return this.each(function(){
  1446. var para = {}; // 保留参数
  1447. var self = this; // 保存组件对象
  1448. var defaults = {
  1449. width : "700px", // 宽度
  1450. height : "400px", // 宽度
  1451. itemWidth : "140px", // 文件项的宽度
  1452. itemHeight : "120px", // 文件项的高度
  1453. url : "/upload/UploadAction", // 上传文件的路径
  1454. fileType : [], // 上传文件的类型
  1455. fileSize : 51200000, // 上传文件的大小
  1456. multiple : true, // 是否可以多个文件上传
  1457. dragDrop : true, // 是否可以拖动上传文件
  1458. // edit : true, // 是否可以编辑文件(裁剪)
  1459. tailor : false, // 是否可以截取图片
  1460. del : true, // 是否可以删除文件
  1461. finishDel : false, // 是否在上传文件完成后删除预览
  1462. /* 提供给外部的接口方法 */
  1463. onSelect : function(selectFiles, allFiles){}, // 选择文件的回调方法 selectFile:当前选中的文件 allFiles:还没上传的全部文件
  1464. onDelete : function(file, files){}, // 删除一个文件的回调方法 file:当前删除的文件 files:删除之后的文件
  1465. onSuccess : function(file, response){}, // 文件上传成功的回调方法
  1466. onFailure : function(file, response){}, // 文件上传失败的回调方法
  1467. onComplete : function(response){} // 上传完成的回调方法
  1468. };
  1469. para = $.extend(defaults,options);
  1470. this.init = function(){
  1471. this.createHtml(); // 创建组件html
  1472. this.createCorePlug(); // 调用核心js
  1473. };
  1474. /**
  1475. * 功能:创建上传所使用的html
  1476. * 参数: 无
  1477. * 返回: 无
  1478. */
  1479. this.createHtml = function(){
  1480. var multiple = ""; // 设置多选的参数
  1481. para.multiple ? multiple = "multiple" : multiple = "";
  1482. var html= '';
  1483. if(para.dragDrop){
  1484. // 创建带有拖动的html
  1485. html += '<form id="uploadForm" action="'+para.url+'" method="post" enctype="multipart/form-data">';
  1486. html += ' <div class="upload_box">';
  1487. html += ' <div class="upload_main">';
  1488. html += ' <div class="upload_choose">';
  1489. html += ' <div class="convent_choice">';
  1490. html += ' <div class="andArea">';
  1491. html += ' <div class="filePicker">点击选择文件</div>';
  1492. html += ' <input id="fileImage" type="file" size="30" name="fileselect[]" '+multiple+'>';
  1493. html += ' </div>';
  1494. html += ' </div>';
  1495. html += ' <span id="fileDragArea" class="upload_drag_area">或者将文件拖到此处</span>';
  1496. html += ' </div>';
  1497. html += ' <div class="status_bar">';
  1498. html += ' <div class="btns">';
  1499. html += ' <div class="webuploader_pick" style="display: none">继续选择</div>';
  1500. html += ' <div class="upload_btn" style="display: none">开始上传</div>';
  1501. html += ' </div>';
  1502. html += ' </div>';
  1503. html += ' <div id="preview" class="upload_preview"></div>';
  1504. html += ' </div>';
  1505. html += ' <div class="upload_submit">';
  1506. html += ' <button type="button" id="fileSubmit" class="upload_submit_btn">确认上传文件</button>';
  1507. html += ' </div>';
  1508. html += ' <div id="uploadInf" class="upload_inf"></div>';
  1509. html += ' </div>';
  1510. html += '</form>';
  1511. }else{
  1512. var imgWidth = parseInt(para.itemWidth.replace("px", ""))-15;
  1513. // 创建不带有拖动的html
  1514. html += '<form id="uploadForm" action="'+para.url+'" method="post" enctype="multipart/form-data">';
  1515. html += ' <div class="upload_box">';
  1516. html += ' <div class="upload_main single_main">';
  1517. html += ' <div class="status_bar">';
  1518. html += ' <div class="btns">';
  1519. html += ' <input id="fileImage" type="file" size="30" name="fileselect[]" '+multiple+'>';
  1520. html += ' <div class="webuploader_pick">选择文件</div>';
  1521. html += ' <div class="upload_btn">开始上传</div>';
  1522. html += ' </div>';
  1523. html += ' </div>';
  1524. html += ' <div id="preview" class="upload_preview">';
  1525. html += ' <div class="add_upload">';
  1526. html += ' <a style="height:'+para.itemHeight+';width:'+para.itemWidth+';" title="点击添加文件" id="rapidAddImg" class="add_imgBox" href="javascript:void(0)">';
  1527. html += ' <div class="uploadImg" style="width:'+imgWidth+'px">';
  1528. html += ' <img class="upload_image" src="/resource/img/multiple/add_img.png" style="width:expression(this.width > '+imgWidth+' ? '+imgWidth+'px : this.width)" />';
  1529. html += ' </div>';
  1530. html += ' </a>';
  1531. html += ' </div>';
  1532. html += ' </div>';
  1533. html += ' </div>';
  1534. html += ' <div class="upload_submit">';
  1535. html += ' <button type="button" id="fileSubmit" class="upload_submit_btn">确认上传文件</button>';
  1536. html += ' </div>';
  1537. html += ' <div id="uploadInf" class="upload_inf"></div>';
  1538. html += ' </div>';
  1539. html += '</form>';
  1540. }
  1541. $(self).append(html).css({"width":para.width,"height":para.height});
  1542. var json_imgs = $('.json_imgs').html();
  1543. if(json_imgs != '' && json_imgs.length != 0 && json_imgs)
  1544. {
  1545. var img_html = '';
  1546. var imgWidth = parseInt(para.itemWidth.replace("px", ""))-15;
  1547. var imgHeight = parseInt(para.itemHeight.replace("px", ""))-10;
  1548. var imgs_arr = json_imgs.split(",");
  1549. var banner_heigt = 73;
  1550. var num = imgs_arr.length;
  1551. if(num == 0)
  1552. {
  1553. banner_heigt = 0;
  1554. }else(num / 8 >= 0)
  1555. {
  1556. banner_heigt += parseInt(num/8) * 73;
  1557. }
  1558. $('.upload_preview').css('height', banner_heigt + 'px');
  1559. for(var i=0; i < imgs_arr.length; i++)
  1560. {
  1561. var file_name =imgs_arr[i].substr(imgs_arr[i].lastIndexOf('/')+1);
  1562. img_html += '<div id="up_uploadList_'+ i +'" class="up_upload_append_list">';
  1563. img_html += ' <div class="file_bar">';
  1564. img_html += ' <div style="padding:5px;">';
  1565. img_html += ' <p class="file_name" title="'+ file_name +'">' + file_name + '</p>';
  1566. img_html += ' <span class="up_file_del" data-index="'+ i +'" title="删除"></span>';
  1567. img_html += ' </div>';
  1568. img_html += ' </div>';
  1569. img_html += ' <a style="height:'+para.itemHeight+';width:'+para.itemWidth+';" href="#" onclick="return false" class="imgBox">';
  1570. img_html += ' <div class="uploadImg" style="width:'+imgWidth+'px;max-width:'+imgWidth+'px;max-height:'+imgHeight+'px;">';
  1571. img_html += ' <img id="up_uploadImage_'+ i +'" class="upload_image up_uploadImage_'+ i +'" src="' + imgs_arr[i] + '" style="width:expression(this.width > '+imgWidth+' ? '+imgWidth+'px : this.width);" />';
  1572. img_html += ' </div>';
  1573. img_html += ' </a>';
  1574. img_html += '</div>';
  1575. }
  1576. }
  1577. $('#preview').append(img_html);
  1578. // 初始化html之后绑定按钮的点击事件
  1579. this.addEvent();
  1580. $(".upload_append_list").hover(
  1581. function (e) {
  1582. $(this).find(".file_bar").addClass("file_hover");
  1583. },function (e) {
  1584. $(this).find(".file_bar").removeClass("file_hover");
  1585. }
  1586. );
  1587. $(".up_upload_append_list").hover(
  1588. function (e) {
  1589. $(this).find(".file_bar").addClass("file_hover");
  1590. },function (e) {
  1591. $(this).find(".file_bar").removeClass("file_hover");
  1592. }
  1593. );
  1594. $(".up_upload_append_list").click(function() {
  1595. // 获取选择的文件索引
  1596. var imgIndex = $(this).children().find('.up_file_del').attr("data-index");
  1597. var imgName = $(this).children().find(".file_name").attr("title");
  1598. var imgSrc = $("#up_uploadImage_"+imgIndex).attr("src");
  1599. // 打开弹出层
  1600. self.createPopupPlug(imgSrc, imgIndex, imgName);
  1601. return false;
  1602. });
  1603. };
  1604. /**
  1605. * 功能:显示统计信息和绑定继续上传和上传按钮的点击事件
  1606. * 参数: 无
  1607. * 返回: 无
  1608. */
  1609. this.funSetStatusInfo = function(files){
  1610. var size = 0;
  1611. var num = files.length;
  1612. var banner_heigt = 73;
  1613. $.each(files, function(k,v){
  1614. // 计算得到文件总大小
  1615. size += v.size;
  1616. });
  1617. // 转化为kb和MB格式。文件的名字、大小、类型都是可以现实出来。
  1618. if (size > 1024 * 1024) {
  1619. size = (Math.round(size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
  1620. } else {
  1621. size = (Math.round(size * 100 / 1024) / 100).toString() + 'KB';
  1622. }
  1623. if(num == 0)
  1624. {
  1625. banner_heigt = 0;
  1626. }else(num / 8 >= 0)
  1627. {
  1628. banner_heigt += parseInt(num/8) * 73;
  1629. }
  1630. $('.upload_preview').css('height', banner_heigt);
  1631. // 设置内容
  1632. // $("#status_info").html("选中"+num+"张文件,共"+size+"。");
  1633. };
  1634. /**
  1635. * 功能:过滤上传的文件格式等
  1636. * 参数: files 本次选择的文件
  1637. * 返回: 通过的文件
  1638. */
  1639. this.funFilterEligibleFile = function(files){
  1640. var arrFiles = []; // 替换的文件数组
  1641. for (var i = 0, file; file = files[i]; i++) {
  1642. // 获取上传文件的后缀名
  1643. var newStr = file.name.split("").reverse().join("");
  1644. if(newStr.split(".")[0] != null){
  1645. var type = newStr.split(".")[0].split("").reverse().join("");
  1646. if(jQuery.inArray(type, para.fileType) > -1){
  1647. // 类型符合,可以上传
  1648. if (file.size >= para.fileSize) {
  1649. z.showTip('warning', '图片大小不能超过3M');
  1650. // alert('您这个"'+ file.name +'"文件大小过大');
  1651. } else {
  1652. // 在这里需要判断当前所有文件中
  1653. arrFiles.push(file);
  1654. }
  1655. }else{
  1656. z.showTip('warning', file.name +'"上传类型不符合');
  1657. // alert('您这个"'+ file.name +'"上传类型不符合');
  1658. }
  1659. }else{
  1660. z.showTip('warning', file.name +'"没有类型, 无法识别');
  1661. // alert('您这个"'+ file.name +'"没有类型, 无法识别');
  1662. }
  1663. }
  1664. return arrFiles;
  1665. };
  1666. /**
  1667. * 功能: 处理参数和格式上的预览html
  1668. * 参数: files 本次选择的文件
  1669. * 返回: 预览的html
  1670. */
  1671. this.funDisposePreviewHtml = function(file, e){
  1672. var html = "";
  1673. var imgWidth = parseInt(para.itemWidth.replace("px", ""))-15;
  1674. var imgHeight = parseInt(para.itemHeight.replace("px", ""))-10;
  1675. // 处理配置参数编辑和删除按钮
  1676. var editHtml = "";
  1677. var delHtml = "";
  1678. if(para.tailor){ // 显示裁剪按钮
  1679. editHtml = '<span class="file_edit" data-index="'+file.index+'" title="编辑"></span>';
  1680. }
  1681. if(para.del){ // 显示删除按钮
  1682. delHtml = '<span class="file_del" data-index="'+file.index+'" title="删除"></span>';
  1683. }
  1684. var newStr = file.name.split("").reverse().join("");
  1685. var type = newStr.split(".")[0].split("").reverse().join("");
  1686. // 处理不同类型文件代表的图标
  1687. var fileImgSrc = "zyupload/skins/images/fileType/";
  1688. if(type == "rar"){
  1689. fileImgSrc = fileImgSrc + "rar.png";
  1690. }else if(type == "zip"){
  1691. fileImgSrc = fileImgSrc + "zip.png";
  1692. }else if(type == "txt"){
  1693. fileImgSrc = fileImgSrc + "txt.png";
  1694. }else if(type == "ppt"){
  1695. fileImgSrc = fileImgSrc + "ppt.png";
  1696. }else if(type == "xls"){
  1697. fileImgSrc = fileImgSrc + "xls.png";
  1698. }else if(type == "pdf"){
  1699. fileImgSrc = fileImgSrc + "pdf.png";
  1700. }else if(type == "psd"){
  1701. fileImgSrc = fileImgSrc + "psd.png";
  1702. }else if(type == "ttf"){
  1703. fileImgSrc = fileImgSrc + "ttf.png";
  1704. }else if(type == "swf"){
  1705. fileImgSrc = fileImgSrc + "swf.png";
  1706. }else{
  1707. fileImgSrc = fileImgSrc + "file.png";
  1708. }
  1709. // 图片上传的是图片还是其他类型文件
  1710. // 处理文件修改
  1711. if (file.type.indexOf("image") == 0) {
  1712. html += '<div id="uploadList_'+ file.index +'" class="upload_append_list">';
  1713. html += ' <div class="file_bar">';
  1714. html += ' <div style="padding:5px;">';
  1715. html += ' <p class="file_name" title="'+file.name+'">' + file.name + '</p>';
  1716. html += editHtml; // 编辑按钮的html
  1717. html += delHtml; // 删除按钮的html
  1718. html += ' </div>';
  1719. html += ' </div>';
  1720. html += ' <a style="height:'+para.itemHeight+';width:'+para.itemWidth+';" href="#" onclick="return false" class="imgBox">';
  1721. html += ' <div class="uploadImg" style="width:'+imgWidth+'px;max-width:'+imgWidth+'px;max-height:'+imgHeight+'px;">';
  1722. html += ' <img id="uploadImage_'+file.index+'" class="upload_image" src="' + e.target.result + '" style="width:expression(this.width > '+imgWidth+' ? '+imgWidth+'px : this.width);" />';
  1723. html += ' </div>';
  1724. html += ' </a>';
  1725. html += '</div>';
  1726. }else{
  1727. html += '<div id="uploadList_'+ file.index +'" class="upload_append_list">';
  1728. html += ' <div class="file_bar">';
  1729. html += ' <div style="padding:5px;">';
  1730. html += ' <p class="file_name">' + file.name + '</p>';
  1731. html += delHtml; // 删除按钮的html
  1732. html += ' </div>';
  1733. html += ' </div>';
  1734. html += ' <a style="height:'+para.itemHeight+';width:'+para.itemWidth+';" href="#" class="imgBox">';
  1735. html += ' <div class="uploadImg" style="width:'+imgWidth+'px">';
  1736. html += ' <img id="uploadImage_'+file.index+'" class="upload_file" src="' + fileImgSrc + '" style="width:expression(this.width > '+imgWidth+' ? '+imgWidth+'px : this.width)" />';
  1737. html += ' </div>';
  1738. html += ' </a>';
  1739. html += '</div>';
  1740. }
  1741. return html;
  1742. };
  1743. /**
  1744. * 功能: 创建弹出层插件,会在其中进行裁剪操作
  1745. * 参数: imgSrc 当前裁剪图片的路径
  1746. * 返回: 无
  1747. */
  1748. this.createPopupPlug = function(imgSrc, index, name){
  1749. // 初始化裁剪插件
  1750. $("body").zyPopup({
  1751. src : imgSrc, // 图片的src路径
  1752. index : index, // 图片在预览区域的索引
  1753. name : name, // 图片的名字
  1754. onTailor : function(val, quondamImgInfo){ // 回调返回裁剪的坐标和宽高的值
  1755. // 做裁剪成功的图片预览处理
  1756. var nWidth = parseInt(para.itemWidth.replace("px", ""));
  1757. var nHeight = parseInt(para.itemHeight.replace("px", ""));
  1758. var qWidth = val.width;
  1759. var qHeight = val.height;
  1760. // 计算出选中区域的的比例
  1761. var ratio = nWidth / qWidth;
  1762. // 计算出原图片在预览区域的宽度和高度
  1763. var width = ratio * quondamImgInfo.width;
  1764. var height = ratio * quondamImgInfo.height;
  1765. // 计算出margin-left和margin-top的值
  1766. var left = val.leftX * ratio;
  1767. var top = val.rightY * ratio - qHeight * ratio;
  1768. $("#uploadImage_" + index).css({
  1769. "width" : width,
  1770. "height" : height,
  1771. "margin-left" : -left,
  1772. "margin-top" : -top
  1773. });
  1774. $("#uploadTailor_" + index).show();
  1775. console.info(val);
  1776. var tailor = "{'leftX':"+val.leftX+",'leftY':"+val.leftY+",'rightX':"+val.rightX+",'rightY':"+val.rightY+",'width':"+val.width+",'height':"+val.height+"}";
  1777. // $.getScript("jquery.json-2.4.js", function(){
  1778. // $("#uploadTailor_" + index).attr("tailor",$.toJSON(val));
  1779. // });
  1780. $("#uploadTailor_" + index).attr("tailor",tailor);
  1781. }
  1782. });
  1783. };
  1784. /**
  1785. * 功能:调用核心插件
  1786. * 参数: 无
  1787. * 返回: 无
  1788. */
  1789. this.createCorePlug = function(){
  1790. var params = {
  1791. fileInput: $("#fileImage").get(0),
  1792. uploadInput: $("#fileSubmit").get(0),
  1793. dragDrop: $("#fileDragArea").get(0),
  1794. url: $("#uploadForm").attr("action"),
  1795. filterFile: function(files) {
  1796. // 过滤合格的文件
  1797. return self.funFilterEligibleFile(files);
  1798. },
  1799. onSelect: function(selectFiles, allFiles) {
  1800. para.onSelect(selectFiles, allFiles); // 回调方法
  1801. self.funSetStatusInfo(ZYFILE.funReturnNeedFiles()); // 显示统计信息
  1802. var html = '', i = 0;
  1803. // 组织预览html
  1804. var funDealtPreviewHtml = function() {
  1805. file = selectFiles[i];
  1806. if (file) {
  1807. var reader = new FileReader();
  1808. reader.onload = function(e) {
  1809. // 处理下配置参数和格式的html
  1810. html += self.funDisposePreviewHtml(file, e);
  1811. i++;
  1812. // 再接着调用此方法递归组成可以预览的html
  1813. funDealtPreviewHtml();
  1814. }
  1815. reader.readAsDataURL(file);
  1816. } else {
  1817. // 走到这里说明文件html已经组织完毕,要把html添加到预览区
  1818. funAppendPreviewHtml(html);
  1819. }
  1820. };
  1821. // 添加预览html
  1822. var funAppendPreviewHtml = function(html){
  1823. // 添加到添加按钮前
  1824. if(para.dragDrop){
  1825. $("#preview").append(html);
  1826. }else{
  1827. $(".add_upload").before(html);
  1828. }
  1829. // 绑定删除按钮
  1830. funBindDelEvent();
  1831. funBindHoverEvent();
  1832. };
  1833. // 绑定删除按钮事件
  1834. var funBindDelEvent = function(){
  1835. if($(".file_del").length>0){
  1836. // 删除方法
  1837. $(".file_del").click(function() {
  1838. ZYFILE.funDeleteFile(parseInt($(this).attr("data-index")), true);
  1839. var banner_heigt = 0;
  1840. var cnt = ZYFILE.uploadFile.length; // 图片数量
  1841. if(cnt == 0)
  1842. {
  1843. banner_heigt = 0;
  1844. }else(cnt / 8 >= 0)
  1845. {
  1846. banner_heigt = 73;
  1847. banner_heigt += parseInt(cnt/8) * 73;
  1848. }
  1849. $('.upload_preview').css('height', banner_heigt + 'px');
  1850. return false;
  1851. });
  1852. }
  1853. if($(".file_edit").length>0){
  1854. // if($("head").html().indexOf("zyPopup")<0){ // 代表没有加载过js和css文件
  1855. // // 动态引入裁剪的js和css文件
  1856. // $("<link>").attr({ rel: "stylesheet",
  1857. // type: "text/css",
  1858. // href: "zyPopup/css/zyPopup.css"
  1859. // }).appendTo("head");
  1860. // $.getScript("zyPopup/js/zyPopup.js", function(){
  1861. // // 编辑方法
  1862. // $(".file_edit").click(function() {
  1863. // // 获取选择的文件索引
  1864. // var imgIndex = $(this).attr("data-index");
  1865. // var imgName = $(this).prev(".file_name").attr("title");
  1866. // var imgSrc = $("#uploadImage_"+imgIndex).attr("src");
  1867. // // 打开弹出层
  1868. // self.createPopupPlug(imgSrc, imgIndex, imgName);
  1869. // return false;
  1870. // });
  1871. // });
  1872. // }else{ // 加载过js和css文件
  1873. // 编辑方法
  1874. $(".upload_append_list").click(function() {
  1875. // 获取选择的文件索引
  1876. var imgIndex = $(this).children().find('.file_del').attr("data-index");
  1877. var imgName = $(this).children().find(".file_name").attr("title");
  1878. var imgSrc = $("#uploadImage_"+imgIndex).attr("src");
  1879. // 打开弹出层
  1880. self.createPopupPlug(imgSrc, imgIndex, imgName);
  1881. return false;
  1882. });
  1883. $(".up_upload_append_list").click(function() {
  1884. var imgIndex = $(this).children().find('.up_file_del').attr("data-index");
  1885. var imgName = $(this).children().find(".file_name").attr("title");
  1886. var imgSrc = $("#up_uploadImage_"+imgIndex).attr("src");
  1887. // 打开弹出层
  1888. self.createPopupPlug(imgSrc, imgIndex, imgName);
  1889. return false;
  1890. });
  1891. // }
  1892. }
  1893. };
  1894. // 绑定显示操作栏事件
  1895. var funBindHoverEvent = function(){
  1896. $(".upload_append_list").hover(
  1897. function (e) {
  1898. $(this).find(".file_bar").addClass("file_hover");
  1899. },function (e) {
  1900. $(this).find(".file_bar").removeClass("file_hover");
  1901. }
  1902. );
  1903. };
  1904. funDealtPreviewHtml();
  1905. },
  1906. onDelete: function(file, files) {
  1907. para.onDelete(file, files); // 回调方法
  1908. // 移除效果
  1909. $("#uploadList_" + file.index).fadeOut();
  1910. // 重新设置统计栏信息
  1911. self.funSetStatusInfo(files);
  1912. console.info("剩下的文件");
  1913. console.info(files);
  1914. },
  1915. /*onProgress: function(file, loaded, total) {
  1916. // var eleProgress = $("#uploadProgress_" + file.index), percent = (loaded / total * 100).toFixed(2) + '%';
  1917. if(eleProgress.is(":hidden")){
  1918. eleProgress.show();
  1919. }
  1920. eleProgress.css("width",percent);
  1921. },*/
  1922. onSuccess: function(file, response) {
  1923. para.onSuccess(file, response); // 回调方法
  1924. // $("#uploadProgress_" + file.index).hide();
  1925. $("#uploadSuccess_" + file.index).show();
  1926. //$("#uploadInf").append("<p>上传成功,文件地址是:" + response + "</p>");
  1927. // 根据配置参数确定隐不隐藏上传成功的文件
  1928. if(para.finishDel){
  1929. // 移除效果
  1930. $("#uploadList_" + file.index).fadeOut();
  1931. // 重新设置统计栏信息
  1932. self.funSetStatusInfo(ZYFILE.funReturnNeedFiles());
  1933. }
  1934. if($("#uploadTailor_"+file.index).length>0){
  1935. $("#uploadTailor_" + file.index).hide();
  1936. }
  1937. },
  1938. onFailure: function(file, response) {
  1939. para.onFailure(file, response); // 回调方法
  1940. // $("#uploadProgress_" + file.index).hide();
  1941. $("#uploadSuccess_" + file.index).show();
  1942. if($("#uploadTailor_"+file.index).length>0){
  1943. $("#uploadTailor_" + file.index).hide();
  1944. }
  1945. $("#uploadInf").append("<p>文件" + file.name + "上传失败!</p>");
  1946. //$("#uploadImage_" + file.index).css("opacity", 0.2);
  1947. },
  1948. onComplete: function(response){
  1949. para.onComplete(response); // 回调方法
  1950. console.info(response);
  1951. },
  1952. onDragOver: function() {
  1953. $(this).addClass("upload_drag_hover");
  1954. },
  1955. onDragLeave: function() {
  1956. $(this).removeClass("upload_drag_hover");
  1957. }
  1958. };
  1959. ZYFILE = $.extend(ZYFILE, params);
  1960. ZYFILE.init();
  1961. };
  1962. /**
  1963. * 功能:绑定事件
  1964. * 参数: 无
  1965. * 返回: 无
  1966. */
  1967. this.addEvent = function(){
  1968. $(".up_file_del").bind("click", function(e){
  1969. if($(".up_file_del").length>0){
  1970. // 获取酒店信息及文件名称
  1971. var hotel_id = $('#hotel_id').val();
  1972. // 后台数据删除成功后删除元素节点
  1973. var data_index = $(this).attr('data-index');
  1974. var pid = 'up_uploadList_' + data_index;
  1975. var src = $('.up_uploadImage_' + data_index).attr("src");
  1976. var param = {hotel_id:hotel_id, src: src};
  1977. $.post('/hotel/hotel/del-hotel-img', param, function(data)
  1978. {
  1979. if(data)
  1980. {
  1981. var banner_heigt = 0;
  1982. var cnt = ZYFILE.uploadFile.length; // 图片数量
  1983. if(cnt == 0)
  1984. {
  1985. banner_heigt = 0;
  1986. }else(cnt / 8 >= 0)
  1987. {
  1988. banner_heigt=73;
  1989. banner_heigt += parseInt(cnt/8) * 73;
  1990. }
  1991. $('.upload_preview').css('height', banner_heigt + 'px');
  1992. }
  1993. }, 'json');
  1994. $(this).parents().find('#' + pid).remove();
  1995. }
  1996. });
  1997. // 如果快捷添加文件按钮存在
  1998. if($(".filePicker").length > 0){
  1999. // 绑定选择事件
  2000. $(".filePicker").bind("click", function(e){
  2001. $("#fileImage").click();
  2002. });
  2003. }
  2004. // 绑定继续添加点击事件
  2005. $(".webuploader_pick").bind("click", function(e){
  2006. $("#fileImage").click();
  2007. });
  2008. // 绑定上传点击事件
  2009. $(".upload_btn").bind("click", function(e){
  2010. // 判断当前是否有文件需要上传
  2011. if(ZYFILE.funReturnNeedFiles().length > 0){
  2012. $("#fileSubmit").click();
  2013. }/*else{
  2014. alert("请先选中文件再点击上传");
  2015. }*/
  2016. });
  2017. // 如果快捷添加文件按钮存在
  2018. if($("#rapidAddImg").length > 0){
  2019. // 绑定添加点击事件
  2020. $("#rapidAddImg").bind("click", function(e){
  2021. $("#fileImage").click();
  2022. });
  2023. }
  2024. };
  2025. // 初始化上传控制层插件
  2026. this.init();
  2027. });
  2028. };
  2029. })(jQuery);