main.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. (function (factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. define(['jquery'], factory);
  4. }
  5. else if (typeof exports === 'object') {
  6. // Node / CommonJS
  7. factory(require('jquery'));
  8. }
  9. else {
  10. factory(jQuery);
  11. }
  12. });
  13. /**
  14. 图片裁剪
  15. 参数:aspectRatio_num 默认裁剪尺寸
  16. **/
  17. function bind_CropAvatar(aspectRatio_num) {
  18. 'use strict';
  19. var console = window.console || { log: function () {} };
  20. var aspectRatio_num = aspectRatio_num;
  21. function CropAvatar($element) {
  22. this.$container = $element;
  23. this.$avatarView = this.$container.find('.avatar-view');
  24. this.$avatar = this.$avatarView.find('img');
  25. this.$avatarModal = this.$container.find('#avatar-modal');
  26. this.$loading = this.$container.find('.loading');
  27. this.$avatarForm = this.$avatarModal.find('.avatar-form');
  28. this.$avatarUpload = this.$avatarForm.find('.avatar-upload');
  29. this.$avatarSrc = this.$avatarForm.find('.avatar-src');
  30. this.$avatarData = this.$avatarForm.find('.avatar-data');
  31. this.$avatarInput = this.$avatarForm.find('.avatar-input');
  32. this.$avatarSave = this.$avatarForm.find('.avatar-save');
  33. this.$avatarBtns = this.$avatarForm.find('.avatar-btns');
  34. this.$avatarWrapper = this.$avatarModal.find('.avatar-wrapper');
  35. this.$avatarPreview = this.$avatarModal.find('.avatar-preview');
  36. this.init();
  37. console.log(this);
  38. }
  39. CropAvatar.prototype = {
  40. constructor: CropAvatar,
  41. support: {
  42. fileList: !!$('<input type="file">').prop('files'),
  43. blobURLs: !!window.URL && URL.createObjectURL,
  44. formData: !!window.FormData
  45. },
  46. init: function () {
  47. this.support.datauri = this.support.fileList && this.support.blobURLs;
  48. if (!this.support.formData) {
  49. this.initIframe();
  50. }
  51. //this.initTooltip();
  52. //this.initModal();
  53. this.addListener();
  54. },
  55. addListener: function () {
  56. this.$avatarView.on('click', $.proxy(this.click, this));
  57. this.$avatarInput.on('change', $.proxy(this.change, this));
  58. this.$avatarForm.on('submit', $.proxy(this.submit, this));
  59. this.$avatarBtns.on('click', $.proxy(this.rotate, this));
  60. },
  61. initPreview: function () {
  62. var url = this.$avatar.attr('src');
  63. this.$avatarPreview.empty().html('<img src="' + url + '">');
  64. },
  65. initIframe: function () {
  66. var target = 'upload-iframe-' + (new Date()).getTime(),
  67. $iframe = $('<iframe>').attr({
  68. name: target,
  69. src: ''
  70. }),
  71. _this = this;
  72. // Ready ifrmae
  73. $iframe.one('load', function () {
  74. // respond response
  75. $iframe.on('load', function () {
  76. var data;
  77. try {
  78. data = $(this).contents().find('body').text();
  79. }
  80. catch (e) {
  81. console.log(e.message);
  82. }
  83. if (data) {
  84. try {
  85. data = $.parseJSON(data);
  86. }
  87. catch (e) {
  88. console.log(e.message);
  89. }
  90. _this.submitDone(data);
  91. }
  92. else {
  93. _this.submitFail('Image upload failed!');
  94. }
  95. _this.submitEnd();
  96. });
  97. });
  98. this.$iframe = $iframe;
  99. this.$avatarForm.attr('target', target).after($iframe.hide());
  100. },
  101. click: function () {
  102. this.$avatarModal.modal('show');
  103. this.initPreview();
  104. },
  105. change: function () {
  106. var files,file;
  107. if (this.support.datauri) {
  108. files = this.$avatarInput.prop('files');
  109. if (files.length > 0) {
  110. file = files[0];
  111. if (this.isImageFile(file)) {
  112. if (this.url) {
  113. URL.revokeObjectURL(this.url); // Revoke the old one
  114. }
  115. this.url = URL.createObjectURL(file);
  116. this.startCropper();
  117. }
  118. }
  119. }
  120. else {
  121. file = this.$avatarInput.val();
  122. if (this.isImageFile(file)) {
  123. this.syncUpload();
  124. }
  125. }
  126. },
  127. submit: function () {
  128. if (!this.$avatarSrc.val() && !this.$avatarInput.val()) {
  129. return false;
  130. }
  131. if (this.support.formData) {
  132. this.ajaxUpload();
  133. return false;
  134. }
  135. },
  136. rotate: function (e) {
  137. var data;
  138. if (this.active) {
  139. data = $(e.target).data();
  140. if (data.method) {
  141. this.$img.cropper(data.method, data.option);
  142. }
  143. }
  144. },
  145. isImageFile: function (file) {
  146. if (file.type) {
  147. return /^image\/\w+$/.test(file.type);
  148. }
  149. else {
  150. return /\.(jpg|jpeg|png|gif)$/.test(file);
  151. }
  152. },
  153. // 开始裁剪
  154. startCropper: function () {
  155. var _this = this;
  156. if (this.active) {
  157. this.$img.cropper('replace', this.url);
  158. }
  159. else {
  160. this.$img = $('<img src="' + this.url + '">');
  161. this.$avatarWrapper.empty().html(this.$img);
  162. this.$img.cropper({
  163. aspectRatio: aspectRatio_num,
  164. preview: this.$avatarPreview.selector,
  165. strict: false,
  166. autoCropArea:1,
  167. // 当改变剪裁容器或图片时的事件函数
  168. crop: function (data) {
  169. var json = [
  170. '{"x":' + data.x,
  171. '"y":' + data.y,
  172. '"height":' + data.height,
  173. '"width":' + data.width,
  174. '"rotate":' + data.rotate + '}'
  175. ].join();
  176. _this.$avatarData.val(json);
  177. }
  178. });
  179. this.active = true;
  180. }
  181. },
  182. // 结束裁剪
  183. stopCropper: function () {
  184. if (this.active) {
  185. this.$img.cropper('destroy');
  186. this.$img.remove();
  187. this.active = false;
  188. }
  189. },
  190. // 上传图片
  191. ajaxUpload: function () {
  192. var url = this.$avatarForm.attr('action'),
  193. data = new FormData(this.$avatarForm[0]),
  194. _this = this;
  195. $.ajax(url, {
  196. type: 'post',
  197. data: data,
  198. dataType: 'json',
  199. processData: false,
  200. contentType: false,
  201. beforeSend: function () {
  202. _this.submitStart();
  203. },
  204. success: function (data) {
  205. _this.submitDone(data);
  206. },
  207. error: function (XMLHttpRequest, textStatus, errorThrown) {
  208. _this.submitFail(textStatus || errorThrown);
  209. },
  210. complete: function () {
  211. _this.submitEnd();
  212. }
  213. });
  214. },
  215. syncUpload: function () {
  216. this.$avatarSave.click();
  217. },
  218. submitStart: function () {
  219. this.$loading.show();
  220. },
  221. submitDone: function (data) {
  222. console.log(data);
  223. if ($.isPlainObject(data) && data.state === 200) {
  224. if (data.result) {
  225. this.url = data.result;
  226. if (this.support.datauri || this.uploaded) {
  227. this.uploaded = false;
  228. this.cropDone();
  229. }
  230. else {
  231. this.uploaded = true;
  232. this.$avatarSrc.val(this.url);
  233. this.startCropper();
  234. }
  235. this.$avatarInput.val('');
  236. }
  237. else if (data.message) {
  238. this.alert(data.message);
  239. }
  240. }
  241. else {
  242. this.alert('Failed to response');
  243. }
  244. },
  245. submitFail: function (msg) {
  246. this.alert(msg);
  247. },
  248. submitEnd: function () {
  249. this.$loading.hide();
  250. },
  251. cropDone: function () {
  252. this.$avatarForm.get(0).reset();
  253. this.$avatar.attr('src', this.url);
  254. this.stopCropper();
  255. this.$avatarModal.modal('hide');
  256. },
  257. alert: function (msg) {
  258. var $alert = [
  259. '<div class="alert alert-danger avater-alert">',
  260. '<button type="button" class="close" data-dismiss="alert">&times;</button>',
  261. msg,
  262. '</div>'
  263. ].join('');
  264. this.$avatarUpload.after($alert);
  265. }
  266. };
  267. $(function () {
  268. return new CropAvatar($('#crop-avatar'));
  269. });
  270. };