main.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. $(function () {
  2. 'use strict';
  3. var console = window.console || { log: function () {} },
  4. $alert = $('.docs-alert'),
  5. $message = $alert.find('.message'),
  6. showMessage = function (message, type) {
  7. $message.text(message);
  8. if (type) {
  9. $message.addClass(type);
  10. }
  11. $alert.fadeIn();
  12. setTimeout(function () {
  13. $alert.fadeOut();
  14. }, 3000);
  15. };
  16. // Demo
  17. // -------------------------------------------------------------------------
  18. (function () {
  19. var $image = $('.img-container > img'),
  20. $dataX = $('#dataX'),
  21. $dataY = $('#dataY'),
  22. $dataHeight = $('#dataHeight'),
  23. $dataWidth = $('#dataWidth'),
  24. $dataRotate = $('#dataRotate'),
  25. options = {
  26. // strict: false,
  27. // responsive: false,
  28. // checkImageOrigin: false
  29. // modal: false,
  30. // guides: false,
  31. // highlight: false,
  32. // background: false,
  33. // autoCrop: false,
  34. // autoCropArea: 0.5,
  35. // dragCrop: false,
  36. // movable: false,
  37. // resizable: false,
  38. // rotatable: false,
  39. // zoomable: false,
  40. // touchDragZoom: false,
  41. // mouseWheelZoom: false,
  42. // minCanvasWidth: 320,
  43. // minCanvasHeight: 180,
  44. // minCropBoxWidth: 160,
  45. // minCropBoxHeight: 90,
  46. // minContainerWidth: 320,
  47. // minContainerHeight: 180,
  48. // build: null,
  49. // built: null,
  50. // dragstart: null,
  51. // dragmove: null,
  52. // dragend: null,
  53. // zoomin: null,
  54. // zoomout: null,
  55. aspectRatio: 16 / 9,
  56. preview: '.img-preview',
  57. crop: function (data) {
  58. $dataX.val(Math.round(data.x));
  59. $dataY.val(Math.round(data.y));
  60. $dataHeight.val(Math.round(data.height));
  61. $dataWidth.val(Math.round(data.width));
  62. $dataRotate.val(Math.round(data.rotate));
  63. }
  64. };
  65. $image.on({
  66. 'build.cropper': function (e) {
  67. console.log(e.type);
  68. },
  69. 'built.cropper': function (e) {
  70. console.log(e.type);
  71. },
  72. 'dragstart.cropper': function (e) {
  73. console.log(e.type, e.dragType);
  74. },
  75. 'dragmove.cropper': function (e) {
  76. console.log(e.type, e.dragType);
  77. },
  78. 'dragend.cropper': function (e) {
  79. console.log(e.type, e.dragType);
  80. },
  81. 'zoomin.cropper': function (e) {
  82. console.log(e.type);
  83. },
  84. 'zoomout.cropper': function (e) {
  85. console.log(e.type);
  86. }
  87. }).cropper(options);
  88. // Methods
  89. $(document.body).on('click', '[data-method]', function () {
  90. var data = $(this).data(),
  91. $target,
  92. result;
  93. if (data.method) {
  94. data = $.extend({}, data); // Clone a new one
  95. if (typeof data.target !== 'undefined') {
  96. $target = $(data.target);
  97. if (typeof data.option === 'undefined') {
  98. try {
  99. data.option = JSON.parse($target.val());
  100. } catch (e) {
  101. console.log(e.message);
  102. }
  103. }
  104. }
  105. result = $image.cropper(data.method, data.option);
  106. if (data.method === 'getCroppedCanvas') {
  107. $('#getCroppedCanvasModal').modal().find('.modal-body').html(result);
  108. }
  109. if ($.isPlainObject(result) && $target) {
  110. try {
  111. $target.val(JSON.stringify(result));
  112. } catch (e) {
  113. console.log(e.message);
  114. }
  115. }
  116. }
  117. }).on('keydown', function (e) {
  118. switch (e.which) {
  119. case 37:
  120. e.preventDefault();
  121. $image.cropper('move', -1, 0);
  122. break;
  123. case 38:
  124. e.preventDefault();
  125. $image.cropper('move', 0, -1);
  126. break;
  127. case 39:
  128. e.preventDefault();
  129. $image.cropper('move', 1, 0);
  130. break;
  131. case 40:
  132. e.preventDefault();
  133. $image.cropper('move', 0, 1);
  134. break;
  135. }
  136. });
  137. // Import image
  138. var $inputImage = $('#inputImage'),
  139. URL = window.URL || window.webkitURL,
  140. blobURL;
  141. if (URL) {
  142. $inputImage.change(function () {
  143. var files = this.files,
  144. file;
  145. if (files && files.length) {
  146. file = files[0];
  147. if (/^image\/\w+$/.test(file.type)) {
  148. blobURL = URL.createObjectURL(file);
  149. $image.one('built.cropper', function () {
  150. URL.revokeObjectURL(blobURL); // Revoke when load complete
  151. }).cropper('reset', true).cropper('replace', blobURL);
  152. $inputImage.val('');
  153. } else {
  154. showMessage('Please choose an image file.');
  155. }
  156. }
  157. });
  158. } else {
  159. $inputImage.parent().remove();
  160. }
  161. // Options
  162. $('.docs-options :checkbox').on('change', function () {
  163. var $this = $(this);
  164. options[$this.val()] = $this.prop('checked');
  165. $image.cropper('destroy').cropper(options);
  166. });
  167. // Tooltips
  168. $('[data-toggle="tooltip"]').tooltip();
  169. }());
  170. });