nice 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,6 +20,11 @@ class NiceJquery
20
20
  @insert_inside: (event) ->
21
21
  act = (ref,ins) -> ref.prepend(ins)
22
22
  NiceJquery.perform_transition(event.ref_node, event.new_node, act)
23
+
24
+ # replace ref node with new node
25
+ @replace: (event) ->
26
+ act = (ref,ins) -> ref.replaceWith(ins)
27
+ NiceJquery.perform_transition(event.ref_node, event.new_node, act)
23
28
 
24
29
  # remove all elements which are not of current state and all elements
25
30
  # which are of current state and secondly annotated to be always updated.
@@ -27,6 +32,9 @@ class NiceJquery
27
32
  $("[data-state]").not("[data-state~='#{event.curr_state}']").remove()
28
33
  $("[data-state~='#{event.curr_state}'][data-state-update!='no']").remove()
29
34
 
35
+ # remove everything under the "root tag"
36
+ @clean_root_tree: (event) ->
37
+ $("[data-state-root]").children().remove()
30
38
 
31
39
  # Browser History Stuff
32
40
  @move_to_url: (event) ->
@@ -105,8 +113,10 @@ class NiceJquery
105
113
  ## add event listener
106
114
  document.addEventListener "nice.dom.InsertAfterEvent", NiceJquery.insert_after, false
107
115
  document.addEventListener "nice.dom.InsertInsideEvent", NiceJquery.insert_inside, false
116
+ document.addEventListener "nice.dom.ReplaceEvent", NiceJquery.replace, false
108
117
  document.addEventListener "nice.dom.RemoveStateEvent", NiceJquery.remove_state_elements, false
109
118
  document.addEventListener "nice.dom.ChangeTopCssEvent", NiceJquery.change_top_css, false
119
+ document.addEventListener "nice.dom.CleanRootEvent", NiceJquery.clean_root_tree, false
110
120
  document.addEventListener "nice.hist.ChangeURLEvent", NiceJquery.move_to_url, false
111
121
  document.addEventListener "nice.hist.PopHistoryEvent", NiceJquery.insert_or_update_back_listener, false
112
122
 
@@ -16,9 +16,10 @@ module Nice
16
16
  doc
17
17
  end
18
18
 
19
- def self.add_elements_of_current_state doc, curr_state
19
+ def self.add_elements_of_current_state doc, curr_state, upper_root=false
20
20
 
21
- # get all nodes of the current state
21
+ # get all nodes of the current state
22
+ doc.css("*[data-state-root]").remove if upper_root # for upper_root option remove tree below root to shorten the search
22
23
  curr_state_nodes = doc.css("[data-state~='#{curr_state}']")
23
24
 
24
25
  # get reference nodes in DOM tree for current nodes and generate js insert statements
@@ -119,6 +120,19 @@ module Nice
119
120
  doc.css("body").add_class("state-#{current_state}")
120
121
  doc
121
122
  end
123
+
124
+ def self.add_root_content doc
125
+ if doc.css("[data-state-root]").length == 0
126
+ raise "You must add add a data-state-root in your application.html to your main container"
127
+ else
128
+ ref_node_name = "[data-state-root]"
129
+ js = []
130
+ doc.css("*[data-state-root]").each do |curr_node|
131
+ js << Nice::Js::Caller.replace_node(curr_node, ref_node_name).gsub(/(\r\n|\n|\r|\t|\s\s)/,'')
132
+ end
133
+ js
134
+ end
135
+ end
122
136
 
123
137
  private
124
138
 
@@ -11,9 +11,17 @@ module Nice
11
11
  "NiceEventDispatcher.dispatch_event(\'nice.dom.InsertInsideEvent\',{new_node:\'#{new_node}\', ref_node:\"#{reference_node_ref}\"});"
12
12
  end
13
13
 
14
+ def self.replace_node new_node, reference_node_ref
15
+ "NiceEventDispatcher.dispatch_event(\'nice.dom.ReplaceEvent\',{new_node:\'#{new_node}\', ref_node:\"#{reference_node_ref}\"});"
16
+ end
17
+
14
18
  def self.generate_js_remove curr_state
15
19
  "NiceEventDispatcher.dispatch_event(\'nice.dom.RemoveStateEvent\',{curr_state:\'#{curr_state}\'});"
16
20
  end
21
+
22
+ def self.clean_root_tree
23
+ "NiceEventDispatcher.dispatch_event(\'nice.dom.CleanRootEvent\',{});"
24
+ end
17
25
 
18
26
 
19
27
  # History Manipulation
data/lib/nice/logic.rb CHANGED
@@ -24,42 +24,75 @@ module Nice
24
24
 
25
25
  # => respond with JS which either first removes all elements of the current state and
26
26
  # later inserts new content OR directly replaces elements
27
+
28
+ ## case 4: the controller changed
29
+ # => replace all contents under the "container yield" mentioned in the application layout file
27
30
 
28
31
  class Logic
29
32
 
30
33
  def self.run current_method, current_path, referer, doc, is_js
31
34
 
32
- current_state = current_method.downcase + current_path.gsub("/", "_")
33
- prev_state = referer.gsub("/","_") unless referer.nil?
35
+ ref_c = Rails.application.routes.recognize_path(referer)[:controller]
36
+ ref_a = Rails.application.routes.recognize_path(referer)[:action]
37
+ curr_c = Rails.application.routes.recognize_path(current_path)[:controller]
38
+ curr_a = Rails.application.routes.recognize_path(current_path)[:action]
39
+
40
+ current_state = "#{current_method.downcase}_#{curr_c}_#{curr_a}"
41
+
42
+ prev_state = referer.gsub("/","_") unless referer.nil?
34
43
 
35
44
  referenced_doc = Nice::HtmlParser.annotate_referencing_nodes doc
36
45
 
37
46
  cleaned_doc = Nice::HtmlParser.remove_elements_not_of_state current_state, referenced_doc
38
47
 
48
+ controller_same = (ref_c == curr_c)
49
+
39
50
  # case 1
40
51
  if !is_js then
41
52
 
42
- Nice::HtmlParser.add_top_css(cleaned_doc, current_state).to_html
53
+ response = Nice::HtmlParser.add_top_css(cleaned_doc, current_state).to_html
43
54
 
44
55
  # case 2
45
- else
56
+ elsif controller_same && is_js then
46
57
  js_stack = ["// remove elements not present in the following state"]
47
58
  js_stack << Nice::Js::Caller.generate_js_remove(current_state)
48
59
 
49
60
  js_stack << "// add new elements"
50
61
  js_stack += Nice::HtmlParser.add_elements_of_current_state(cleaned_doc,current_state).compact
51
-
52
- js_stack << "// add browser history scripts"
62
+
63
+ # case 4
64
+ elsif !controller_same && is_js then
65
+ js_stack = ["// remove elements not present in the following state"]
66
+ js_stack << Nice::Js::Caller.generate_js_remove(current_state)
67
+
68
+ js_stack << ["// remove elements below root"]
69
+ js_stack << Nice::Js::Caller.clean_root_tree
70
+
71
+ js_stack << "// add new content tree blow root tag"
72
+ js_stack += Nice::HtmlParser.add_root_content(cleaned_doc).compact
73
+
74
+ js_stack << "// add elements of the current state above root"
75
+ js_stack += Nice::HtmlParser.add_elements_of_current_state(cleaned_doc,current_state,true).compact
76
+ end
77
+
78
+ # add completing js
79
+ if is_js then
80
+ js_stack << "// add browser history scripts"
53
81
  js_stack << Nice::Js::Caller.move_to_url(current_path,"title")
54
82
  js_stack << Nice::Js::Caller.insert_or_update_back_listener(referer)
55
83
 
56
84
  js_stack << "// inform ui on state change"
57
85
  js_stack << Nice::Js::Caller.state_did_change(prev_state,current_state)
58
86
 
59
- js_stack << Nice::Js::Caller.change_top_css(current_state)
60
-
61
- js_stack.join("\n")
87
+ js_stack << "// switch body css class"
88
+ js_stack << Nice::Js::Caller.change_top_css(current_state)
89
+
90
+ js_stack << "// AJAX RAILS ENGINE (nice.codebility.com)"
91
+
92
+ response = js_stack.join("\n")
62
93
  end
94
+
95
+ response
63
96
  end
64
97
 
65
98
  end
data/lib/nice/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nice
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-20 00:00:00.000000000 Z
12
+ date: 2012-08-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -97,7 +97,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
97
  version: '0'
98
98
  segments:
99
99
  - 0
100
- hash: 3643352142035015313
100
+ hash: -579488533932706128
101
101
  required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  none: false
103
103
  requirements:
@@ -106,10 +106,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  segments:
108
108
  - 0
109
- hash: 3643352142035015313
109
+ hash: -579488533932706128
110
110
  requirements: []
111
111
  rubyforge_project:
112
- rubygems_version: 1.8.24
112
+ rubygems_version: 1.8.22
113
113
  signing_key:
114
114
  specification_version: 3
115
115
  summary: Nice / Nizza is a posh little town in south France.