From 24c2a0146599e41e976433693834c2b929ac1058 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Tue, 4 Feb 2020 17:03:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=80=E6=9C=AF=E6=A0=87=E7=AD=BEtag?= =?UTF-8?q?=E7=9A=84html+js+css?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modals/admin-edit-tag-repertoire-modal.js | 31 +++++++++ .../admins/tag_repertoires/index.js | 65 +++++++++++++++++++ .../admins/tag_repertoires_controller.rb | 43 ++++++++++++ .../_edit_sub_repertoire_modal.html.erb | 2 +- .../admins/tag_repertoires/destroy.js.erb | 2 + app/views/admins/tag_repertoires/edit.js.erb | 2 + .../admins/tag_repertoires/index.html.erb | 15 +++++ .../_create_tag_repertoire_modal.html.erb | 28 ++++++++ .../_edit_tag_repertoire_modal.html.erb | 23 +++++++ .../tag_repertoires/shared/_list.html.erb | 25 +++++++ .../admins/tag_repertoires/update.js.erb | 2 + 11 files changed, 237 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/admins/modals/admin-edit-tag-repertoire-modal.js create mode 100644 app/assets/javascripts/admins/tag_repertoires/index.js create mode 100644 app/controllers/admins/tag_repertoires_controller.rb create mode 100644 app/views/admins/tag_repertoires/destroy.js.erb create mode 100644 app/views/admins/tag_repertoires/edit.js.erb create mode 100644 app/views/admins/tag_repertoires/index.html.erb create mode 100644 app/views/admins/tag_repertoires/shared/_create_tag_repertoire_modal.html.erb create mode 100644 app/views/admins/tag_repertoires/shared/_edit_tag_repertoire_modal.html.erb create mode 100644 app/views/admins/tag_repertoires/shared/_list.html.erb create mode 100644 app/views/admins/tag_repertoires/update.js.erb diff --git a/app/assets/javascripts/admins/modals/admin-edit-tag-repertoire-modal.js b/app/assets/javascripts/admins/modals/admin-edit-tag-repertoire-modal.js new file mode 100644 index 000000000..e45a0789c --- /dev/null +++ b/app/assets/javascripts/admins/modals/admin-edit-tag-repertoire-modal.js @@ -0,0 +1,31 @@ +$(document).on('turbolinks:load', function() { + $('.admin-modal-container').on('show.bs.modal', '.modal.admin-edit-tag-repertoire-modal', function(){ + var $modal = $('.modal.admin-edit-tag-repertoire-modal'); + var $form = $modal.find('form.admin-edit-tag-repertoire-form'); + + $form.validate({ + errorElement: 'span', + errorClass: 'danger text-danger', + rules: { + 'tag_repertoire[name]': { + required: true, + maxlength: 20 + } + } + }); + + $modal.on('click', '.submit-btn', function(){ + $form.find('.error').html(''); + var url = $form.attr('action'); + + if ($form.valid()) { + $.ajax({ + method: 'PATCH', + dataType: 'script', + url: url, + data: $form.serialize() + }); + } + }); + }); +}); \ No newline at end of file diff --git a/app/assets/javascripts/admins/tag_repertoires/index.js b/app/assets/javascripts/admins/tag_repertoires/index.js new file mode 100644 index 000000000..82e182feb --- /dev/null +++ b/app/assets/javascripts/admins/tag_repertoires/index.js @@ -0,0 +1,65 @@ +$(document).on('turbolinks:load', function() { + if ($('body.admins-tag-repertoires-index-page').length > 0) { + + // ============== 新建 =============== + var $modal = $('.modal.admin-create-tag-repertoire-modal'); + var $form = $modal.find('form.admin-create-tag-repertoire-form'); + var $nameInput = $form.find('input[name="name"]'); + + $form.validate({ + errorElement: 'span', + errorClass: 'danger text-danger', + rules: { + name: { + required: true + } + } + }); + + // modal ready fire + $modal.on('show.bs.modal', function () { + $nameInput.val(''); + }); + + $modal.on('click', '.submit-btn', function(){ + $form.find('.error').html(''); + + if ($form.valid()) { + var url = $form.data('url'); + + $.ajax({ + method: 'POST', + dataType: 'json', + url: url, + data: $form.serialize(), + success: function(){ + $.notify({ message: '创建成功' }); + $modal.modal('hide'); + + setTimeout(function(){ + window.location.reload(); + }, 500); + }, + error: function(res){ + var data = res.responseJSON; + $form.find('.error').html(data.message); + } + }); + } + }); + + $(".tag-repertoire-list-container").on("change", '.tag-repertoire-source-form', function () { + var s_id = $(this).attr("data-id"); + var s_value = $(this).val(); + var s_name = $(this).attr("name"); + var json = {}; + json[s_name] = s_value; + $.ajax({ + url: "/admins/tag_repertoires/" + s_id, + type: "PUT", + dataType:'script', + data: json + }); + }); + } +}); \ No newline at end of file diff --git a/app/controllers/admins/tag_repertoires_controller.rb b/app/controllers/admins/tag_repertoires_controller.rb new file mode 100644 index 000000000..57ed66b1c --- /dev/null +++ b/app/controllers/admins/tag_repertoires_controller.rb @@ -0,0 +1,43 @@ +class Admins::TagRepertoiresController < Admins::BaseController + + def index + @sub_repertoire = current_sub_repertoire + @tag_repertoires = current_sub_repertoire.tag_repertoires + end + + def create + name = params[:name].to_s.strip + return render_error('名称重复') if current_sub_repertoire.tag_repertoires.where(name: name).exists? + TagRepertoire.create!(name: name, sub_repertoire_id: current_sub_repertoire.id) + render_ok + end + + def edit + @tag_repertoire = current_tag_repertoire + end + + def update + if params[:tag_repertoire] && params[:tag_repertoire][:name].present? + name = params[:tag_repertoire][:name].to_s.strip + current_tag_repertoire.update_attributes!(name: name) + end + @tag_repertoires = current_tag_repertoire.sub_repertoire&.tag_repertoires + end + + def destroy + @tag_repertoire_id = params[:id] + current_tag_repertoire.destroy! + end + + private + + def current_sub_repertoire + @_current_sub_repertoire = SubRepertoire.find params[:sub_repertoire_id] + end + + def current_tag_repertoire + @_current_tag_repertoire = TagRepertoire.find params[:id] + end + + +end \ No newline at end of file diff --git a/app/views/admins/sub_repertoires/shared/_edit_sub_repertoire_modal.html.erb b/app/views/admins/sub_repertoires/shared/_edit_sub_repertoire_modal.html.erb index a92bd8c58..28ff8a169 100644 --- a/app/views/admins/sub_repertoires/shared/_edit_sub_repertoire_modal.html.erb +++ b/app/views/admins/sub_repertoires/shared/_edit_sub_repertoire_modal.html.erb @@ -20,4 +20,4 @@ - \ No newline at end of file + diff --git a/app/views/admins/tag_repertoires/destroy.js.erb b/app/views/admins/tag_repertoires/destroy.js.erb new file mode 100644 index 000000000..710f10ca9 --- /dev/null +++ b/app/views/admins/tag_repertoires/destroy.js.erb @@ -0,0 +1,2 @@ +$.notify({ message: '删除成功' }); +$(".tag-repertoire-item-<%= @tag_repertoire_id %>").remove(); \ No newline at end of file diff --git a/app/views/admins/tag_repertoires/edit.js.erb b/app/views/admins/tag_repertoires/edit.js.erb new file mode 100644 index 000000000..5969eedf7 --- /dev/null +++ b/app/views/admins/tag_repertoires/edit.js.erb @@ -0,0 +1,2 @@ +$('.admin-modal-container').html("<%= j( render partial: 'admins/tag_repertoires/shared/edit_tag_repertoire_modal', locals: { tag_repertoire: @tag_repertoire } ) %>"); +$('.modal.admin-edit-tag-repertoire-modal').modal('show'); \ No newline at end of file diff --git a/app/views/admins/tag_repertoires/index.html.erb b/app/views/admins/tag_repertoires/index.html.erb new file mode 100644 index 000000000..122cfc6f6 --- /dev/null +++ b/app/views/admins/tag_repertoires/index.html.erb @@ -0,0 +1,15 @@ +<% define_admin_breadcrumbs do %> + <% add_admin_breadcrumb('技术体系', admins_repertoires_path) %> + <% add_admin_breadcrumb(@sub_repertoire&.repertoire&.name, admins_sub_repertoires_path(repertoire_id: @sub_repertoire&.repertoire_id)) %> + <% add_admin_breadcrumb(@sub_repertoire.name) %> +<% end %> + +
序号 | +知识标签 | +操作 | +
---|---|---|
<%= index + 1 %> | +<%= tag.name %> | ++ <%= link_to '编辑', edit_admins_tag_repertoire_path(tag), remote: true, class: 'action' %> + <%= delete_link '删除', admins_tag_repertoire_path(tag, element: ".tag-repertoire-item-#{tag.id}"), class: 'delete-tag-repertoire-action' %> + | +