ATNativeJSSDK.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. checkAdStatus : function(placementId) {
  82. if (undefined != platformBridge && platformBridge != null) {
  83. return platformBridge.checkAdStatus(placementId);
  84. } else {
  85. cc.log("You must run on Android or iOS.");
  86. }
  87. return "";
  88. },
  89. showAd : function(placementId, adViewProperty) {
  90. if (undefined != platformBridge && platformBridge != null) {
  91. platformBridge.showAd(placementId, JSON.stringify(adViewProperty.getAdViewProperty()));
  92. } else {
  93. cc.log("You must run on Android or iOS.");
  94. }
  95. },
  96. showAdInScenario : function(placementId, adViewProperty, scenario="") {
  97. if (undefined != platformBridge && platformBridge != null) {
  98. platformBridge.showAdInScenario(placementId, JSON.stringify(adViewProperty.getAdViewProperty()), scenario);
  99. } else {
  100. cc.log("You must run on Android or iOS.");
  101. }
  102. },
  103. removeAd : function(placementId) {
  104. if (undefined != platformBridge && platformBridge != null) {
  105. platformBridge.removeAd(placementId);
  106. } else {
  107. cc.log("You must run on Android or iOS.");
  108. }
  109. },
  110. createLoadAdSize : function(width, height) {
  111. var loadAdSize = {};
  112. loadAdSize["width"] = width;
  113. loadAdSize["height"] = height;
  114. return loadAdSize;
  115. },
  116. AdViewProperty : cc.Class({
  117. parent : null,
  118. appIcon : null,
  119. mainImage: null,
  120. title:null,
  121. desc:null,
  122. adLogo:null,
  123. cta:null,
  124. rating:null,
  125. dislike:null,
  126. createItemViewProperty: function(x, y ,width ,height ,backgroundColor ,textColor ,textSize, isCustomClick = false) {
  127. var itemProperty = {};
  128. itemProperty["x"] = x;
  129. itemProperty["y"] = y;
  130. itemProperty["width"] = width;
  131. itemProperty["height"] = height;
  132. itemProperty["backgroundColor"] = backgroundColor;
  133. itemProperty["textColor"] = textColor;
  134. itemProperty["textSize"] = textSize;
  135. itemProperty["isCustomClick"] = isCustomClick;
  136. return itemProperty;
  137. },
  138. getAdViewProperty: function() {
  139. var nativeViewProperty = {};
  140. if(this.parent != null){
  141. nativeViewProperty["parent"] = this.parent;
  142. }
  143. if(this.appIcon != null){
  144. nativeViewProperty["icon"] = this.appIcon;
  145. }
  146. if(this.mainImage != null){
  147. nativeViewProperty["mainImage"] = this.mainImage;
  148. }
  149. if(this.title != null){
  150. nativeViewProperty["title"] = this.title;
  151. }
  152. if(this.desc != null){
  153. nativeViewProperty["desc"] = this.desc;
  154. }
  155. if(this.adLogo != null){
  156. nativeViewProperty["adLogo"] = this.adLogo;
  157. }
  158. if(this.cta != null){
  159. nativeViewProperty["cta"] = this.cta;
  160. }
  161. if(this.rating != null){
  162. nativeViewProperty["rating"] = this.rating;
  163. }
  164. if(this.dislike != null){
  165. nativeViewProperty["dislike"] = this.dislike;
  166. }
  167. return nativeViewProperty;
  168. }
  169. })
  170. };
  171. const LoadedCallbackKey = "NativeLoaded";
  172. const LoadFailCallbackKey = "NativeLoadFail";
  173. const CloseCallbackKey = "NativeCloseButtonTapped";
  174. const ClickCallbackKey = "NativeClick";
  175. const ShowCallbackKey = "NativeShow";
  176. const VideoStartKey = "NativeVideoStart";
  177. const VideoEndKey = "NativeVideoEnd";
  178. window.ATNativeJSSDK = ATNativeSDK;