cropit.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import Ember from 'ember';
  2. export default Ember.Service.extend({
  3. includedJs: false,
  4. loadedJs: false,
  5. load: function() {
  6. if (!this.get('includedJs')) {
  7. this._includeJs();
  8. }
  9. if (!this.get('loadedJs')) {
  10. return this._loadingPromise();
  11. } else {
  12. return this._loadedPromise();
  13. }
  14. },
  15. _includeJs: function() {
  16. Ember.$.getScript(this.get('_includedJsPath'));
  17. this.set('includedJs', true);
  18. },
  19. _includedJsPath: function() {
  20. return this.get('staticUrl') + 'misago/js/cropit.js';
  21. }.property(),
  22. _loadingPromise: function() {
  23. var self = this;
  24. return new Ember.RSVP.Promise(function(resolve) {
  25. var wait = function() {
  26. if (typeof Ember.$.fn.cropit === "undefined") {
  27. Ember.run.later(function () {
  28. wait();
  29. }, 200);
  30. } else {
  31. self.set('loadedJs', true);
  32. Ember.run(null, resolve);
  33. }
  34. };
  35. wait();
  36. });
  37. },
  38. _loadedPromise: function() {
  39. // we have already loaded croppic.js, resolve away!
  40. return new Ember.RSVP.Promise(function(resolve) {
  41. Ember.run(null, resolve);
  42. });
  43. }
  44. });