WebView.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. "atomic component";
  2. // TODO: expose to inspector
  3. var WIDTH = Atomic.graphics.width - 100;
  4. var HEIGHT = Atomic.graphics.height - 100;
  5. var home = "http://www.atomicgameengine.com";
  6. var bookmarks = {
  7. "Atomic" : "http://www.atomicgameengine.com",
  8. "Google" : "http://www.google.com",
  9. "YouTube" : "https://www.youtube.com",
  10. "Steam" : "https://store.steampowered.com",
  11. "Reddit" : "https://www.reddit.com/r/gamedev",
  12. "Penny Arcade" : "https://www.penny-arcade.com/"
  13. };
  14. // Create the UI view
  15. var view = new Atomic.UIView();
  16. // enable to get debug window
  17. // Atomic.UI.debugShowSettingsWindow(view);
  18. var newTabButton;
  19. var newTabContent;
  20. // WebBrowser example component
  21. exports.component = function(self) {
  22. var window = new Atomic.UIWindow();
  23. window.text = "UIWebView Browser Example";
  24. window.setSize(WIDTH, HEIGHT);
  25. var mainLayout = new Atomic.UILayout();
  26. mainLayout.axis = Atomic.UI_AXIS_Y;
  27. mainLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  28. mainLayout.gravity = Atomic.UI_GRAVITY_ALL;
  29. var tabContainer = new Atomic.UITabContainer();
  30. tabContainer.gravity = Atomic.UI_GRAVITY_ALL;
  31. newTabButton = new Atomic.UIButton();
  32. newTabButton.text = " ";
  33. newTabButton.onClick = function() {
  34. createBrowserTab(tabContainer, home);
  35. // set the current page to the created page (ignoring the new tab dummy page)
  36. tabContainer.currentPage = tabContainer.numPages - 2;
  37. return true;
  38. };
  39. newTabContent = new Atomic.UIWidget();
  40. tabContainer.tabLayout.addChild(newTabButton);
  41. tabContainer.contentRoot.addChild(newTabContent);
  42. // change id, so we don't initiate a page change when clicked
  43. newTabButton.id = "NewTabButton";
  44. // create a startup with our home address
  45. createBrowserTab(tabContainer, home);
  46. tabContainer.currentPage = 0;
  47. // Add to main UI view and center
  48. mainLayout.addChild(tabContainer);
  49. window.addChild(mainLayout);
  50. view.addChild(window);
  51. window.center();
  52. };
  53. function createBookmarks(webView, layout, bookmarks) {
  54. for (var text in bookmarks) {
  55. var button = new Atomic.UIButton();
  56. button.text = text;
  57. button.skinBg = "TBButton.flat";
  58. // button layout and font desc
  59. var buttonLP = new Atomic.UILayoutParams();
  60. buttonLP.width = 120,
  61. buttonLP.height = 18;
  62. button.layoutParams = buttonLP;
  63. var fontDesc = new Atomic.UIFontDescription();
  64. fontDesc.size = 11;
  65. fontDesc.id = "Vera";
  66. button.fontDescription = fontDesc;
  67. layout.addChild(button);
  68. var webClient = webView.webClient;
  69. (function() {
  70. var url = bookmarks[text];
  71. button.onClick = function() { webClient.loadURL(url); };
  72. })();
  73. }
  74. }
  75. function createBrowserTab(tabContainer, url) {
  76. var contentRoot = tabContainer.contentRoot;
  77. var tabLayout = tabContainer.tabLayout;
  78. var layout = new Atomic.UILayout();
  79. layout.axis = Atomic.UI_AXIS_Y;
  80. layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  81. layout.gravity = Atomic.UI_GRAVITY_ALL;
  82. layout.spacing = 8;
  83. var tabButton = new Atomic.UIButton();
  84. tabButton.text = "Loading...";
  85. // button layout and font desc
  86. var buttonLP = new Atomic.UILayoutParams();
  87. buttonLP.width = 160,
  88. buttonLP.height = 32;
  89. tabButton.layoutParams = buttonLP;
  90. var fontDesc = new Atomic.UIFontDescription();
  91. fontDesc.size = 11;
  92. fontDesc.id = "Vera";
  93. tabButton.fontDescription = fontDesc;
  94. // tabButtons with URL text by default open the URL upon clicking them
  95. // we don't want this behaviour
  96. tabButton.urlEnabled = false;
  97. tabLayout.addChildBefore(tabButton, newTabButton);
  98. // address bar
  99. var addressLayout = new Atomic.UILayout();
  100. addressLayout.gravity = Atomic.UI_GRAVITY_ALL;
  101. addressLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  102. // navigation buttons
  103. var backButton = new Atomic.UIButton();
  104. backButton.text = "Back";
  105. addressLayout.addChild(backButton);
  106. var fwdButton = new Atomic.UIButton();
  107. fwdButton.text = "Forward";
  108. addressLayout.addChild(fwdButton);
  109. var reloadButton = new Atomic.UIButton();
  110. reloadButton.text = "Reload";
  111. addressLayout.addChild(reloadButton);
  112. var homeButton = new Atomic.UIButton();
  113. homeButton.text = "Home";
  114. addressLayout.addChild(homeButton);
  115. var addressEdit = new Atomic.UIEditField();
  116. addressEdit.gravity = Atomic.UI_GRAVITY_ALL;
  117. addressLayout.addChild(addressEdit);
  118. layout.addChild(addressLayout);
  119. var webView = new WebView.UIWebView(url);
  120. webView.gravity = Atomic.UI_GRAVITY_ALL;
  121. var webClient = webView.webClient;
  122. // bookmark bar
  123. var bookmarkLayout = new Atomic.UILayout();
  124. bookmarkLayout.gravity = Atomic.UI_GRAVITY_ALL;
  125. createBookmarks( webView, bookmarkLayout, bookmarks);
  126. layout.addChild(bookmarkLayout);
  127. layout.addChild(webView);
  128. contentRoot.addChildBefore(layout, newTabContent);
  129. // initial state
  130. reloadButton.disable();
  131. backButton.disable();
  132. fwdButton.disable();
  133. // go home
  134. homeButton.onClick = function() { webClient.loadURL(home);};
  135. // reload
  136. reloadButton.onClick = function() { webClient.reload(); };
  137. // Forward/Back
  138. fwdButton.onClick = function() { webClient.goForward(); };
  139. backButton.onClick = function() { webClient.goBack(); };
  140. // events
  141. // update the addressEdit at start and stop of url load
  142. webView.subscribeToEvent(webClient, "WebViewLoadStateChange", function(ev) {
  143. ev.canGoBack ? backButton.enable() : backButton.disable();
  144. ev.canGoForward ? fwdButton.enable() : fwdButton.disable();
  145. });
  146. // update the addressEdit at start and stop of url load
  147. webView.subscribeToEvent(webClient, "WebViewAddressChange", function(ev) {
  148. // we can now reload
  149. reloadButton.enable();
  150. addressEdit.text = ev.url;
  151. });
  152. webView.subscribeToEvent(webClient, "WebViewTitleChange", function(ev) {
  153. tabButton.text = ev.title;
  154. });
  155. webView.subscribeToEvent(webClient, "WebViewPopupRequest", function(ev) {
  156. webClient.loadURL(ev.url);
  157. });
  158. //this.subscribeToEvent(this.countEditField, "UIWidgetEditComplete", (ev) => this.handleUIWidgetEditCompleteEvent(ev));
  159. webView.subscribeToEvent(addressEdit, "UIWidgetEditComplete", function(ev) {
  160. var url = addressEdit.text;
  161. url = url.replace(" ", "%20");
  162. if (url.indexOf(".") == -1) {
  163. url = "http://www.google.com/search?q=" + url;
  164. }
  165. if (url.indexOf("://") == -1) {
  166. url = "https://" + url;
  167. }
  168. if (!url.length)
  169. return;
  170. webClient.loadURL(url);
  171. });
  172. }