avatar-crop-form.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import Ember from 'ember';
  2. export default Ember.Component.extend({
  3. classNames: 'avatar-crop',
  4. cropit: Ember.inject.service('cropit'),
  5. isLoading: true,
  6. isCropping: false,
  7. suffix: 'org',
  8. isNested: Ember.computed.equal('suffix', 'tmp'),
  9. token: null,
  10. crop: null,
  11. apiUrl: function() {
  12. return 'users/' + this.auth.get('user.id') + '/avatar';
  13. }.property(),
  14. finalToken: function() {
  15. return this.get('token') || this.get('options.crop_org.token');
  16. }.property('token', 'options.crop_org'),
  17. avatarSize: function() {
  18. if (this.get('isNested')) {
  19. return this.get('options.crop_tmp.size');
  20. } else {
  21. return this.get('options.crop_org.size');
  22. }
  23. }.property('options.crop_tmp.size', 'options.crop_org.size'),
  24. imagePath: function() {
  25. var src = Ember.$('base').attr('href') + 'user-avatar/';
  26. src += this.get('suffix') + ':' + this.get('finalToken') + '/';
  27. src += this.get('auth.user.id') + '.png';
  28. return src;
  29. }.property('suffix', 'finalToken', 'auth.user.id'),
  30. loadLibrary: function() {
  31. var self = this;
  32. this.get('cropit').load().then(function() {
  33. self.set('isLoading', false);
  34. Ember.run.scheduleOnce('afterRender', function() {
  35. // initialise cropper
  36. var $cropper = self.$('.image-cropper');
  37. $cropper.width(self.get('avatarSize'));
  38. $cropper.cropit({
  39. width: self.get('avatarSize'),
  40. height: self.get('avatarSize'),
  41. imageState: {
  42. src: self.get('imagePath')
  43. },
  44. onImageLoaded: function() {
  45. if (self.get('isNested')) {
  46. // center uploaded image
  47. var zoomLevel = $cropper.cropit('zoom');
  48. var imageSize = $cropper.cropit('imageSize');
  49. // is it wider than taller?
  50. if (imageSize.width > imageSize.height) {
  51. var displayedWidth = (imageSize.width * zoomLevel);
  52. var offsetX = (displayedWidth - self.get('avatarSize')) / -2;
  53. $cropper.cropit('offset', { x: offsetX, y: 0 });
  54. } else if (imageSize.width < imageSize.height) {
  55. var displayedHeight = (imageSize.height * zoomLevel);
  56. var offsetY = (displayedHeight - self.get('avatarSize')) / -2;
  57. $cropper.cropit('offset', { x: 0, y: offsetY });
  58. }
  59. } else {
  60. // use preserved crop
  61. var crop = self.get('options.crop_org.crop');
  62. if (crop) {
  63. $cropper.cropit('zoom', crop.zoom);
  64. $cropper.cropit('offset', { x: crop.x, y: crop.y });
  65. }
  66. }
  67. }
  68. });
  69. });
  70. });
  71. }.on('didInsertElement'),
  72. destroyCrop: function() {
  73. this.$('.image-cropper').cropit('disable');
  74. }.on('willDestroyElement'),
  75. actions: {
  76. crop: function() {
  77. if (this.get('isCropping')) { return; }
  78. this.set('isCropping', true);
  79. var opName = 'crop_' + this.get('suffix');
  80. var $cropper = this.$('.image-cropper');
  81. var crop = {
  82. 'offset': $cropper.cropit('offset'),
  83. 'zoom': $cropper.cropit('zoom')
  84. };
  85. var self = this;
  86. this.ajax.post(this.get('apiUrl'), {
  87. 'avatar': opName,
  88. 'crop': crop
  89. }).then(function(data) {
  90. if (self.isDestroyed) { return; }
  91. self.toast.success(data.detail);
  92. self.get('options').setProperties(data.options);
  93. self.set('auth.user.avatar_hash', data.avatar_hash);
  94. self.set('activeForm', 'select-avatar-type-form');
  95. }, function(jhXHR) {
  96. if (self.isDestroyed) { return; }
  97. self.set('isCropping', false);
  98. if (jhXHR.status === 400) {
  99. self.toast.error(jhXHR.responseJSON.detail);
  100. } else {
  101. self.toast.apiError(jhXHR);
  102. }
  103. }).finally(function() {
  104. if (self.isDestroyed) { return; }
  105. });
  106. },
  107. cancel: function() {
  108. this.set('activeForm', 'select-avatar-type-form');
  109. }
  110. }
  111. });