<%inherit file="/base/base.html"/>

<%block name="title">
    ${_('%s File Edit') % c.repo_name}
</%block>

<%block name="js_extra">
  <script src="${h.url('/codemirror/lib/codemirror.js')}"></script>
  <script src="${h.url('/js/codemirror_loadmode.js')}"></script>
  <script src="${h.url('/codemirror/mode/meta.js')}"></script>
</%block>
<%block name="css_extra">
  <link rel="stylesheet" type="text/css" href="${h.url('/codemirror/lib/codemirror.css')}"/>
</%block>

<%block name="header_menu">
    ${self.menu('repositories')}
</%block>

<%def name="breadcrumbs_links()">
    ${_('Edit file')} @ ${h.show_id(c.cs)}
</%def>

<%def name="main()">
${self.repo_context_bar('files')}
<div class="panel panel-primary">
    <div class="panel-heading clearfix">
        <div class="pull-left">
            ${self.breadcrumbs()}
        </div>
        <div class="pull-right">
            <a href="#">${_('Branch')}: ${c.cs.branch}</a>
        </div>
    </div>
    <div class="panel-body" id="edit">
        <div id="files_data">
            ${h.form(h.url.current(),method='post',id='eform')}
            <h3 class="files_location">
              ${_('Location')}: ${h.files_breadcrumbs(c.repo_name,c.cs.raw_id,c.file.path)}
            </h3>
            <div id="body" class="panel panel-default">
              <div class="panel-heading clearfix form-inline form-group-sm">
                    <span>
                        <span><i class="icon-doc-inv"></i></span>
                        <span>${h.link_to(h.show_id(c.file.changeset),h.url('changeset_home',repo_name=c.repo_name,revision=c.file.changeset.raw_id))}</span>
                        <span>${h.format_byte_size(c.file.size,binary=True)}</span>
                        <span>${c.file.mimetype}</span>
                        <select class="form-control" id="mimetype" name="mimetype"></select>
                    </span>
                    <span class="pull-right buttons">
                      ${h.link_to(_('Show Annotation'),h.url('files_annotate_home',repo_name=c.repo_name,revision=c.cs.raw_id,f_path=c.f_path),class_="btn btn-default btn-xs")}
                      ${h.link_to(_('Show as Raw'),h.url('files_raw_home',repo_name=c.repo_name,revision=c.cs.raw_id,f_path=c.f_path),class_="btn btn-default btn-xs")}
                      ${h.link_to(_('Download as Raw'),h.url('files_rawfile_home',repo_name=c.repo_name,revision=c.cs.raw_id,f_path=c.f_path),class_="btn btn-default btn-xs")}
                      % if h.HasRepoPermissionLevel('write')(c.repo_name):
                       % if not c.file.is_binary:
                        ${h.link_to(_('Source'),h.url('files_home',repo_name=c.repo_name,revision=c.cs.raw_id,f_path=c.f_path),class_="btn btn-default btn-xs")}
                       % endif
                      % endif
                    </span>
              </div>
              <div class="panel-body no-padding">
                <textarea id="editor" name="content" style="display:none">${h.escape(h.safe_str(c.file.content))|n}</textarea>
              </div>
            </div>
            <div>
              <div class="form-group">
                  <label>${_('Commit Message')}</label>
                  <textarea class="form-control" id="commit" name="message" placeholder="${c.default_message}"></textarea>
              </div>
              <div class="form-group buttons">
                ${h.submit('commit',_('Commit Changes'),class_="btn btn-success")}
                ${h.reset('reset',_('Reset'),class_="btn btn-default")}
              </div>
            </div>
            ${h.end_form()}
        </div>
    </div>
</div>

<script>'use strict';
    $(document).ready(function(){
        var reset_url = ${h.jshtml(h.url('files_home',repo_name=c.repo_name,revision=c.cs.raw_id,f_path=c.file.path))};
        var myCodeMirror = initCodeMirror('editor', ${h.jshtml(request.script_name)}, reset_url);

       //inject new modes, based on codeMirrors modeInfo object
        var $mimetype_select = $('#mimetype');
        $mimetype_select.each(function(){
            var modes_select = this;
            var index = 1;
            for(var i=0;i<CodeMirror.modeInfo.length;i++){
                var m = CodeMirror.modeInfo[i];
                var opt = new Option(m.name, m.mime);
                $(opt).attr('mode', m.mode);
                if (m.mime == 'text/plain') {
                    // default plain text
                    $(opt).prop('selected', true);
                    modes_select.options[0] = opt;
                } else {
                    modes_select.options[index++] = opt;
                }
            }
        });
        // try to detect the mode based on the file we edit
        var detected_mode = CodeMirror.findModeByExtension(${h.js(c.file.extension)});
        if(detected_mode){
            setCodeMirrorMode(myCodeMirror, detected_mode);
            $($mimetype_select.find('option[value="'+detected_mode.mime+'"]')[0]).prop('selected', true);
        }

        $mimetype_select.on('change', function(e){
            var selected = e.currentTarget;
            var node = selected.options[selected.selectedIndex];
            var detected_mode = CodeMirror.findModeByMIME(node.value);
            setCodeMirrorMode(myCodeMirror, detected_mode);
        });
    });
</script>
</%def>
