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

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

<%block name="js_extra">
  <script type="text/javascript" src="${h.url('/codemirror/lib/codemirror.js')}"></script>
  <script type="text/javascript" src="${h.url('/js/codemirror_loadmode.js')}"></script>
  <script type="text/javascript" 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="box">
    <!-- box / title -->
    <div class="title">
        ${self.breadcrumbs()}
        <ul class="links">
            <li>
              <span><a href="#">${_('Branch')}: ${c.cs.branch}</a></span>
            </li>
        </ul>
    </div>
    <div class="table" id="edit">
        <div id="files_data">
          ${h.form(h.url.current(),method='post',id='eform',enctype="multipart/form-data", class_="form-horizontal")}
          <h3 class="files_location">
            ${_('Location')}: ${h.files_breadcrumbs(c.repo_name,c.cs.raw_id,c.f_path)} /
              <span id="filename_container" class="file reviewer_ac">
                  <input class="input-small" type="text" value="" size="30" name="filename" id="filename" placeholder="${_('Enter filename...')}">
                  <input type="hidden" value="${c.f_path}" size="30" name="location" id="location">
                  ${_('or')} <div class="btn btn-small" id="upload_file_enable">${_('Upload File')}</div>
              </span>
              <span id="upload_file_container" class="reviewer_ac" style="display:none">
                  <input type="file"  size="20" name="upload_file" id="upload_file">
                  ${_('or')} <div class="btn btn-small" id="file_enable">${_('Create New File')}</div>
              </span>
          </h3>
            <div id="body" class="codeblock">
            <div class="code-header" id="set_mode_header">
                <label class="commit" for="set_mode">${_('New file mode')}</label>
                <select id="set_mode" name="set_mode"/>
            </div>
                <div id="editor_container">
                    <pre id="editor_pre"></pre>
                    <textarea id="editor" name="content" style="display:none"></textarea>
                </div>
                <div style="padding: 10px;color:#666666">${_('Commit Message')}</div>
                <textarea id="commit" name="message" style="height: 100px;width: 99%;margin-left:4px" placeholder="${c.default_message}"></textarea>
            </div>
            <div style="text-align: left;padding-top: 5px">
            ${h.submit('commit',_('Commit Changes'),class_="btn btn-small btn-success")}
            ${h.reset('reset',_('Reset'),class_="btn btn-small")}
            </div>
            ${h.end_form()}
            <script type="text/javascript">
            var reset_url = "${h.url('files_home',repo_name=c.repo_name,revision=c.cs.raw_id,f_path=c.f_path)}";
            var myCodeMirror = initCodeMirror('editor', "${request.script_name}", reset_url);

            //inject new modes, based on codeMirrors modeInfo object
            $('#set_mode').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;
                    }
                }
            });
            $('#set_mode').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 filenameInput = $('#filename');
                var proposed_ext = CodeMirror.findExtensionByMode(detected_mode);
                var file_data = CodeMirror.getFilenameAndExt(filenameInput.val());
                var filename = file_data['filename'] || 'filename1';
                filenameInput.val(filename + '.' + proposed_ext);
            });

            // on type the new filename set mode
            $('#filename').keyup(function(e){
                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);
                        $('#set_mode').val(detected_mode.mime);
                    }
                }
            });
            </script>
        </div>
    </div>
</div>
</%def>
