Class: Formotion::RowType::WebViewRow

Inherits:
Base show all
Includes:
BW::KVO
Defined in:
lib/formotion/row_type/web_view_row.rb

Constant Summary collapse

WEB_VIEW_TAG =
1100

Instance Attribute Summary

Attributes inherited from Base

#row, #tableView

Instance Method Summary collapse

Methods inherited from Base

#_on_select, #after_build, #after_delete, #break_with_semaphore, #button?, #cellEditingStyle, #cell_style, #delete_row, #done_editing, field_buffer, #indentWhileEditing?, #initialize, #input_accessory_view, #on_delete, #update_cell, #with_semaphore

Constructor Details

This class inherits a constructor from Formotion::RowType::Base

Instance Method Details

#build_cell(cell) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/formotion/row_type/web_view_row.rb', line 29

def build_cell(cell)
  cell.selectionStyle = self.row.selection_style || UITableViewCellSelectionStyleBlue
  
  @loading = true
  @web_view = UIWebView.alloc.init
  @web_view.delegate = self
  set_page
  
  observe(self.row, "value") do |old_value, new_value|
    break_with_semaphore do
      set_page
    end
  end
  
  @web_view.tag = WEB_VIEW_TAG
  @web_view.contentMode = UIViewContentModeScaleAspectFit
  @web_view.backgroundColor = UIColor.clearColor
  cell.addSubview(@web_view)

  cell.swizzle(:layoutSubviews) do
    def layoutSubviews
      old_layoutSubviews

      # viewWithTag is terrible, but I think it's ok to use here...
      formotion_field = self.viewWithTag(WEB_VIEW_TAG)

      field_frame = formotion_field.frame
      field_frame.origin.y = 10
      field_frame.origin.x = self.textLabel.frame.origin.x + self.textLabel.frame.size.width + Formotion::RowType::Base.field_buffer
      field_frame.size.width  = self.frame.size.width - field_frame.origin.x - Formotion::RowType::Base.field_buffer
      field_frame.size.height = self.frame.size.height - Formotion::RowType::Base.field_buffer
      formotion_field.frame = field_frame
    end
  end
end

#loadingObject



25
26
27
# File 'lib/formotion/row_type/web_view_row.rb', line 25

def loading
  @loading
end

#on_select(tableView, tableViewDelegate) ⇒ Object



65
66
67
68
69
# File 'lib/formotion/row_type/web_view_row.rb', line 65

def on_select(tableView, tableViewDelegate)
  if !row.editable?
    return
  end
end

#set_pageObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/formotion/row_type/web_view_row.rb', line 10

def set_page
  if row.value =~/^https?:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?\/?([a-zA-Z0-9\-\._\?\,\'\/\+&%\$#\=~])*$/
    @loading = true
    req = NSURLRequest.requestWithURL(NSURL.URLWithString(row.value))
    @web_view.loadRequest(req)
  else
    @web_view.loadHTMLString(row.value, baseURL:nil) if row.value
    @loading = false
  end
end

#stringByEvaluatingJavaScriptFromString(script) ⇒ Object



21
22
23
# File 'lib/formotion/row_type/web_view_row.rb', line 21

def stringByEvaluatingJavaScriptFromString(script)
  @web_view.stringByEvaluatingJavaScriptFromString(script)
end

#webViewDidFinishLoad(web_view) ⇒ Object

def webViewDidStartLoad(web_view) end



77
78
79
# File 'lib/formotion/row_type/web_view_row.rb', line 77

def webViewDidFinishLoad(web_view)
  @loading = false
end