2022-03-06 13:52:19 -05:00
|
|
|
import { isBlank } from "@ember/utils";
|
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-04-18 17:52:59 -04:00
|
|
|
|
|
|
|
const adIndex = {
|
|
|
|
topic_list_top: null,
|
|
|
|
topic_above_post_stream: null,
|
|
|
|
topic_above_suggested: null,
|
2020-09-04 07:24:14 -04:00
|
|
|
post_bottom: null,
|
2022-07-07 07:53:29 -04:00
|
|
|
topic_list_between: null,
|
2019-04-18 17:52:59 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export default AdComponent.extend({
|
|
|
|
classNames: ["house-creative"],
|
|
|
|
classNameBindings: ["adUnitClass"],
|
2022-07-07 07:53:29 -04:00
|
|
|
attributeBindings: ["colspanAttribute:colspan"],
|
2019-04-18 17:52:59 -04:00
|
|
|
adHtml: "",
|
|
|
|
|
2022-07-07 07:53:29 -04:00
|
|
|
@discourseComputed
|
|
|
|
colspanAttribute() {
|
|
|
|
return this.tagName === "td" ? "5" : null;
|
|
|
|
},
|
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("placement", "showAd")
|
2019-04-18 17:52:59 -04:00
|
|
|
adUnitClass(placement, showAd) {
|
|
|
|
return showAd ? `house-${placement}` : "";
|
|
|
|
},
|
|
|
|
|
2022-07-07 07:53:29 -04:00
|
|
|
@discourseComputed(
|
|
|
|
"showToGroups",
|
|
|
|
"showAfterPost",
|
|
|
|
"showAfterTopicListItem",
|
|
|
|
"showOnCurrentPage"
|
|
|
|
)
|
|
|
|
showAd(
|
|
|
|
showToGroups,
|
|
|
|
showAfterPost,
|
|
|
|
showAfterTopicListItem,
|
|
|
|
showOnCurrentPage
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
showToGroups &&
|
|
|
|
(showAfterPost || showAfterTopicListItem) &&
|
|
|
|
showOnCurrentPage
|
|
|
|
);
|
2019-04-18 17:52:59 -04:00
|
|
|
},
|
|
|
|
|
2022-07-07 07:53:29 -04:00
|
|
|
@discourseComputed("postNumber", "placement")
|
|
|
|
showAfterPost(postNumber, placement) {
|
|
|
|
if (!postNumber && placement !== "topic-list-between") {
|
2019-04-18 17:52:59 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.isNthPost(
|
2019-11-18 10:07:55 -05:00
|
|
|
parseInt(this.site.get("house_creatives.settings.after_nth_post"), 10)
|
2019-04-18 17:52:59 -04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2022-07-07 07:53:29 -04:00
|
|
|
@discourseComputed("placement")
|
|
|
|
showAfterTopicListItem(placement) {
|
|
|
|
if (placement !== "topic-list-between") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.isNthTopicListItem(
|
|
|
|
parseInt(this.site.get("house_creatives.settings.after_nth_topic"), 10)
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
chooseAdHtml() {
|
|
|
|
const houseAds = this.site.get("house_creatives"),
|
|
|
|
placement = this.get("placement").replace(/-/g, "_"),
|
|
|
|
adNames = this.adsNamesForSlot(placement);
|
|
|
|
|
2024-06-27 12:43:54 -04:00
|
|
|
// filter out ads that should not be shown on the current page
|
|
|
|
const filteredAds = adNames.filter((adName) => {
|
|
|
|
const ad = houseAds.creatives[adName];
|
|
|
|
return (
|
2024-07-05 03:30:18 -04:00
|
|
|
ad &&
|
|
|
|
(!ad.category_ids?.length ||
|
|
|
|
ad.category_ids.includes(this.currentCategoryId))
|
2024-06-27 12:43:54 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
if (filteredAds.length > 0) {
|
|
|
|
if (!adIndex[placement]) {
|
|
|
|
adIndex[placement] = 0;
|
2024-04-09 13:54:11 -04:00
|
|
|
}
|
2024-06-27 12:43:54 -04:00
|
|
|
let ad = houseAds.creatives[filteredAds[adIndex[placement]]] || "";
|
|
|
|
adIndex[placement] = (adIndex[placement] + 1) % filteredAds.length;
|
|
|
|
return ad.html;
|
2019-04-18 17:52:59 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
adsNamesForSlot(placement) {
|
2019-05-03 13:26:16 -04:00
|
|
|
const houseAds = this.site.get("house_creatives");
|
|
|
|
|
|
|
|
if (!houseAds || !houseAds.settings) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const adsForSlot = houseAds.settings[placement];
|
2019-04-18 17:52:59 -04:00
|
|
|
|
2022-03-06 13:52:19 -05:00
|
|
|
if (Object.keys(houseAds.creatives).length > 0 && !isBlank(adsForSlot)) {
|
2019-04-18 17:52:59 -04:00
|
|
|
return adsForSlot.split("|");
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
refreshAd() {
|
|
|
|
this.set("adHtml", this.chooseAdHtml());
|
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (!this.get("showAd")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-03 16:04:04 -04:00
|
|
|
if (adIndex.topic_list_top === null) {
|
2019-04-18 17:52:59 -04:00
|
|
|
// start at a random spot in the ad inventory
|
2024-06-27 12:43:54 -04:00
|
|
|
const houseAds = this.site.get("house_creatives");
|
2020-09-04 07:24:14 -04:00
|
|
|
Object.keys(adIndex).forEach((placement) => {
|
2019-04-18 17:52:59 -04:00
|
|
|
const adNames = this.adsNamesForSlot(placement);
|
2024-06-27 12:43:54 -04:00
|
|
|
if (adNames.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// filter out ads that should not be shown on the current page
|
|
|
|
const filteredAds = adNames.filter((adName) => {
|
|
|
|
const ad = houseAds.creatives[adName];
|
|
|
|
return (
|
2024-07-05 03:30:18 -04:00
|
|
|
ad &&
|
|
|
|
(!ad.category_ids?.length ||
|
|
|
|
ad.category_ids.includes(this.currentCategoryId))
|
2024-06-27 12:43:54 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
adIndex[placement] = Math.floor(Math.random() * filteredAds.length);
|
2019-04-18 17:52:59 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.refreshAd();
|
2020-09-04 07:24:14 -04:00
|
|
|
},
|
2019-04-18 17:52:59 -04:00
|
|
|
});
|