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

233 lines
5.4 KiB
Plaintext
Raw Normal View History

import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
import {
default as computed,
observes
} from "ember-addons/ember-computed-decorators";
2018-10-22 14:49:32 -04:00
import loadScript from "discourse/lib/load-script";
let _loaded = false,
2018-10-22 14:49:32 -04:00
_promise = null,
renderCounts = {},
2018-10-22 14:49:32 -04:00
publisher_id = Discourse.SiteSettings.adsense_publisher_code;
2017-05-29 18:00:38 -04:00
function parseAdWidth(value) {
2018-10-22 14:49:32 -04:00
if (value === "responsive") {
return "auto";
}
const w = parseInt(value.substring(0, 3).trim(), 10);
if (isNaN(w)) {
return "auto";
} else {
return `${w}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";
}
const h = parseInt(value.substring(4, 7).trim(), 10);
if (isNaN(h)) {
return "auto";
} else {
return `${h}px`;
}
}
function loadAdsense() {
if (_loaded) {
return Ember.RSVP.resolve();
}
if (_promise) {
return _promise;
}
2016-02-22 12:11:29 -05:00
const adsenseSrc =
2018-10-22 14:49:32 -04:00
("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
}
const DESKTOP_SETTINGS = {
"topic-list-top": {
code: "adsense_topic_list_top_code",
sizes: "adsense_topic_list_top_ad_sizes"
},
"topic-above-post-stream": {
code: "adsense_topic_above_post_stream_code",
sizes: "adsense_topic_above_post_stream_ad_sizes"
},
"topic-above-suggested": {
code: "adsense_topic_above_suggested_code",
sizes: "adsense_topic_above_suggested_ad_sizes"
},
"post-bottom": {
code: "adsense_post_bottom_code",
sizes: "adsense_post_bottom_ad_sizes"
}
};
const MOBILE_SETTINGS = {
"topic-list-top": {
code: "adsense_mobile_topic_list_top_code",
sizes: "adsense_mobile_topic_list_top_ad_size"
},
"topic-above-post-stream": {
code: "adsense_mobile_topic_above_post_stream_code",
sizes: "adsense_mobile_topic_above_post_stream_ad_size"
},
"topic-above-suggested": {
code: "adsense_mobile_topic_above_suggested_code",
sizes: "adsense_mobile_topic_above_suggested_ad_size"
},
"post-bottom": {
code: "adsense_mobile_post_bottom_code",
sizes: "adsense_mobile_post_bottom_ad_size"
}
};
export default AdComponent.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() {
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) {
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]);
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);
},
@observes("listLoading")
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
}
},
2017-05-29 18:00:38 -04:00
@computed("ad_width")
isResponsive(adWidth) {
return adWidth === "auto";
},
2017-05-29 18:00:38 -04:00
@computed("placement", "showAd")
classForSlot(placement, showAd) {
return showAd ? `adsense-${placement}`.htmlSafe() : "";
},
2017-05-29 18:00:38 -04:00
@computed("isResponsive")
autoAdFormat(isResponsive) {
return isResponsive ? "auto".htmlSafe() : false;
},
2017-05-29 18:00:38 -04:00
@computed("ad_width", "ad_height", "isResponsive")
adWrapperStyle(w, h, isResponsive) {
return (isResponsive ? "" : `width: ${w}; height: ${h};`).htmlSafe();
},
@computed("adWrapperStyle", "isResponsive")
adInsStyle(adWrapperStyle, isResponsive) {
2018-10-22 14:49:32 -04:00
return `display: ${
isResponsive ? "block" : "inline-block"
}; ${adWrapperStyle}`.htmlSafe();
},
@computed("currentUser.trust_level")
showToTrustLevel(trustLevel) {
2018-10-22 14:49:32 -04:00
return !(
trustLevel && trustLevel > this.siteSettings.adsense_through_trust_level
2018-10-22 14:49:32 -04:00
);
},
@computed(
"showToTrustLevel",
"showToGroups",
"showAfterPost",
"showOnCurrentPage"
)
showAd(showToTrustLevel, showToGroups, showAfterPost, showOnCurrentPage) {
return (
this.siteSettings.adsense_publisher_code &&
showToTrustLevel &&
showToGroups &&
showAfterPost &&
showOnCurrentPage
);
},
@computed("postNumber")
showAfterPost(postNumber) {
if (!postNumber) {
return true;
}
return this.isNthPost(
parseInt(this.siteSettings.adsense_nth_post_code, 10)
);
}
});