Class: Gloo::Objs::Page

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/web_svr/page.rb

Constant Summary collapse

KEYWORD =
'page'.freeze
KEYWORD_SHORT =
'page'.freeze
ON_RENDER =

Events

'on_render'.freeze
ON_PRERENDER =
'on_prerender'.freeze
AFTER_RENDER =
'after_render'.freeze
PARAMS =

Parameters used during render.

'params'.freeze
ID =
'id'.freeze
HEAD =

Content

'head'.freeze
BODY =
'body'.freeze
CONTENT =
'content'.freeze
TITLE =
'title'.freeze
LAYOUT =

Layout for this page. If not specified, use the layout for the app.

'layout'.freeze
CONTENT_TYPE =

Return Content type and HTML Code

'content_type'.freeze
HTML_CONTENT =
'html'.freeze
TEXT_CONTENT =
'text'.freeze
JSON_CONTENT =
'json'.freeze
FILE_CONTENT =
'file'.freeze
RETURN_CODE =
'return_code'.freeze
FILE_TYPE =

Children for FILE pages.

'file_type'.freeze
FILE_PATH =
'file_path'.freeze
FILE_NAME =
'file_name'.freeze
FILE_DATA =
'file_data'.freeze
DOWNLOAD_FILE =
'download_file'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_unload, #pn, #remove_child, #root?, #send_message, #set_parent, #sql_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.messagesObject

Get a list of message names that this object receives.



313
314
315
# File 'lib/gloo/objs/web_svr/page.rb', line 313

def self.messages
  return super + [ 'render' ]
end

.render_params(content, params) ⇒ Object

Render content with the given params. Params might be nil, in which case the content is returned with no changes.



474
475
476
477
478
479
480
481
# File 'lib/gloo/objs/web_svr/page.rb', line 474

def self.render_params content, params
  return content unless params

  renderer = ERB.new( content )
  content = renderer.result_with_hash( params )

  return content
end

.short_typenameObject

The short name of the object type.



58
59
60
# File 'lib/gloo/objs/web_svr/page.rb', line 58

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



51
52
53
# File 'lib/gloo/objs/web_svr/page.rb', line 51

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:



269
270
271
# File 'lib/gloo/objs/web_svr/page.rb', line 269

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/gloo/objs/web_svr/page.rb', line 278

def add_default_children
  fac = @engine.factory

  fac.create_script ON_RENDER, '', self
  fac.create_script AFTER_RENDER, '', self
  fac.create_can PARAMS, self

  params = { :name => HEAD,
    :type => Gloo::Objs::Element.typename,
    :value => nil,
    :parent => self }
  head = fac.create params
  content = fac.create_can CONTENT, head
  params = { :name => TITLE,
    :type => Gloo::Objs::Element.typename,
    :value => nil,
    :parent => content }
  title = fac.create params

  params = { :name => BODY,
    :type => Gloo::Objs::Element.typename,
    :value => nil,
    :parent => self }
  body = fac.create params
  content = fac.create_can CONTENT, body
end

#bodyObject

Get the body element.



100
101
102
# File 'lib/gloo/objs/web_svr/page.rb', line 100

def body
  return find_child BODY
end

#body_contentObject

Get the body content. This might be in a content child, or it might be the body object itself.



109
110
111
112
113
114
115
# File 'lib/gloo/objs/web_svr/page.rb', line 109

def body_content
  body_obj = body
  return nil unless body_obj

  content_obj = body_obj.find_child CONTENT
  return content_obj ? content_obj : body_obj
end

#content_typeObject

Get the content type.



176
177
178
179
# File 'lib/gloo/objs/web_svr/page.rb', line 176

def content_type
  type = find_child CONTENT_TYPE
  return type ? type.value : nil
end

#download_fileObject



532
533
534
535
536
537
538
# File 'lib/gloo/objs/web_svr/page.rb', line 532

def download_file
  o = find_child DOWNLOAD_FILE
  return false unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o&.value
end

#file_dataObject

Get the File content



513
514
515
516
517
518
519
# File 'lib/gloo/objs/web_svr/page.rb', line 513

def file_data
  o = find_child FILE_DATA
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o&.value
end

#file_nameObject

Get the name of the file.



524
525
526
527
528
529
530
# File 'lib/gloo/objs/web_svr/page.rb', line 524

def file_name
  o = find_child FILE_NAME
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o&.value
end

#file_pathObject

Get the path to the file.



502
503
504
505
506
507
508
# File 'lib/gloo/objs/web_svr/page.rb', line 502

def file_path
  o = find_child FILE_PATH
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o&.value
end

#file_typeObject

Get the type of the file.



491
492
493
494
495
496
497
# File 'lib/gloo/objs/web_svr/page.rb', line 491

def file_type
  o = find_child FILE_TYPE
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o&.value
end

#headObject

Get the head element.



80
81
82
# File 'lib/gloo/objs/web_svr/page.rb', line 80

def head
  return find_child HEAD
end

#head_contentObject

Get the header content. This might be in a content child, or it might be the head object itself.



89
90
91
92
93
94
95
# File 'lib/gloo/objs/web_svr/page.rb', line 89

def head_content
  head_obj = head
  return nil unless head_obj

  content_obj = head_obj.find_child CONTENT
  return content_obj ? content_obj : head_obj
end

#init_paramsObject

Init params container with request values.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/gloo/objs/web_svr/page.rb', line 120

def init_params
  params_can = find_child PARAMS
  return nil unless params_can

  # First set URL route params if there are any.
  if @request&.request_params&.route_params
    @request.request_params.route_params.each_with_index do |route_p,i|
      o = params_can.children[i]
      o.set_value( route_p ) if o && o.name != ID
    end
  end

  if @request
    url_params = @request.request_params.query_params
    url_params.each do |k,v|
      o = params_can.find_child k
      o.set_value( v ) if o
    end

    @request.request_params.body_params.each do |k,v|
      o = params_can.find_child k
      o.set_value( v ) if o
    end
  end
end

#is_file?Boolean

Returns:



214
215
216
# File 'lib/gloo/objs/web_svr/page.rb', line 214

def is_file?
  return content_type == FILE_CONTENT
end

#is_html?Boolean

Is the return type HTML?

Returns:



195
196
197
198
# File 'lib/gloo/objs/web_svr/page.rb', line 195

def is_html?
  return true if content_type.nil?
  return content_type == HTML_CONTENT
end

#is_json?Boolean

Is the return type JSON?

Returns:



210
211
212
# File 'lib/gloo/objs/web_svr/page.rb', line 210

def is_json?
  return content_type == JSON_CONTENT
end

#is_text?Boolean

Is the return type TEXT?

Returns:



203
204
205
# File 'lib/gloo/objs/web_svr/page.rb', line 203

def is_text?
  return content_type == TEXT_CONTENT
end

#msg_renderObject

Get the expiration date for the certificate.



320
321
322
323
324
# File 'lib/gloo/objs/web_svr/page.rb', line 320

def msg_render
  content = self.render
  @engine.heap.it.set_to content 
  return content
end

#multiline_value?Boolean

Does this object support multi-line values? Initially only true for scripts.

Returns:



73
74
75
# File 'lib/gloo/objs/web_svr/page.rb', line 73

def multiline_value?
  return false
end

#page_layoutObject

Get the layout for this page.



184
185
186
187
188
189
190
# File 'lib/gloo/objs/web_svr/page.rb', line 184

def page_layout
  o = find_child LAYOUT
  return nil unless o

  o = Gloo::Objs::Alias.resolve_alias( @engine, o )
  return o
end

#page_layout_or_app_layoutObject

Get the layout for this page or if none for the app.



429
430
431
432
433
434
435
# File 'lib/gloo/objs/web_svr/page.rb', line 429

def page_layout_or_app_layout
  layout = page_layout
  return layout if layout

  return nil unless @engine.app_running?
  return @engine.running_app.obj.default_page_layout
end

#params_hashObject

Get the params hash from the child object. Returns nil if there is none.



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/gloo/objs/web_svr/page.rb', line 150

def params_hash
  params_can = find_child PARAMS
  return nil unless params_can

  h = {}
  params_can.children.each do |o|
    # resolve aliases
    o = Gloo::Objs::Alias.resolve_alias( @engine, o )
    h[ o.name ] = o.value
  end

  return h
end

#redirect_set?Boolean

Is there a redirect page set in the running app?

Returns:



341
342
343
344
# File 'lib/gloo/objs/web_svr/page.rb', line 341

def redirect_set?
  return false unless @engine.app_running?
  return @engine.running_app.obj.redirect
end

#render(request = nil) ⇒ Object

Render the page. If this is being called from the web server, the request will be passed in and will include request context such as params.



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/gloo/objs/web_svr/page.rb', line 368

def render request=nil
  @request = request

  # TODO : refactor this
  set_id if @request

  # Put params from request into params container.
  init_params

  # Run the on prerender script
  run_on_prerender

  # Get Params hash before running on render
  params = params_hash

  run_on_render
  return nil if redirect_set?

  if is_html?
    contents = render_html params
  elsif is_json?
    contents = render_json
  elsif is_text?
    contents = render_text params
  elsif is_file?
    contents = render_file params
  else
    @engine.err "Unknown content type: #{content_type}"
    return nil
  end

  run_after_render
  @request = nil
  return nil if redirect_set?

  return contents
end

#render_file(params) ⇒ Object

Render a file.



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/gloo/objs/web_svr/page.rb', line 543

def render_file params
  type = file_type
  inline_content = file_data
  if inline_content.nil?
    data = File.binread( file_path )
  else
    data = inline_content
  end
  code = Gloo::WebSvr::ResponseCode::SUCCESS
  fname = file_name
  download = download_file

  return Gloo::WebSvr::Response.new( 
    @engine, code, type, data, true, fname, download )
end

#render_html(params) ⇒ Object

Render the page as HTML.



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/gloo/objs/web_svr/page.rb', line 409

def render_html params
  head_obj = render_with_params head_content, :render_html, params
  body_obj = render_with_params body_content, :render_html, params

  layout = page_layout_or_app_layout
  if layout
    @engine.log.debug "Using Page Layout: #{layout.pn}"
    contents = layout.render_layout( head_obj, body_obj )
  else
    @engine.log.debug "No layout for page."
    contents = wrap( 'html', head_obj + body_obj )
  end

  return Gloo::WebSvr::Response.html_response( 
    @engine, contents, return_code )
end

#render_jsonObject

Render the page as JSON.



440
441
442
443
444
# File 'lib/gloo/objs/web_svr/page.rb', line 440

def render_json
  json_content = Gloo::Objs::Json.convert_obj_to_json( body )

  return Gloo::WebSvr::Response.json_response( @engine, json_content, return_code )
end

#render_text(params) ⇒ Object

Render the page as TEXT.



449
450
451
452
# File 'lib/gloo/objs/web_svr/page.rb', line 449

def render_text params
  text_content = render_with_params body_content, :render_text, params
  return Gloo::WebSvr::Response.text_response( @engine, text_content, return_code )
end

#render_with_params(obj, render_ƒ, params) ⇒ Object

Given an object and a render message, render the object. If the obj is nil, return an empty string. If the params are nil, no param rendering is done.



459
460
461
462
463
464
465
466
467
# File 'lib/gloo/objs/web_svr/page.rb', line 459

def render_with_params obj, render_ƒ, params
  return '' unless obj

  content = Element.render_obj( obj, render_ƒ, @engine )
  # content = Page.render_params( content, params ) if params
  content = @engine.running_app.obj.embedded_renderer.render content, params

  return content
end

#return_codeObject

Get the return code. SUCCESS is the default if none is set.



168
169
170
171
# File 'lib/gloo/objs/web_svr/page.rb', line 168

def return_code
  code = find_child RETURN_CODE
  return code ? code.value : Gloo::WebSvr::ResponseCode::SUCCESS
end

#run_after_renderObject

Run the on rendered script if there is one.



250
251
252
253
254
255
256
257
# File 'lib/gloo/objs/web_svr/page.rb', line 250

def run_after_render
  o = find_child AFTER_RENDER
  return unless o

  @engine.log.debug "running after_render for page"

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#run_on_prerenderObject

Run the on prerender script if there is one.



226
227
228
229
230
231
232
233
# File 'lib/gloo/objs/web_svr/page.rb', line 226

def run_on_prerender
  o = find_child ON_PRERENDER
  return unless o

  @engine.log.debug "running on_prerender for page"

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#run_on_renderObject

Run the on render script if there is one.



238
239
240
241
242
243
244
245
# File 'lib/gloo/objs/web_svr/page.rb', line 238

def run_on_render
  o = find_child ON_RENDER
  return unless o

  @engine.log.debug "running on_render for page"

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#set_idObject

Set the ID parameter if there is one.



349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/gloo/objs/web_svr/page.rb', line 349

def set_id
  return unless @request.request_params.id
  @engine.log.info "Setting ID: #{@request.request_params.id}"

  params_can = find_child PARAMS
  return nil unless params_can

  id_obj = params_can.find_child( ID )
  return unless id_obj

  id_obj.set_value( @request.request_params.id )
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



65
66
67
# File 'lib/gloo/objs/web_svr/page.rb', line 65

def set_value( new_value )
  self.value = new_value.to_s
end

#wrap(tag, content, id = nil, classes = nil) ⇒ Object

Wrap the content in the tag with id and class.



334
335
336
# File 'lib/gloo/objs/web_svr/page.rb', line 334

def wrap( tag, content, id=nil, classes=nil )
  return "<#{tag}>#{content}</#{tag}>"
end