diff --git a/app/assets/javascripts/attributes_editor.js b/app/assets/javascripts/attributes_editor.js new file mode 100644 index 00000000..4304fdf8 --- /dev/null +++ b/app/assets/javascripts/attributes_editor.js @@ -0,0 +1,40 @@ +$(document).ready(function () { + $('.js-show-category-suggestions').click(function () { + var content_type = $(this).closest('.attributes-editor').data('content-type'); + var result_container = $(this).siblings(".suggest-categories-container").first(); + + $.ajax({ + dataType: "json", + url: "/api/v1/categories/suggest/" + content_type, + success: function (data) { + var existing_categories = $('.js-category-label').map(function(){ + return $.trim($(this).text()); + }).get(); + + var new_categories = data.filter(c => !existing_categories.includes(c)); + var any_suggestions = new_categories.length > 0; + if (any_suggestions) { + result_container.html(''); + $.each(new_categories, function(i, value) { + result_container.append( + $('') + .attr('href', '#') + .addClass('category-suggestion-link chip hoverable') + .text(value) + .on('click', function () { + $('.js-category-input').text($(this).text()); + $('.js-category-input').siblings('label').addClass('active'); + return false; + }) + ); + }); + } else { + result_container.text("It doesn't look like there are any suggestions right now. Since suggestions are constantly growing, check back later and there might be more!"); + } + } + }); + + $(this).hide(); + return false; + }); +}); \ No newline at end of file diff --git a/app/views/content/attributes.html.erb b/app/views/content/attributes.html.erb index 8ac39763..de744cf7 100644 --- a/app/views/content/attributes.html.erb +++ b/app/views/content/attributes.html.erb @@ -4,7 +4,7 @@ - + + <% end %> +
  • +
    + add + Add another category +
    +
    + <%= form_for(current_user.attribute_categories.build, method: :post) do |f| %> + <%= f.hidden_field :entity_type, value: @content_type %> +
    +
    + <%= f.text_area :label, class: 'materialize-textarea js-category-input' %> + <%= f.label :label, 'Category label' %> +
    +
    + <%= f.submit 'Add new category', class: "btn #{@content_type_class.color}" %> +
    +
    + +
    +
    + New: Notebook.ai can now suggest additional categories for your pages. +
    +

    +

    + <%= f.button "Show suggestions", class: 'small btn white black-text js-show-category-suggestions' %> +
    + <% end %> +
    +
  • + + \ No newline at end of file