Files

132 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2018-10-22 19:49:32 +01:00
import {
default as computed,
observes
} from "ember-addons/ember-computed-decorators";
2018-06-20 09:15:15 -06:00
2018-06-07 17:17:19 -06:00
var _loaded = false,
2018-10-22 19:49:32 +01:00
_promise = null,
currentUser = Discourse.User.current(),
propertyId = Discourse.SiteSettings.codefund_property_id;
2018-06-07 17:17:19 -06:00
function loadCodeFund() {
if (_loaded) {
return Ember.RSVP.resolve();
}
2018-06-20 09:15:15 -06:00
2018-06-07 17:17:19 -06:00
if (_promise) {
return _promise;
}
const url = "https://codefund.app/properties/" + propertyId + "/funder.json";
2018-06-07 17:17:19 -06:00
2018-06-20 09:15:15 -06:00
_promise = new Promise(function(resolve, reject) {
2018-06-07 17:17:19 -06:00
let xhr = new XMLHttpRequest();
2018-10-22 19:49:32 +01:00
xhr.open("GET", url);
2018-06-07 17:17:19 -06:00
xhr.onreadystatechange = handler;
2018-10-22 19:49:32 +01:00
xhr.responseType = "json";
xhr.setRequestHeader("Accept", "application/json");
2018-06-07 17:17:19 -06:00
xhr.send();
function handler() {
if (this.readyState === this.DONE) {
_loaded = true;
2018-06-07 17:49:15 -06:00
2018-06-07 17:17:19 -06:00
if (this.status === 200) {
resolve(this.response);
} else {
2018-10-22 19:49:32 +01:00
reject(
new Error(
"getJSON: `" + url + "` failed with status: [" + this.status + "]"
)
);
2018-06-07 17:17:19 -06:00
}
}
2018-10-22 19:49:32 +01:00
}
2018-06-07 17:17:19 -06:00
});
return _promise;
}
export default Ember.Component.extend({
2018-10-22 19:49:32 +01:00
classNameBindings: [
":codefund-ad",
"classForSlot",
"isResponsive:codefund-responsive"
],
2018-06-07 17:17:19 -06:00
propertyId: propertyId,
adRequested: false,
adDetails: {},
_triggerAds() {
if (!propertyId) return;
2018-10-22 19:49:32 +01:00
this.set("adRequested", true);
loadCodeFund()
.then(data => {
_loaded = false;
_promise = null;
this.set("adDetails", data);
this.set("adRequested", false);
})
.catch(error => console.log(error));
2018-06-07 17:17:19 -06:00
},
didInsertElement() {
this._super();
2018-10-22 19:49:32 +01:00
if (!this.get("showAd")) {
return;
}
2018-06-07 17:17:19 -06:00
2018-10-22 19:49:32 +01:00
if (this.get("listLoading")) {
return;
}
2018-06-07 17:17:19 -06:00
2018-10-22 19:49:32 +01:00
Ember.run.scheduleOnce("afterRender", this, this._triggerAds);
2018-06-07 17:17:19 -06:00
},
2018-10-22 19:49:32 +01:00
@observes("listLoading")
2018-06-07 17:17:19 -06:00
waitForLoad: function() {
2018-10-22 19:49:32 +01:00
if (this.get("adRequested")) {
return;
} // already requested that this ad unit be populated
if (!this.get("listLoading")) {
Ember.run.scheduleOnce("afterRender", this, this._triggerAds);
2018-06-07 17:17:19 -06:00
}
2018-06-20 09:15:15 -06:00
},
2018-06-07 17:17:19 -06:00
checkTrustLevels: function() {
2018-10-22 19:49:32 +01:00
return !(
currentUser &&
currentUser.get("trust_level") >
Discourse.SiteSettings.codefund_through_trust_level
);
}.property("trust_level"),
2018-06-07 17:17:19 -06:00
2018-10-22 19:49:32 +01:00
@computed("checkTrustLevels")
2018-06-20 09:15:15 -06:00
showAd: function(checkTrustLevels) {
return Discourse.SiteSettings.codefund_property_id && checkTrustLevels;
},
2018-06-07 17:17:19 -06:00
2018-10-22 19:49:32 +01:00
@computed("placement")
2018-06-20 09:15:15 -06:00
displayPostBottom: function(placement) {
2018-10-22 19:49:32 +01:00
return placement === "post-bottom";
2018-06-20 09:15:15 -06:00
},
2018-06-07 17:17:19 -06:00
2018-10-22 19:49:32 +01:00
@computed("placement")
2018-06-07 17:17:19 -06:00
displayTopicAbovePostStream: function() {
2018-10-22 19:49:32 +01:00
return this.get("placement") === "topic-above-post-stream";
2018-06-20 09:15:15 -06:00
},
2018-06-07 17:17:19 -06:00
2018-10-22 19:49:32 +01:00
@computed("placement")
2018-06-07 17:17:19 -06:00
displayTopicAboveSuggested: function() {
2018-10-22 19:49:32 +01:00
return this.get("placement") === "topic-above-suggested";
2018-06-20 09:15:15 -06:00
},
2018-06-07 17:17:19 -06:00
2018-10-22 19:49:32 +01:00
@computed("placement")
2018-06-07 17:17:19 -06:00
displayTopicListTop: function() {
2018-10-22 19:49:32 +01:00
return this.get("placement") === "topic-list-top";
2018-06-20 09:15:15 -06:00
}
2018-06-07 17:17:19 -06:00
});