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";
|
2023-11-07 16:12:30 -05:00
|
|
|
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
|
|
|
|
2021-09-21 14:27:10 -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,
|
2021-09-21 14:27:10 -04:00
|
|
|
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);
|
2021-09-21 14:27:10 -04:00
|
|
|
|
|
|
|
let publisherId = this.siteSettings.adbutler_publisher_id;
|
|
|
|
this.set("publisherId", publisherId);
|
|
|
|
|
2019-09-16 14:26:17 -04:00
|
|
|
_c++;
|
2021-09-21 14:27:10 -04:00
|
|
|
|
2019-09-16 14:26:17 -04:00
|
|
|
this.divs.push({
|
2021-11-09 19:58:31 -05:00
|
|
|
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()) {
|
2020-01-06 12:01:40 -05:00
|
|
|
return; // Don't load external JS during tests
|
|
|
|
}
|
|
|
|
|
2021-09-21 14:27:10 -04:00
|
|
|
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 || "";
|
2019-09-16 23:46:32 -04:00
|
|
|
window.AdButler.ads.push({
|
2020-09-04 07:24:14 -04:00
|
|
|
handler: function (opt) {
|
2019-09-16 23:46:32 -04:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("currentUser.trust_level")
|
2019-09-16 14:26:17 -04:00
|
|
|
showToTrustLevel(trustLevel) {
|
|
|
|
return !(
|
2021-09-21 14:27:10 -04:00
|
|
|
trustLevel && trustLevel > this.siteSettings.adbutler_through_trust_level
|
2019-09-16 14:26:17 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed(
|
2021-09-21 14:27:10 -04:00
|
|
|
"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
|
|
|
)
|
2021-09-21 14:27:10 -04:00
|
|
|
showAd(
|
|
|
|
publisherId,
|
|
|
|
showToTrustLevel,
|
|
|
|
showToGroups,
|
|
|
|
showAfterPost,
|
|
|
|
showOnCurrentPage
|
|
|
|
) {
|
2019-09-16 14:26:17 -04:00
|
|
|
return (
|
|
|
|
publisherId &&
|
|
|
|
showToTrustLevel &&
|
|
|
|
showToGroups &&
|
|
|
|
showAfterPost &&
|
|
|
|
showOnCurrentPage
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("postNumber")
|
2019-09-16 14:26:17 -04:00
|
|
|
showAfterPost(postNumber) {
|
|
|
|
if (!postNumber) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-18 10:07:55 -05:00
|
|
|
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
|
|
|
});
|