refactoring

This commit is contained in:
Mojca Lorber 2017-06-06 13:18:44 +02:00
parent acdea2198d
commit 39dda1ad5d
6 changed files with 29 additions and 76 deletions

View file

@ -3,55 +3,5 @@
$('.delete-repo-option').initializeModal('#delete-repo-modal');
$('.rename-repo-option').initializeModal('#rename-repo-modal');
$('.create-new-repository').initializeModal('#create-new-repo-modal');
})();
// create new
function init() {
initCreateNewModal();
}
function initCreateNewModal() {
var link = $("#create-new-repository");
var modal = $("#create-new-modal");
var submitBtn = modal.find(".modal-footer [data-action='submit']");
link.on("click", function() {
$.ajax({
url: link.attr("data-url"),
type: "GET",
dataType: "json",
success: function (data) {
var modalBody = modal.find(".modal-body");
modalBody.html(data.html);
modalBody.find("form")
.on("ajax:success", function(ev, data, status) {
// Redirect to index page
$(location).attr("href", data.url);
})
.on("ajax:error", function(ev, data, status) {
// Display errors if needed
$(this).renderFormErrors("repository", data.responseJSON);
});
modal.modal("show");
modalBody.find("input[type='text']").focus();
},
error: function (error) {
// TODO
}
});
});
submitBtn.on("click", function() {
// Submit the form inside modal
$(this).closest(".modal").find(".modal-body form").submit();
});
modal.on("hidden.bs.modal", function(e) {
modal.find(".modal-body form").off("ajax:success ajax:error");
modal.find(".modal-body").html("");
});
}
init();

View file

@ -16,7 +16,7 @@ class RepositoriesController < ApplicationController
format.json {
render json: {
html: render_to_string({
partial: "repositories/index/create_new_modal_body.html.erb"
partial: "create_new_repository_modal.html.erb"
})
}
}

View file

@ -0,0 +1,23 @@
<div class="modal" id="create-new-repo-modal" tabindex="-1" role="dialog" aria-labelledby="create-new-modal-label">
<%= bootstrap_form_for [@team, @new_repository], remote: :true do |f| %>
<div class="modal-dialog" role="document">
<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="create-new-modal-label">
<%= t("repositories.index.modal_create.title") %>
</h4>
</div>
<div class="modal-body">
<%= f.text_field :name,
label: t("repositories.index.modal_create.name_label"),
placeholder: t("repositories.index.modal_create.name_placeholder") %>
</div>
<div class="modal-footer">
<%= f.submit t("repositories.index.modal_create.submit"), class: "btn btn-primary" %>
<button type="button" class="btn btn-default" data-dismiss="modal"><%= t("general.cancel") %></button>
</div>
</div>
</div>
<% end %>
</div>

View file

@ -3,7 +3,6 @@
<% if current_team %>
<%= render partial: "repositories/breadcrumbs.html.erb",
locals: { teams: @teams, current_team: current_team, type: @type } %>
<%= render partial: "repositories/index/create_new_modal.html.erb" %>
<% active_repo = @repositories.find_by_id(params[:repository]) %>
<% active_repo = @repositories.first if !active_repo %>
@ -23,10 +22,11 @@
<% unless can_create_new_repository(current_team) %>
class="disabled"
<% end %>>
<a href='#'
data-url="<%= create_new_modal_team_repositories_path %>"
<a
<% if can_create_new_repository(current_team) %>
id='create-new-repository'
href="<%= create_new_modal_team_repositories_path %>"
class='create-new-repository'
data-remote='true'
<% end %>>
<span class="glyphicon glyphicon-plus"></span>
<span class="hidden-xs">&nbsp;<%= t('repositories.index.add_new_repository_tab') %></span>

View file

@ -1,17 +0,0 @@
<div class="modal" id="create-new-modal" tabindex="-1" role="dialog" aria-labelledby="create-new-modal-label">
<div class="modal-dialog" role="document">
<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="create-new-modal-label">
<%= t("repositories.index.modal_create.title") %>
</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-action="submit"><%= t("repositories.index.modal_create.submit") %></button>
<button type="button" class="btn btn-default" data-dismiss="modal"><%= t("general.cancel") %></button>
</div>
</div>
</div>
</div>

View file

@ -1,3 +0,0 @@
<%= bootstrap_form_for [@team, @new_repository], remote: :true do |f| %>
<%= f.text_field :name, label: t("repositories.index.modal_create.name_label"), placeholder: t("repositories.index.modal_create.name_placeholder") %>
<% end %>