From 1cd33da72266158108403109695219d45f3eea10 Mon Sep 17 00:00:00 2001 From: Eric Berry Date: Thu, 7 Jun 2018 17:17:19 -0600 Subject: [PATCH 1/5] Add CodeFund --- .../discourse/components/codefund-ad.js.es6 | 98 +++++++++++++++++++ .../components/adplugin-container.hbs | 10 ++ .../templates/components/codefund-ad.hbs | 29 ++++++ .../components/codefund/post-bottom.hbs | 8 ++ .../codefund/topic-above-post-stream.hbs | 11 +++ .../codefund/topic-above-suggested.hbs | 11 +++ .../components/codefund/topic-list-top.hbs | 11 +++ .../discourse-adplugin.hbs | 6 ++ .../discourse-adplugin.hbs | 6 ++ .../discourse-adplugin.hbs | 6 ++ .../initializers/initialize-ad-plugin.js.es6 | 21 ++-- config/locales/client.en.yml | 3 +- config/locales/server.en.yml | 11 +++ config/settings.yml | 37 ++++++- plugin.rb | 66 +++++++++++++ 15 files changed, 326 insertions(+), 8 deletions(-) create mode 100644 assets/javascripts/discourse/components/codefund-ad.js.es6 create mode 100644 assets/javascripts/discourse/templates/components/codefund-ad.hbs create mode 100644 assets/javascripts/discourse/templates/components/codefund/post-bottom.hbs create mode 100644 assets/javascripts/discourse/templates/components/codefund/topic-above-post-stream.hbs create mode 100644 assets/javascripts/discourse/templates/components/codefund/topic-above-suggested.hbs create mode 100644 assets/javascripts/discourse/templates/components/codefund/topic-list-top.hbs diff --git a/assets/javascripts/discourse/components/codefund-ad.js.es6 b/assets/javascripts/discourse/components/codefund-ad.js.es6 new file mode 100644 index 0000000..3a4307b --- /dev/null +++ b/assets/javascripts/discourse/components/codefund-ad.js.es6 @@ -0,0 +1,98 @@ +var _loaded = false, + _promise = null, + currentUser = Discourse.User.current(), + propertyId = Discourse.SiteSettings.codefund_property_code; + +function loadCodeFund() { + if (_loaded) { + return Ember.RSVP.resolve(); + } + + if (_promise) { + return _promise; + } + + const url = 'https://codefund.io/t/s/' + propertyId + '/details.json'; + + _promise = new Promise(function(resolve, reject){ + let xhr = new XMLHttpRequest(); + + xhr.open('GET', url); + xhr.onreadystatechange = handler; + xhr.responseType = 'json'; + xhr.setRequestHeader('Accept', 'application/json'); + xhr.send(); + + function handler() { + if (this.readyState === this.DONE) { + _loaded = true; + if (this.status === 200) { + resolve(this.response); + } else { + reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']')); + } + } + }; + }); + + return _promise; +} + + +export default Ember.Component.extend({ + classNameBindings: [':codefund-ad', 'classForSlot', 'isResponsive:codefund-responsive'], + propertyId: propertyId, + adRequested: false, + adDetails: {}, + + _triggerAds() { + this.set('adRequested', true); + loadCodeFund().then((data) => { + this.set('adDetails', data); + this.set('adRequested', false); + console.log('Loaded ad'); + }).catch(error => console.log(error)); + }, + + didInsertElement() { + this._super(); + + if (!this.get('showAd')) { return; } + + if (this.get('listLoading')) { return; } + + Ember.run.scheduleOnce('afterRender', this, this._triggerAds); + }, + + waitForLoad: function() { + if (this.get('adRequested')) { return; } // already requested that this ad unit be populated + if (!this.get('listLoading')) { + Ember.run.scheduleOnce('afterRender', this, this._triggerAds); + } + }.observes('listLoading'), + + checkTrustLevels: function() { + return !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.codefund_through_trust_level)); + }.property('trust_level'), + + showAd: function() { + return Discourse.SiteSettings.codefund_property_code && this.get('checkTrustLevels'); + }.property('checkTrustLevels'), + + displayPostBottom: function() { + return this.get('placement') === 'post-bottom'; + }.property('placement'), + + displayTopicAbovePostStream: function() { + return this.get('placement') === 'topic-above-post-stream'; + }.property('placement'), + + displayTopicAboveSuggested: function() { + return this.get('placement') === 'topic-above-suggested'; + }.property('placement'), + + displayTopicListTop: function() { + return this.get('placement') === 'topic-list-top'; + }.property('placement') + +}); diff --git a/assets/javascripts/discourse/templates/components/adplugin-container.hbs b/assets/javascripts/discourse/templates/components/adplugin-container.hbs index 65e1b28..bcffef5 100644 --- a/assets/javascripts/discourse/templates/components/adplugin-container.hbs +++ b/assets/javascripts/discourse/templates/components/adplugin-container.hbs @@ -14,6 +14,11 @@ {{amazon-product-links placement="post-bottom" postNumber=model.post_number}} {{/if}} {{/if}} + {{#if model.postSpecificCountCodeFund}} + {{#if siteSettings.codefund_below_post_enabled}} + {{codefund-ad placement="post-bottom" postNumber=model.post_number}} + {{/if}} + {{/if}} {{else}} {{#if model.postSpecificCountAdsense}} {{#if siteSettings.adsense_post_bottom_code}} @@ -30,4 +35,9 @@ {{amazon-product-links placement="post-bottom" postNumber=model.post_number}} {{/if}} {{/if}} + {{#if model.postSpecificCountCodeFund}} + {{#if siteSettings.codefund_below_post_enabled}} + {{codefund-ad placement="post-bottom" postNumber=model.post_number}} + {{/if}} + {{/if}} {{/if}} diff --git a/assets/javascripts/discourse/templates/components/codefund-ad.hbs b/assets/javascripts/discourse/templates/components/codefund-ad.hbs new file mode 100644 index 0000000..e89aac2 --- /dev/null +++ b/assets/javascripts/discourse/templates/components/codefund-ad.hbs @@ -0,0 +1,29 @@ +{{#if showAd}} + {{#if site.mobileView}} + {{#if displayPostBottom}} + {{partial "components/codefund/post-bottom"}} + {{/if}} + {{#if displayTopicAbovePostStream}} + {{partial "components/codefund/topic-above-post-stream"}} + {{/if}} + {{#if displayTopicAboveSuggested}} + {{partial "components/codefund/topic-above-suggested"}} + {{/if}} + {{#if displayTopicListTop}} + {{partial "components/codefund/topic-list-top"}} + {{/if}} + {{else}} + {{#if displayPostBottom}} + {{partial "components/codefund/post-bottom"}} + {{/if}} + {{#if displayTopicAbovePostStream}} + {{partial "components/codefund/topic-above-post-stream"}} + {{/if}} + {{#if displayTopicAboveSuggested}} + {{partial "components/codefund/topic-above-suggested"}} + {{/if}} + {{#if displayTopicListTop}} + {{partial "components/codefund/topic-list-top"}} + {{/if}} + {{/if}} +{{/if}} diff --git a/assets/javascripts/discourse/templates/components/codefund/post-bottom.hbs b/assets/javascripts/discourse/templates/components/codefund/post-bottom.hbs new file mode 100644 index 0000000..70461da --- /dev/null +++ b/assets/javascripts/discourse/templates/components/codefund/post-bottom.hbs @@ -0,0 +1,8 @@ + + + {{#if siteSettings.codefund_display_advertiser_labels}} + {{siteSettings.codefund_advertiser_short_label}} + {{/if}} + {{adDetails.headline}} {{adDetails.description}} + + diff --git a/assets/javascripts/discourse/templates/components/codefund/topic-above-post-stream.hbs b/assets/javascripts/discourse/templates/components/codefund/topic-above-post-stream.hbs new file mode 100644 index 0000000..cd6ea94 --- /dev/null +++ b/assets/javascripts/discourse/templates/components/codefund/topic-above-post-stream.hbs @@ -0,0 +1,11 @@ + + + {{#if siteSettings.codefund_display_advertiser_labels}} + {{siteSettings.codefund_advertiser_label}} + {{/if}} + {{adDetails.headline}} {{adDetails.description}} + + + ads via codefund.io + + diff --git a/assets/javascripts/discourse/templates/components/codefund/topic-above-suggested.hbs b/assets/javascripts/discourse/templates/components/codefund/topic-above-suggested.hbs new file mode 100644 index 0000000..7d0a19c --- /dev/null +++ b/assets/javascripts/discourse/templates/components/codefund/topic-above-suggested.hbs @@ -0,0 +1,11 @@ + + + {{#if siteSettings.codefund_display_advertiser_labels}} + {{siteSettings.codefund_advertiser_label}} + {{/if}} + {{adDetails.headline}} {{adDetails.description}} + + + ads via codefund.io + + diff --git a/assets/javascripts/discourse/templates/components/codefund/topic-list-top.hbs b/assets/javascripts/discourse/templates/components/codefund/topic-list-top.hbs new file mode 100644 index 0000000..7ac35a7 --- /dev/null +++ b/assets/javascripts/discourse/templates/components/codefund/topic-list-top.hbs @@ -0,0 +1,11 @@ + + + {{#if siteSettings.codefund_display_advertiser_labels}} + {{siteSettings.codefund_advertiser_label}} + {{/if}} + {{adDetails.headline}} {{adDetails.description}} + + + ads via codefund.io + + diff --git a/assets/javascripts/discourse/templates/connectors/discovery-list-container-top/discourse-adplugin.hbs b/assets/javascripts/discourse/templates/connectors/discovery-list-container-top/discourse-adplugin.hbs index af418d9..f0e9a5d 100644 --- a/assets/javascripts/discourse/templates/connectors/discovery-list-container-top/discourse-adplugin.hbs +++ b/assets/javascripts/discourse/templates/connectors/discovery-list-container-top/discourse-adplugin.hbs @@ -8,6 +8,9 @@ {{#if siteSettings.amazon_mobile_topic_list_top_src_code}} {{amazon-product-links placement="topic-list-top" listLoading=listLoading}} {{/if}} + {{#if siteSettings.codefund_top_of_topic_list_enabled}} + {{codefund-ad placement="topic-list-top" listLoading=listLoading refreshOnChange=listLoading}} + {{/if}} {{else}} {{#if siteSettings.adsense_topic_list_top_code}} {{google-adsense placement="topic-list-top" listLoading=listLoading}} @@ -18,4 +21,7 @@ {{#if siteSettings.amazon_topic_list_top_src_code}} {{amazon-product-links placement="topic-list-top" listLoading=listLoading}} {{/if}} + {{#if siteSettings.codefund_top_of_topic_list_enabled}} + {{codefund-ad placement="topic-list-top" listLoading=listLoading refreshOnChange=listLoading}} + {{/if}} {{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/discourse-adplugin.hbs b/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/discourse-adplugin.hbs index ed36964..e57429a 100644 --- a/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/discourse-adplugin.hbs +++ b/assets/javascripts/discourse/templates/connectors/topic-above-post-stream/discourse-adplugin.hbs @@ -8,6 +8,9 @@ {{#if siteSettings.amazon_mobile_topic_above_post_stream_src_code}} {{amazon-product-links placement="topic-above-post-stream"}} {{/if}} + {{#if siteSettings.codefund_above_post_stream_enabled}} + {{codefund-ad placement="topic-above-post-stream"}} + {{/if}} {{else}} {{#if siteSettings.adsense_topic_above_post_stream_code}} {{google-adsense placement="topic-above-post-stream"}} @@ -18,4 +21,7 @@ {{#if siteSettings.amazon_topic_above_post_stream_src_code}} {{amazon-product-links placement="topic-above-post-stream"}} {{/if}} + {{#if siteSettings.codefund_above_post_stream_enabled}} + {{codefund-ad placement="topic-above-post-stream"}} + {{/if}} {{/if}} \ No newline at end of file diff --git a/assets/javascripts/discourse/templates/connectors/topic-above-suggested/discourse-adplugin.hbs b/assets/javascripts/discourse/templates/connectors/topic-above-suggested/discourse-adplugin.hbs index 8e3f62c..8f45084 100644 --- a/assets/javascripts/discourse/templates/connectors/topic-above-suggested/discourse-adplugin.hbs +++ b/assets/javascripts/discourse/templates/connectors/topic-above-suggested/discourse-adplugin.hbs @@ -8,6 +8,9 @@ {{#if siteSettings.amazon_mobile_topic_above_suggested_src_code}} {{amazon-product-links placement="topic-above-suggested"}} {{/if}} + {{#if siteSettings.codefund_above_suggested_enabled}} + {{codefund-ad placement="topic-above-suggested"}} + {{/if}} {{else}} {{#if siteSettings.adsense_topic_above_suggested_code}} {{google-adsense placement="topic-above-suggested"}} @@ -18,4 +21,7 @@ {{#if siteSettings.amazon_topic_above_suggested_src_code}} {{amazon-product-links placement="topic-above-suggested"}} {{/if}} + {{#if siteSettings.codefund_above_suggested_enabled}} + {{codefund-ad placement="topic-above-suggested"}} + {{/if}} {{/if}} \ No newline at end of file diff --git a/assets/javascripts/initializers/initialize-ad-plugin.js.es6 b/assets/javascripts/initializers/initialize-ad-plugin.js.es6 index 74aac40..9e216fc 100644 --- a/assets/javascripts/initializers/initialize-ad-plugin.js.es6 +++ b/assets/javascripts/initializers/initialize-ad-plugin.js.es6 @@ -1,5 +1,7 @@ import PostModel from 'discourse/models/post'; -import { withPluginApi } from 'discourse/lib/plugin-api'; +import { + withPluginApi +} from 'discourse/lib/plugin-api'; export default { name: 'initialize-ad-plugin', @@ -7,19 +9,23 @@ export default { const siteSettings = container.lookup('site-settings:main'); PostModel.reopen({ - postSpecificCountDFP: function() { + postSpecificCountDFP: function () { return this.isNthPost(parseInt(siteSettings.dfp_nth_post_code)); }.property('post_number'), - postSpecificCountAdsense: function() { + postSpecificCountAdsense: function () { return this.isNthPost(parseInt(siteSettings.adsense_nth_post_code)); }.property('post_number'), - postSpecificCountAmazon: function() { + postSpecificCountAmazon: function () { return this.isNthPost(parseInt(siteSettings.amazon_nth_post_code)); }.property('post_number'), - isNthPost: function(n) { + postSpecificCountCodeFund: function () { + return this.isNthPost(parseInt(siteSettings.codefund_nth_post_code)); + }.property('post_number'), + + isNthPost: function (n) { if (n && n > 0) { return (this.get('post_number') % n) === 0; } else { @@ -32,7 +38,10 @@ export default { api.decorateWidget('post:after', dec => { if (dec.canConnectComponent) { - return dec.connect({ component: 'adplugin-container', context: 'model' }); + return dec.connect({ + component: 'adplugin-container', + context: 'model' + }); } // Old way for backwards compatibility diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index bb8ba7d..876dfc1 100755 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -8,4 +8,5 @@ en: categories: dfp_plugin: 'DFP' adsense_plugin: 'Adsense' - amazon_plugin: 'Amazon' \ No newline at end of file + amazon_plugin: 'Amazon' + codefund_plugin: 'CodeFund' \ No newline at end of file diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index b3f15f4..0e659ea 100755 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -78,3 +78,14 @@ en: amazon_mobile_post_bottom_ad_width_code: "Input your ad width (mobile)" amazon_mobile_post_bottom_ad_height_code: "Input your ad height (mobile)" amazon_nth_post_code: "Show an ad after every N posts, where N is this value." + + codefund_property_code: "Your CodeFund property ID" + codefund_advertiser_label: "Label that appears before the advertisement (e.g. Advertiser or Supporter)" + codefund_advertiser_short_label: "Abbreviated label that appears before the advertisement (e.g. Ad)" + codefund_display_advertiser_labels: "Show the advertiser label (e.g. 'Advertiser') on the ads" + codefund_through_trust_level: "Show your ads to users based on trust levels. Users with trust level higher than this value will not see ads" + codefund_nth_post_code: "Show an ad after every N posts, where N is this value" + codefund_below_post_enabled: "Show an ad below each blog post" + codefund_above_post_stream_enabled: "Show an ad above the post stream" + codefund_above_suggested_enabled: "Show an ad above the suggested topic list" + codefund_top_of_topic_list_enabled: "Show an ad above the topic list" diff --git a/config/settings.yml b/config/settings.yml index 1d119d8..10cade2 100755 --- a/config/settings.yml +++ b/config/settings.yml @@ -340,4 +340,39 @@ amazon_plugin: default: '' amazon_mobile_post_bottom_ad_height_code: client: true - default: '' \ No newline at end of file + default: '' + +codefund_plugin: + codefund_property_code: + client: true + default: '' + regex: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' + codefund_advertiser_label: + client: true + default: 'Advertiser' + codefund_advertiser_short_label: + client: true + default: 'Ad' + codefund_through_trust_level: + client: true + default: 2 + enum: 'TrustLevelSetting' + codefund_nth_post_code: + client: true + default: 4 + min: 1 + codefund_display_advertiser_labels: + default: true + client: true + codefund_below_post_enabled: + default: true + client: true + codefund_above_post_stream_enabled: + default: true + client: true + codefund_above_suggested_enabled: + default: true + client: true + codefund_top_of_topic_list_enabled: + default: true + client: true diff --git a/plugin.rb b/plugin.rb index d560dac..cff4605 100755 --- a/plugin.rb +++ b/plugin.rb @@ -6,6 +6,9 @@ register_css < Date: Thu, 7 Jun 2018 17:28:20 -0600 Subject: [PATCH 2/5] Cleaned up debugs --- assets/javascripts/discourse/components/codefund-ad.js.es6 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/assets/javascripts/discourse/components/codefund-ad.js.es6 b/assets/javascripts/discourse/components/codefund-ad.js.es6 index 3a4307b..666a0e3 100644 --- a/assets/javascripts/discourse/components/codefund-ad.js.es6 +++ b/assets/javascripts/discourse/components/codefund-ad.js.es6 @@ -7,7 +7,7 @@ function loadCodeFund() { if (_loaded) { return Ember.RSVP.resolve(); } - + if (_promise) { return _promise; } @@ -26,6 +26,7 @@ function loadCodeFund() { function handler() { if (this.readyState === this.DONE) { _loaded = true; + if (this.status === 200) { resolve(this.response); } else { @@ -48,9 +49,10 @@ export default Ember.Component.extend({ _triggerAds() { this.set('adRequested', true); loadCodeFund().then((data) => { + _loaded = false; + _promise = null; this.set('adDetails', data); this.set('adRequested', false); - console.log('Loaded ad'); }).catch(error => console.log(error)); }, From 11a621fd50d9626f33230c09afafdefb7b026c75 Mon Sep 17 00:00:00 2001 From: Eric Berry Date: Thu, 7 Jun 2018 17:49:15 -0600 Subject: [PATCH 3/5] Only display ad if content exists --- .../discourse/components/codefund-ad.js.es6 | 8 ++- .../templates/components/codefund-ad.hbs | 54 ++++++++++--------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/assets/javascripts/discourse/components/codefund-ad.js.es6 b/assets/javascripts/discourse/components/codefund-ad.js.es6 index 666a0e3..b45ffba 100644 --- a/assets/javascripts/discourse/components/codefund-ad.js.es6 +++ b/assets/javascripts/discourse/components/codefund-ad.js.es6 @@ -26,7 +26,7 @@ function loadCodeFund() { function handler() { if (this.readyState === this.DONE) { _loaded = true; - + if (this.status === 200) { resolve(this.response); } else { @@ -95,6 +95,10 @@ export default Ember.Component.extend({ displayTopicListTop: function() { return this.get('placement') === 'topic-list-top'; - }.property('placement') + }.property('placement'), + + hasAd: function() { + return ((this.get('adDetails.description') || '').length > 0); + }.property('adDetails') }); diff --git a/assets/javascripts/discourse/templates/components/codefund-ad.hbs b/assets/javascripts/discourse/templates/components/codefund-ad.hbs index e89aac2..3902d2b 100644 --- a/assets/javascripts/discourse/templates/components/codefund-ad.hbs +++ b/assets/javascripts/discourse/templates/components/codefund-ad.hbs @@ -1,29 +1,31 @@ {{#if showAd}} - {{#if site.mobileView}} - {{#if displayPostBottom}} - {{partial "components/codefund/post-bottom"}} - {{/if}} - {{#if displayTopicAbovePostStream}} - {{partial "components/codefund/topic-above-post-stream"}} - {{/if}} - {{#if displayTopicAboveSuggested}} - {{partial "components/codefund/topic-above-suggested"}} - {{/if}} - {{#if displayTopicListTop}} - {{partial "components/codefund/topic-list-top"}} - {{/if}} - {{else}} - {{#if displayPostBottom}} - {{partial "components/codefund/post-bottom"}} - {{/if}} - {{#if displayTopicAbovePostStream}} - {{partial "components/codefund/topic-above-post-stream"}} - {{/if}} - {{#if displayTopicAboveSuggested}} - {{partial "components/codefund/topic-above-suggested"}} - {{/if}} - {{#if displayTopicListTop}} - {{partial "components/codefund/topic-list-top"}} + {{#if hasAd}} + {{#if site.mobileView}} + {{#if displayPostBottom}} + {{partial "components/codefund/post-bottom"}} + {{/if}} + {{#if displayTopicAbovePostStream}} + {{partial "components/codefund/topic-above-post-stream"}} + {{/if}} + {{#if displayTopicAboveSuggested}} + {{partial "components/codefund/topic-above-suggested"}} + {{/if}} + {{#if displayTopicListTop}} + {{partial "components/codefund/topic-list-top"}} + {{/if}} + {{else}} + {{#if displayPostBottom}} + {{partial "components/codefund/post-bottom"}} + {{/if}} + {{#if displayTopicAbovePostStream}} + {{partial "components/codefund/topic-above-post-stream"}} + {{/if}} + {{#if displayTopicAboveSuggested}} + {{partial "components/codefund/topic-above-suggested"}} + {{/if}} + {{#if displayTopicListTop}} + {{partial "components/codefund/topic-list-top"}} + {{/if}} {{/if}} {{/if}} -{{/if}} +{{/if}} \ No newline at end of file From 1b7f642da6f95e4f32d723e4ec98e806e158f23d Mon Sep 17 00:00:00 2001 From: Eric Berry Date: Tue, 12 Jun 2018 15:21:23 -0600 Subject: [PATCH 4/5] Cleanup --- .../discourse/components/codefund-ad.js.es6 | 4 -- .../templates/components/codefund-ad.hbs | 52 +++++++++---------- 2 files changed, 25 insertions(+), 31 deletions(-) diff --git a/assets/javascripts/discourse/components/codefund-ad.js.es6 b/assets/javascripts/discourse/components/codefund-ad.js.es6 index b45ffba..9bb428e 100644 --- a/assets/javascripts/discourse/components/codefund-ad.js.es6 +++ b/assets/javascripts/discourse/components/codefund-ad.js.es6 @@ -97,8 +97,4 @@ export default Ember.Component.extend({ return this.get('placement') === 'topic-list-top'; }.property('placement'), - hasAd: function() { - return ((this.get('adDetails.description') || '').length > 0); - }.property('adDetails') - }); diff --git a/assets/javascripts/discourse/templates/components/codefund-ad.hbs b/assets/javascripts/discourse/templates/components/codefund-ad.hbs index 3902d2b..1c0f80d 100644 --- a/assets/javascripts/discourse/templates/components/codefund-ad.hbs +++ b/assets/javascripts/discourse/templates/components/codefund-ad.hbs @@ -1,31 +1,29 @@ {{#if showAd}} - {{#if hasAd}} - {{#if site.mobileView}} - {{#if displayPostBottom}} - {{partial "components/codefund/post-bottom"}} - {{/if}} - {{#if displayTopicAbovePostStream}} - {{partial "components/codefund/topic-above-post-stream"}} - {{/if}} - {{#if displayTopicAboveSuggested}} - {{partial "components/codefund/topic-above-suggested"}} - {{/if}} - {{#if displayTopicListTop}} - {{partial "components/codefund/topic-list-top"}} - {{/if}} - {{else}} - {{#if displayPostBottom}} - {{partial "components/codefund/post-bottom"}} - {{/if}} - {{#if displayTopicAbovePostStream}} - {{partial "components/codefund/topic-above-post-stream"}} - {{/if}} - {{#if displayTopicAboveSuggested}} - {{partial "components/codefund/topic-above-suggested"}} - {{/if}} - {{#if displayTopicListTop}} - {{partial "components/codefund/topic-list-top"}} - {{/if}} + {{#if site.mobileView}} + {{#if displayPostBottom}} + {{partial "components/codefund/post-bottom"}} + {{/if}} + {{#if displayTopicAbovePostStream}} + {{partial "components/codefund/topic-above-post-stream"}} + {{/if}} + {{#if displayTopicAboveSuggested}} + {{partial "components/codefund/topic-above-suggested"}} + {{/if}} + {{#if displayTopicListTop}} + {{partial "components/codefund/topic-list-top"}} + {{/if}} + {{else}} + {{#if displayPostBottom}} + {{partial "components/codefund/post-bottom"}} + {{/if}} + {{#if displayTopicAbovePostStream}} + {{partial "components/codefund/topic-above-post-stream"}} + {{/if}} + {{#if displayTopicAboveSuggested}} + {{partial "components/codefund/topic-above-suggested"}} + {{/if}} + {{#if displayTopicListTop}} + {{partial "components/codefund/topic-list-top"}} {{/if}} {{/if}} {{/if}} \ No newline at end of file From 9e84a89f767604260aa64e810d9c8dc518d038cc Mon Sep 17 00:00:00 2001 From: Eric Berry Date: Wed, 20 Jun 2018 09:15:15 -0600 Subject: [PATCH 5/5] Refactors per review --- .../discourse/components/codefund-ad.js.es6 | 34 ++++++++++++------- .../initializers/initialize-ad-plugin.js.es6 | 12 +++---- config/locales/server.en.yml | 4 +-- config/settings.yml | 4 +-- 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/assets/javascripts/discourse/components/codefund-ad.js.es6 b/assets/javascripts/discourse/components/codefund-ad.js.es6 index 9bb428e..46e985a 100644 --- a/assets/javascripts/discourse/components/codefund-ad.js.es6 +++ b/assets/javascripts/discourse/components/codefund-ad.js.es6 @@ -1,20 +1,22 @@ +import { default as computed, observes } from 'ember-addons/ember-computed-decorators'; + var _loaded = false, _promise = null, currentUser = Discourse.User.current(), - propertyId = Discourse.SiteSettings.codefund_property_code; + propertyId = Discourse.SiteSettings.codefund_property_id; function loadCodeFund() { if (_loaded) { return Ember.RSVP.resolve(); } - + if (_promise) { return _promise; } const url = 'https://codefund.io/t/s/' + propertyId + '/details.json'; - _promise = new Promise(function(resolve, reject){ + _promise = new Promise(function(resolve, reject) { let xhr = new XMLHttpRequest(); xhr.open('GET', url); @@ -66,35 +68,41 @@ export default Ember.Component.extend({ Ember.run.scheduleOnce('afterRender', this, this._triggerAds); }, + @observes('listLoading') waitForLoad: function() { if (this.get('adRequested')) { return; } // already requested that this ad unit be populated if (!this.get('listLoading')) { Ember.run.scheduleOnce('afterRender', this, this._triggerAds); } - }.observes('listLoading'), + }, checkTrustLevels: function() { return !((currentUser) && (currentUser.get('trust_level') > Discourse.SiteSettings.codefund_through_trust_level)); }.property('trust_level'), - showAd: function() { - return Discourse.SiteSettings.codefund_property_code && this.get('checkTrustLevels'); - }.property('checkTrustLevels'), + @computed('checkTrustLevels') + showAd: function(checkTrustLevels) { + return Discourse.SiteSettings.codefund_property_id && checkTrustLevels; + }, - displayPostBottom: function() { - return this.get('placement') === 'post-bottom'; - }.property('placement'), + @computed('placement') + displayPostBottom: function(placement) { + return placement === 'post-bottom'; + }, + @computed('placement') displayTopicAbovePostStream: function() { return this.get('placement') === 'topic-above-post-stream'; - }.property('placement'), + }, + @computed('placement') displayTopicAboveSuggested: function() { return this.get('placement') === 'topic-above-suggested'; - }.property('placement'), + }, + @computed('placement') displayTopicListTop: function() { return this.get('placement') === 'topic-list-top'; - }.property('placement'), + } }); diff --git a/assets/javascripts/initializers/initialize-ad-plugin.js.es6 b/assets/javascripts/initializers/initialize-ad-plugin.js.es6 index 9e216fc..abc2274 100644 --- a/assets/javascripts/initializers/initialize-ad-plugin.js.es6 +++ b/assets/javascripts/initializers/initialize-ad-plugin.js.es6 @@ -9,23 +9,23 @@ export default { const siteSettings = container.lookup('site-settings:main'); PostModel.reopen({ - postSpecificCountDFP: function () { + postSpecificCountDFP: function() { return this.isNthPost(parseInt(siteSettings.dfp_nth_post_code)); }.property('post_number'), - postSpecificCountAdsense: function () { + postSpecificCountAdsense: function() { return this.isNthPost(parseInt(siteSettings.adsense_nth_post_code)); }.property('post_number'), - postSpecificCountAmazon: function () { + postSpecificCountAmazon: function() { return this.isNthPost(parseInt(siteSettings.amazon_nth_post_code)); }.property('post_number'), - postSpecificCountCodeFund: function () { - return this.isNthPost(parseInt(siteSettings.codefund_nth_post_code)); + postSpecificCountCodeFund: function() { + return this.isNthPost(parseInt(siteSettings.codefund_nth_post)); }.property('post_number'), - isNthPost: function (n) { + isNthPost: function(n) { if (n && n > 0) { return (this.get('post_number') % n) === 0; } else { diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 0e659ea..18d8275 100755 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -79,12 +79,12 @@ en: amazon_mobile_post_bottom_ad_height_code: "Input your ad height (mobile)" amazon_nth_post_code: "Show an ad after every N posts, where N is this value." - codefund_property_code: "Your CodeFund property ID" + codefund_property_id: "Your CodeFund property ID" codefund_advertiser_label: "Label that appears before the advertisement (e.g. Advertiser or Supporter)" codefund_advertiser_short_label: "Abbreviated label that appears before the advertisement (e.g. Ad)" codefund_display_advertiser_labels: "Show the advertiser label (e.g. 'Advertiser') on the ads" codefund_through_trust_level: "Show your ads to users based on trust levels. Users with trust level higher than this value will not see ads" - codefund_nth_post_code: "Show an ad after every N posts, where N is this value" + codefund_nth_post: "Show an ad after every N posts, where N is this value" codefund_below_post_enabled: "Show an ad below each blog post" codefund_above_post_stream_enabled: "Show an ad above the post stream" codefund_above_suggested_enabled: "Show an ad above the suggested topic list" diff --git a/config/settings.yml b/config/settings.yml index 10cade2..944f62b 100755 --- a/config/settings.yml +++ b/config/settings.yml @@ -343,7 +343,7 @@ amazon_plugin: default: '' codefund_plugin: - codefund_property_code: + codefund_property_id: client: true default: '' regex: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' @@ -357,7 +357,7 @@ codefund_plugin: client: true default: 2 enum: 'TrustLevelSetting' - codefund_nth_post_code: + codefund_nth_post: client: true default: 4 min: 1