2019-04-18 17:52:59 -04:00
|
|
|
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
|
2020-03-23 04:40:29 -04:00
|
|
|
import discourseComputed, { observes } from "discourse-common/utils/decorators";
|
2018-10-22 14:49:32 -04:00
|
|
|
import loadScript from "discourse/lib/load-script";
|
2016-12-05 13:31:11 -05:00
|
|
|
|
2019-04-16 11:57:16 -04:00
|
|
|
let _loaded = false,
|
2018-10-22 14:49:32 -04:00
|
|
|
_promise = null,
|
2021-09-21 14:27:10 -04:00
|
|
|
renderCounts = {};
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2017-05-29 18:00:38 -04:00
|
|
|
function parseAdWidth(value) {
|
2018-10-22 14:49:32 -04:00
|
|
|
if (value === "responsive") {
|
|
|
|
return "auto";
|
|
|
|
}
|
2020-08-21 09:53:34 -04:00
|
|
|
if (value.startsWith("fluid")) {
|
|
|
|
return "fluid";
|
|
|
|
}
|
2019-11-18 10:07:55 -05:00
|
|
|
const w = parseInt(value.substring(0, 3).trim(), 10);
|
2019-09-10 17:01:01 -04:00
|
|
|
if (isNaN(w)) {
|
|
|
|
return "auto";
|
|
|
|
} else {
|
|
|
|
return `${w}px`;
|
|
|
|
}
|
2015-08-25 00:30:23 -04:00
|
|
|
}
|
|
|
|
|
2017-05-29 18:00:38 -04:00
|
|
|
function parseAdHeight(value) {
|
2018-10-22 14:49:32 -04:00
|
|
|
if (value === "responsive") {
|
|
|
|
return "auto";
|
|
|
|
}
|
2020-08-21 09:53:34 -04:00
|
|
|
if (value.startsWith("fluid")) {
|
|
|
|
return "fluid";
|
|
|
|
}
|
2019-11-18 10:07:55 -05:00
|
|
|
const h = parseInt(value.substring(4, 7).trim(), 10);
|
2019-09-10 17:01:01 -04:00
|
|
|
if (isNaN(h)) {
|
|
|
|
return "auto";
|
|
|
|
} else {
|
|
|
|
return `${h}px`;
|
|
|
|
}
|
2015-08-25 00:30:23 -04:00
|
|
|
}
|
|
|
|
|
2016-12-05 13:31:11 -05:00
|
|
|
function loadAdsense() {
|
|
|
|
if (_loaded) {
|
|
|
|
return Ember.RSVP.resolve();
|
2015-09-02 01:23:17 -04:00
|
|
|
}
|
2015-09-03 20:51:44 -04:00
|
|
|
|
2016-12-05 13:31:11 -05:00
|
|
|
if (_promise) {
|
|
|
|
return _promise;
|
|
|
|
}
|
2016-02-22 12:11:29 -05:00
|
|
|
|
2019-04-16 11:57:16 -04:00
|
|
|
const adsenseSrc =
|
2018-10-22 14:49:32 -04:00
|
|
|
("https:" === document.location.protocol ? "https:" : "http:") +
|
|
|
|
"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
|
2020-09-04 07:24:14 -04:00
|
|
|
_promise = loadScript(adsenseSrc, { scriptTag: true }).then(function () {
|
2016-12-05 13:31:11 -05:00
|
|
|
_loaded = true;
|
|
|
|
});
|
2016-02-22 12:11:29 -05:00
|
|
|
|
2016-12-05 13:31:11 -05:00
|
|
|
return _promise;
|
2016-02-22 12:11:29 -05:00
|
|
|
}
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2019-07-08 15:10:25 -04:00
|
|
|
const DESKTOP_SETTINGS = {
|
|
|
|
"topic-list-top": {
|
|
|
|
code: "adsense_topic_list_top_code",
|
2020-09-04 07:24:14 -04:00
|
|
|
sizes: "adsense_topic_list_top_ad_sizes",
|
2019-07-08 15:10:25 -04:00
|
|
|
},
|
|
|
|
"topic-above-post-stream": {
|
|
|
|
code: "adsense_topic_above_post_stream_code",
|
2020-09-04 07:24:14 -04:00
|
|
|
sizes: "adsense_topic_above_post_stream_ad_sizes",
|
2019-07-08 15:10:25 -04:00
|
|
|
},
|
|
|
|
"topic-above-suggested": {
|
|
|
|
code: "adsense_topic_above_suggested_code",
|
2020-09-04 07:24:14 -04:00
|
|
|
sizes: "adsense_topic_above_suggested_ad_sizes",
|
2019-07-08 15:10:25 -04:00
|
|
|
},
|
|
|
|
"post-bottom": {
|
|
|
|
code: "adsense_post_bottom_code",
|
2020-09-04 07:24:14 -04:00
|
|
|
sizes: "adsense_post_bottom_ad_sizes",
|
|
|
|
},
|
2016-02-19 15:06:13 -05:00
|
|
|
};
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2019-07-08 15:10:25 -04:00
|
|
|
const MOBILE_SETTINGS = {
|
|
|
|
"topic-list-top": {
|
|
|
|
code: "adsense_mobile_topic_list_top_code",
|
2020-09-04 07:24:14 -04:00
|
|
|
sizes: "adsense_mobile_topic_list_top_ad_size",
|
2019-07-08 15:10:25 -04:00
|
|
|
},
|
|
|
|
"topic-above-post-stream": {
|
|
|
|
code: "adsense_mobile_topic_above_post_stream_code",
|
2020-09-04 07:24:14 -04:00
|
|
|
sizes: "adsense_mobile_topic_above_post_stream_ad_size",
|
2019-07-08 15:10:25 -04:00
|
|
|
},
|
|
|
|
"topic-above-suggested": {
|
|
|
|
code: "adsense_mobile_topic_above_suggested_code",
|
2020-09-04 07:24:14 -04:00
|
|
|
sizes: "adsense_mobile_topic_above_suggested_ad_size",
|
2019-07-08 15:10:25 -04:00
|
|
|
},
|
|
|
|
"post-bottom": {
|
|
|
|
code: "adsense_mobile_post_bottom_code",
|
2020-09-04 07:24:14 -04:00
|
|
|
sizes: "adsense_mobile_post_bottom_ad_size",
|
|
|
|
},
|
2019-07-08 15:10:25 -04:00
|
|
|
};
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
export default AdComponent.extend({
|
2018-10-22 14:49:32 -04:00
|
|
|
classNameBindings: [
|
|
|
|
":google-adsense",
|
|
|
|
"classForSlot",
|
2020-09-04 07:24:14 -04:00
|
|
|
"isResponsive:adsense-responsive",
|
2018-10-22 14:49:32 -04:00
|
|
|
],
|
2015-08-25 00:30:23 -04:00
|
|
|
loadedGoogletag: false,
|
|
|
|
|
2021-09-21 14:27:10 -04:00
|
|
|
publisher_id: null,
|
2017-05-29 18:00:38 -04:00
|
|
|
ad_width: null,
|
|
|
|
ad_height: null,
|
2015-08-28 01:34:14 -04:00
|
|
|
|
2017-05-29 18:00:38 -04:00
|
|
|
adRequested: false,
|
|
|
|
|
2016-11-08 16:46:09 -05:00
|
|
|
init() {
|
2019-07-08 15:10:25 -04:00
|
|
|
let config, size;
|
|
|
|
const placement = this.get("placement");
|
|
|
|
|
|
|
|
if (this.site.mobileView) {
|
|
|
|
config = MOBILE_SETTINGS[placement];
|
|
|
|
} else {
|
|
|
|
config = DESKTOP_SETTINGS[placement];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!renderCounts[placement]) {
|
|
|
|
renderCounts[placement] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sizes = (this.siteSettings[config.sizes] || "").split("|");
|
|
|
|
|
2019-07-08 15:18:34 -04:00
|
|
|
if (sizes.length === 1) {
|
2019-07-08 15:10:25 -04:00
|
|
|
size = sizes[0];
|
|
|
|
} else {
|
|
|
|
size = sizes[renderCounts[placement] % sizes.length];
|
|
|
|
renderCounts[placement] += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set("ad_width", parseAdWidth(size));
|
|
|
|
this.set("ad_height", parseAdHeight(size));
|
|
|
|
this.set("ad_code", this.siteSettings[config.code]);
|
2021-09-21 14:27:10 -04:00
|
|
|
this.set("publisher_id", this.siteSettings.adsense_publisher_code);
|
2015-08-27 22:21:40 -04:00
|
|
|
this._super();
|
|
|
|
},
|
2016-02-19 15:06:13 -05:00
|
|
|
|
2016-11-08 16:46:09 -05:00
|
|
|
_triggerAds() {
|
2020-01-06 12:01:40 -05:00
|
|
|
if (Ember.testing) {
|
|
|
|
return; // Don't load external JS during tests
|
|
|
|
}
|
|
|
|
|
2018-10-22 14:49:32 -04:00
|
|
|
this.set("adRequested", true);
|
2020-09-04 07:24:14 -04:00
|
|
|
loadAdsense().then(function () {
|
2016-12-05 13:31:11 -05:00
|
|
|
const adsbygoogle = window.adsbygoogle || [];
|
2016-11-09 16:46:09 -05:00
|
|
|
|
2016-12-05 13:31:11 -05:00
|
|
|
try {
|
|
|
|
adsbygoogle.push({}); // ask AdSense to fill one ad unit
|
|
|
|
} catch (ex) {}
|
|
|
|
});
|
2016-11-08 16:46:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super();
|
2016-11-09 16:46:09 -05:00
|
|
|
|
2018-10-22 14:49:32 -04:00
|
|
|
if (!this.get("showAd")) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-21 15:55:03 -05:00
|
|
|
|
2018-10-22 14:49:32 -04:00
|
|
|
if (this.get("listLoading")) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-29 18:00:38 -04:00
|
|
|
|
2018-10-22 14:49:32 -04:00
|
|
|
Ember.run.scheduleOnce("afterRender", this, this._triggerAds);
|
2016-11-08 16:46:09 -05:00
|
|
|
},
|
|
|
|
|
2019-04-16 11:57:16 -04:00
|
|
|
@observes("listLoading")
|
2019-04-18 14:10:56 -04:00
|
|
|
waitForLoad() {
|
2018-10-22 14:49:32 -04:00
|
|
|
if (this.get("adRequested")) {
|
|
|
|
return;
|
|
|
|
} // already requested that this ad unit be populated
|
|
|
|
if (!this.get("listLoading")) {
|
|
|
|
Ember.run.scheduleOnce("afterRender", this, this._triggerAds);
|
2017-05-29 18:00:38 -04:00
|
|
|
}
|
2019-04-16 11:57:16 -04:00
|
|
|
},
|
2017-05-29 18:00:38 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("ad_width")
|
2019-04-18 14:10:56 -04:00
|
|
|
isResponsive(adWidth) {
|
2020-08-21 09:53:34 -04:00
|
|
|
return ["auto", "fluid"].includes(adWidth);
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed("ad_width")
|
|
|
|
isFluid(adWidth) {
|
|
|
|
return adWidth === "fluid";
|
2019-04-16 11:57:16 -04:00
|
|
|
},
|
2017-05-29 18:00:38 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("placement", "showAd")
|
2019-04-18 14:10:56 -04:00
|
|
|
classForSlot(placement, showAd) {
|
|
|
|
return showAd ? `adsense-${placement}`.htmlSafe() : "";
|
2019-04-16 11:57:16 -04:00
|
|
|
},
|
2017-05-29 18:00:38 -04:00
|
|
|
|
2020-08-21 10:24:12 -04:00
|
|
|
@discourseComputed("isResponsive", "isFluid")
|
|
|
|
autoAdFormat(isResponsive, isFluid) {
|
2020-08-21 10:48:41 -04:00
|
|
|
return isResponsive
|
|
|
|
? isFluid
|
|
|
|
? "fluid".htmlSafe()
|
|
|
|
: "auto".htmlSafe()
|
|
|
|
: false;
|
2019-04-16 11:57:16 -04:00
|
|
|
},
|
2017-05-29 18:00:38 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("ad_width", "ad_height", "isResponsive")
|
2019-04-18 14:10:56 -04:00
|
|
|
adWrapperStyle(w, h, isResponsive) {
|
2019-04-16 11:57:16 -04:00
|
|
|
return (isResponsive ? "" : `width: ${w}; height: ${h};`).htmlSafe();
|
|
|
|
},
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("adWrapperStyle", "isResponsive")
|
2019-04-18 14:10:56 -04:00
|
|
|
adInsStyle(adWrapperStyle, isResponsive) {
|
2018-10-22 14:49:32 -04:00
|
|
|
return `display: ${
|
2019-04-16 11:57:16 -04:00
|
|
|
isResponsive ? "block" : "inline-block"
|
|
|
|
}; ${adWrapperStyle}`.htmlSafe();
|
|
|
|
},
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("currentUser.trust_level")
|
2019-04-18 14:10:56 -04:00
|
|
|
showToTrustLevel(trustLevel) {
|
2018-10-22 14:49:32 -04:00
|
|
|
return !(
|
2019-04-18 17:52:59 -04:00
|
|
|
trustLevel && trustLevel > this.siteSettings.adsense_through_trust_level
|
2018-10-22 14:49:32 -04:00
|
|
|
);
|
2019-04-16 11:57:16 -04:00
|
|
|
},
|
2016-11-21 15:55:03 -05:00
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed(
|
2021-09-21 14:27:10 -04:00
|
|
|
"publisher_id",
|
2019-06-05 15:52:52 -04:00
|
|
|
"showToTrustLevel",
|
|
|
|
"showToGroups",
|
|
|
|
"showAfterPost",
|
|
|
|
"showOnCurrentPage"
|
|
|
|
)
|
2021-09-21 14:27:10 -04:00
|
|
|
showAd(
|
|
|
|
publisherId,
|
|
|
|
showToTrustLevel,
|
|
|
|
showToGroups,
|
|
|
|
showAfterPost,
|
|
|
|
showOnCurrentPage
|
|
|
|
) {
|
2019-04-18 14:10:56 -04:00
|
|
|
return (
|
2021-09-21 14:27:10 -04:00
|
|
|
publisherId &&
|
2019-04-18 14:10:56 -04:00
|
|
|
showToTrustLevel &&
|
|
|
|
showToGroups &&
|
2019-06-05 15:52:52 -04:00
|
|
|
showAfterPost &&
|
|
|
|
showOnCurrentPage
|
2019-04-18 14:10:56 -04:00
|
|
|
);
|
2019-04-18 17:52:59 -04:00
|
|
|
},
|
|
|
|
|
2020-03-23 04:40:29 -04:00
|
|
|
@discourseComputed("postNumber")
|
2019-04-18 17:52:59 -04:00
|
|
|
showAfterPost(postNumber) {
|
|
|
|
if (!postNumber) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-18 10:07:55 -05:00
|
|
|
return this.isNthPost(
|
|
|
|
parseInt(this.siteSettings.adsense_nth_post_code, 10)
|
|
|
|
);
|
2020-09-04 07:24:14 -04:00
|
|
|
},
|
2015-12-24 04:50:22 -05:00
|
|
|
});
|