sitelogo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. (function (factory) {
  2. if (typeof window.define === 'function' && window.define.amd) {
  3. window.define(['jquery'], factory);
  4. } else if (typeof window.exports === 'object') {
  5. // Node / CommonJS
  6. factory(window.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 = $("body").find('#avatar-modal');
  18. //this.$loading = $("#page-wrapper").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.$avatarPreviewClass = "#avatar-modal .avatar-preview";
  29. this.init();
  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. this.$avatarSave.on('click', $.proxy(this.ajaxUpload, this));
  53. },
  54. initTooltip: function () {
  55. this.$avatarView.tooltip({
  56. placement: 'bottom'
  57. });
  58. },
  59. initModal: function () {
  60. this.$avatarModal.modal({ show: false });
  61. },
  62. initPreview: function () {
  63. var url = this.$avatar.attr('src');
  64. this.$avatarPreview.empty().html('<img src="' + url + '">');
  65. },
  66. initIframe: function () {
  67. var target = 'upload-iframe-' + (new Date()).getTime(),
  68. $iframe = $('<iframe>').attr({
  69. name: target,
  70. src: ''
  71. }),
  72. $this = this;
  73. // Ready ifrmae
  74. $iframe.one('load', function () {
  75. // respond response
  76. $iframe.on('load', function () {
  77. var data;
  78. var e;
  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('图片解析失败!');
  93. }
  94. $this.submitEnd();
  95. });
  96. });
  97. this.$iframe = $iframe;
  98. this.$avatarForm.attr('target', target).after($iframe.hide());
  99. },
  100. click: function () {
  101. if ($.fn.iwbModal) {
  102. this.$avatarModal.iwbModal('show2');
  103. } else {
  104. this.$avatarModal.modal('show');
  105. }
  106. this.initPreview();
  107. },
  108. change: function () {
  109. var files,
  110. file;
  111. if (this.support.datauri) {
  112. files = this.$avatarInput.prop('files');
  113. if (files.length > 0) {
  114. file = files[0];
  115. if (this.isImageFile(file)) {
  116. if (this.url) {
  117. URL.revokeObjectURL(this.url); // Revoke the old one
  118. }
  119. this.url = URL.createObjectURL(file);
  120. this.startCropper();
  121. }
  122. }
  123. } else {
  124. file = this.$avatarInput.val();
  125. if (this.isImageFile(file)) {
  126. this.syncUpload();
  127. }
  128. }
  129. },
  130. submit: function () {
  131. if (!this.$avatarSrc.val() && !this.$avatarInput.val()) {
  132. return false;
  133. }
  134. if (this.support.formData) {
  135. this.ajaxUpload();
  136. return false;
  137. }
  138. return true;
  139. },
  140. rotate: function (e) {
  141. var data;
  142. if (this.active) {
  143. data = $(e.target).data();
  144. if (data.method) {
  145. this.$img.cropper(data.method, data.option);
  146. }
  147. }
  148. },
  149. isImageFile: function (file) {
  150. if (file.type) {
  151. return /^image\/\w+$/.test(file.type);
  152. } else {
  153. return /\.(jpg|jpeg|png|gif)$/.test(file);
  154. }
  155. },
  156. startCropper: function () {
  157. var $this = this;
  158. if (this.active) {
  159. this.$img.cropper('replace', this.url);
  160. } else {
  161. this.$img = $('<img src="' + this.url + '">');
  162. this.$avatarWrapper.empty().html(this.$img);
  163. this.$img.cropper({
  164. aspectRatio: 1,
  165. preview: this.$avatarPreviewClass,
  166. strict: false,
  167. crop: function (data) {
  168. var json = [
  169. '{"x":' + data.x,
  170. '"y":' + data.y,
  171. '"height":' + data.height,
  172. '"width":' + data.width,
  173. '"rotate":' + data.rotate + '}'
  174. ].join();
  175. $this.$avatarData.val(json);
  176. }
  177. });
  178. this.active = true;
  179. }
  180. },
  181. stopCropper: function () {
  182. if (this.active) {
  183. this.$img.cropper('destroy');
  184. this.$img.remove();
  185. this.active = false;
  186. }
  187. },
  188. //ajaxUpload: function () {
  189. // var url = this.$avatarForm.attr('action'),
  190. // data = new FormData(this.$avatarForm[0]),
  191. // $this = this;
  192. // $.ajax(url, {
  193. // headers: {'X-XSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
  194. // type: 'post',
  195. // data: data,
  196. // dataType: 'json',
  197. // processData: false,
  198. // contentType: false,
  199. // //beforeSend: function () {
  200. // // $this.submitStart();
  201. // //},
  202. // success: function (data) {
  203. // $this.submitDone(data);
  204. // },
  205. // error: function (xmlHttpRequest, textStatus, errorThrown) {
  206. // $this.submitFail(textStatus || errorThrown);
  207. // }
  208. // //complete: function () {
  209. // // $this.submitEnd();
  210. // //}
  211. // });
  212. //},
  213. ajaxUpload: function () {
  214. var url = this.$avatarForm.attr('action'),
  215. // data = new FormData(this.$avatarForm[0]),
  216. $this = this;
  217. abp.ui.setBusy();
  218. var fileImg = this.$img.cropper("getCroppedCanvas").toDataURL('image/jpg');
  219. console.log(fileImg);
  220. abp.ajax({
  221. url: url,
  222. type: 'POST',
  223. dataType: "json",
  224. contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
  225. data: { Avatar: fileImg },
  226. success: function (data) {
  227. $this.submitDone(data);
  228. },
  229. error: function () {
  230. abp.ui.clearBusy();
  231. }
  232. });
  233. },
  234. syncUpload: function () {
  235. this.$avatarSave.click();
  236. },
  237. submitStart: function () {
  238. this.$loading.fadeIn();
  239. },
  240. submitDone: function (data) {
  241. abp.ui.clearBusy();
  242. console.log(data);
  243. if (data) {
  244. this.url = data;
  245. if (this.support.datauri || this.uploaded) {
  246. this.uploaded = false;
  247. this.cropDone();
  248. } else {
  249. this.uploaded = true;
  250. this.$avatarSrc.val(this.url);
  251. this.startCropper();
  252. }
  253. this.$avatarInput.val('');
  254. }
  255. // if ($.isPlainObject(data)) {
  256. // if (data.result) {
  257. // this.url = data.result;
  258. // if (this.support.datauri || this.uploaded) {
  259. // this.uploaded = false;
  260. // this.cropDone();
  261. // } else {
  262. // this.uploaded = true;
  263. // this.$avatarSrc.val(this.url);
  264. // this.startCropper();
  265. // }
  266. // this.$avatarInput.val('');
  267. // } else if (data.message) {
  268. // this.alert(data.message);
  269. // }
  270. //} else {
  271. // this.alert('请求失败');
  272. //}
  273. },
  274. submitFail: function (msg) {
  275. this.alert(msg);
  276. },
  277. submitEnd: function () {
  278. this.$loading.fadeOut();
  279. },
  280. cropDone: function () {
  281. this.$avatarForm.get(0).reset();
  282. this.$avatar.attr('src', this.url);
  283. this.stopCropper();
  284. this.$avatarModal.modal('hide');
  285. },
  286. alert: function (msg) {
  287. var $alert = [
  288. '<div class="alert alert-danger avater-alert">',
  289. '<button type="button" class="close" data-dismiss="alert">&times;</button>',
  290. msg,
  291. '</div>'
  292. ].join('');
  293. this.$avatarUpload.after($alert);
  294. }
  295. };
  296. $(function () {
  297. return new CropAvatar($('#crop-avatar'));
  298. });
  299. });