discourse-adplugin/assets/javascripts/discourse/components/google-adsense.js.es6

240 lines
7.1 KiB
Plaintext
Raw Normal View History

2018-10-22 14:49:32 -04:00
import loadScript from "discourse/lib/load-script";
var _loaded = false,
2018-10-22 14:49:32 -04:00
_promise = null,
currentUser = Discourse.User.current(),
publisher_id = Discourse.SiteSettings.adsense_publisher_code;
2018-10-22 14:49:32 -04:00
const mobileView = Discourse.Site.currentProp("mobileView");
2017-05-29 18:00:38 -04:00
function parseAdWidth(value) {
2018-10-22 14:49:32 -04:00
if (value === "responsive") {
return "auto";
}
return `${parseInt(value.substring(0, 3).trim())}px`;
}
2017-05-29 18:00:38 -04:00
function parseAdHeight(value) {
2018-10-22 14:49:32 -04:00
if (value === "responsive") {
return "auto";
}
return `${parseInt(value.substring(4, 7).trim())}px`;
}
function loadAdsense() {
if (_loaded) {
return Ember.RSVP.resolve();
}
if (_promise) {
return _promise;
}
2016-02-22 12:11:29 -05:00
2018-10-22 14:49:32 -04:00
var adsenseSrc =
("https:" === document.location.protocol ? "https:" : "http:") +
"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
_promise = loadScript(adsenseSrc, { scriptTag: true }).then(function() {
_loaded = true;
});
2016-02-22 12:11:29 -05:00
return _promise;
2016-02-22 12:11:29 -05:00
}
var data = {
2018-10-22 14:49:32 -04:00
"topic-list-top": {},
"topic-above-post-stream": {},
"topic-above-suggested": {},
"post-bottom": {}
};
if (Discourse.SiteSettings.adsense_publisher_code) {
if (!mobileView && Discourse.SiteSettings.adsense_topic_list_top_code) {
2018-10-22 14:49:32 -04:00
data["topic-list-top"]["ad_code"] =
Discourse.SiteSettings.adsense_topic_list_top_code;
data["topic-list-top"]["ad_width"] = parseAdWidth(
Discourse.SiteSettings.adsense_topic_list_top_ad_sizes
);
data["topic-list-top"]["ad_height"] = parseAdHeight(
Discourse.SiteSettings.adsense_topic_list_top_ad_sizes
);
}
if (mobileView && Discourse.SiteSettings.adsense_mobile_topic_list_top_code) {
2018-10-22 14:49:32 -04:00
data["topic-list-top"]["ad_code"] =
Discourse.SiteSettings.adsense_mobile_topic_list_top_code;
data["topic-list-top"]["ad_width"] = parseAdWidth(
Discourse.SiteSettings.adsense_mobile_topic_list_top_ad_size
);
data["topic-list-top"]["ad_height"] = parseAdHeight(
Discourse.SiteSettings.adsense_mobile_topic_list_top_ad_size
);
}
2018-10-22 14:49:32 -04:00
if (
!mobileView &&
Discourse.SiteSettings.adsense_topic_above_post_stream_code
) {
data["topic-above-post-stream"]["ad_code"] =
Discourse.SiteSettings.adsense_topic_above_post_stream_code;
data["topic-above-post-stream"]["ad_width"] = parseAdWidth(
Discourse.SiteSettings.adsense_topic_above_post_stream_ad_sizes
);
data["topic-above-post-stream"]["ad_height"] = parseAdHeight(
Discourse.SiteSettings.adsense_topic_above_post_stream_ad_sizes
);
}
2018-10-22 14:49:32 -04:00
if (
mobileView &&
Discourse.SiteSettings.adsense_mobile_topic_above_post_stream_code
) {
data["topic-above-post-stream"]["ad_code"] =
Discourse.SiteSettings.adsense_mobile_topic_above_post_stream_code;
data["topic-above-post-stream"]["ad_width"] = parseAdWidth(
Discourse.SiteSettings.adsense_mobile_topic_above_post_stream_ad_size
);
data["topic-above-post-stream"]["ad_height"] = parseAdHeight(
Discourse.SiteSettings.adsense_mobile_topic_above_post_stream_ad_size
);
}
2018-10-22 14:49:32 -04:00
if (
!mobileView &&
Discourse.SiteSettings.adsense_topic_above_suggested_code
) {
data["topic-above-suggested"]["ad_code"] =
Discourse.SiteSettings.adsense_topic_above_suggested_code;
data["topic-above-suggested"]["ad_width"] = parseAdWidth(
Discourse.SiteSettings.adsense_topic_above_suggested_ad_sizes
);
data["topic-above-suggested"]["ad_height"] = parseAdHeight(
Discourse.SiteSettings.adsense_topic_above_suggested_ad_sizes
);
}
2018-10-22 14:49:32 -04:00
if (
mobileView &&
Discourse.SiteSettings.adsense_mobile_topic_above_suggested_code
) {
data["topic-above-suggested"]["ad_code"] =
Discourse.SiteSettings.adsense_mobile_topic_above_suggested_code;
data["topic-above-suggested"]["ad_width"] = parseAdWidth(
Discourse.SiteSettings.adsense_mobile_topic_above_suggested_ad_size
);
data["topic-above-suggested"]["ad_height"] = parseAdHeight(
Discourse.SiteSettings.adsense_mobile_topic_above_suggested_ad_size
);
}
if (!mobileView && Discourse.SiteSettings.adsense_post_bottom_code) {
2018-10-22 14:49:32 -04:00
data["post-bottom"]["ad_code"] =
Discourse.SiteSettings.adsense_post_bottom_code;
data["post-bottom"]["ad_width"] = parseAdWidth(
Discourse.SiteSettings.adsense_post_bottom_ad_sizes
);
data["post-bottom"]["ad_height"] = parseAdHeight(
Discourse.SiteSettings.adsense_post_bottom_ad_sizes
);
}
if (mobileView && Discourse.SiteSettings.adsense_mobile_post_bottom_code) {
2018-10-22 14:49:32 -04:00
data["post-bottom"]["ad_code"] =
Discourse.SiteSettings.adsense_mobile_post_bottom_code;
data["post-bottom"]["ad_width"] = parseAdWidth(
Discourse.SiteSettings.adsense_mobile_post_bottom_ad_size
);
data["post-bottom"]["ad_height"] = parseAdHeight(
Discourse.SiteSettings.adsense_mobile_post_bottom_ad_size
);
}
}
export default Ember.Component.extend({
2018-10-22 14:49:32 -04:00
classNameBindings: [
":google-adsense",
"classForSlot",
"isResponsive:adsense-responsive"
],
loadedGoogletag: false,
publisher_id: publisher_id,
2017-05-29 18:00:38 -04:00
ad_width: null,
ad_height: null,
2017-05-29 18:00:38 -04:00
adRequested: false,
init() {
2018-10-22 14:49:32 -04:00
this.set("ad_width", data[this.placement]["ad_width"]);
this.set("ad_height", data[this.placement]["ad_height"]);
this.set("ad_code", data[this.placement]["ad_code"]);
this._super();
},
_triggerAds() {
2018-10-22 14:49:32 -04:00
this.set("adRequested", true);
loadAdsense().then(function() {
const adsbygoogle = window.adsbygoogle || [];
try {
adsbygoogle.push({}); // ask AdSense to fill one ad unit
} catch (ex) {}
});
},
didInsertElement() {
this._super();
2018-10-22 14:49:32 -04:00
if (!this.get("showAd")) {
return;
}
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);
},
2017-05-29 18:00:38 -04:00
waitForLoad: function() {
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
}
2018-10-22 14:49:32 -04:00
}.observes("listLoading"),
2017-05-29 18:00:38 -04:00
isResponsive: function() {
2018-10-22 14:49:32 -04:00
return this.get("ad_width") === "auto";
}.property("ad_width"),
2017-05-29 18:00:38 -04:00
classForSlot: function() {
2018-10-22 14:49:32 -04:00
return `adsense-${this.get("placement")}`.htmlSafe();
}.property("placement"),
2017-05-29 18:00:38 -04:00
autoAdFormat: function() {
2018-10-22 14:49:32 -04:00
return this.get("isResponsive") ? "auto".htmlSafe() : false;
}.property("isResponsive"),
2017-05-29 18:00:38 -04:00
adWrapperStyle: function() {
2018-10-22 14:49:32 -04:00
return (this.get("isResponsive")
? ""
: `width: ${this.get("ad_width")}; height: ${this.get("ad_height")};`
).htmlSafe();
}.property("ad_width", "ad_height"),
adInsStyle: function() {
2018-10-22 14:49:32 -04:00
return `display: ${
this.get("isResponsive") ? "block" : "inline-block"
}; ${this.get("adWrapperStyle")}`.htmlSafe();
}.property("adWrapperStyle", "isResponsive"),
checkTrustLevels: function() {
2018-10-22 14:49:32 -04:00
return !(
currentUser &&
currentUser.get("trust_level") >
Discourse.SiteSettings.adsense_through_trust_level
);
}.property("trust_level"),
showAd: function() {
2018-10-22 14:49:32 -04:00
return (
Discourse.SiteSettings.adsense_publisher_code &&
this.get("checkTrustLevels")
);
}.property("checkTrustLevels")
});