attachment-update.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. $(document).ready(function() {
  2. var is_init = false;
  3. var jcrop_api;
  4. function getRandom() {
  5. var dim = [jcrop_api.ui.stage.width,jcrop_api.ui.stage.height];
  6. return [
  7. Math.round(Math.random() * dim[0]),
  8. Math.round(Math.random() * dim[1]),
  9. Math.round(Math.random() * dim[0]),
  10. Math.round(Math.random() * dim[1])
  11. ];
  12. };
  13. $('#setSelectButton').click(function(event) {
  14. event.preventDefault();
  15. event.stopImmediatePropagation();
  16. $('#cropButton').removeClass("hide");
  17. $('#releaseButton').removeClass("hide");
  18. $('#setSelectButton').addClass("hide");
  19. $('.original-image').Jcrop({
  20. boxWidth: "800",
  21. onChange: function (c) {
  22. $('#crop-x').val(c.x);
  23. $('#crop-y').val(c.y);
  24. $('#crop-w').val(c.w);
  25. $('#crop-h').val(c.h);
  26. }
  27. }, function() {
  28. jcrop_api = this;
  29. var thumbnail = this.initComponent('Thumbnailer', {
  30. width: 130,
  31. height: 130
  32. });
  33. this.ui.preview = thumbnail;
  34. jcrop_api.newSelection();
  35. var bound = getRandom();
  36. jcrop_api.setSelect(bound);
  37. $('#crop-x').val(bound[0]);
  38. $('#crop-y').val(bound[1]);
  39. $('#crop-w').val(bound[2]);
  40. $('#crop-h').val(bound[3]);
  41. if (is_init == false) {
  42. is_init = true;
  43. jcrop_api.container.on('cropmove cropend', function(e, s, c) {
  44. $('#crop-x').val(c.x);
  45. $('#crop-y').val(c.y);
  46. $('#crop-w').val(c.w);
  47. $('#crop-h').val(c.h);
  48. });
  49. $('#text-inputs').on('change', 'input', function(e) {
  50. jcrop_api.animateTo([
  51. parseInt($('#crop-x').val()),
  52. parseInt($('#crop-y').val()),
  53. parseInt($('#crop-w').val()),
  54. parseInt($('#crop-h').val())
  55. ]);
  56. });
  57. }
  58. });
  59. return false;
  60. });
  61. $('#releaseButton').click(function(event) {
  62. event.preventDefault();
  63. event.stopImmediatePropagation();
  64. $('#cropButton').addClass("hide");
  65. $('#releaseButton').addClass("hide");
  66. $('#setSelectButton').removeClass("hide");
  67. jcrop_api.destroy();
  68. $(".original-image").attr("style", "");
  69. return false;
  70. });
  71. $('#cropButton').click(function(event) {
  72. event.preventDefault();
  73. event.stopImmediatePropagation();
  74. $.ajax({
  75. type: "POST",
  76. url: cropUrl,
  77. data: {
  78. x: $('#crop-x').val(),
  79. y: $('#crop-y').val(),
  80. w: $('#crop-w').val(),
  81. h: $('#crop-h').val(),
  82. type: $("input[name='crop-apply-type']:checked").val()
  83. },
  84. success: function(msg) {
  85. if (msg.status == true) {
  86. location.reload();
  87. } else {
  88. console.log(msg);
  89. //alert(msg.error);
  90. }
  91. }
  92. });
  93. return false;
  94. });
  95. });