Merge pull request #48 from discourse/merge_master_changes

Merge master changes
This commit is contained in:
Guo Xiang Tan 2016-12-09 02:19:47 +01:00 committed by GitHub
commit 392fd76679
10 changed files with 158 additions and 75 deletions

View File

@ -1,14 +1,17 @@
import { withPluginApi } from 'discourse/lib/plugin-api';
import PageTracker from 'discourse/lib/page-tracker';
import loadScript from 'discourse/lib/load-script';
var ad_width = '';
var ad_height = '';
var ad_mobile_width = 320;
var ad_mobile_height = 50;
var currentUser = Discourse.User.current();
var publisher_id = Discourse.SiteSettings.adsense_publisher_code;
var mobile_width = 320;
var mobile_height = 50;
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;
const mobileView = Discourse.Site.currentProp('mobileView');
@ -22,34 +25,22 @@ function splitHeightInt(value) {
return str.trim();
}
// On each page change, the child is removed and elements part of Adsense's googleads are removed/undefined.
function changePage() {
const ads = document.getElementById("adsense_loader");
if (ads) {
ads.parentNode.removeChild(ads);
for (var key in window) {
// Undefining all elements starting with google except for googletag so that the reloading doesn't affect dfp. Potential future
// conflicts may occur if other plugins have element starting with google.
if(key.indexOf('google') !== -1 && key.indexOf('googletag') === -1) {
window[key] = undefined;
}
}
function loadAdsense() {
if (_loaded) {
return Ember.RSVP.resolve();
}
// Reinitialize script so that the ad can reload
const ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.id="adsense_loader";
ga.src = '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
const s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
}
if (_promise) {
return _promise;
}
function oldPluginCode() {
PageTracker.current().on('change', changePage);
}
var adsenseSrc = (('https:' === document.location.protocol) ? 'https:' : 'http:') + '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
_promise = loadScript(adsenseSrc, { scriptTag: true }).then(function() {
_loaded = true;
});
function watchPageChanges(api) {
api.onPageChange(changePage);
return _promise;
}
withPluginApi('0.1', watchPageChanges, { noApi: oldPluginCode });
var data = {
"topic-list-top" : {},
@ -106,7 +97,7 @@ export default Ember.Component.extend({
mobile_width: mobile_width,
mobile_height: mobile_height,
init: function() {
init() {
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"] );
@ -114,6 +105,24 @@ export default Ember.Component.extend({
this._super();
},
_triggerAds() {
loadAdsense().then(function() {
const adsbygoogle = window.adsbygoogle || [];
try {
adsbygoogle.push({}); // ask AdSense to fill one ad unit
} catch (ex) {}
});
},
didInsertElement() {
this._super();
if (!this.get('showAd')) { return; }
Ember.run.scheduleOnce('afterRender', this, this._triggerAds);
},
adWrapperStyle: function() {
return `width: ${this.get('ad_width')}px; height: ${this.get('ad_height')}px;`.htmlSafe();
}.property('ad_width', 'ad_height'),
@ -137,4 +146,8 @@ export default Ember.Component.extend({
checkTrustLevels: function() {
return !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.adsense_through_trust_level));
}.property('trust_level'),
showAd: function() {
return Discourse.SiteSettings.adsense_publisher_code && this.get('checkTrustLevels');
}.property('checkTrustLevels')
});

View File

@ -56,6 +56,11 @@ function custom_targeting(key_array, value_array, location) {
}
function defineSlot(divId, placement, settings, isMobile) {
if (!settings.dfp_publisher_id) {
return;
}
var ad, width, height;
if (ads[divId]) {
@ -185,6 +190,10 @@ export default Ember.Component.extend({
return `width: ${this.get('width')}px;`.htmlSafe();
}.property('width'),
showAd: function() {
return Discourse.SiteSettings.dfp_publisher_id && this.get('checkTrustLevels');
}.property('checkTrustLevels'),
checkTrustLevels: function() {
return !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.dfp_through_trust_level));
}.property('trust_level'),
@ -205,6 +214,8 @@ export default Ember.Component.extend({
}.observes('refreshOnChange'),
_initGoogleDFP: function() {
if (!this.get('showAd')) { return; }
var self = this;
loadGoogle(this.siteSettings).then(function() {
self.set('loadedGoogletag', true);

View File

@ -0,0 +1,33 @@
{{#if site.mobileView}}
{{#if model.postSpecificCountAdsense}}
{{#if siteSettings.adsense_mobile_post_bottom_code}}
{{google-adsense placement="post-bottom" postNumber=model.post_number}}
{{/if}}
{{/if}}
{{#if model.postSpecificCountDFP}}
{{#if siteSettings.dfp_mobile_post_bottom_code}}
{{google-dfp-ad placement="post-bottom" category=model.topic.category.slug postNumber=model.post_number}}
{{/if}}
{{/if}}
{{#if model.postSpecificCountAmazon}}
{{#if siteSettings.amazon_mobile_post_bottom_src_code}}
{{amazon-product-links placement="post-bottom" postNumber=model.post_number}}
{{/if}}
{{/if}}
{{else}}
{{#if model.postSpecificCountAdsense}}
{{#if siteSettings.adsense_post_bottom_code}}
{{google-adsense placement="post-bottom" postNumber=model.post_number}}
{{/if}}
{{/if}}
{{#if model.postSpecificCountDFP}}
{{#if siteSettings.dfp_post_bottom_code}}
{{google-dfp-ad placement="post-bottom" category=model.topic.category.slug postNumber=model.post_number}}
{{/if}}
{{/if}}
{{#if model.postSpecificCountAmazon}}
{{#if siteSettings.amazon_post_bottom_src_code}}
{{amazon-product-links placement="post-bottom" postNumber=model.post_number}}
{{/if}}
{{/if}}
{{/if}}

View File

@ -1,4 +1,4 @@
{{#if checkTrustLevels}}
{{#if showAd}}
{{#if site.mobileView}}
<div class="google-adsense-label" style={{adTitleStyleMobile}}><h2>ADVERTISEMENT</h2></div>
<div class="google-adsense-content" style={{adWrapperStyleMobile}}>
@ -7,10 +7,6 @@
data-ad-client="ca-pub-{{publisher_id}}"
data-ad-slot={{ad_mobile_code}}>
</ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
{{else}}
<div class="google-adsense-label"><h2>ADVERTISEMENT</h2></div>
@ -20,10 +16,6 @@
data-ad-client="ca-pub-{{publisher_id}}"
data-ad-slot={{ad_code}}>
</ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
{{/if}}
{{/if}}

View File

@ -1,4 +1,4 @@
{{#if checkTrustLevels}}
{{#if showAd}}
{{#if site.mobileView}}
<div class="google-dfp-ad-label" style={{adTitleStyleMobile}}><h2>ADVERTISEMENT</h2></div>
<div id={{divId}} style={{adWrapperStyle}} class="dfp-ad-unit" align=center></div>

View File

@ -1,9 +1,21 @@
{{#if siteSettings.adsense_topic_list_top_code}}
{{google-adsense placement="topic-list-top"}}
{{/if}}
{{#if siteSettings.dfp_topic_list_top_code}}
{{google-dfp-ad placement="topic-list-top" refreshOnChange=loading category=category.slug}}
{{/if}}
{{#if siteSettings.amazon_topic_list_top_src_code}}
{{amazon-product-links placement="topic-list-top"}}
{{#if site.mobileView}}
{{#if siteSettings.adsense_mobile_topic_list_top_code}}
{{google-adsense placement="topic-list-top"}}
{{/if}}
{{#if siteSettings.dfp_mobile_topic_list_top_code}}
{{google-dfp-ad placement="topic-list-top" refreshOnChange=loading category=category.slug}}
{{/if}}
{{#if siteSettings.amazon_mobile_topic_list_top_src_code}}
{{amazon-product-links placement="topic-list-top"}}
{{/if}}
{{else}}
{{#if siteSettings.adsense_topic_list_top_code}}
{{google-adsense placement="topic-list-top"}}
{{/if}}
{{#if siteSettings.dfp_topic_list_top_code}}
{{google-dfp-ad placement="topic-list-top" refreshOnChange=loading category=category.slug}}
{{/if}}
{{#if siteSettings.amazon_topic_list_top_src_code}}
{{amazon-product-links placement="topic-list-top"}}
{{/if}}
{{/if}}

View File

@ -1,9 +1 @@
{{#if postSpecificCountAdsense}}
{{google-adsense placement="post-bottom" postNumber=post_number}}
{{/if}}
{{#if postSpecificCountDFP}}
{{google-dfp-ad placement="post-bottom" category=topic.category.slug postNumber=post_number}}
{{/if}}
{{#if postSpecificCountAmazon}}
{{amazon-product-links placement="post-bottom" postNumber=post_number}}
{{/if}}
{{adplugin-container model=this}}

View File

@ -1,9 +1,21 @@
{{#if siteSettings.adsense_topic_above_post_stream_code}}
{{google-adsense placement="topic-above-post-stream"}}
{{/if}}
{{#if siteSettings.dfp_topic_above_post_stream_code}}
{{google-dfp-ad placement="topic-above-post-stream" refreshOnChange=model.id category=model.category.slug}}
{{/if}}
{{#if siteSettings.amazon_topic_above_post_stream_src_code}}
{{amazon-product-links placement="topic-above-post-stream"}}
{{#if site.mobileView}}
{{#if siteSettings.adsense_mobile_topic_above_post_stream_code}}
{{google-adsense placement="topic-above-post-stream"}}
{{/if}}
{{#if siteSettings.dfp_mobile_topic_above_post_stream_code}}
{{google-dfp-ad placement="topic-above-post-stream" refreshOnChange=model.id category=model.category.slug}}
{{/if}}
{{#if siteSettings.amazon_mobile_topic_above_post_stream_src_code}}
{{amazon-product-links placement="topic-above-post-stream"}}
{{/if}}
{{else}}
{{#if siteSettings.adsense_topic_above_post_stream_code}}
{{google-adsense placement="topic-above-post-stream"}}
{{/if}}
{{#if siteSettings.dfp_topic_above_post_stream_code}}
{{google-dfp-ad placement="topic-above-post-stream" refreshOnChange=model.id category=model.category.slug}}
{{/if}}
{{#if siteSettings.amazon_topic_above_post_stream_src_code}}
{{amazon-product-links placement="topic-above-post-stream"}}
{{/if}}
{{/if}}

View File

@ -1,9 +1,21 @@
{{#if siteSettings.adsense_topic_above_suggested_code}}
{{google-adsense placement="topic-above-suggested"}}
{{/if}}
{{#if siteSettings.dfp_topic_above_suggested_code}}
{{google-dfp-ad placement="topic-above-suggested" refreshOnChange=model.id category=model.category.slug}}
{{/if}}
{{#if siteSettings.amazon_topic_above_suggested_src_code}}
{{amazon-product-links placement="topic-above-suggested"}}
{{#if site.mobileView}}
{{#if siteSettings.adsense_mobile_topic_above_suggested_code}}
{{google-adsense placement="topic-above-suggested"}}
{{/if}}
{{#if siteSettings.dfp_mobile_topic_above_suggested_code}}
{{google-dfp-ad placement="topic-above-suggested" refreshOnChange=model.id category=model.category.slug}}
{{/if}}
{{#if siteSettings.amazon_mobile_topic_above_suggested_src_code}}
{{amazon-product-links placement="topic-above-suggested"}}
{{/if}}
{{else}}
{{#if siteSettings.adsense_topic_above_suggested_code}}
{{google-adsense placement="topic-above-suggested"}}
{{/if}}
{{#if siteSettings.dfp_topic_above_suggested_code}}
{{google-dfp-ad placement="topic-above-suggested" refreshOnChange=model.id category=model.category.slug}}
{{/if}}
{{#if siteSettings.amazon_topic_above_suggested_src_code}}
{{amazon-product-links placement="topic-above-suggested"}}
{{/if}}
{{/if}}

View File

@ -30,6 +30,12 @@ export default {
withPluginApi('0.1', api => {
api.decorateWidget('post:after', dec => {
if (dec.canConnectComponent) {
return dec.connect({ component: 'adplugin-container', context: 'model' });
}
// Old way for backwards compatibility
return dec.connect({
templateName: 'connectors/post-bottom/discourse-adplugin',
context: 'model'