Class: Nice::Logic

Inherits:
Object
  • Object
show all
Defined in:
lib/nice/logic.rb

Overview

> replace all contents under the “container yield” mentioned in the application layout file

Class Method Summary collapse

Class Method Details

.run(current_method, current_path, referer, doc, is_js) ⇒ Object



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/nice/logic.rb', line 33

def self.run current_method, current_path, referer, doc, is_js

    ref_c = Rails.application.routes.recognize_path(referer)[:controller]
    ref_a = Rails.application.routes.recognize_path(referer)[:action]
	  curr_c = Rails.application.routes.recognize_path(current_path)[:controller]
	  curr_a = Rails.application.routes.recognize_path(current_path)[:action]
	
	  current_state = "#{current_method.downcase}_#{curr_c}_#{curr_a}"
	
	  prev_state = referer.gsub("/","_") unless referer.nil?

  	referenced_doc = Nice::HtmlParser.annotate_referencing_nodes doc	

  	cleaned_doc = Nice::HtmlParser.remove_elements_not_of_state current_state, referenced_doc
  	
  	controller_same = (ref_c == curr_c)
  	
  	# case 1
  	if !is_js then   

      response = Nice::HtmlParser.add_top_css(cleaned_doc, current_state).to_html

  	# case 2
  	elsif controller_same && is_js then   
  		js_stack = ["// remove elements not present in the following state"]
  		js_stack << Nice::Js::Caller.generate_js_remove(current_state)

  		js_stack << "// add new elements"
  		js_stack += Nice::HtmlParser.add_elements_of_current_state(cleaned_doc,current_state).compact
  		
  	# case 4
  	elsif !controller_same && is_js then
  		js_stack = ["// remove elements not present in the following state"]
  		js_stack << Nice::Js::Caller.generate_js_remove(current_state)
  		      	  
      js_stack << ["// remove elements below root"]
  		js_stack << Nice::Js::Caller.clean_root_tree
  		
  		js_stack << "// add new content tree blow root tag"
  		js_stack += Nice::HtmlParser.add_root_content(cleaned_doc).compact
  		
  		js_stack << "// add elements of the current state above root"
  		js_stack += Nice::HtmlParser.add_elements_of_current_state(cleaned_doc,current_state,true).compact      		
  	end
  	
  	# add completing js 
  	if is_js then
  	  js_stack << "// add browser history scripts"		
  		js_stack << Nice::Js::Caller.move_to_url(current_path,"title")
  		js_stack << Nice::Js::Caller.insert_or_update_back_listener(referer)

  		js_stack << "// inform ui on state change"
  		js_stack << Nice::Js::Caller.state_did_change(prev_state,current_state)

      js_stack << "// switch body css class"      		
  		js_stack << Nice::Js::Caller.change_top_css(current_state)
  		
  	  js_stack << "// AJAX RAILS ENGINE (nice.codebility.com)"		      		
  		      		
  		response = js_stack.join("\n")
  	end
  	
  	response
end