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

<%block name="title">
    ${_('%s Files Add') % 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()">
    ${_('Add New 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',enctype="multipart/form-data", class_="form-inline")}
            <h3 class="files_location">
              ${_('Location')}: ${h.files_breadcrumbs(c.repo_name,c.cs.raw_id,c.f_path)} /
              <span id="filename_container" class="file">
                  <input class="form-control input-small" type="text" value="" size="30" name="filename" id="filename" placeholder="${_('Enter filename...')}">
                  <input type="hidden" value="${c.f_path}" name="location" id="location">
                  ${_('or')} <button type="button" class="btn btn-default btn-sm" id="upload_file_enable">${_('Upload File')}</button>
              </span>
              <span id="upload_file_container" style="display:none">
                  <input class="form-control" type="file" size="20" name="upload_file" id="upload_file">
                  ${_('or')} <button type="button" class="btn btn-default btn-sm" id="file_enable">${_('Create New File')}</button>
              </span>
            </h3>
            <div id="body" class="panel panel-default">
              <div class="panel-heading clearfix">
                  <div class="pull-left">
                    <label>${_('New file type')}
                        <select class="form-control" id="mimetype" name="mimetype"></select>
                    </label>
                  </div>
              </div>
              <div class="panel-body no-padding">
                <textarea id="editor" name="content" style="display:none"></textarea>
              </div>
            </div>
            <div>
              <div>
                  <div>${_('Commit Message')}</div>
                  <textarea class="form-control" name="message" placeholder="${c.default_message}"></textarea>
              </div>
              <div class="buttons">
                ${h.submit('commit',_('Commit Changes'),class_="btn btn-success")}
                ${h.reset('reset',_('Reset'),class_="btn btn-default")}
              </div>
            </div>
            ${h.end_form()}
            <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.f_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;
                            }
                        }
                    });
                    var $filename_input = $('#filename');
                    $mimetype_select.change(function(e){
                        var selected = e.currentTarget;
                        var node = selected.options[selected.selectedIndex];
                        var detected_mode = CodeMirror.findModeByMIME(node.value);
                        setCodeMirrorMode(myCodeMirror, detected_mode);

                        var proposed_ext = CodeMirror.findExtensionByMode(detected_mode);
                        var file_data = CodeMirror.getFilenameAndExt($filename_input.val());
                        var filename = file_data['filename'] || 'filename1';
                        $filename_input.val(filename + '.' + proposed_ext);
                    });

                    // on type the new filename set mode
                    $filename_input.keyup(function(){
                        var file_data = CodeMirror.getFilenameAndExt(this.value);
                        if(file_data['ext'] != null){
                            var detected_mode = CodeMirror.findModeByExtension(file_data['ext']) || CodeMirror.findModeByMIME('text/plain');
                            if (detected_mode){
                                setCodeMirrorMode(myCodeMirror, detected_mode);
                                $mimetype_select.val(detected_mode.mime);
                            }
                        }
                    });
                });
            </script>
        </div>
    </div>
</div>
</%def>
