jsajax_wizard 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d47308716886d846f412552df1209e87c256301406d322e51eeb8b524f779d1
4
- data.tar.gz: 7226513448a6d28ab34b06ac9e832db49e0e8ea64ff34ef15f0d6bfd0e7cb1cd
3
+ metadata.gz: 44ae828bcda7ae8306c1012efd612f2ee13f462e4aa62a4449b8cd780aced624
4
+ data.tar.gz: 1c4c9bb9169ea27f2618ec0f6e0299afe18ccf9d8af502ef4c2e4fb5360950e1
5
5
  SHA512:
6
- metadata.gz: d577bbacdb719b9c161c6e7e14a64574ccb7f0a390eb7eab134bf052db352c85eec4c9282f8f93c05d15128e3bfc908514bffd4a7dbea5afc2cd2c24ad7b88db
7
- data.tar.gz: aaab0ad3519c47d8c4e5700bc6674f795ae3cf6bbe78d18c1d6f274a5305385ea8abcaa707fb862c05e799b1306965c8b8439ba54d4efe600a45530d9c6b111d
6
+ metadata.gz: 0ceadd9dbda94ed48fe241f736a865ae434bc0b1087d7f3098da4bb23a18e46278b60cba85bfa63f93e47ee1556556a1333ca1e5ae3fbc7a199ea2be2aa705da
7
+ data.tar.gz: a6d6abb9c4ff6540687b83f5fad0facf44f8446170cb2a61263d2cc7ce7e47c3f87b1c338acc16c4f8cc363c23bcc30c79df575adf300deace9c3d18d12563be
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -29,6 +29,11 @@
29
29
  # AJAX triggered from a button press with the text response passed to eval
30
30
  # jaw.add_request server: 'hello', element: {type: 'button', event: 'onclick'}, target_eval: true
31
31
 
32
+ # AJAX triggered from a speech recognition result
33
+ # jaw.add_request server: 'hello', trigger: 'recognition.onresult', target_eval: true
34
+
35
+
36
+
32
37
 
33
38
  require 'rexle'
34
39
  require 'rexle-builder'
@@ -46,8 +51,10 @@ class JsAjaxWizard
46
51
 
47
52
  end
48
53
 
49
- def add_request(server: '', element: {}, target_element: {}, target_eval: false)
50
- @requests << [server, element, target_eval || target_element ]
54
+ def add_request(server: '', element: {}, trigger: nil, target_element: {},
55
+ target_eval: false)
56
+ type = element.any? ? [:element, element] : [:trigger, trigger]
57
+ @requests << [server, type, target_element, target_eval ]
51
58
  end
52
59
 
53
60
  def to_html()
@@ -59,57 +66,101 @@ class JsAjaxWizard
59
66
  doc = Rexle.new(html)
60
67
  puts 'doc.xml: ' + doc.xml(pretty: true) if @debug
61
68
  add_events(doc)
62
- doc.root.element('body').add(Rexle.new(build_js))
69
+ js = build_js(doc)
70
+ doc.root.element('body').add(Rexle.new(js))
63
71
 
64
72
  doc.xml
65
73
  end
66
74
 
67
75
  private
68
76
 
69
- def add_events(doc)
77
+ def add_element_function(element, i, server, target, target_eval)
78
+
79
+ puts ('element: ' + element.inspect).debug if @debug
80
+ puts '::' + [element, i, server, target, target_eval].inspect if @debug
81
+
82
+ a = []
83
+
84
+ a << if element.is_a?(Hash) then
85
+
86
+ if element[:type] == 'text' then
70
87
 
71
- @requests.each.with_index do |x,i|
72
-
73
- puts ('x: ' + x.inspect).debug if @debug
74
- element = x[1]
75
-
76
- selector = if element[:id] then
77
- '#' + element[:id]
78
- elsif element[:type]
79
- "*[@type='%s']" % element[:type]
80
- end
81
-
82
- if @debug then
83
- puts ("selector: %s" % selector.inspect).debug
84
- puts 'doc: ' + doc.xml(pretty: true).inspect
85
- end
86
-
87
- e = doc.at_css(selector)
88
- puts ('e: ' + e.inspect).debug if @debug
89
- next unless e
90
- puts ('e: ' + e.inspect).debug if @debug
91
-
92
- func = 'ajaxCall' + (i+1).to_s
93
- event = e.attributes[:type] == 'button' ? func + '()' : func + '(this)'
94
-
95
- puts ('element: ' + element.inspect).debug if @debug
96
-
97
- key = if element[:event] then
98
-
99
88
  if element[:event].to_sym == :on_enter then
100
- event = func + '(event.keyCode, this)'
101
- :onkeyup
89
+ "
90
+ function ajaxCall#{i+1}(keyCode, e) {
91
+
92
+ if (keyCode==13){
93
+ ajaxRequest('#{server}' + e.value, ajaxResponse#{i+1})
94
+ }
95
+
96
+ }
97
+ "
102
98
  else
103
- element[:event].to_sym
99
+ "
100
+ function ajaxCall#{i+1}(e) {
101
+ ajaxRequest('#{server}' + e.value, ajaxResponse#{i+1})
102
+ }
103
+ "
104
104
  end
105
105
 
106
+ elsif element[:type] == 'timer'
107
+ "
108
+ setInterval(
109
+ function() {
110
+ ajaxRequest('#{server}', ajaxResponse#{i+1})
111
+ }, #{element[:interval]});
112
+ "
106
113
  else
107
- e.attributes[:type] == 'button' ? :onclick : :onkeyup
114
+ "
115
+ function ajaxCall#{i+1}() {
116
+ ajaxRequest('#{server}', ajaxResponse#{i+1})
117
+ }
118
+ "
108
119
  end
109
120
 
110
- e.attributes[key] = event
121
+ else
122
+
123
+ "
124
+ function ajaxCall#{i+1}(s) {
125
+ ajaxRequest('#{server}' + escape(s), ajaxResponse#{i+1})
126
+ }
127
+ "
128
+ end
129
+
130
+ a << "
131
+ function ajaxResponse#{i+1}(xhttp) {
132
+ "
133
+
134
+ if target.is_a?(Hash) and target[:id] then
135
+
136
+ a << " document.getElementById('#{target[:id]}')" +
137
+ ".innerHTML = xhttp.responseText;"
138
+ end
139
+
140
+ if target_eval then
141
+
142
+ a << " eval(xhttp.responseText);"
111
143
 
112
144
  end
145
+
146
+ a << "
147
+ }
148
+ "
149
+
150
+ a.join
151
+
152
+
153
+ end
154
+
155
+ def add_events(doc)
156
+
157
+ @requests.each.with_index do |x,i|
158
+
159
+ puts ('request x: ' + x.inspect).debug if @debug
160
+
161
+ method(('modify_' + x[1].first.to_s).to_sym).call(x[1].last, doc, i)
162
+
163
+ end
113
164
 
114
165
  doc
115
166
  end
@@ -118,7 +169,9 @@ class JsAjaxWizard
118
169
 
119
170
  html = @requests.map.with_index do |x,i|
120
171
 
121
- e, e2 = x[1..-1]
172
+ raw_e, e2 = x[1..-1]
173
+
174
+ e = raw_e.last
122
175
 
123
176
  # e = element e.g. {type: 'button', event: 'onclick'}
124
177
  # e2 = target_element: {id: '', property: :innerHTML}
@@ -161,11 +214,8 @@ class JsAjaxWizard
161
214
 
162
215
  end
163
216
 
164
- def build_js()
217
+ def build_js(doc)
165
218
 
166
- func_calls = @requests.length.times.map do |i|
167
- "// ajaxCall#{i+1}();"
168
- end
169
219
 
170
220
  ajax=<<EOF
171
221
  function ajaxRequest(url, cFunction) {
@@ -183,68 +233,79 @@ EOF
183
233
 
184
234
  funcs_defined = @requests.map.with_index do |x,i|
185
235
 
186
- a = []
187
- server, element, target = x
188
-
189
- a << if element[:type] == 'text' then
236
+ puts ('x: ' + x.inspect).debug if @debug
237
+ server, raw_type, target, target_eval = x
190
238
 
191
- if element[:event].to_sym == :on_enter then
192
- "
193
- function ajaxCall#{i+1}(keyCode, e) {
194
-
195
- if (keyCode==13){
196
- ajaxRequest('#{server}' + e.value, ajaxResponse#{i+1})
197
- }
198
-
199
- }
200
- "
201
- else
202
- "
203
- function ajaxCall#{i+1}(e) {
204
- ajaxRequest('#{server}' + e.value, ajaxResponse#{i+1})
205
- }
206
- "
207
- end
208
-
209
- elsif element[:type] == 'timer'
210
- "
211
- setInterval(
212
- function() {
213
- ajaxRequest('#{server}', ajaxResponse#{i+1})
214
- }, #{element[:interval]});
215
- "
216
- else
217
- "
218
- function ajaxCall#{i+1}() {
219
- ajaxRequest('#{server}', ajaxResponse#{i+1})
220
- }
221
- "
239
+ if raw_type.first == :trigger then
240
+ modify_trigger_function(raw_type.last, i, server, doc)
222
241
  end
242
+
243
+ add_element_function(raw_type.last, i, server, target, target_eval)
244
+
245
+ end
246
+
247
+ s = "\n\n" + ajax + "\n\n" + funcs_defined.join
248
+ "\n <script>\n%s\n </script>\n" % s
223
249
 
224
- a << "
225
- function ajaxResponse#{i+1}(xhttp) {
226
- "
227
- if target.is_a? Hash and target[:id] then
228
-
229
- a << " document.getElementById('#{target[:id]}')" +
230
- ".innerHTML = xhttp.responseText;"
250
+ end
231
251
 
252
+ def modify_element(element, doc, i)
253
+
254
+ selector = if element[:id] then
255
+ '#' + element[:id]
256
+ elsif element[:type]
257
+ "*[@type='%s']" % element[:type]
258
+ end
259
+
260
+ if @debug then
261
+ puts ("selector: %s" % selector.inspect).debug
262
+ puts 'doc: ' + doc.xml(pretty: true).inspect
263
+ end
264
+
265
+ e = doc.at_css(selector)
266
+ puts ('e: ' + e.inspect).debug if @debug
267
+ return unless e
268
+ puts ('e: ' + e.inspect).debug if @debug
269
+
270
+ func = 'ajaxCall' + (i+1).to_s
271
+ event = e.attributes[:type] == 'button' ? func + '()' : func + '(this)'
272
+
273
+ puts ('element: ' + element.inspect).debug if @debug
274
+
275
+ key = if element[:event] then
276
+
277
+ if element[:event].to_sym == :on_enter then
278
+ event = func + '(event.keyCode, this)'
279
+ :onkeyup
232
280
  else
233
-
234
- a << " eval(xhttp.responseText);"
235
-
281
+ element[:event].to_sym
236
282
  end
237
283
 
238
- a << "
239
- }
240
- "
241
-
242
- a.join
284
+ else
285
+ e.attributes[:type] == 'button' ? :onclick : :onkeyup
243
286
  end
244
-
245
- s = func_calls.join("\n") + "\n\n" + ajax + "\n\n" + funcs_defined.join
246
- "\n <script>\n%s\n </script>\n" % s
247
-
287
+
288
+ e.attributes[key] = event
289
+ end
290
+
291
+ def modify_trigger(element, doc, i)
292
+ end
293
+
294
+ def modify_trigger_function(trigger, i, server, doc)
295
+
296
+ doc.root.xpath('//script').each do |script|
297
+ puts 'script: ' + script.inspect
298
+ s = script.text.to_s
299
+
300
+ script.text = s.sub(/#{trigger} = function\(\) {[^}]+/) {|x|
301
+ a = x.lines
302
+ indent = a[1][/\s+/]
303
+ a[0..-2].join + indent + "ajaxCall#{i+1}(event.results[0][0]." +
304
+ "transcript);\n" + a[-1]
305
+ }
306
+
307
+ end
308
+
248
309
  end
249
310
 
250
311
  # find the ajax requests
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsajax_wizard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  pZTsghadsRxoGiI7mehrgxkeLLA7q3Zpafsvw3mLrERdifNsVFNBUDMjfE/OnJq8
36
36
  EOz+UVQEw8EJ9wHqaJYIbHBq
37
37
  -----END CERTIFICATE-----
38
- date: 2019-10-06 00:00:00.000000000 Z
38
+ date: 2019-10-21 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: rexle
metadata.gz.sig CHANGED
Binary file