main2.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. aspectRatio: 16 / 9,
  27. preview: '.img-preview',
  28. crop: function (data) {
  29. $dataX.val(Math.round(data.x));
  30. $dataY.val(Math.round(data.y));
  31. $dataHeight.val(Math.round(data.height));
  32. $dataWidth.val(Math.round(data.width));
  33. $dataRotate.val(Math.round(data.rotate));
  34. }
  35. };
  36. $image.on({
  37. 'build.cropper': function (e) {
  38. console.log(e.type);
  39. },
  40. 'built.cropper': function (e) {
  41. console.log(e.type);
  42. }
  43. }).cropper(options);
  44. // Methods
  45. $(document.body).on('click', '[data-method]', function () {
  46. var data = $(this).data(),
  47. $target,
  48. result;
  49. if (data.method) {
  50. data = $.extend({}, data); // Clone a new one
  51. if (typeof data.target !== 'undefined') {
  52. $target = $(data.target);
  53. if (typeof data.option === 'undefined') {
  54. try {
  55. data.option = JSON.parse($target.val());
  56. } catch (e) {
  57. console.log(e.message);
  58. }
  59. }
  60. }
  61. result = $image.cropper(data.method, data.option);
  62. if (data.method === 'getDataURL') {
  63. $('#getDataURLModal').modal().find('.modal-body').html('<img src="' + result + '">');
  64. }
  65. if ($.isPlainObject(result) && $target) {
  66. try {
  67. $target.val(JSON.stringify(result));
  68. } catch (e) {
  69. console.log(e.message);
  70. }
  71. }
  72. }
  73. }).on('keydown', function (e) {
  74. switch (e.which) {
  75. case 37:
  76. e.preventDefault();
  77. $image.cropper('move', -1, 0);
  78. break;
  79. case 38:
  80. e.preventDefault();
  81. $image.cropper('move', 0, -1);
  82. break;
  83. case 39:
  84. e.preventDefault();
  85. $image.cropper('move', 1, 0);
  86. break;
  87. case 40:
  88. e.preventDefault();
  89. $image.cropper('move', 0, 1);
  90. break;
  91. }
  92. });
  93. // Import image
  94. var $inputImage = $('#inputImage'),
  95. URL = window.URL || window.webkitURL,
  96. blobURL;
  97. if (URL) {
  98. $inputImage.change(function () {
  99. var files = this.files,
  100. file;
  101. if (files && files.length) {
  102. file = files[0];
  103. if (/^image\/\w+$/.test(file.type)) {
  104. blobURL = URL.createObjectURL(file);
  105. $image.one('built.cropper', function () {
  106. URL.revokeObjectURL(blobURL); // Revoke when load complete
  107. }).cropper('reset', true).cropper('replace', blobURL);
  108. $inputImage.val('');
  109. } else {
  110. showMessage('Please choose an image file.');
  111. }
  112. }
  113. });
  114. } else {
  115. $inputImage.parent().remove();
  116. }
  117. // Options
  118. $('.docs-options :checkbox').on('change', function () {
  119. var $this = $(this);
  120. options[$this.val()] = $this.prop('checked');
  121. $image.cropper('destroy').cropper(options);
  122. });
  123. // Tooltips
  124. $('[data-toggle="tooltip"]').tooltip();
  125. }());
  126. });