2019-04-18 17:52:59 -04:00
|
|
|
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
|
|
|
|
import {
|
|
|
|
default as computed,
|
2019-06-14 12:10:11 -04:00
|
|
|
observes
|
2019-04-18 17:52:59 -04:00
|
|
|
} from "ember-addons/ember-computed-decorators";
|
|
|
|
|
|
|
|
const adIndex = {
|
|
|
|
topic_list_top: null,
|
|
|
|
topic_above_post_stream: null,
|
|
|
|
topic_above_suggested: null,
|
|
|
|
post_bottom: null
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AdComponent.extend({
|
|
|
|
classNames: ["house-creative"],
|
|
|
|
classNameBindings: ["adUnitClass"],
|
|
|
|
adHtml: "",
|
|
|
|
|
|
|
|
@computed("placement", "showAd")
|
|
|
|
adUnitClass(placement, showAd) {
|
|
|
|
return showAd ? `house-${placement}` : "";
|
|
|
|
},
|
|
|
|
|
2019-06-05 15:52:52 -04:00
|
|
|
@computed("showToGroups", "showAfterPost", "showOnCurrentPage")
|
|
|
|
showAd(showToGroups, showAfterPost, showOnCurrentPage) {
|
|
|
|
return showToGroups && showAfterPost && showOnCurrentPage;
|
2019-04-18 17:52:59 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("postNumber")
|
|
|
|
showAfterPost(postNumber) {
|
|
|
|
if (!postNumber) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.isNthPost(
|
|
|
|
parseInt(this.site.get("house_creatives.settings.after_nth_post"))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
chooseAdHtml() {
|
|
|
|
const houseAds = this.site.get("house_creatives"),
|
|
|
|
placement = this.get("placement").replace(/-/g, "_"),
|
|
|
|
adNames = this.adsNamesForSlot(placement);
|
|
|
|
|
|
|
|
if (adNames.length > 0) {
|
|
|
|
if (!adIndex[placement]) {
|
|
|
|
adIndex[placement] = 0;
|
|
|
|
}
|
|
|
|
let ad = houseAds.creatives[adNames[adIndex[placement]]] || "";
|
|
|
|
adIndex[placement] = (adIndex[placement] + 1) % adNames.length;
|
|
|
|
return ad;
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (
|
|
|
|
Object.keys(houseAds.creatives).length > 0 &&
|
|
|
|
!Ember.isBlank(adsForSlot)
|
|
|
|
) {
|
|
|
|
return adsForSlot.split("|");
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@observes("refreshOnChange")
|
|
|
|
refreshAd() {
|
|
|
|
if (this.get("listLoading")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set("adHtml", this.chooseAdHtml());
|
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
if (!this.get("showAd")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("listLoading")) {
|
|
|
|
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
|
|
|
|
Object.keys(adIndex).forEach(placement => {
|
|
|
|
const adNames = this.adsNamesForSlot(placement);
|
|
|
|
adIndex[placement] = Math.floor(Math.random() * adNames.length);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.refreshAd();
|
|
|
|
}
|
|
|
|
});
|