image-cropping.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define("/forms/image-cropping", ["jquery", "Site"], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(require("jquery"), require("Site"));
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(global.jQuery, global.Site);
  11. global.formsImageCropping = mod.exports;
  12. }
  13. })(this, function (_jquery, _Site) {
  14. "use strict";
  15. _jquery = babelHelpers.interopRequireDefault(_jquery);
  16. (0, _jquery.default)(document).ready(function ($$$1) {
  17. (0, _Site.run)();
  18. }); // Example Cropper Simple
  19. // ----------------------
  20. (function () {
  21. (0, _jquery.default)("#simpleCropper img").cropper({
  22. preview: "#simpleCropperPreview >.img-preview",
  23. responsive: true
  24. });
  25. })(); // Example Cropper Full
  26. // --------------------
  27. (function () {
  28. var $exampleFullCropper = (0, _jquery.default)("#exampleFullCropper img"),
  29. $inputDataX = (0, _jquery.default)("#inputDataX"),
  30. $inputDataY = (0, _jquery.default)("#inputDataY"),
  31. $inputDataHeight = (0, _jquery.default)("#inputDataHeight"),
  32. $inputDataWidth = (0, _jquery.default)("#inputDataWidth");
  33. var options = {
  34. aspectRatio: 16 / 9,
  35. preview: "#exampleFullCropperPreview > .img-preview",
  36. responsive: true,
  37. crop: function crop() {
  38. var data = (0, _jquery.default)(this).data('cropper').getCropBoxData();
  39. $inputDataX.val(Math.round(data.left));
  40. $inputDataY.val(Math.round(data.top));
  41. $inputDataHeight.val(Math.round(data.height));
  42. $inputDataWidth.val(Math.round(data.width));
  43. }
  44. }; // set up cropper
  45. $exampleFullCropper.cropper(options); // set up method buttons
  46. (0, _jquery.default)(document).on("click", "[data-cropper-method]", function () {
  47. var data = (0, _jquery.default)(this).data(),
  48. method = (0, _jquery.default)(this).data('cropper-method'),
  49. result;
  50. if (method) {
  51. result = $exampleFullCropper.cropper(method, data.option);
  52. }
  53. if (method === 'getCroppedCanvas') {
  54. (0, _jquery.default)('#getDataURLModal').modal().find('.modal-body').html(result);
  55. }
  56. }); // deal wtih uploading
  57. var $inputImage = (0, _jquery.default)("#inputImage");
  58. if (window.FileReader) {
  59. $inputImage.change(function () {
  60. var fileReader = new FileReader(),
  61. files = this.files,
  62. file;
  63. if (!files.length) {
  64. return;
  65. }
  66. file = files[0];
  67. if (/^image\/\w+$/.test(file.type)) {
  68. fileReader.readAsDataURL(file);
  69. fileReader.onload = function () {
  70. $exampleFullCropper.cropper("reset", true).cropper("replace", this.result);
  71. $inputImage.val("");
  72. };
  73. } else {
  74. showMessage("Please choose an image file.");
  75. }
  76. });
  77. } else {
  78. $inputImage.addClass("hide");
  79. } // set data
  80. (0, _jquery.default)("#setCropperData").click(function () {
  81. var data = {
  82. left: parseInt($inputDataX.val()),
  83. top: parseInt($inputDataY.val()),
  84. width: parseInt($inputDataWidth.val()),
  85. height: parseInt($inputDataHeight.val())
  86. };
  87. $exampleFullCropper.cropper("setCropBoxData", data);
  88. });
  89. })();
  90. });