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

154 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-03-06 13:52:19 -05:00
import { scheduleOnce } from "@ember/runloop";
2023-11-07 12:07:33 -05:00
import RSVP from "rsvp";
import loadScript from "discourse/lib/load-script";
2022-03-06 13:52:19 -05:00
import { isTesting } from "discourse-common/config/environment";
import discourseComputed from "discourse-common/utils/decorators";
2023-11-07 12:07:33 -05:00
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
2019-09-16 14:26:17 -04:00
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) {
2022-03-06 13:52:19 -05:00
return RSVP.resolve();
2019-09-16 14:26:17 -04:00
}
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() {
2022-03-06 13:52:19 -05:00
if (isTesting()) {
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();
2022-03-06 13:52:19 -05:00
scheduleOnce("afterRender", this, this._triggerAds);
2019-09-16 14:26:17 -04:00
},
@discourseComputed
showAdbutlerAds() {
if (!this.currentUser) {
return true;
}
return this.currentUser.show_adbutler_ads;
2019-09-16 14:26:17 -04:00
},
@discourseComputed(
"publisherId",
"showAdbutlerAds",
2019-09-16 14:35:58 -04:00
"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,
showAdbutlerAds,
showToGroups,
showAfterPost,
showOnCurrentPage
) {
2019-09-16 14:26:17 -04:00
return (
publisherId &&
showAdbutlerAds &&
2019-09-16 14:26:17 -04:00
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
});