main.js 7.6KB

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