Enable snippet exporting.

This commit is contained in:
Jason Croft 2016-05-12 12:28:08 -04:00
parent c16345ab12
commit 30e602a740
3 changed files with 103 additions and 7 deletions

View file

@ -554,8 +554,8 @@ var ui = {
markdown: $(".ui-view-area .markdown-body") markdown: $(".ui-view-area .markdown-body")
}, },
modal: { modal: {
snippetProjects: $("#snippetImportModalProjects"), snippetImportProjects: $("#snippetImportModalProjects"),
snippetSnippets: $("#snippetImportModalSnippets") snippetImportSnippets: $("#snippetImportModalSnippets")
} }
}; };
@ -1150,7 +1150,33 @@ ui.toolbar.export.googleDrive.click(function (e) {
//export to gist //export to gist
ui.toolbar.export.gist.attr("href", noteurl + "/gist"); ui.toolbar.export.gist.attr("href", noteurl + "/gist");
//export to snippet //export to snippet
ui.toolbar.export.snippet.attr("href", noteurl + "/snippet"); ui.toolbar.export.snippet.click(function() {
$.get(serverurl + '/gitlab')
.success(function (data) {
$("#snippetExportModalAccessToken").val(data.accesstoken);
$("#snippetExportModalBaseURL").val(data.baseURL);
$("#snippetExportModalLoading").hide();
$("#snippetExportModal").modal('toggle');
$("#snippetExportModalProjects").find('option').remove().end().append('<option value="init" selected="selected" disabled="disabled">Select From Available Projects</option>');
if (data.projects) {
data.projects.sort(function(a,b) {
return (a.path_with_namespace < b.path_with_namespace) ? -1 : ((a.path_with_namespace > b.path_with_namespace) ? 1 : 0);
});
data.projects.forEach(function(project) {
$('<option>').val(project.id).text(project.path_with_namespace).appendTo("#snippetExportModalProjects");
});
$("#snippetExportModalProjects").prop('disabled',false);
}
$("#snippetExportModalLoading").hide();
})
.error(function (data) {
showMessageModal('<i class="fa fa-gitlab"></i> Import from Snippet', 'Unable to fetch gitlab parameters :(', '', '', false);
})
.complete(function () {
//na
});
return false;
});
//import from dropbox //import from dropbox
ui.toolbar.import.dropbox.click(function () { ui.toolbar.import.dropbox.click(function () {
var options = { var options = {
@ -1256,7 +1282,7 @@ ui.toolbar.beta.slide.attr("href", noteurl + "/slide");
//modal actions //modal actions
//snippet projects //snippet projects
ui.modal.snippetProjects.change(function() { ui.modal.snippetImportProjects.change(function() {
var accesstoken = $("#snippetImportModalAccessToken").val(), var accesstoken = $("#snippetImportModalAccessToken").val(),
baseURL = $("#snippetImportModalBaseURL").val(), baseURL = $("#snippetImportModalBaseURL").val(),
project = $("#snippetImportModalProjects").val(); project = $("#snippetImportModalProjects").val();
@ -1280,7 +1306,7 @@ ui.modal.snippetProjects.change(function() {
}); });
}); });
//snippet snippets //snippet snippets
ui.modal.snippetSnippets.change(function() { ui.modal.snippetImportSnippets.change(function() {
var project = $("#snippetImportModalProjects").val(), var project = $("#snippetImportModalProjects").val(),
snippet = $("#snippetImportModalSnippets").val(); snippet = $("#snippetImportModalSnippets").val();
@ -1478,6 +1504,31 @@ $("#snippetImportModalConfirm").click(function () {
} }
}); });
//snippet export modal
$("#snippetExportModalConfirm").click(function() {
var accesstoken = $("#snippetExportModalAccessToken").val(),
baseURL = $("#snippetExportModalBaseURL").val(),
data = {
title: $("#snippetExportModalTitle").val(),
file_name: $("#snippetExportModalFileName").val(),
code: editor.getValue(),
visibility_level: $("#snippetExportModalVisibility").val()
};
$("#snippetExportModalLoading").show();
var fullURL = baseURL + '/api/v3/projects/' + $("#snippetExportModalProjects").val() + '/snippets?access_token=' + accesstoken;
$.post(fullURL
, data
, function(ret) {
$("#snippetExportModalLoading").hide();
$('#snippetExportModal').modal('hide');
var redirect = baseURL + '/' + $("#snippetExportModalProjects option[value='" + $("#snippetExportModalProjects").val() + "']").text() + '/snippets/' + ret.id;
showMessageModal('<i class="fa fa-gitlab"></i> Export to Snippet', 'Export Successful!', redirect, 'View Snippet Here', true);
}
, 'json'
);
});
function parseToEditor(data) { function parseToEditor(data) {
var parsed = toMarkdown(data); var parsed = toMarkdown(data);
if (parsed) if (parsed)

View file

@ -189,4 +189,49 @@
</div> </div>
</div> </div>
</div> </div>
<!-- snippet export modal -->
<div class="modal fade" id="snippetExportModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title" id="myModalLabel">Export to Snippet</h4>
</div>
<div class="modal-body">
<input type="hidden" id="snippetExportModalAccessToken" />
<input type="hidden" id="snippetExportModalBaseURL" />
<div class="ui-field-contain" style="display:table;margin-bottom:10px;width:100%;">
<div style="display:table-row;margin-bottom:5px;">
<label style="display:table-cell;">Title:</label>
<input class="form-control" placeholder="new snippet" type="text" id="snippetExportModalTitle" />
</div>
<div style="display:table-row;margin-bottom:5px;">
<label style="display:table-cell;">File Name:</label>
<input class="form-control" placeholder="new_snippet.md" type="text" id="snippetExportModalFileName" />
</div>
<div style="display:table-row;margin-bottom:5px;">
<label style="display:table-cell;">Project:</label>
<select class="form-control" id="snippetExportModalProjects" style="display:table-cell;">
<option value="init" selected="selected" disabled="disabled">Select From Available Projects</option>
</select>
</div>
<div style="display:table-row;margin-bottom:5px;">
<label style="display:table-cell;">Visibility:</label>
<select class="form-control" id="snippetExportModalVisibility" style="display:table-cell;">
<option value="" selected="selected" disabled="disabled">Select Visibility Level</option>
<option value="0">Private</option>
<option value="10">Internal</option>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<span id="snippetExportModalLoading"><i class="fa fa-refresh fa-spin"></i></span>
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" id="snippetExportModalConfirm">Export</button>
</div>
</div>
</div>
</div>
<%- include modal %> <%- include modal %>

View file

@ -43,7 +43,7 @@
</li> </li>
<% } %> <% } %>
<% if(typeof gitlab !== 'undefined' && gitlab) { %> <% if(typeof gitlab !== 'undefined' && gitlab) { %>
<li role="presentation"><a role="menuitem" class="ui-save-snippet" tabindex="-1" href="#" target="_blank"><i class="fa fa-gitlab fa-fw"></i> Snippet</a> <li role="presentation"><a role="menuitem" class="ui-save-snippet" href="#" data-toggle="modal" data-target="#snippetExportModal"><i class="fa fa-gitlab fa-fw"></i> Snippet</a>
</li> </li>
<% } %> <% } %>
<li class="divider"></li> <li class="divider"></li>
@ -136,7 +136,7 @@
</li> </li>
<% } %> <% } %>
<% if(typeof gitlab !== 'undefined' && gitlab) { %> <% if(typeof gitlab !== 'undefined' && gitlab) { %>
<li role="presentation"><a role="menuitem" class="ui-save-snippet" tabindex="-1" href="#" target="_blank"><i class="fa fa-gitlab fa-fw"></i> Snippet</a> <li role="presentation"><a role="menuitem" class="ui-save-snippet" href="#" data-toggle="modal" data-target="#snippetExportModal"><i class="fa fa-gitlab fa-fw"></i> Snippet</a>
</li> </li>
<% } %> <% } %>
<li class="divider"></li> <li class="divider"></li>