Ei kuvausta

sitelogo.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 || {
  13. log: function() {}
  14. };
  15. function CropAvatar($element) {
  16. this.$container = $element;
  17. this.$avatarView = this.$container.find('.avatar-view');
  18. this.$avatar = this.$avatarView.find('img');
  19. this.$avatarModal = $("body").find('#avatar-modal');
  20. this.$loading = $("#page-wrapper").find('.loading');
  21. this.$avatarForm = this.$avatarModal.find('.avatar-form');
  22. this.$avatarUpload = this.$avatarForm.find('.avatar-upload');
  23. this.$avatarSrc = this.$avatarForm.find('.avatar-src');
  24. this.$avatarData = this.$avatarForm.find('.avatar-data');
  25. this.$avatarInput = this.$avatarForm.find('.avatar-input');
  26. this.$avatarSave = this.$avatarForm.find('.avatar-save');
  27. this.$avatarBtns = this.$avatarForm.find('.avatar-btns');
  28. this.$avatarWrapper = this.$avatarModal.find('.avatar-wrapper');
  29. this.$avatarPreview = this.$avatarModal.find('.avatar-preview');
  30. this.init();
  31. }
  32. CropAvatar.prototype = {
  33. constructor: CropAvatar,
  34. support: {
  35. fileList: !!$('<input type="file">').prop('files'),
  36. blobURLs: !!window.URL && URL.createObjectURL,
  37. formData: !!window.FormData
  38. },
  39. init: function() {
  40. this.support.datauri = this.support.fileList && this.support.blobURLs;
  41. if(!this.support.formData) {
  42. this.initIframe();
  43. }
  44. this.initTooltip();
  45. this.initModal();
  46. this.addListener();
  47. },
  48. addListener: function() {
  49. this.$avatarView.on('click', $.proxy(this.click, this));
  50. this.$avatarInput.on('change', $.proxy(this.change, this));
  51. this.$avatarForm.on('submit', $.proxy(this.submit, this));
  52. this.$avatarBtns.on('click', $.proxy(this.rotate, this));
  53. },
  54. initTooltip: function() {
  55. this.$avatarView.tooltip({
  56. placement: 'bottom'
  57. });
  58. },
  59. initModal: function() {
  60. this.$avatarModal.modal({
  61. show: false
  62. });
  63. },
  64. initPreview: function() {
  65. var url = this.$avatar.attr('src');
  66. // this.$avatarPreview.empty().html('<img src="' + url + '">');
  67. },
  68. initIframe: function() {
  69. var target = 'upload-iframe-' + (new Date()).getTime(),
  70. $iframe = $('<iframe>').attr({
  71. name: target,
  72. src: ''
  73. }),
  74. _this = this;
  75. // Ready ifrmae
  76. $iframe.one('load', function() {
  77. // respond response
  78. $iframe.on('load', function() {
  79. var data;
  80. try {
  81. data = $(this).contents().find('body').text();
  82. } catch(e) {
  83. console.log(e.message);
  84. }
  85. if(data) {
  86. try {
  87. data = $.parseJSON(data);
  88. } catch(e) {
  89. console.log(e.message);
  90. }
  91. _this.submitDone(data);
  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,
  107. file;
  108. if(this.support.datauri) {
  109. files = this.$avatarInput.prop('files');
  110. if(files.length > 0) {
  111. file = files[0];
  112. if(this.isImageFile(file)) {
  113. if(this.url) {
  114. URL.revokeObjectURL(this.url); // Revoke the old one
  115. }
  116. this.url = URL.createObjectURL(file);
  117. this.startCropper();
  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. } else {
  149. return /\.(jpg|jpeg|png|gif)$/.test(file);
  150. }
  151. },
  152. startCropper: function() {
  153. var _this = this;
  154. if(this.active) {
  155. this.$img.cropper('replace', this.url);
  156. } else {
  157. this.$img = $('<img src="' + this.url + '">');
  158. this.$avatarWrapper.empty().html(this.$img);
  159. this.$img.cropper({
  160. aspectRatio: 1,
  161. preview: this.$avatarPreview.selector,
  162. strict: false,
  163. // crop: function(data) {
  164. // var json = [
  165. // '{"x":' + data.x,
  166. // '"y":' + data.y,
  167. // '"height":' + data.height,
  168. // '"width":' + data.width,
  169. // '"rotate":' + data.rotate + '}'
  170. // ].join();
  171. // _this.$avatarData.val(json);
  172. // }
  173. });
  174. this.active = true;
  175. }
  176. },
  177. stopCropper: function() {
  178. if(this.active) {
  179. this.$img.cropper('destroy');
  180. this.$img.remove();
  181. this.active = false;
  182. }
  183. },
  184. // ajaxUpload: function() {
  185. // var url = this.$avatarForm.attr('action'),
  186. // data = new FormData(this.$avatarForm[0]),
  187. // _this = this;
  188. //
  189. // $.ajax(url, {
  190. // headers: {
  191. // 'X-XSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  192. // },
  193. // type: 'post',
  194. // data: data,
  195. // dataType: 'json',
  196. // processData: false,
  197. // contentType: false,
  198. //
  199. // beforeSend: function() {
  200. // _this.submitStart();
  201. // },
  202. //
  203. // success: function(data) {
  204. // _this.submitDone(data);
  205. // },
  206. //
  207. // error: function(XMLHttpRequest, textStatus, errorThrown) {
  208. // if (this.uploaded) {
  209. // this.uploaded = false;
  210. // this.cropDone();
  211. // // this.uploaded = true;this.support.datauri ||
  212. // // this.$avatarSrc.val(this.url);
  213. // // this.startCropper();
  214. // } else {
  215. // this.uploaded = true;
  216. // this.$avatarSrc.val(this.url);
  217. // this.startCropper();
  218. // this.cropDone();
  219. // }
  220. // },
  221. //
  222. // complete: function() {
  223. // _this.submitEnd();
  224. // }
  225. // });
  226. // },
  227. syncUpload: function() {
  228. this.$avatarSave.click();
  229. },
  230. submitStart: function() {
  231. this.$loading.fadeIn();
  232. },
  233. // submitDone: function(data) {
  234. // if($.isPlainObject(data)) {
  235. // if(data.result) {
  236. // this.url = data.result;
  237. // if(this.support.datauri || this.uploaded) {
  238. // this.uploaded = false;
  239. // this.cropDone();
  240. // } else {
  241. // this.uploaded = true;
  242. // this.$avatarSrc.val(this.url);
  243. // this.startCropper();
  244. // }
  245. // this.$avatarInput.val('');
  246. // } else if(data.message) {
  247. // this.alert(data.message);
  248. // }
  249. // } else {
  250. // this.alert('Failed to response');
  251. // }
  252. // },
  253. submitFail: function(msg) {
  254. this.alert(msg);
  255. },
  256. submitEnd: function() {
  257. this.$loading.fadeOut();
  258. },
  259. cropDone: function() {
  260. this.$avatarForm.get(0).reset();
  261. this.$avatar.attr('src', this.url);
  262. this.stopCropper();
  263. this.$avatarModal.modal('hide');
  264. },
  265. alert: function(msg) {
  266. var $alert = [
  267. '<div class="alert alert-danger avater-alert">',
  268. '<button type="button" class="close" data-dismiss="alert">&times;</button>',
  269. msg,
  270. '</div>'
  271. ].join('');
  272. this.$avatarUpload.after($alert);
  273. }
  274. };
  275. $(function() {
  276. return new CropAvatar($('#crop-avatar'));
  277. });
  278. });