Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. declare var Cropper: any;
  2.  
  3. class PhotoCropper {
  4.  
  5.  
  6. prepareCropper(): void {
  7. $("#preview-process").hide();
  8. $("#response-alert").hide();
  9. $("#response-alert .text-internal_error").hide();
  10.  
  11. $("#thumbForm").submit(function (e) {
  12. let form = $(this);
  13. let targetUrl = form.attr("action");
  14. console.log(targetUrl);
  15. console.log(form.serialize());
  16.  
  17. if (PhotoCropper.validateCropData()) {
  18. console.log("validated! Send request");
  19. $.ajax({
  20. type: "POST",
  21. url: targetUrl,
  22. data: form.serialize(),
  23. beforeSend: function () {
  24. $("#preview-process").show();
  25. },
  26. success: function (e) {
  27. $("#preview-process").hide();
  28. console.log(e);
  29. if (e.success) {
  30. window.location = e.redirectUrl;
  31. }
  32. else if (e.isInternalError) {
  33. $("#response-alert").show();
  34. $("#response-alert .text-internal_error").show();
  35. }
  36. }
  37. });
  38. }
  39. else {
  40. $("#response-alert").show();
  41. $("#response-alert .text-empty_area").show();
  42. }
  43.  
  44. e.preventDefault();
  45. });
  46. }
  47.  
  48. static validateCropData(): boolean {
  49. if ($("#CropX").val() == 0 && $("#CropY").val() == 0) {
  50. return false;
  51. }
  52. return true;
  53. }
  54.  
  55. initialCropImage(img: any): void {
  56. var console = window.console || { log: function () { } };
  57. var URL = window.URL || window.webkitURL;
  58.  
  59. var $download = $('#download');
  60. var $dataX = $('#CropX');
  61. var $dataY = $('#CropY');
  62. var $dataHeight = $('#CropHeight');
  63. var $dataWidth = $('#CropWidth');
  64. var $dataRotate = $('#dataRotate');
  65. var $dataScaleX = $('#dataScaleX');
  66. var $dataScaleY = $('#dataScaleY');
  67. var $initX = $("#initX");
  68. var $initY = $("#initY");
  69. var $initW = $("#initW");
  70. var $initH = $("#initH");
  71. var options = {
  72. aspectRatio: 1,
  73. preview: '.preview-crop',
  74. viewMode: 0,
  75. ready: function (e) {
  76.  
  77. let initialData = {
  78. x: Math.round($initX.val()),
  79. y: Math.round($initY.val()),
  80. width: Math.round($initW.val()),
  81. height: Math.round($initH.val()),
  82. };
  83.  
  84. if (initialData.x > 0 && initialData.y > 0) {
  85. console.log(initialData);
  86. img.cropper('setData', initialData);
  87. }
  88. },
  89. crop: function (e) {
  90. $dataX.val(Math.round(e.detail.x));
  91. $dataY.val(Math.round(e.detail.y));
  92. $dataHeight.val(Math.round(e.detail.height));
  93. $dataWidth.val(Math.round(e.detail.width));
  94. }
  95. };
  96.  
  97. var height = img.height() + 4;
  98.  
  99. $('.preview-crop').css({
  100. width: '100%', //width, sets the starting size to the same as orig image
  101. overflow: 'hidden',
  102. height: height,
  103. maxWidth: img.width(),
  104. maxHeight: height
  105. });
  106.  
  107. img.cropper(options);
  108. }
  109.  
  110. cropImage(): void {
  111. Cropper.noConflict();
  112. this.initialCropImage($("#target"));
  113. }
  114. }
  115.  
  116. $(() => {
  117. 'use strict';
  118. var photoCropper = new PhotoCropper();
  119. photoCropper.prepareCropper();
  120. photoCropper.cropImage();
  121. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement