ATNativeJSSDK.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. var ATAndroidNativeJS = require("./Android/ATAndroidNativeJS");
  2. var ATiOSNativeJS = require("./iOS/ATiOSNativeJS");
  3. var initPlatformBridge = function() {
  4. if (cc.sys.os === cc.sys.OS_IOS) {
  5. return ATiOSNativeJS;
  6. } else if (cc.sys.os === cc.sys.OS_ANDROID) {
  7. return ATAndroidNativeJS;
  8. }
  9. };
  10. var platformBridge = initPlatformBridge();
  11. var ATNativeSDK = ATNativeSDK || {
  12. ATNativeListener : {
  13. developerCallback : null,
  14. onNativeAdLoaded : function (placementId) {
  15. if(this.developerCallback != null && this.developerCallback.onNativeAdLoaded != null && undefined != this.developerCallback.onNativeAdLoaded) {
  16. this.developerCallback.onNativeAdLoaded(placementId);
  17. }
  18. },
  19. onNativeAdLoadFail : function(placementId, errorInfo) {
  20. if(this.developerCallback != null && this.developerCallback.onNativeAdLoadFail != null && undefined != this.developerCallback.onNativeAdLoadFail) {
  21. this.developerCallback.onNativeAdLoadFail(placementId, errorInfo);
  22. }
  23. },
  24. onNativeAdShow : function(placementId, callbackInfo) {
  25. if(this.developerCallback != null && this.developerCallback.onNativeAdShow != null && undefined != this.developerCallback.onNativeAdShow) {
  26. this.developerCallback.onNativeAdShow(placementId, callbackInfo);
  27. }
  28. },
  29. onNativeAdClick : function(placementId, callbackInfo) {
  30. if(this.developerCallback != null && this.developerCallback.onNativeAdClick != null && undefined != this.developerCallback.onNativeAdClick) {
  31. this.developerCallback.onNativeAdClick(placementId, callbackInfo);
  32. }
  33. },
  34. onNativeAdVideoStart : function(placementId) {
  35. if(this.developerCallback != null && this.developerCallback.onNativeAdVideoStart != null && undefined != this.developerCallback.onNativeAdVideoStart) {
  36. this.developerCallback.onNativeAdVideoStart(placementId,);
  37. }
  38. },
  39. onNativeAdVideoEnd : function(placementId) {
  40. if(this.developerCallback != null && this.developerCallback.onNativeAdVideoEnd != null && undefined !=this.developerCallback.onNativeAdVideoEnd) {
  41. this.developerCallback.onNativeAdVideoEnd(placementId);
  42. }
  43. },
  44. onNativeAdCloseButtonTapped : function(placementId, callbackInfo) {
  45. if(this.developerCallback != null && this.developerCallback.onNativeAdCloseButtonTapped != null && undefined != this.developerCallback.onNativeAdCloseButtonTapped) {
  46. this.developerCallback.onNativeAdCloseButtonTapped(placementId, callbackInfo);
  47. }
  48. }
  49. },
  50. loadNative : function(placementId, settings={}) {
  51. if (undefined != platformBridge && platformBridge != null) {
  52. platformBridge.loadNative(placementId, JSON.stringify(settings));
  53. } else {
  54. cc.log("You must run on Android or iOS.");
  55. }
  56. },
  57. setAdListener : function(listener) {
  58. var eventJSON = {};
  59. eventJSON[LoadedCallbackKey]="ATNativeJSSDK.ATNativeListener.onNativeAdLoaded",
  60. eventJSON[LoadFailCallbackKey]= "ATNativeJSSDK.ATNativeListener.onNativeAdLoadFail",
  61. eventJSON[CloseCallbackKey]="ATNativeJSSDK.ATNativeListener.onNativeAdCloseButtonTapped",
  62. eventJSON[ClickCallbackKey]= "ATNativeJSSDK.ATNativeListener.onNativeAdClick",
  63. eventJSON[ShowCallbackKey]= "ATNativeJSSDK.ATNativeListener.onNativeAdShow",
  64. eventJSON[VideoStartKey]= "ATNativeJSSDK.ATNativeListener.onNativeAdVideoStart",
  65. eventJSON[VideoEndKey]= "ATNativeJSSDK.ATNativeListener.onNativeAdVideoEnd"
  66. if (undefined != platformBridge && platformBridge != null) {
  67. platformBridge.setAdListener(JSON.stringify(eventJSON));
  68. } else {
  69. cc.log("You must run on Android or iOS.");
  70. }
  71. this.ATNativeListener.developerCallback = listener;
  72. },
  73. hasAdReady : function(placementId) {
  74. if (undefined != platformBridge && platformBridge != null) {
  75. return platformBridge.hasAdReady(placementId);
  76. } else {
  77. cc.log("You must run on Android or iOS.");
  78. }
  79. return false;
  80. },
  81. showAd : function(placementId, adViewProperty) {
  82. if (undefined != platformBridge && platformBridge != null) {
  83. platformBridge.showAd(placementId, JSON.stringify(adViewProperty.getAdViewProperty()));
  84. } else {
  85. cc.log("You must run on Android or iOS.");
  86. }
  87. },
  88. removeAd : function(placementId) {
  89. if (undefined != platformBridge && platformBridge != null) {
  90. platformBridge.removeAd(placementId);
  91. } else {
  92. cc.log("You must run on Android or iOS.");
  93. }
  94. },
  95. createLoadAdSize : function(width, height) {
  96. var loadAdSize = {};
  97. loadAdSize["width"] = width;
  98. loadAdSize["height"] = height;
  99. return loadAdSize;
  100. },
  101. AdViewProperty : cc.Class({
  102. parent : null,
  103. appIcon : null,
  104. mainImage: null,
  105. title:null,
  106. desc:null,
  107. adLogo:null,
  108. cta:null,
  109. rating:null,
  110. createItemViewProperty: function(x, y ,width ,height ,backgroundColor ,textColor ,textSize) {
  111. var itemProperty = {};
  112. itemProperty["x"] = x;
  113. itemProperty["y"] = y;
  114. itemProperty["width"] = width;
  115. itemProperty["height"] = height;
  116. itemProperty["backgroundColor"] = backgroundColor;
  117. itemProperty["textColor"] = textColor;
  118. itemProperty["textSize"] = textSize;
  119. return itemProperty;
  120. },
  121. getAdViewProperty: function() {
  122. var nativeViewProperty = {};
  123. if(this.parent != null){
  124. nativeViewProperty["parent"] = this.parent;
  125. }
  126. if(this.appIcon != null){
  127. nativeViewProperty["icon"] = this.appIcon;
  128. }
  129. if(this.mainImage != null){
  130. nativeViewProperty["mainImage"] = this.mainImage;
  131. }
  132. if(this.title != null){
  133. nativeViewProperty["title"] = this.title;
  134. }
  135. if(this.desc != null){
  136. nativeViewProperty["desc"] = this.desc;
  137. }
  138. if(this.adLogo != null){
  139. nativeViewProperty["adLogo"] = this.adLogo;
  140. }
  141. if(this.cta != null){
  142. nativeViewProperty["cta"] = this.cta;
  143. }
  144. if(this.rating != null){
  145. nativeViewProperty["rating"] = this.rating;
  146. }
  147. return nativeViewProperty;
  148. }
  149. })
  150. };
  151. const LoadedCallbackKey = "NativeLoaded";
  152. const LoadFailCallbackKey = "NativeLoadFail";
  153. const CloseCallbackKey = "NativeCloseButtonTapped";
  154. const ClickCallbackKey = "NativeClick";
  155. const ShowCallbackKey = "NativeShow";
  156. const VideoStartKey = "NativeVideoStart";
  157. const VideoEndKey = "NativeVideoEnd";
  158. window.ATNativeJSSDK = ATNativeSDK;