discourse-adplugin/assets/javascripts/discourse/components/codefund-ad.js.es6

134 lines
3.0 KiB
Plaintext
Raw Normal View History

import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
2018-10-22 14:49:32 -04:00
import {
default as computed,
observes
} from "ember-addons/ember-computed-decorators";
2018-06-20 11:15:15 -04:00
let _loaded = false,
_promise = null;
const currentUser = Discourse.User.current(),
2018-10-22 14:49:32 -04:00
propertyId = Discourse.SiteSettings.codefund_property_id;
2018-06-07 19:17:19 -04:00
function loadCodeFund() {
if (_loaded) {
return Ember.RSVP.resolve();
}
2018-06-20 11:15:15 -04:00
2018-06-07 19:17:19 -04:00
if (_promise) {
return _promise;
}
const url = "https://codefund.app/properties/" + propertyId + "/funder.json";
2018-06-07 19:17:19 -04:00
2018-06-20 11:15:15 -04:00
_promise = new Promise(function(resolve, reject) {
2018-06-07 19:17:19 -04:00
let xhr = new XMLHttpRequest();
2018-10-22 14:49:32 -04:00
xhr.open("GET", url);
2018-06-07 19:17:19 -04:00
xhr.onreadystatechange = handler;
2018-10-22 14:49:32 -04:00
xhr.responseType = "json";
xhr.setRequestHeader("Accept", "application/json");
2018-06-07 19:17:19 -04:00
xhr.send();
function handler() {
if (this.readyState === this.DONE) {
_loaded = true;
2018-06-07 19:49:15 -04:00
2018-06-07 19:17:19 -04:00
if (this.status === 200) {
resolve(this.response);
} else {
2018-10-22 14:49:32 -04:00
reject(
new Error(
"getJSON: `" + url + "` failed with status: [" + this.status + "]"
)
);
2018-06-07 19:17:19 -04:00
}
}
2018-10-22 14:49:32 -04:00
}
2018-06-07 19:17:19 -04:00
});
return _promise;
}
export default AdComponent.extend({
2019-04-16 14:48:25 -04:00
classNameBindings: [":codefund-ad"],
2018-06-07 19:17:19 -04:00
propertyId: propertyId,
adRequested: false,
adDetails: {},
displayPostBottom: Ember.computed.equal("placement", "post-bottom"),
displayTopicAbovePostStream: Ember.computed.equal(
"placement",
"topic-above-post-stream"
),
displayTopicAboveSuggested: Ember.computed.equal(
"placement",
"topic-above-suggested"
),
displayTopicListTop: Ember.computed.equal("placement", "topic-list-top"),
2018-06-07 19:17:19 -04:00
_triggerAds() {
if (!propertyId) return;
2018-10-22 14:49:32 -04: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 19:17:19 -04:00
},
didInsertElement() {
this._super();
2018-10-22 14:49:32 -04:00
if (!this.get("showAd")) {
return;
}
2018-06-07 19:17:19 -04:00
2018-10-22 14:49:32 -04:00
if (this.get("listLoading")) {
return;
}
2018-06-07 19:17:19 -04:00
2018-10-22 14:49:32 -04:00
Ember.run.scheduleOnce("afterRender", this, this._triggerAds);
2018-06-07 19:17:19 -04:00
},
2018-10-22 14:49:32 -04:00
@observes("listLoading")
waitForLoad() {
2018-10-22 14:49:32 -04: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 19:17:19 -04:00
}
2018-06-20 11:15:15 -04:00
},
2018-06-07 19:17:19 -04:00
@computed("currentUser.trust_level")
showToTrustLevel(trustLevel) {
2018-10-22 14:49:32 -04:00
return !(
trustLevel && trustLevel > this.siteSettings.codefund_through_trust_level
2018-10-22 14:49:32 -04:00
);
2019-04-16 14:48:25 -04:00
},
2018-10-22 14:49:32 -04:00
@computed("showToTrustLevel", "showToGroups", "showAfterPost")
showAd(showToTrustLevel, showToGroups, showAfterPost) {
return (
this.siteSettings.codefund_property_id &&
showToTrustLevel &&
showToGroups &&
showAfterPost
);
},
@computed("postNumber")
showAfterPost(postNumber) {
if (!postNumber) {
return true;
}
return this.isNthPost(parseInt(this.siteSettings.codefund_nth_post));
2018-06-20 11:15:15 -04:00
}
2018-06-07 19:17:19 -04:00
});