jsajax_wizard 0.2.2 → 0.3.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9109f2df9aa36be696a07f567abb93063d6d1c52922fd1e238a6eb4d40b711e
4
- data.tar.gz: 275891dcee8e8b5aeb6adaeab39e93af233461d6504e3f2c926b0d7546910fab
3
+ metadata.gz: 89704032c52cf38c12530d284a7b7374bfa85bb945def3b7f3f2aefa876f9131
4
+ data.tar.gz: 54b45c9161ec895b29bec017a242ab37941ddc5f101f99582067764d00953726
5
5
  SHA512:
6
- metadata.gz: bb78b176471f9ebd9b701ff5db8e151edc30ff0b391da62ba0d0c565ea571f8d4754e910556db85e7a03bb4d90434efa84e87840ca4381f94567e84cd03c52ac
7
- data.tar.gz: 3fbf00b323808692e7550940c44a6feb8a8da1ad634b63a87f82915f8b58037fc2ce972ca36357b837490df7a31f7d2d3c3aa9331739dda28fdea8961164b26b
6
+ metadata.gz: 19b66ee5f96e404bbce6a6d7f6c74918a83bbd458b82c799573cf96bfa39ddc72fbdc4103cbd29ab4ba14a96e1127784040303e70a7540bae31e8275701c0aac
7
+ data.tar.gz: c227a108f313819be77e9e03a046f722030c3b7f4b97e1e4ed81e0a367b9053429819fb26ed7036aec625183cb17cda3fc53885b2f00e871156851d5ec93f6b8
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -21,10 +21,22 @@
21
21
  # jaw.add_request server: 'hello', element: {type: 'timer',
22
22
  # interval: '5000'}, target_element: {id: 'demo', property: :innerHTML}
23
23
 
24
+ # 03 Oct 2019 : An HTML document can be passed in containing an
25
+ # AJAX placeholder e.g.
26
+ # <button onclick='$[x][/someurl/]'></button><div id='demo'>$x</div>
27
+ #
28
+
29
+ # AJAX triggered from a button press with the text response passed to eval
30
+ # jaw.add_request server: 'hello', element: {type: 'button', event: 'onclick'}, target_eval: true
31
+
32
+ # AJAX triggered from a speech recognition result
33
+ # jaw.add_request server: 'hello', trigger: 'recognition.onresult', target_eval: true
34
+
24
35
 
25
36
 
26
37
 
27
38
  require 'rexle'
39
+ require 'rxfhelper'
28
40
  require 'rexle-builder'
29
41
 
30
42
  class JsAjaxWizard
@@ -40,8 +52,10 @@ class JsAjaxWizard
40
52
 
41
53
  end
42
54
 
43
- def add_request(server: '', element: {}, target_element: {})
44
- @requests << [server, element, target_element]
55
+ def add_request(server: '', element: {}, trigger: nil, target_element: {},
56
+ target_eval: false)
57
+ type = element.any? ? [:element, element] : [:trigger, trigger]
58
+ @requests << [server, type, target_element, target_eval ]
45
59
  end
46
60
 
47
61
  def to_html()
@@ -53,55 +67,100 @@ class JsAjaxWizard
53
67
  doc = Rexle.new(html)
54
68
  puts 'doc.xml: ' + doc.xml(pretty: true) if @debug
55
69
  add_events(doc)
56
- doc.root.element('body').add(Rexle.new(build_js))
70
+ js = build_js(doc)
71
+ doc.root.element('body').add(Rexle.new(js))
57
72
 
58
73
  doc.xml
59
74
  end
60
75
 
61
76
  private
62
77
 
63
- def add_events(doc)
78
+ def add_element_function(element, i, server, target, target_eval)
79
+
80
+ puts ('element: ' + element.inspect).debug if @debug
81
+ puts '::' + [element, i, server, target, target_eval].inspect if @debug
82
+
83
+ a = []
84
+
85
+ a << if element.is_a?(Hash) then
86
+
87
+ if element[:type] == 'text' then
64
88
 
65
- @requests.each.with_index do |x,i|
66
-
67
- element = x[1]
68
-
69
- selector = if element[:id] then
70
- '#' + element[:id]
71
- elsif element[:type]
72
- "*[@type='%s']" % element[:type]
73
- end
74
-
75
- if @debug then
76
- puts ("selector: %s" % selector.inspect).debug
77
- puts 'doc: ' + doc.xml(pretty: true).inspect
78
- end
79
-
80
- e = doc.at_css(selector)
81
- puts ('e: ' + e.inspect).debug if @debug
82
- next unless e
83
- puts ('e: ' + e.inspect).debug if @debug
84
-
85
- func = 'ajaxCall' + (i+1).to_s
86
- event = e.attributes[:type] == 'button' ? func + '()' : func + '(this)'
87
-
88
- puts ('element: ' + element.inspect).debug if @debug
89
-
90
- key = if element[:event] then
91
-
92
89
  if element[:event].to_sym == :on_enter then
93
- event = func + '(event.keyCode, this)'
94
- :onkeyup
90
+ "
91
+ function ajaxCall#{i+1}(keyCode, e) {
92
+
93
+ if (keyCode==13){
94
+ ajaxRequest('#{server}' + e.value, ajaxResponse#{i+1})
95
+ }
96
+
97
+ }
98
+ "
95
99
  else
96
- element[:event].to_sym
100
+ "
101
+ function ajaxCall#{i+1}(e) {
102
+ ajaxRequest('#{server}' + e.value, ajaxResponse#{i+1})
103
+ }
104
+ "
97
105
  end
98
106
 
107
+ elsif element[:type] == 'timer'
108
+ "
109
+ setInterval(
110
+ function() {
111
+ ajaxRequest('#{server}', ajaxResponse#{i+1})
112
+ }, #{element[:interval]});
113
+ "
99
114
  else
100
- e.attributes[:type] == 'button' ? :onclick : :onkeyup
115
+ "
116
+ function ajaxCall#{i+1}() {
117
+ ajaxRequest('#{server}', ajaxResponse#{i+1})
118
+ }
119
+ "
101
120
  end
102
121
 
103
- e.attributes[key] = event
122
+ else
123
+
124
+ "
125
+ function ajaxCall#{i+1}(s) {
126
+ ajaxRequest('#{server}' + escape(s), ajaxResponse#{i+1})
127
+ }
128
+ "
129
+ end
130
+
131
+ a << "
132
+ function ajaxResponse#{i+1}(xhttp) {
133
+ "
134
+
135
+ if target.is_a?(Hash) and target[:id] then
136
+
137
+ a << " document.getElementById('#{target[:id]}')" +
138
+ ".innerHTML = xhttp.responseText;"
139
+ end
140
+
141
+ if target_eval then
142
+
143
+ a << " eval(xhttp.responseText);"
144
+
145
+ end
146
+
147
+ a << "
148
+ }
149
+ "
150
+
151
+ a.join
152
+
153
+
154
+ end
155
+
156
+ def add_events(doc)
157
+
158
+ @requests.each.with_index do |x,i|
159
+
160
+ puts ('request x: ' + x.inspect).debug if @debug
104
161
 
162
+ method(('modify_' + x[1].first.to_s).to_sym).call(x[1].last, doc, i)
163
+
105
164
  end
106
165
 
107
166
  doc
@@ -111,7 +170,9 @@ class JsAjaxWizard
111
170
 
112
171
  html = @requests.map.with_index do |x,i|
113
172
 
114
- e, e2 = x[1..-1]
173
+ raw_e, e2 = x[1..-1]
174
+
175
+ e = raw_e.last
115
176
 
116
177
  # e = element e.g. {type: 'button', event: 'onclick'}
117
178
  # e2 = target_element: {id: '', property: :innerHTML}
@@ -127,8 +188,8 @@ class JsAjaxWizard
127
188
  else
128
189
  ['']
129
190
  end
130
-
131
- a << "<div id='%s'></div>" % [e2[:id]]
191
+
192
+ a << "<div id='%s'></div>" % [e2[:id]] if e2.is_a? Hash
132
193
  a.join("\n")
133
194
 
134
195
  end
@@ -154,11 +215,8 @@ class JsAjaxWizard
154
215
 
155
216
  end
156
217
 
157
- def build_js()
218
+ def build_js(doc)
158
219
 
159
- func_calls = @requests.length.times.map do |i|
160
- "// ajaxCall#{i+1}();"
161
- end
162
220
 
163
221
  ajax=<<EOF
164
222
  function ajaxRequest(url, cFunction) {
@@ -176,56 +234,79 @@ EOF
176
234
 
177
235
  funcs_defined = @requests.map.with_index do |x,i|
178
236
 
179
- a = []
180
- server, element, target_element = x
237
+ puts ('x: ' + x.inspect).debug if @debug
238
+ server, raw_type, target, target_eval = x
181
239
 
182
- a << if element[:type] == 'text' then
240
+ if raw_type.first == :trigger then
241
+ modify_trigger_function(raw_type.last, i, server, doc)
242
+ end
183
243
 
184
- if element[:event].to_sym == :on_enter then
185
- "
186
- function ajaxCall#{i+1}(keyCode, e) {
187
-
188
- if (keyCode==13){
189
- ajaxRequest('#{server}' + e.value, ajaxResponse#{i+1})
190
- }
244
+ add_element_function(raw_type.last, i, server, target, target_eval)
245
+
246
+ end
247
+
248
+ s = "\n\n" + ajax + "\n\n" + funcs_defined.join
249
+ "\n <script>\n%s\n </script>\n" % s
250
+
251
+ end
191
252
 
192
- }
193
- "
194
- else
195
- "
196
- function ajaxCall#{i+1}(e) {
197
- ajaxRequest('#{server}' + e.value, ajaxResponse#{i+1})
198
- }
199
- "
200
- end
201
-
202
- elsif element[:type] == 'timer'
203
- "
204
- setInterval(
205
- function() {
206
- ajaxRequest('#{server}', ajaxResponse#{i+1})
207
- }, #{element[:interval]});
208
- "
253
+ def modify_element(element, doc, i)
254
+
255
+ selector = if element[:id] then
256
+ '#' + element[:id]
257
+ elsif element[:type]
258
+ "*[@type='%s']" % element[:type]
259
+ end
260
+
261
+ if @debug then
262
+ puts ("selector: %s" % selector.inspect).debug
263
+ puts 'doc: ' + doc.xml(pretty: true).inspect
264
+ end
265
+
266
+ e = doc.at_css(selector)
267
+ puts ('e: ' + e.inspect).debug if @debug
268
+ return unless e
269
+ puts ('e: ' + e.inspect).debug if @debug
270
+
271
+ func = 'ajaxCall' + (i+1).to_s
272
+ event = e.attributes[:type] == 'button' ? func + '()' : func + '(this)'
273
+
274
+ puts ('element: ' + element.inspect).debug if @debug
275
+
276
+ key = if element[:event] then
277
+
278
+ if element[:event].to_sym == :on_enter then
279
+ event = func + '(event.keyCode, this)'
280
+ :onkeyup
209
281
  else
210
- "
211
- function ajaxCall#{i+1}() {
212
- ajaxRequest('#{server}', ajaxResponse#{i+1})
213
- }
214
- "
282
+ element[:event].to_sym
215
283
  end
216
-
217
- a << "
218
- function ajaxResponse#{i+1}(xhttp) {
219
- document.getElementById('#{target_element[:id]}').innerHTML = xhttp.responseText;
220
- }
221
- "
222
-
223
- a.join
284
+
285
+ else
286
+ e.attributes[:type] == 'button' ? :onclick : :onkeyup
224
287
  end
225
-
226
- s = func_calls.join("\n") + "\n\n" + ajax + "\n\n" + funcs_defined.join
227
- "\n <script>\n%s\n </script>\n" % s
228
-
288
+
289
+ e.attributes[key] = event
290
+ end
291
+
292
+ def modify_trigger(element, doc, i)
293
+ end
294
+
295
+ def modify_trigger_function(trigger, i, server, doc)
296
+
297
+ doc.root.xpath('//script').each do |script|
298
+ puts 'script: ' + script.inspect
299
+ s = script.text.to_s
300
+
301
+ script.text = s.sub(/#{trigger} = function\(\) {[^}]+/) {|x|
302
+ a = x.lines
303
+ indent = a[1][/\s+/]
304
+ a[0..-2].join + indent + "ajaxCall#{i+1}(event.results[0][0]." +
305
+ "transcript);\n" + a[-1]
306
+ }
307
+
308
+ end
309
+
229
310
  end
230
311
 
231
312
  # find the ajax requests
@@ -233,15 +314,15 @@ function ajaxResponse#{i+1}(xhttp) {
233
314
  def scan_requests(html)
234
315
 
235
316
 
236
- a = html.scan(/\$\[([^\]])+\]\(([^\)]+)/)
317
+ a = html.scan(/\$\[([^\]]+)?\]\(([^\)]+)/)
237
318
 
238
319
  a.each do |var, url|
239
320
 
240
321
  #== fetch the trigger element details
241
322
 
242
- tag = html[/<[^<]+\$\[([^\]])+\]\(#{url}[^\>]+>/]
323
+ tag = html[/<[^<]+\$\[([^\]]+)?\]\(#{url}[^\>]+>/]
243
324
  element_name = tag[/(?<=<)\w+/]
244
- event = tag[/(\w+)=["']\$\[([^\]])+\]\(#{url}/,1]
325
+ event = tag[/(\w+)=["']\$\[([^\]]+)?\]\(#{url}/,1]
245
326
 
246
327
  # is there an id?
247
328
  id = tag[/(?<=id=["'])[^"']+/]
@@ -254,6 +335,13 @@ function ajaxResponse#{i+1}(xhttp) {
254
335
 
255
336
  element = h2.merge(event: event)
256
337
 
338
+ if var.nil? then
339
+
340
+ add_request(server: url, element: element, target_eval: true)
341
+ next
342
+
343
+ end
344
+
257
345
  #== fetch the target element details
258
346
 
259
347
  tag2 = html[/<[^<]+>\$#{var}</]
@@ -264,7 +352,7 @@ function ajaxResponse#{i+1}(xhttp) {
264
352
 
265
353
  property = if inner_html then
266
354
 
267
- html.sub!(/>\$#{var}</,'')
355
+ html.sub!(/>\$#{var}</,'>&nbsp;<')
268
356
  :innerHTML
269
357
 
270
358
  end
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.2
4
+ version: 0.3.1
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-02 00:00:00.000000000 Z
38
+ date: 2020-06-26 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: rexle
@@ -46,7 +46,7 @@ dependencies:
46
46
  version: '1.5'
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
- version: 1.5.2
49
+ version: 1.5.7
50
50
  type: :runtime
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
@@ -56,7 +56,27 @@ dependencies:
56
56
  version: '1.5'
57
57
  - - ">="
58
58
  - !ruby/object:Gem::Version
59
- version: 1.5.2
59
+ version: 1.5.7
60
+ - !ruby/object:Gem::Dependency
61
+ name: rxfhelper
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 1.0.0
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 1.0.0
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '1.0'
60
80
  description:
61
81
  email: james@jamesrobertson.eu
62
82
  executables: []
metadata.gz.sig CHANGED
Binary file