WebView.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 Game Engine" : "http://www.atomicgameengine.com",
  8. "Google" : "http://www.google.com",
  9. "YouTube" : "https://www.youtube.com"
  10. };
  11. // Create the UI view
  12. var view = new Atomic.UIView();
  13. var newTabButton;
  14. var newTabContent;
  15. // WebBrowser example component
  16. exports.component = function(self) {
  17. var window = new Atomic.UIWindow();
  18. window.text = "UIWebView Example Browser";
  19. window.setSize(WIDTH, HEIGHT);
  20. var mainLayout = new Atomic.UILayout();
  21. mainLayout.axis = Atomic.UI_AXIS_Y;
  22. mainLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  23. mainLayout.gravity = Atomic.UI_GRAVITY_ALL;
  24. var tabContainer = new Atomic.UITabContainer();
  25. tabContainer.gravity = Atomic.UI_GRAVITY_ALL;
  26. newTabButton = new Atomic.UIButton();
  27. newTabButton.text = " ";
  28. newTabButton.onClick = function() {
  29. createBrowserTab(tabContainer, home);
  30. return true;
  31. }
  32. newTabContent = new Atomic.UIWidget();
  33. tabContainer.tabLayout.addChild(newTabButton);
  34. tabContainer.contentRoot.addChild(newTabContent);
  35. // change id, so we don't initiate a page change when clicked
  36. newTabButton.id = "NewTabButton";
  37. // create a startup with our home address
  38. createBrowserTab(tabContainer, home);
  39. // Add to main UI view and center
  40. mainLayout.addChild(tabContainer);
  41. window.addChild(mainLayout);
  42. view.addChild(window);
  43. window.center();
  44. }
  45. function createBookmarks(webView, layout, bookmarks) {
  46. for (var text in bookmarks) {
  47. var button = new Atomic.UIButton();
  48. button.text = text;
  49. button.skinBg = "TBButton.flat";
  50. layout.addChild(button);
  51. var webClient = webView.webClient;
  52. (function() {
  53. var url = bookmarks[text];
  54. button.onClick = function() { webClient.loadURL(url); }
  55. })();
  56. }
  57. }
  58. function createBrowserTab(tabContainer, url) {
  59. var contentRoot = tabContainer.contentRoot;
  60. var tabLayout = tabContainer.tabLayout;
  61. var layout = new Atomic.UILayout();
  62. layout.axis = Atomic.UI_AXIS_Y;
  63. layout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  64. layout.gravity = Atomic.UI_GRAVITY_ALL;
  65. layout.spacing = 16;
  66. var tabButton = new Atomic.UIButton();
  67. tabButton.text = "...";
  68. tabButton.squeezable = true;
  69. tabButton.skinBg = "TBButton.flat";
  70. // tabButtons with URL text by default open the URL upon clicking them
  71. // we don't want this behaviour
  72. tabButton.urlEnabled = false;
  73. tabLayout.addChildBefore(tabButton, newTabButton);
  74. // address bar
  75. var addressLayout = new Atomic.UILayout();
  76. addressLayout.gravity = Atomic.UI_GRAVITY_ALL;
  77. addressLayout.layoutDistribution = Atomic.UI_LAYOUT_DISTRIBUTION_GRAVITY;
  78. // navigation buttons
  79. var backButton = new Atomic.UIButton();
  80. backButton.text = "Back";
  81. addressLayout.addChild(backButton);
  82. var fwdButton = new Atomic.UIButton();
  83. fwdButton.text = "Forward";
  84. addressLayout.addChild(fwdButton);
  85. var reloadButton = new Atomic.UIButton();
  86. reloadButton.text = "Reload";
  87. addressLayout.addChild(reloadButton);
  88. var homeButton = new Atomic.UIButton();
  89. homeButton.text = "Home";
  90. addressLayout.addChild(homeButton);
  91. var addressEdit = new Atomic.UIEditField();
  92. addressEdit.gravity = Atomic.UI_GRAVITY_ALL;
  93. addressLayout.addChild(addressEdit);
  94. layout.addChild(addressLayout);
  95. var webView = new WebView.UIWebView(url);
  96. var webClient = webView.webClient;
  97. // bookmark bar
  98. var bookmarkLayout = new Atomic.UILayout();
  99. bookmarkLayout.gravity = Atomic.UI_GRAVITY_ALL;
  100. createBookmarks( webView, bookmarkLayout, bookmarks);
  101. layout.addChild(bookmarkLayout);
  102. layout.addChild(webView);
  103. contentRoot.addChildBefore(layout, newTabContent);
  104. // initial state
  105. reloadButton.disable();
  106. backButton.disable();
  107. fwdButton.disable();
  108. // go home
  109. homeButton.onClick = function() { webClient.loadURL(home)};
  110. // reload
  111. reloadButton.onClick = function() { webClient.reload(); };
  112. // Forward/Back
  113. fwdButton.onClick = function() { webClient.goForward(); }
  114. backButton.onClick = function() { webClient.goBack(); }
  115. // events
  116. // update the addressEdit at start and stop of url load
  117. webView.subscribeToEvent(webClient, "WebViewLoadStateChange", function(ev) {
  118. ev.canGoBack ? backButton.enable() : backButton.disable();
  119. ev.canGoForward ? fwdButton.enable() : fwdButton.disable();
  120. });
  121. // update the addressEdit at start and stop of url load
  122. webView.subscribeToEvent(webClient, "WebViewAddressChange", function(ev) {
  123. // we can now reload
  124. reloadButton.enable();
  125. addressEdit.text = ev.url;
  126. });
  127. webView.subscribeToEvent(webClient, "WebViewTitleChange", function(ev) {
  128. tabButton.text = ev.title;
  129. });
  130. }
  131. /*
  132. button_ = new UIButton(context_);
  133. button_->SetText(filename.CString());
  134. button_->SetSqueezable(true);
  135. button_->SetSkinBg("TBButton.flat");
  136. button_->SetValue(1);
  137. editorTabLayout_->AddChild(button_->GetInternalWidget());
  138. TBButton* closebutton = new TBButton();
  139. editorTabLayout_->AddChild(closebutton);
  140. closebutton->SetSkinBg(TBIDC("TBWindow.close"));
  141. closebutton->SetIsFocusable(false);
  142. closebutton->SetID(TBIDC("tabclose"));
  143. */
  144. // The Web View
  145. //var webView = new WebView.UIWebView("https://ace.c9.io/build/kitchen-sink.html");
  146. //var webView = new WebView.UIWebView("https://store.steampowered.com");
  147. //var webView = new WebView.UIWebView("https://pixlcore.com/demos/webcamjs/demos/basic.html");
  148. //var webView = new WebView.UIWebView("https://getmosh.io/");
  149. //var webView = new WebView.UIWebView("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_textarea");