Class: EditPage

Inherits:
XHTMLPage show all
Includes:
Assert, Localization
Defined in:
lib/ribit/webpage.rb

Constant Summary

Constants included from Localization

Localization::ACTION_CATEGORY_CREATION_NOTICE, Localization::ACTION_DEFAULT_PAGE_FAILED, Localization::ACTION_EDIT_CANCELED, Localization::ACTION_NEW_PAGE_OK, Localization::ACTION_SAVE_CATEGORY_OK, Localization::ACTION_SAVE_OK, Localization::NO_PARENT_OPTION, Localization::TITLE_CATEGORIES_PAGE, Localization::TITLE_NEW_CATEGORY, Localization::TITLE_NEW_PAGE

Instance Method Summary collapse

Methods included from Localization

#get_localized, #ribitData_defined?

Methods included from Assert

assert, #assert, assert_nil, #assert_nil, #assert_not_nil, assert_not_nil, raise_exception

Methods inherited from XHTMLPage

#add_head_element, #get_data, #insert_html_text, #set_message

Methods inherited from WebPage

#get_data, #output_page

Constructor Details

#initialize(ribitData, pageID, previousPageID, ribitConfig) ⇒ EditPage

Returns a new instance of EditPage.



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/ribit/webpage.rb', line 472

def initialize( ribitData, pageID, previousPageID, ribitConfig )
  assert_not_nil( ribitData )
  assert_not_nil( ribitConfig )
  assert_not_nil( previousPageID )
  
  @ribitData = ribitData
  @ribitConfig = ribitConfig
  @pageID = pageID
  @logger = RibitLogger.new( EditPage )
  @hiddenFormFields = Array.new
  @formActionParams = PropertyList.new
  
  @previousPageID = previousPageID
  add_hidden_form_field( HiddenFormInputTag.new( Constants::PREV_FORM_PARAM_NAME, previousPageID ) )
  
  template = ribitData.get_page_by_full_name( 'core::edittemplate' )
  super( template.data )
end

Instance Method Details

#add_form_action_params(key, value) ⇒ Object



502
503
504
# File 'lib/ribit/webpage.rb', line 502

def add_form_action_params( key, value )
  @formActionParams.put( key, value )
end

#add_hidden_form_field(hiddenFieldTag) ⇒ Object



497
498
499
# File 'lib/ribit/webpage.rb', line 497

def add_hidden_form_field( hiddenFieldTag )
  @hiddenFormFields.push( hiddenFieldTag )
end

#get_content_typeObject

Always text/html



492
493
494
# File 'lib/ribit/webpage.rb', line 492

def get_content_type
  return 'text/html'
end

#handle_element(id, element) ⇒ Object



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/ribit/webpage.rb', line 507

def handle_element( id, element )
  case id
  when 'text:title'
    if ( @pageID == Constants::NEW_PAGE_ID )
      insert_html_text( element, get_localized( TITLE_NEW_PAGE ) )
    else
      insert_html_text( element, @ribitData.get_page_by_id( @pageID ).title )
    end  
    
  when 'text:body'
    if ( @pageID == Constants::NEW_PAGE_ID )
      element.text = ''
    else
      #puts "### get_replacement_data: " + @ribitData[@pageID].data
      
      # allow entity encoding happen
      element.text = @ribitData.get_page_by_id( @pageID ).data
    end    
    
  when "replace:category"
    if ( @pageID == Constants::NEW_PAGE_ID )
      categoryFullName = @formActionParams.get( Constants::CATEGORY_REQUEST_PARAM_NAME )
      
      if ( /^::/.match( categoryFullName ) == nil )
        categoryFullName = '::' + categoryFullName
      end
    else
      categoryFullName = @ribitData.get_page_by_id( @pageID ).category.full_name
    end
    
    element.text = element.text.gsub( /#\{category\}/, categoryFullName )
    
  when "set:page-name"
    if ( @pageID == Constants::NEW_PAGE_ID )
      element.attributes['value'] = @formActionParams.get( Constants::PAGE_NAME_REQUEST_PARAM_NAME )
    else
      element.attributes['value'] = @ribitData.get_page_by_id( @pageID ).name
    end
    
    element.text = nil
    
  when 'link:validator'
    if ( element.name != 'a' )
      raise RibitException, "link:validator is not suitable for element #{element.name}", caller
    end
    
    # NOTE: this could be nicer but should work (we know too many details)
    if ( @pageID == Constants::NEW_PAGE_ID )
      viewUrl = EditNewActionAdapter.new(
        @formActionParams.get( Constants::PAGE_NAME_REQUEST_PARAM_NAME ),
        @formActionParams.get( Constants::CATEGORY_REQUEST_PARAM_NAME ),
        @previousPageID ).get_url
      
    else
      viewUrl = EditActionAdapter.new( @pageID ).get_url
    end
    baseUrlStr = @ribitConfig.get_base_url
    
    validator = W3CValidatorLinkActionAdapter.new( baseUrlStr + '/' + viewUrl.to_s )
    #@logger.debug( "validator url: " + validator.get_url )
    
    actionEle = LinkActionElement.new( id, element )
    actionEle.url = validator.get_url
    
  when 'action:form'
    if ( element.name != 'form' )
      raise RibitException, "action:form is not suitable for element #{element.name}", caller
    end
    
    adapter = FormActionAdapter.new( @pageID )
    
    # add action parameters
    @formActionParams.keys.each do |key|
      adapter.add_param( key, @formActionParams.get( key ) )
    end
    
    actionEle = FormActionElement.new( id, element )
    # NOTE: prevent double encoding (could be nicer)
    actionEle.url = adapter.get_url( false )
    
    if ( not @hiddenFormFields.empty? )
      # input fields can't be directly under 'form'
      p = DivTag.new()
      
      # NOTE: here we rely on that 'action' is FormActionElement
      actionEle.insert_to_top( p )
      
      @hiddenFormFields.each do |fieldTag|
        p.add_element( fieldTag )
      end
      
    end
  else
    super( id, element )
  end    
  
end