2016-02-22 12:11:29 -05:00
|
|
|
import { withPluginApi } from 'discourse/lib/plugin-api';
|
2015-08-25 00:30:23 -04:00
|
|
|
import PageTracker from 'discourse/lib/page-tracker';
|
2016-12-05 13:31:11 -05:00
|
|
|
import loadScript from 'discourse/lib/load-script';
|
|
|
|
|
|
|
|
var _loaded = false,
|
|
|
|
_promise = null,
|
|
|
|
ad_width = '',
|
|
|
|
ad_height = '',
|
|
|
|
ad_mobile_width = 320,
|
|
|
|
ad_mobile_height = 50,
|
|
|
|
currentUser = Discourse.User.current(),
|
|
|
|
publisher_id = Discourse.SiteSettings.adsense_publisher_code,
|
|
|
|
mobile_width = 320,
|
|
|
|
mobile_height = 50;
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2016-02-19 15:06:13 -05:00
|
|
|
const mobileView = Discourse.Site.currentProp('mobileView');
|
2015-08-27 22:21:40 -04:00
|
|
|
|
2015-08-25 00:30:23 -04:00
|
|
|
function splitWidthInt(value) {
|
2016-02-19 15:06:13 -05:00
|
|
|
var str = value.substring(0, 3);
|
|
|
|
return str.trim();
|
2015-08-25 00:30:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function splitHeightInt(value) {
|
2016-02-19 15:06:13 -05:00
|
|
|
var str = value.substring(4, 7);
|
|
|
|
return str.trim();
|
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
|
|
|
|
2016-12-05 13:31:11 -05: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
|
|
|
|
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
|
|
|
|
2015-08-27 22:21:40 -04:00
|
|
|
var data = {
|
|
|
|
"topic-list-top" : {},
|
|
|
|
"topic-above-post-stream" : {},
|
|
|
|
"topic-above-suggested" : {},
|
2016-02-19 15:06:13 -05:00
|
|
|
"post-bottom" : {}
|
|
|
|
};
|
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) {
|
|
|
|
data["topic-list-top"]["ad_code"] = Discourse.SiteSettings.adsense_topic_list_top_code;
|
|
|
|
data["topic-list-top"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes));
|
|
|
|
data["topic-list-top"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_list_top_ad_sizes));
|
|
|
|
}
|
|
|
|
if (mobileView && Discourse.SiteSettings.adsense_mobile_topic_list_top_code) {
|
|
|
|
data["topic-list-top"]["ad_mobile_code"] = Discourse.SiteSettings.adsense_mobile_topic_list_top_code;
|
|
|
|
}
|
|
|
|
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"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_above_post_stream_ad_sizes));
|
|
|
|
data["topic-above-post-stream"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_above_post_stream_ad_sizes));
|
|
|
|
}
|
|
|
|
if (mobileView && Discourse.SiteSettings.adsense_mobile_topic_above_post_stream_code) {
|
|
|
|
data["topic-above-post-stream"]["ad_mobile_code"] = Discourse.SiteSettings.adsense_mobile_topic_above_post_stream_code;
|
|
|
|
}
|
|
|
|
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"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_topic_above_suggested_ad_sizes));
|
|
|
|
data["topic-above-suggested"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_topic_above_suggested_ad_sizes));
|
|
|
|
}
|
|
|
|
if (mobileView && Discourse.SiteSettings.adsense_mobile_topic_above_suggested_code) {
|
|
|
|
data["topic-above-suggested"]["ad_mobile_code"] = Discourse.SiteSettings.adsense_mobile_topic_above_suggested_code;
|
|
|
|
}
|
|
|
|
if (!mobileView && Discourse.SiteSettings.adsense_post_bottom_code) {
|
|
|
|
data["post-bottom"]["ad_code"] = Discourse.SiteSettings.adsense_post_bottom_code;
|
|
|
|
data["post-bottom"]["ad_width"] = parseInt(splitWidthInt(Discourse.SiteSettings.adsense_post_bottom_ad_sizes));
|
|
|
|
data["post-bottom"]["ad_height"] = parseInt(splitHeightInt(Discourse.SiteSettings.adsense_post_bottom_ad_sizes));
|
|
|
|
}
|
|
|
|
if (mobileView && Discourse.SiteSettings.adsense_mobile_post_bottom_code) {
|
|
|
|
data["post-bottom"]["ad_mobile_code"] = Discourse.SiteSettings.adsense_mobile_post_bottom_code;
|
|
|
|
}
|
2015-08-25 00:30:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Ember.Component.extend({
|
|
|
|
classNames: ['google-adsense'],
|
|
|
|
loadedGoogletag: false,
|
|
|
|
|
|
|
|
publisher_id: publisher_id,
|
|
|
|
ad_width: ad_width,
|
|
|
|
ad_height: ad_height,
|
2015-08-31 23:44:56 -04:00
|
|
|
ad_mobile_width: ad_mobile_width,
|
|
|
|
ad_mobile_height: ad_mobile_height,
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2015-08-28 01:34:14 -04:00
|
|
|
mobile_width: mobile_width,
|
|
|
|
mobile_height: mobile_height,
|
|
|
|
|
2016-11-08 16:46:09 -05:00
|
|
|
init() {
|
2015-08-27 22:21:40 -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-31 23:20:52 -04:00
|
|
|
this.set('ad_mobile_code', data[this.placement]["ad_mobile_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() {
|
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
|
|
|
|
2016-11-21 15:55:03 -05:00
|
|
|
if (!this.get('showAd')) { return; }
|
|
|
|
|
2016-11-08 16:46:09 -05:00
|
|
|
Ember.run.scheduleOnce('afterRender', this, this._triggerAds);
|
|
|
|
},
|
|
|
|
|
2015-08-25 00:30:23 -04:00
|
|
|
adWrapperStyle: function() {
|
2016-04-04 16:51:34 -04:00
|
|
|
return `width: ${this.get('ad_width')}px; height: ${this.get('ad_height')}px;`.htmlSafe();
|
2015-08-25 00:30:23 -04:00
|
|
|
}.property('ad_width', 'ad_height'),
|
|
|
|
|
|
|
|
adInsStyle: function() {
|
|
|
|
return `display: inline-block; ${this.get('adWrapperStyle')}`.htmlSafe();
|
|
|
|
}.property('adWrapperStyle'),
|
|
|
|
|
|
|
|
adWrapperStyleMobile: function() {
|
2016-04-04 16:51:34 -04:00
|
|
|
return `width: ${this.get('ad_mobile_width')}px; height: ${this.get('ad_mobile_height')}px;`.htmlSafe();
|
2015-08-31 23:44:56 -04:00
|
|
|
}.property('ad_mobile_width', 'ad_mobile_height'),
|
2015-08-25 00:30:23 -04:00
|
|
|
|
2015-10-28 19:51:53 -04:00
|
|
|
adTitleStyleMobile: function() {
|
|
|
|
return `width: ${this.get('ad_mobile_width')}px;`.htmlSafe();
|
|
|
|
}.property('ad_mobile_width'),
|
|
|
|
|
2015-08-25 00:30:23 -04:00
|
|
|
adInsStyleMobile: function() {
|
|
|
|
return `display: inline-block; ${this.get('adWrapperStyleMobile')}`.htmlSafe();
|
|
|
|
}.property('adWrapperStyleMobile'),
|
2015-09-15 22:11:50 -04:00
|
|
|
|
|
|
|
checkTrustLevels: function() {
|
|
|
|
return !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level));
|
|
|
|
}.property('trust_level'),
|
2016-11-21 15:55:03 -05:00
|
|
|
|
|
|
|
showAd: function() {
|
|
|
|
return Discourse.SiteSettings.adsense_publisher_code && this.get('checkTrustLevels');
|
|
|
|
}.property('checkTrustLevels')
|
2015-12-24 04:50:22 -05:00
|
|
|
});
|