###
### Main "edit" template
###
### General wrapper for edit actions
###
##
## Is the document being edited by another user?
## If yes, warn about it and show a forcing link:
##
#if($tdoc.getLocked() && "$!request.get('force')" == '')
  #template("startpage.vm")
  <div class="main">
   <div id="mainContentArea">
    ##
    $response.setStatus(423)
    #set($newquerystring = "$!{request.getQueryString().replaceAll('&', '&amp;').replaceAll('&amp;amp;', '&amp;')}"
                         + '&amp;force=1')
    #set($forceEditURL = $doc.getURL($xcontext.getAction(), ${newquerystring}))
    #xwikimessageboxstart($services.localization.render('notice') "$services.localization.render('doclockedby') $xwiki.getUserName($doc.getLockingUser())")
    <a href="$forceEditURL">$services.localization.render('forcelock')</a>
    #xwikimessageboxend()
    ##
   </div>## mainContentArea
  </div>## main
  #template("endpage.vm")
#else
  ##
  ## Determine what editor to show
  ##
  #if("$!editor" == '')
    ## Normally no special characters should exist in the editor parameter, so this is just a protection measure.
    #set($editor = "$!{escapetool.xml($request.getParameter('editor'))}")
    #if($editor == '')
      ## Determine the sheets using the default document translation because objects are accessible only from the default translation.
      #if(!$services.sheet.getSheets($doc, $xcontext.action).isEmpty())
        #set($editor = 'inline')
      #elseif (($xwiki.getUserPreference('editor') == 'Wysiwyg') && ($tdoc.syntax.toIdString() == 'xwiki/1.0' && !$tdoc.isAdvancedContent() || $services.wysiwyg.isSyntaxSupported($tdoc.syntax.toIdString())))
        #set($editor = 'wysiwyg')
      #else
        #set($editor = 'wiki')
      #end
    #end
  #end
  #if($editor == 'inline')
    ## The inline editor uses the view layout, unlike the rest of the editors.
    #template('startpage.vm')
    #template('editinline.vm')
    #template('endpage.vm')
  #else
  ##
  ## "Compute" the title
  ##
  #set ($title = $services.localization.render("platform.web.editors.${editor}.pageTitle", [$tdoc.plainTitle]))
  #if ($title == "platform.web.editors.${editor}.pageTitle")
    #set ($title = $services.localization.render('platform.web.editors.unknown.pageTitle', [$tdoc.plainTitle]))
  #end
  ##
  ## Header
  ##
  #template("xwikivars.vm")
  #template("layoutvars.vm")
  #template("htmlheader.vm")
  #if($displayMainMenu)
    <div id="menuview">
      #template("menus_view.vm")
    </div>
  #end
  #template("header.vm")
  #if($editor == 'wiki' || $editor == 'wysiwyg')
    <form id="edit" method="post" action="$doc.getURL('preview')" class="withLock form">
    <div class="hidden">
    ## CSRF prevention
    <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" />
    ## Pass the section number when the form is submitted in order to affect only the specified section.
    ## We need to test if the section number is present to prevent a NumberFormatException for the empty string.
    #if("$!{request.section}" != '')
      <input type="hidden" name="section" value="$!{escapetool.xml($request.section)}" />
    #end
    ## This input is used by the full screen widget: it stores the selector of the maximized element, so that the full screen can be restored when coming back from preview.
    <input type="hidden" name="x-maximized" value="$!{escapetool.xml($request.get('x-maximized'))}"/>
    </div>
  #end
  ##################
  ##   Edit zone  ##
  ##################
  <div id="xwikieditor">
  <div id="xwikieditorinner">
  <div class="leftsidecolumns main">
  <div id="editcolumn" class="editor editor-${editor}">
  <div class="alleditcontent">
  <div id="mainEditArea">
  #if($editor == 'wiki' || $editor == 'wysiwyg' || $editor == 'inline')
    <div class='edit-meta-tools'>
      <a id='editParentTrigger' class='tool edit-parent' href='#mainEditArea' title="$services.localization.render('core.editors.content.parentField.edit.title')">[$services.localization.render('core.editors.content.parentField.edit')]</a>
    </div>
  #end
  #template("hierarchy.vm")
  <div class="row">
    #if (($editor == 'wiki' || $editor == 'wysiwyg') && !$request.section)
      <div id="editMeta" class="col-xs-12 col-md-7">
        #template('editmeta.vm')
      </div>
    #else
      <div id="document-title" class="col-xs-12 col-md-7"><h1>
      #if($editor == 'rights')
        $services.localization.render('core.editors.rights.title', [$escapetool.xml($doc.plainTitle), $doc.getURL()])
      #elseif($editor == 'object')
        $services.localization.render('core.editors.object.title', [$escapetool.xml($doc.plainTitle), $doc.getURL()])
      #elseif($editor == 'class')
        $services.localization.render('core.editors.class.title', [$escapetool.xml($doc.fullName), $doc.getURL()])
      #end
      </h1></div>
    #end
    #if($displayContentMenu)
      <div class="col-xs-12 col-md-5">
        #template("menus_content.vm")
      </div>
    #end
  </div> ## row
  #template("edit${editor}.vm")
  ## This javascript must be placed here since it uses velocity variables set in the template above.
  <script type="text/javascript">
    //<![CDATA[
    if ($('tmEditWysiwyg')) {
      // Manage switch from wiki to wysiwyg, content must be preserved between those editors.
      Event.observe($('tmEditWysiwyg'), "click", function(event) {
        #if($doc.syntax.toIdString() == "xwiki/1.0")
        if (checkAdvancedContent("$services.localization.render('checkadvancedcontent')") == false) {
          Event.stop(event);
          return;
        }
        #end
        document.forms.${formname}.action='$doc.getURL("edit", "editor=wysiwyg&$languageparams")';
        document.forms.${formname}.submit();
        Event.stop(event);
      });

      // Manage switch from wysiwyg to wiki, content must be preserved between those editors.
      Event.observe($('tmEditWiki'), "click", function(event) {
        document.forms.${formname}.action='$doc.getURL("edit", "editor=wiki&$languageparams")';
        document.forms.${formname}.submit();
        Event.stop(event);
      });
    }
    //]]>
  </script>
  </div> ## mainEditArea
  </div> ## alleditcontent
  </div> ## editcolumn
  <div class="clearfix"></div>
  </div> ## leftsidecolumns
  #template("editpanels.vm")
  <div class="clearfix"></div>
  </div> ## xwikieditorinner
  </div> ## xwikieditor
  ##
  ## Footer
  ##
  #if($editor == 'wiki' || $editor == 'wysiwyg')
    </form>
  #end
  #template("footer.vm")
  #template("htmlfooter.vm")
  #end ## $editor != 'inline'
#end
