main.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup Ads Ads
  6. * @ingroup UnaModules
  7. *
  8. * @{
  9. */
  10. function BxAdsMain(oOptions) {
  11. this._sActionsUri = oOptions.sActionUri;
  12. this._sActionsUrl = oOptions.sActionUrl;
  13. this._sObjName = oOptions.sObjName == undefined ? 'oBxAdsMain' : oOptions.sObjName;
  14. this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'slide' : oOptions.sAnimationEffect;
  15. this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
  16. this._aHtmlIds = oOptions.aHtmlIds == undefined ? {} : oOptions.aHtmlIds;
  17. this._oRequestParams = oOptions.oRequestParams == undefined ? {} : oOptions.oRequestParams;
  18. }
  19. BxAdsMain.prototype.registerTracker = function(iId) {
  20. var $this = this;
  21. var sIdAd = $this._aHtmlIds['unit'] + iId;
  22. var fRegister = function(bCheckOverlap) {
  23. if(typeof window.glBxAdsTracked === 'undefined')
  24. window.glBxAdsTracked = [];
  25. $(window).on('load scroll', function() {
  26. var sIdAd = $this._aHtmlIds['unit'] + iId;
  27. if(glBxAdsTracked[sIdAd])
  28. return;
  29. var oElement = document.getElementById(sIdAd);
  30. if(oElement && $this.isVisible(oElement, bCheckOverlap)) {
  31. $this.registerImpression(oElement, iId);
  32. glBxAdsTracked[sIdAd] = true;
  33. }
  34. });
  35. };
  36. var oItem = $('#' + sIdAd);
  37. if(oItem.has('img.bx-base-text-unit-cover').length != 0)
  38. oItem.find('img.bx-base-text-unit-cover').load(function() {
  39. fRegister(false);
  40. });
  41. else
  42. fRegister(true);
  43. };
  44. BxAdsMain.prototype.registerTrakerForTimeline = function(iId, sHtmlId) {
  45. var $this = this;
  46. if(typeof window.glBxAdsTracked === 'undefined')
  47. window.glBxAdsTracked = [];
  48. $(window).on('load scroll', function() {
  49. if(glBxAdsTracked[sHtmlId])
  50. return;
  51. var oEvent = $('#' + sHtmlId).parents('.bx-tl-item:first');
  52. if(!oEvent || oEvent.length == 0)
  53. return;
  54. oEvent = oEvent.get(0);
  55. if(oEvent && $this.isVisible(oEvent, true)) {
  56. $this.registerImpression(oEvent, iId);
  57. glBxAdsTracked[sHtmlId] = true;
  58. }
  59. });
  60. };
  61. BxAdsMain.prototype.registerImpression = function(oElement, iId) {
  62. jQuery.get (
  63. this._sActionsUrl + 'register_impression/' + iId,
  64. this._getDefaultData(),
  65. function(oData) {
  66. processJsonData(oData);
  67. },
  68. 'json'
  69. );
  70. };
  71. BxAdsMain.prototype.registerClick = function(oElement, iId) {
  72. jQuery.get (
  73. this._sActionsUrl + 'register_click/' + iId,
  74. this._getDefaultData(),
  75. function(oData) {
  76. processJsonData(oData);
  77. },
  78. 'json'
  79. );
  80. return false;
  81. };
  82. BxAdsMain.prototype.isVisible = function(oElement, bCheckOverlap) {
  83. var rect = oElement.getBoundingClientRect();
  84. var vWidth = window.innerWidth || document.documentElement.clientWidth;
  85. var vHeight = window.innerHeight || document.documentElement.clientHeight;
  86. var fEfp = function (x, y) {
  87. return document.elementFromPoint(x, y)
  88. };
  89. if(rect.right < 0 || rect.bottom < 0 || rect.left > vWidth || rect.top > vHeight)
  90. return false;
  91. if(!bCheckOverlap)
  92. return true
  93. return (oElement.contains(fEfp(rect.left, rect.top)) || oElement.contains(fEfp(rect.right, rect.top)) || oElement.contains(fEfp(rect.right, rect.bottom)) || oElement.contains(fEfp(rect.left, rect.bottom)));
  94. };
  95. BxAdsMain.prototype.loadingInBlock = function(e, bShow) {
  96. var oParent = $(e).length ? $(e).parents('.bx-db-container:first') : $('body');
  97. bx_loading(oParent, bShow);
  98. };
  99. BxAdsMain.prototype._getDefaultData = function() {
  100. var oDate = new Date();
  101. return jQuery.extend({}, this._oRequestParams, {_t:oDate.getTime()});
  102. };
  103. /** @} */