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

159 lines
3.6 KiB
Plaintext
Raw Normal View History

2019-09-16 14:26:17 -04:00
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
2019-09-16 14:26:17 -04:00
import loadScript from "discourse/lib/load-script";
let _loaded = false,
2019-09-16 14:35:58 -04:00
_promise = null,
_c = 0;
2019-09-16 14:26:17 -04:00
function loadAdbutler(adserverHostname) {
2019-09-16 14:26:17 -04:00
if (_loaded) {
return Ember.RSVP.resolve();
}
if (_promise) {
return _promise;
}
2019-09-16 14:35:58 -04:00
_promise = loadScript("https://" + adserverHostname + "/app.js", {
2020-09-04 07:24:14 -04:00
scriptTag: true,
}).then(function () {
2019-09-16 14:26:17 -04:00
_loaded = true;
});
return _promise;
}
export default AdComponent.extend({
divs: null,
publisherId: null,
2019-09-16 14:26:17 -04:00
init() {
2019-09-16 14:35:58 -04:00
let dimensions = [728, 90];
let configKey = "adbutler_";
let className = "adbutler-";
let dimClassName = "adbutler-ad";
2019-09-16 14:26:17 -04:00
2019-09-16 14:35:58 -04:00
this.set("divs", []);
2019-09-16 14:26:17 -04:00
if (this.site.mobileView) {
2019-09-16 14:35:58 -04:00
dimensions = [320, 50];
configKey += "mobile_";
className += "mobile-";
dimClassName = "adbutler-mobile-ad";
2019-09-16 14:26:17 -04:00
}
2019-09-16 14:35:58 -04:00
configKey += this.get("placement").replace(/-/g, "_") + "_zone_id";
2019-09-16 14:26:17 -04:00
this.set("configKey", configKey);
className += this.get("placement");
2019-09-16 14:35:58 -04:00
this.set("className", className + " " + dimClassName);
2019-09-16 14:26:17 -04:00
let zoneId = this.siteSettings[configKey];
this.set("zoneId", zoneId);
2019-09-16 14:35:58 -04:00
let divId = "placement-" + zoneId + "-" + _c;
2019-09-16 14:26:17 -04:00
this.set("divId", divId);
let publisherId = this.siteSettings.adbutler_publisher_id;
this.set("publisherId", publisherId);
2019-09-16 14:26:17 -04:00
_c++;
2019-09-16 14:26:17 -04:00
this.divs.push({
divId,
publisherId,
zoneId,
dimensions,
2019-09-16 14:26:17 -04:00
});
this._super();
},
_triggerAds() {
if (Ember.testing) {
return; // Don't load external JS during tests
}
const adserverHostname = this.siteSettings.adbutler_adserver_hostname;
loadAdbutler(adserverHostname).then(
2020-09-04 07:24:14 -04:00
function () {
2019-09-16 14:35:58 -04:00
if (this.divs.length > 0) {
let abkw = window.abkw || "";
window.AdButler.ads.push({
2020-09-04 07:24:14 -04:00
handler: function (opt) {
window.AdButler.register(
2019-09-16 14:35:58 -04:00
opt.place.publisherId,
opt.place.zoneId,
opt.place.dimensions,
opt.place.divId,
opt
);
},
opt: {
place: this.divs.pop(),
keywords: abkw,
domain: adserverHostname,
2020-09-04 07:24:14 -04:00
click: "CLICK_MACRO_PLACEHOLDER",
},
2019-09-16 14:35:58 -04:00
});
}
}.bind(this)
);
2019-09-16 14:26:17 -04:00
},
didInsertElement() {
this._super();
Ember.run.scheduleOnce("afterRender", this, this._triggerAds);
},
@observes("listLoading")
waitForLoad() {
if (this.get("adRequested")) {
return;
} // already requested that this ad unit be populated
if (!this.get("listLoading")) {
Ember.run.scheduleOnce("afterRender", this, this._triggerAds);
}
},
@discourseComputed("currentUser.trust_level")
2019-09-16 14:26:17 -04:00
showToTrustLevel(trustLevel) {
return !(
trustLevel && trustLevel > this.siteSettings.adbutler_through_trust_level
2019-09-16 14:26:17 -04:00
);
},
@discourseComputed(
"publisherId",
2019-09-16 14:35:58 -04:00
"showToTrustLevel",
"showToGroups",
2019-09-16 14:26:17 -04:00
"showAfterPost",
2019-09-16 14:35:58 -04:00
"showOnCurrentPage"
2019-09-16 14:26:17 -04:00
)
showAd(
publisherId,
showToTrustLevel,
showToGroups,
showAfterPost,
showOnCurrentPage
) {
2019-09-16 14:26:17 -04:00
return (
publisherId &&
showToTrustLevel &&
showToGroups &&
showAfterPost &&
showOnCurrentPage
);
},
@discourseComputed("postNumber")
2019-09-16 14:26:17 -04:00
showAfterPost(postNumber) {
if (!postNumber) {
return true;
}
return this.isNthPost(parseInt(this.siteSettings.adbutler_nth_post, 10));
2020-09-04 07:24:14 -04:00
},
2019-09-16 14:26:17 -04:00
});