2019-04-18 17:52:59 -04:00
|
|
|
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
|
2019-04-16 11:57:16 -04:00
|
|
|
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";
|
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,
|
|
|
|
currentUser = Discourse.User.current(),
|
|
|
|
publisher_id = Discourse.SiteSettings.adsense_publisher_code;
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2018-10-22 14:49:32 -04:00
|
|
|
const mobileView = Discourse.Site.currentProp("mobileView");
|
2015-08-27 22:21:40 -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";
|
|
|
|
}
|
|
|
|
return `${parseInt(value.substring(0, 3).trim())}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";
|
|
|
|
}
|
|
|
|
return `${parseInt(value.substring(4, 7).trim())}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";
|
2016-12-05 13:31:11 -05:00
|
|
|
_promise = loadScript(adsenseSrc, { scriptTag: true }).then(function() {
|
|
|
|
_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-04-16 11:57:16 -04:00
|
|
|
let data = {
|
2018-10-22 14:49:32 -04:00
|
|
|
"topic-list-top": {},
|
|
|
|
"topic-above-post-stream": {},
|
|
|
|
"topic-above-suggested": {},
|
|
|
|
"post-bottom": {}
|
2016-02-19 15:06:13 -05:00
|
|
|
};
|
2015-08-25 00:30:23 -04:00
|
|
|
|
|
|
|
if (Discourse.SiteSettings.adsense_publisher_code) {
|
2016-02-19 15:06:13 -05:00
|
|
|
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
|
|
|
|
);
|
2016-02-19 15:06:13 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
|
);
|
2016-02-19 15:06:13 -05:00
|
|
|
}
|
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
|
|
|
|
);
|
2016-02-19 15:06:13 -05:00
|
|
|
}
|
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
|
|
|
|
);
|
2016-02-19 15:06:13 -05:00
|
|
|
}
|
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
|
|
|
|
);
|
2016-02-19 15:06:13 -05:00
|
|
|
}
|
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
|
|
|
|
);
|
2016-02-19 15:06:13 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
|
);
|
2016-02-19 15:06:13 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
|
);
|
2016-02-19 15:06:13 -05: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",
|
|
|
|
"isResponsive:adsense-responsive"
|
|
|
|
],
|
2015-08-25 00:30:23 -04:00
|
|
|
loadedGoogletag: false,
|
|
|
|
|
|
|
|
publisher_id: publisher_id,
|
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() {
|
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"]);
|
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() {
|
2018-10-22 14:49:32 -04:00
|
|
|
this.set("adRequested", true);
|
2016-12-05 13:31:11 -05:00
|
|
|
loadAdsense().then(function() {
|
|
|
|
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
|
|
|
|
2019-04-16 11:57:16 -04:00
|
|
|
@computed("ad_width")
|
2019-04-18 14:10:56 -04:00
|
|
|
isResponsive(adWidth) {
|
2019-04-16 11:57:16 -04:00
|
|
|
return adWidth === "auto";
|
|
|
|
},
|
2017-05-29 18:00:38 -04:00
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@computed("placement", "showAd")
|
|
|
|
classForSlot(placement, showAd) {
|
|
|
|
return showAd ? `adsense-${placement}`.htmlSafe() : "";
|
2019-04-16 11:57:16 -04:00
|
|
|
},
|
2017-05-29 18:00:38 -04:00
|
|
|
|
2019-04-16 11:57:16 -04:00
|
|
|
@computed("isResponsive")
|
2019-04-18 14:10:56 -04:00
|
|
|
autoAdFormat(isResponsive) {
|
2019-04-16 11:57:16 -04:00
|
|
|
return isResponsive ? "auto".htmlSafe() : false;
|
|
|
|
},
|
2017-05-29 18:00:38 -04:00
|
|
|
|
2019-04-16 11:57:16 -04:00
|
|
|
@computed("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
|
|
|
|
2019-04-16 11:57:16 -04:00
|
|
|
@computed("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
|
|
|
|
2019-04-18 14:10:56 -04:00
|
|
|
@computed("currentUser.trust_level")
|
|
|
|
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
|
|
|
|
2019-04-18 17:52:59 -04:00
|
|
|
@computed("showToTrustLevel", "showToGroups", "showAfterPost")
|
|
|
|
showAd(showToTrustLevel, showToGroups, showAfterPost) {
|
2019-04-18 14:10:56 -04:00
|
|
|
return (
|
2019-04-18 17:52:59 -04:00
|
|
|
this.siteSettings.adsense_publisher_code &&
|
2019-04-18 14:10:56 -04:00
|
|
|
showToTrustLevel &&
|
|
|
|
showToGroups &&
|
2019-04-18 17:52:59 -04:00
|
|
|
showAfterPost
|
2019-04-18 14:10:56 -04:00
|
|
|
);
|
2019-04-18 17:52:59 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("postNumber")
|
|
|
|
showAfterPost(postNumber) {
|
|
|
|
if (!postNumber) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.isNthPost(parseInt(this.siteSettings.adsense_nth_post_code));
|
2019-04-16 11:57:16 -04:00
|
|
|
}
|
2015-12-24 04:50:22 -05:00
|
|
|
});
|