main.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 = $('body').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. strict: false,
  162. crop: function (data) {
  163. var json = [
  164. '{"x":' + data.x,
  165. '"y":' + data.y,
  166. '"height":' + data.height,
  167. '"width":' + data.width,
  168. '"rotate":' + data.rotate + '}'
  169. ].join();
  170. _this.$avatarData.val(json);
  171. }
  172. });
  173. this.active = true;
  174. }
  175. },
  176. stopCropper: function () {
  177. if (this.active) {
  178. this.$img.cropper('destroy');
  179. this.$img.remove();
  180. this.active = false;
  181. }
  182. },
  183. ajaxUpload: function () {
  184. var url = this.$avatarForm.attr('action'),
  185. data = new FormData(this.$avatarForm[0]),
  186. _this = this;
  187. $.ajax(url, {
  188. type: 'post',
  189. data: data,
  190. dataType: 'json',
  191. processData: false,
  192. contentType: false,
  193. beforeSend: function () {
  194. _this.submitStart();
  195. },
  196. success: function (data) {
  197. _this.submitDone(data);
  198. },
  199. error: function (XMLHttpRequest, textStatus, errorThrown) {
  200. _this.submitFail(textStatus || errorThrown);
  201. },
  202. complete: function () {
  203. _this.submitEnd();
  204. }
  205. });
  206. },
  207. syncUpload: function () {
  208. this.$avatarSave.click();
  209. },
  210. submitStart: function () {
  211. this.$loading.fadeIn();
  212. },
  213. submitDone: function (data) {
  214. // console.log(data);
  215. if ($.isPlainObject(data) && data.code) {
  216. if (data.code) {
  217. this.url = data.data.url;
  218. $('#thumb').val(data.data.thumb);
  219. if (this.support.datauri || this.uploaded) {
  220. this.uploaded = false;
  221. this.cropDone();
  222. } else {
  223. this.uploaded = true;
  224. this.$avatarSrc.val(this.url);
  225. this.startCropper();
  226. }
  227. this.$avatarInput.val('');
  228. } else if (data.msg) {
  229. this.alert(data.msg);
  230. }
  231. } else {
  232. this.alert('Failed to response');
  233. }
  234. },
  235. submitFail: function (msg) {
  236. this.alert(msg);
  237. },
  238. submitEnd: function () {
  239. this.$loading.fadeOut();
  240. },
  241. cropDone: function () {
  242. this.$avatarForm.get(0).reset();
  243. this.$avatar.attr('src', this.url);
  244. this.stopCropper();
  245. this.$avatarModal.modal('hide');
  246. },
  247. alert: function (msg) {
  248. var $alert = [
  249. '<div class="alert alert-danger avater-alert">',
  250. '<button type="button" class="close" data-dismiss="alert">&times;</button>',
  251. msg,
  252. '</div>'
  253. ].join('');
  254. this.$avatarUpload.after($alert);
  255. }
  256. };
  257. $(function () {
  258. return new CropAvatar($('#crop-avatar'));
  259. });
  260. });