module ProMotion::WebScreenModule

Attributes

detector_types[RW]
scale_to_fit[RW]
webview[RW]

Public Instance Methods

back() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 86
def back; web.goBack if can_go_back; end
can_go_back() click to toggle source

Navigation

# File lib/ProMotion/web/web_screen_module.rb, line 84
def can_go_back; web.canGoBack; end
can_go_forward() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 85
def can_go_forward; web.canGoForward; end
check_content_data() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 71
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) click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 54
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(&block) click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 79
def current_url(&block)
  evaluate('document.URL', &block)
end
forward() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 87
def forward; web.goForward if can_go_forward; end
html(&block) click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 75
def html(&block)
  evaluate('document.documentElement.outerHTML', &block)
end
on_init() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 18
def on_init
  # TODO: Remove in 3.0
end
open_in_chrome(in_request) click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 92
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) click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 100
def open_in_safari(in_request)
  # Open UIWebView delegate links in Safari.
  UIApplication.sharedApplication.openURL(in_request.URL)
end
open_url(url) click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 50
def open_url(url)
  web.loadRequest NSURLRequest.requestWithURL(url.to_url)
end
refresh() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 88
def refresh; web.reload; end
Also aliased as: reload
reload()
Alias for: refresh
screen_setup() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 8
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) click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 37
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() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 26
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() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 89
def stop; web.stopLoading; end
web() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 22
def web
  self.webview
end

Protected Instance Methods

data_detector_types() click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 107
def data_detector_types
  Array(self.detector_types).reduce(UIDataDetectorTypeNone) do |detectors, dt|
    detectors | map_detector_symbol(dt)
  end
end
map_detector_symbol(symbol) click to toggle source
# File lib/ProMotion/web/web_screen_module.rb, line 113
def map_detector_symbol(symbol)
  {
    phone:    UIDataDetectorTypePhoneNumber,
    link:     UIDataDetectorTypeLink,
    address:  UIDataDetectorTypeAddress,
    event:    UIDataDetectorTypeCalendarEvent,
    all:      UIDataDetectorTypeAll
  }[symbol] || UIDataDetectorTypeNone
end