Advertisement
Guest User

Untitled

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