Module: ProMotion::WebScreenModule
- Included in:
- WebScreen
- Defined in:
- lib/ProMotion/web/web_screen_module.rb
Instance Attribute Summary collapse
-
#detector_types ⇒ Object
Returns the value of attribute detector_types.
-
#external_links ⇒ Object
Returns the value of attribute external_links.
-
#scale_to_fit ⇒ Object
Returns the value of attribute scale_to_fit.
-
#webview ⇒ Object
Returns the value of attribute webview.
Instance Method Summary collapse
- #back ⇒ Object
-
#can_go_back ⇒ Object
Navigation.
- #can_go_forward ⇒ Object
- #check_content_data ⇒ Object
- #convert_retina_images(content) ⇒ Object
- #current_url ⇒ Object
- #evaluate(js) ⇒ Object
- #forward ⇒ Object
- #html ⇒ Object
- #on_init ⇒ Object
- #open_in_chrome(in_request) ⇒ Object
- #open_in_safari(in_request) ⇒ Object
- #open_url(url) ⇒ Object
- #refresh ⇒ Object (also: #reload)
- #screen_setup ⇒ Object
- #set_content(content) ⇒ Object
- #set_initial_content ⇒ Object
- #stop ⇒ Object
- #web ⇒ Object
- #web_view_setup ⇒ Object
-
#webView(webView, didFailLoadWithError: error) ⇒ Object
UIWebViewDelegate Methods - Camelcase.
- #webViewDidFinishLoad(webView) ⇒ Object
- #webViewDidStartLoad(webView) ⇒ Object
Instance Attribute Details
#detector_types ⇒ Object
Returns the value of attribute detector_types.
4 5 6 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 4 def detector_types @detector_types end |
#external_links ⇒ Object
Returns the value of attribute external_links.
4 5 6 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 4 def external_links @external_links end |
#scale_to_fit ⇒ Object
Returns the value of attribute scale_to_fit.
4 5 6 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 4 def scale_to_fit @scale_to_fit end |
#webview ⇒ Object
Returns the value of attribute webview.
4 5 6 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 4 def webview @webview end |
Instance Method Details
#back ⇒ Object
103 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 103 def back; web.goBack if can_go_back; end |
#can_go_back ⇒ Object
Navigation
101 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 101 def can_go_back; web.canGoBack; end |
#can_go_forward ⇒ Object
102 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 102 def can_go_forward; web.canGoForward; end |
#check_content_data ⇒ Object
84 85 86 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 84 def check_content_data mp("Missing #content method in WebScreen #{self.class.to_s}.", force_color: :red) unless self.respond_to?(:content) end |
#convert_retina_images(content) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 67 def convert_retina_images(content) #Convert images over to retina if the images exist. if UIScreen.mainScreen.bounds.respondsToSelector('displayLinkWithTarget:selector:') && UIScreen.mainScreen.bounds.scale == 2.0 # Thanks BubbleWrap! https://github.com/rubymotion/BubbleWrap/blob/master/motion/core/device/ios/screen.rb#L9 content.gsub!(/src=['"](.*?)\.(jpg|gif|png)['"]/) do |img| if File.exists?(File.join(NSBundle.mainBundle.resourcePath, "#{$1}@2x.#{$2}")) # Create a UIImage to get the width and height of hte @2x image tmp_image = UIImage.imageNamed("/#{$1}@2x.#{$2}") new_width = tmp_image.size.width / 2 new_height = tmp_image.size.height / 2 img = "src=\"#{$1}@2x.#{$2}\" width=\"#{new_width}\" height=\"#{new_height}\"" end end end content end |
#current_url ⇒ Object
96 97 98 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 96 def current_url evaluate('document.URL') end |
#evaluate(js) ⇒ Object
92 93 94 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 92 def evaluate(js) self.webview.stringByEvaluatingJavaScriptFromString(js) end |
#forward ⇒ Object
104 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 104 def forward; web.goForward if can_go_forward; end |
#html ⇒ Object
88 89 90 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 88 def html evaluate("document.documentElement.outerHTML") end |
#on_init ⇒ Object
16 17 18 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 16 def on_init # TODO: Remove in 3.0 end |
#open_in_chrome(in_request) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 109 def open_in_chrome(in_request) # Add pod 'OpenInChrome' to your Rakefile if you want links to open in Google Chrome for users. # This will fall back to Safari if the user doesn't have Chrome installed. chrome_controller = OpenInChromeController.sharedInstance return open_in_safari(in_request) unless chrome_controller.isChromeInstalled chrome_controller.openInChrome(in_request.URL) end |
#open_in_safari(in_request) ⇒ Object
117 118 119 120 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 117 def open_in_safari(in_request) # Open UIWebView delegate links in Safari. UIApplication.sharedApplication.openURL(in_request.URL) end |
#open_url(url) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 60 def open_url(url) request = NSURLRequest.requestWithURL( url.is_a?(NSURL) ? url : NSURL.URLWithString(url) ) web.loadRequest request end |
#refresh ⇒ Object Also known as: reload
105 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 105 def refresh; web.reload; end |
#screen_setup ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 6 def screen_setup check_content_data self.external_links ||= false self.scale_to_fit ||= false self.detector_types ||= :none web_view_setup set_initial_content end |
#set_content(content) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 46 def set_content(content) content_path = File.join(NSBundle.mainBundle.resourcePath, content) if File.exists? content_path content_string = File.read content_path content_base_url = NSURL.fileURLWithPath NSBundle.mainBundle.resourcePath self.web.loadHTMLString(convert_retina_images(content_string), baseURL:content_base_url) else # We assume the user wants to load an arbitrary string into the web view self.web.loadHTMLString(content, baseURL:nil) end end |
#set_initial_content ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 35 def set_initial_content return unless self.respond_to?(:content) && self.content if self.content.is_a?(NSURL) open_url(self.content) elsif self.content.is_a?(NSMutableURLRequest) web.loadRequest self.content else set_content(self.content) end end |
#stop ⇒ Object
106 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 106 def stop; web.stopLoading; end |
#web ⇒ Object
31 32 33 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 31 def web self.webview end |
#web_view_setup ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 20 def web_view_setup self.webview ||= add UIWebView.new, { frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height), delegate: self, data_detector_types: data_detector_types } self.webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight self.webview.scalesPageToFit = self.scale_to_fit self.webview.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal end |
#webView(webView, didFailLoadWithError: error) ⇒ Object
UIWebViewDelegate Methods - Camelcase
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 123 def webView(in_web, shouldStartLoadWithRequest:in_request, navigationType:in_type) if %w(http https).include?(in_request.URL.scheme) if self.external_links == true && in_type == UIWebViewNavigationTypeLinkClicked if defined?(OpenInChromeController) open_in_chrome in_request else open_in_safari in_request end return false # don't allow the web view to load the link. end end load_request_enable = true #return true on default for local file loading. load_request_enable = !!on_request(in_request, in_type) if self.respond_to?(:on_request) load_request_enable end |
#webViewDidFinishLoad(webView) ⇒ Object
144 145 146 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 144 def webViewDidFinishLoad(webView) load_finished if self.respond_to?(:load_finished) end |
#webViewDidStartLoad(webView) ⇒ Object
140 141 142 |
# File 'lib/ProMotion/web/web_screen_module.rb', line 140 def webViewDidStartLoad(webView) load_started if self.respond_to?(:load_started) end |