discourse-adplugin/assets/javascripts/discourse/components/adbutler-ad.js.es6

152 lines
3.5 KiB
Plaintext
Raw Normal View History

2019-09-16 14:26:17 -04:00
import AdComponent from "discourse/plugins/discourse-adplugin/discourse/components/ad-component";
2019-09-16 14:35:58 -04:00
import {
default as computed,
observes
} from "ember-addons/ember-computed-decorators";
2019-09-16 14:26:17 -04:00
import loadScript from "discourse/lib/load-script";
const publisherId = Discourse.SiteSettings.adbutler_publisher_id;
const adserverHostname = Discourse.SiteSettings.adbutler_adserver_hostname;
let _loaded = false,
2019-09-16 14:35:58 -04:00
_promise = null,
_c = 0;
2019-09-16 14:26:17 -04:00
function loadAdbutler() {
if (_loaded) {
return Ember.RSVP.resolve();
}
if (_promise) {
return _promise;
}
2019-09-16 14:35:58 -04:00
_promise = loadScript("https://" + adserverHostname + "/app.js", {
scriptTag: true
}).then(function() {
2019-09-16 14:26:17 -04:00
_loaded = true;
});
return _promise;
}
export default AdComponent.extend({
divs: null,
init() {
2019-09-16 14:35:58 -04:00
let dimensions = [728, 90];
let configKey = "adbutler_";
let className = "adbutler-";
let dimClassName = "adbutler-ad";
2019-09-16 14:26:17 -04:00
2019-09-16 14:35:58 -04:00
this.set("divs", []);
2019-09-16 14:26:17 -04:00
if (this.site.mobileView) {
2019-09-16 14:35:58 -04:00
dimensions = [320, 50];
configKey += "mobile_";
className += "mobile-";
dimClassName = "adbutler-mobile-ad";
2019-09-16 14:26:17 -04:00
}
2019-09-16 14:35:58 -04:00
configKey += this.get("placement").replace(/-/g, "_") + "_zone_id";
2019-09-16 14:26:17 -04:00
this.set("configKey", configKey);
className += this.get("placement");
2019-09-16 14:35:58 -04:00
this.set("className", className + " " + dimClassName);
2019-09-16 14:26:17 -04:00
let zoneId = this.siteSettings[configKey];
this.set("zoneId", zoneId);
2019-09-16 14:35:58 -04:00
let divId = "placement-" + zoneId + "-" + _c;
2019-09-16 14:26:17 -04:00
this.set("divId", divId);
_c++;
this.divs.push({
divId: divId,
publisherId: publisherId,
zoneId: zoneId,
dimensions: dimensions
});
this.set("publisherId", publisherId);
this._super();
},
_triggerAds() {
if (Ember.testing) {
return; // Don't load external JS during tests
}
2019-09-16 14:35:58 -04:00
loadAdbutler().then(
function() {
if (this.divs.length > 0) {
let abkw = window.abkw || "";
window.AdButler.ads.push({
2019-09-16 14:35:58 -04:00
handler: function(opt) {
window.AdButler.register(
2019-09-16 14:35:58 -04:00
opt.place.publisherId,
opt.place.zoneId,
opt.place.dimensions,
opt.place.divId,
opt
);
},
opt: {
place: this.divs.pop(),
keywords: abkw,
domain: adserverHostname,
click: "CLICK_MACRO_PLACEHOLDER"
}
});
}
}.bind(this)
);
2019-09-16 14:26:17 -04:00
},
didInsertElement() {
this._super();
Ember.run.scheduleOnce("afterRender", this, this._triggerAds);
},
@observes("listLoading")
waitForLoad() {
if (this.get("adRequested")) {
return;
} // already requested that this ad unit be populated
if (!this.get("listLoading")) {
Ember.run.scheduleOnce("afterRender", this, this._triggerAds);
}
},
@computed("currentUser.trust_level")
showToTrustLevel(trustLevel) {
return !(
trustLevel &&
trustLevel > Discourse.SiteSettings.adbutler_through_trust_level
);
},
@computed(
2019-09-16 14:35:58 -04:00
"showToTrustLevel",
"showToGroups",
2019-09-16 14:26:17 -04:00
"showAfterPost",
2019-09-16 14:35:58 -04:00
"showOnCurrentPage"
2019-09-16 14:26:17 -04:00
)
showAd(showToTrustLevel, showToGroups, showAfterPost, showOnCurrentPage) {
return (
publisherId &&
showToTrustLevel &&
showToGroups &&
showAfterPost &&
showOnCurrentPage
);
},
@computed("postNumber")
showAfterPost(postNumber) {
if (!postNumber) {
return true;
}
return this.isNthPost(parseInt(this.siteSettings.adbutler_nth_post, 10));
2019-09-16 14:26:17 -04:00
}
});