bootstrap.js 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. require(['form', 'upload'], function (Form, Upload) {
  2. var _bindevent = Form.events.bindevent;
  3. Form.events.bindevent = function (form) {
  4. _bindevent.apply(this, [form]);
  5. if ($("#croppertpl").length == 0) {
  6. var allowAttr = [
  7. 'aspectRatio', 'autoCropArea', 'cropBoxMovable', 'cropBoxResizable', 'minCropBoxWidth', 'minCropBoxHeight', 'minContainerWidth', 'minContainerHeight',
  8. 'minCanvasHeight', 'minCanvasWidth', 'croppedWidth', 'croppedHeight', 'croppedMinWidth', 'croppedMinHeight', 'croppedMaxWidth', 'croppedMaxHeight', 'fillColor',
  9. 'containerMinHeight', 'containerMaxHeight', 'customWidthHeight', 'customAspectRatio'
  10. ];
  11. String.prototype.toLineCase = function () {
  12. return this.replace(/[A-Z]/g, function (match) {
  13. return "-" + match.toLowerCase();
  14. });
  15. };
  16. var btnAttr = [];
  17. $.each(allowAttr, function (i, j) {
  18. btnAttr.push('data-' + j.toLineCase() + '="<%=data.' + j + '%>"');
  19. });
  20. var btn = '<button class="btn btn-success btn-cropper btn-xs" data-input-id="<%=data.inputId%>" ' + btnAttr.join(" ") + ' style="position:absolute;top:10px;right:15px;">裁剪</button>';
  21. var insertBtn = function () {
  22. return arguments[0].replace(arguments[2], btn + arguments[2]);
  23. };
  24. $("<script type='text/html' id='croppertpl'>" + Upload.config.previewtpl.replace(/<li(.*?)>(.*?)<\/li>/, insertBtn) + "</script>").appendTo("body");
  25. }
  26. $(".plupload[data-preview-id],.faupload[data-preview-id]").each(function () {
  27. var preview_id = $(this).data("preview-id");
  28. var previewObj = $("#" + preview_id);
  29. var tpl = previewObj.length > 0 ? previewObj.data("template") : '';
  30. if (!tpl) {
  31. if (!$(this).hasClass("cropper")) {
  32. $(this).addClass("cropper");
  33. }
  34. previewObj.data("template", "croppertpl");
  35. }
  36. });
  37. //图片裁剪
  38. $(document).off('click', '.btn-cropper').on('click', '.btn-cropper', function () {
  39. var image = $(this).closest("li").find('.thumbnail').data('url');
  40. var input = $("#" + $(this).data("input-id"));
  41. var url = image;
  42. var data = $(this).data();
  43. var params = [];
  44. $.each(allowAttr, function (i, j) {
  45. if (typeof data[j] !== 'undefined' && data[j] !== '') {
  46. params.push(j + '=' + data[j]);
  47. }
  48. });
  49. try {
  50. var parentWin = (parent ? parent : window);
  51. parentWin.Fast.api.open('/addons/cropper/index/cropper?url=' + image + (params.length > 0 ? '&' + params.join('&') : ''), '裁剪', {
  52. callback: function (data) {
  53. if (typeof data !== 'undefined') {
  54. var arr = data.dataURI.split(','), mime = arr[0].match(/:(.*?);/)[1],
  55. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  56. while (n--) {
  57. u8arr[n] = bstr.charCodeAt(n);
  58. }
  59. var urlArr = url.split('.');
  60. var suffix = 'png';
  61. url = urlArr.join('');
  62. var filename = url.substr(url.lastIndexOf('/') + 1);
  63. var exp = new RegExp("\\." + suffix + "$", "i");
  64. filename = exp.test(filename) ? filename : filename + "." + suffix;
  65. var file = new File([u8arr], filename, {type: mime});
  66. Upload.api.send(file, function (data) {
  67. input.val(input.val().replace(image, data.url)).trigger("change");
  68. }, function (data) {
  69. });
  70. }
  71. },
  72. area: [Math.min(parentWin.$(parentWin.window).width(), Config.cropper.dialogWidth) + "px", Math.min(parentWin.$(parentWin.window).height(), Config.cropper.dialogHeight) + "px"],
  73. });
  74. } catch (e) {
  75. console.error(e);
  76. }
  77. return false;
  78. });
  79. }
  80. });