Project

General

Profile

Patch #34122 » 0001-Store-autocomplete-data-sources-in-a-JS.patch

Marius BĂLTEANU, 2020-10-16 08:23

View differences:

app/helpers/application_helper.rb
1741 1741
    end
1742 1742
  end
1743 1743

  
1744
  def autocomplete_data_sources(project)
1745
    {
1746
      issues: auto_complete_issues_path(:project_id => project, :q => '')
1747
    }
1748
  end
1749

  
1750
  def heads_for_auto_complete(project)
1751
    data_sources = autocomplete_data_sources(project)
1752

  
1753
    javascript_tag(
1754
      "rm = window.rm || {};" +
1755
      "rm.AutoComplete = rm.AutoComplete || {};" +
1756
      "rm.AutoComplete.dataSources = '#{data_sources.to_json}';"
1757
    )
1758
  end
1759

  
1744 1760
  private
1745 1761

  
1746 1762
  def wiki_helper
app/helpers/custom_fields_helper.rb
84 84
    if custom_value.custom_field.full_text_formatting?
85 85
      css += ' wiki-edit'
86 86
      data = {
87
        :auto_complete => true,
88
        :issues_url => auto_complete_issues_path(:project_id => custom_value.customized.project, :q => '')
89
      } if custom_value.customized&.try(:project)
87
        :auto_complete => true
88
      }
90 89
    end
91 90
    custom_value.custom_field.format.edit_tag(
92 91
      self,
......
133 132
    if custom_field.full_text_formatting?
134 133
      css += ' wiki-edit'
135 134
      data = {
136
        :auto_complete => true,
137
        :issues_url => auto_complete_issues_path(:q => '')
135
        :auto_complete => true
138 136
      }
139 137
    end
140 138
    custom_field.format.bulk_edit_tag(
app/views/documents/_form.html.erb
5 5
<p><%= f.text_field :title, :required => true, :size => 60 %></p>
6 6
<p><%= f.text_area :description, :cols => 60, :rows => 15, :class => 'wiki-edit',
7 7
                  :data => {
8
                      :auto_complete => true,
9
                      :issues_url => auto_complete_issues_path(:project_id => @project, :q => '')
8
                      :auto_complete => true
10 9
                  }
11 10
%></p>
12 11

  
app/views/issues/_edit.html.erb
31 31
      <fieldset><legend><%= l(:field_notes) %></legend>
32 32
      <%= f.text_area :notes, :cols => 60, :rows => 10, :class => 'wiki-edit',
33 33
            :data => {
34
                :auto_complete => true,
35
                :issues_url => auto_complete_issues_path(:project_id => @issue.project, :q => '')
34
                :auto_complete => true
36 35
            },
37 36
            :no_label => true %>
38 37
      <%= wikitoolbar_for 'issue_notes', preview_issue_path(:project_id => @project, :issue_id => @issue) %>
app/views/issues/_form.html.erb
38 38
                   :rows => [[10, @issue.description.to_s.length / 50].max, 20].min,
39 39
                   :data => {
40 40
                       :auto_complete => true,
41
                       :issues_url => auto_complete_issues_path(:project_id => @issue.project, :q => '')
42 41
                   },
43 42
                   :no_label => true %>
44 43
  <% end %>
......
54 53
<% end %>
55 54

  
56 55
<% heads_for_wiki_formatter %>
56
<%= heads_for_auto_complete(@issue.project) %>
57 57

  
58 58
<%= javascript_tag do %>
59 59
$(document).ready(function(){
app/views/issues/bulk_edit.html.erb
221 221
<legend><%= l(:field_notes) %></legend>
222 222
<%= text_area_tag 'notes', @notes, :cols => 60, :rows => 10, :class => 'wiki-edit',
223 223
                  :data => {
224
                      :auto_complete => true,
225
                      :issues_url => auto_complete_issues_path(:project_id => @project, :q => '')
224
                      :auto_complete => true
226 225
                  }
227 226
%>
228 227
<%= wikitoolbar_for 'notes' %>
app/views/journals/_notes_form.html.erb
6 6
    <%= text_area_tag 'journal[notes]', @journal.notes, :id => "journal_#{@journal.id}_notes", :class => 'wiki-edit',
7 7
          :rows => (@journal.notes.blank? ? 10 : [[10, @journal.notes.length / 50].max, 100].min),
8 8
          :data => {
9
              :auto_complete => true,
10
              :issues_url => auto_complete_issues_path(:project_id => @project, :q => '')
9
              :auto_complete => true
11 10
          }
12 11
    %>
13 12
    <% if @journal.safe_attribute? 'private_notes' %>
app/views/layouts/base.html.erb
13 13
<%= stylesheet_link_tag 'rtl', :media => 'all' if l(:direction) == 'rtl' %>
14 14
<%= javascript_heads %>
15 15
<%= heads_for_theme %>
16
<%= heads_for_auto_complete(@project) %>
16 17
<%= call_hook :view_layouts_base_html_head %>
17 18
<!-- page specific tags -->
18 19
<%= yield :header_tags -%>
......
23 24

  
24 25
<div class="flyout-menu js-flyout-menu">
25 26

  
26

  
27 27
    <% if User.current.logged? || !Setting.login_required? %>
28 28
        <div class="flyout-menu__search">
29 29
            <%= form_tag({:controller => 'search', :action => 'index', :id => @project}, :method => :get ) do %>
......
82 82
        </label>
83 83
        <%= text_field_tag 'q', @question, :size => 20, :class => 'small', :accesskey => accesskey(:quick_search),
84 84
                            :data => {
85
                                :auto_complete => true,
86
                                :issues_url => auto_complete_issues_path(:q => '')
85
                                :auto_complete => true
87 86
                            } %>
88 87
        <% end %>
89 88
        <%= render_project_jump_box %>
app/views/messages/_form.html.erb
26 26
<%= f.text_area :content, :cols => 80, :rows => 15, :class => 'wiki-edit', :id => 'message_content',
27 27
                :accesskey => accesskey(:edit),
28 28
                :data => {
29
                    :auto_complete => true,
30
                    :issues_url => auto_complete_issues_path(:project_id => @project, :q => '')
29
                    :auto_complete => true
31 30
                }
32 31
%></p>
33 32
<%= wikitoolbar_for 'message_content', preview_board_message_path(:board_id => @board, :id => @message) %>
app/views/news/_form.html.erb
5 5
<p><%= f.text_area :summary, :cols => 60, :rows => 2 %></p>
6 6
<p><%= f.text_area :description, :required => true, :cols => 60, :rows => 15, :class => 'wiki-edit',
7 7
                   :data => {
8
                       :auto_complete => true,
9
                       :issues_url => auto_complete_issues_path(:project_id => @project, :q => '')
8
                       :auto_complete => true
10 9
                   }
11 10
%></p>
12 11
<p id="attachments_form"><label><%= l(:label_attachment_plural) %></label><%= render :partial => 'attachments/form', :locals => {:container => @news} %></p>
app/views/news/show.html.erb
55 55
<div class="box">
56 56
    <%= text_area 'comment', 'comments', :cols => 80, :rows => 15, :class => 'wiki-edit',
57 57
                  :data => {
58
                    :auto_complete => true,
59
                    :issues_url => auto_complete_issues_path(:project_id => @project, :q => '')
58
                    :auto_complete => true
60 59
                  }
61 60
    %>
62 61
    <%= wikitoolbar_for 'comment_comments', preview_news_path(:project_id => @project, :id => @news) %>
app/views/search/index.html.erb
5 5
<%= label_tag "search-input", l(:description_search), :class => "hidden-for-sighted" %>
6 6
<p><%= text_field_tag 'q', @question, :size => 60, :id => 'search-input',
7 7
                      :data => {
8
                          :auto_complete => true,
9
                          :issues_url => auto_complete_issues_path(:q => '')
8
                          :auto_complete => true
10 9
                      } %>
11 10
<%= project_select_tag %>
12 11
<%= hidden_field_tag 'all_words', '', :id => nil %>
app/views/wiki/edit.html.erb
16 16
<%= text_area_tag 'content[text]', @text, :cols => 100, :rows => 25, :accesskey => accesskey(:edit),
17 17
                  :class => 'wiki-edit',
18 18
                  :data => {
19
                      :auto_complete => true,
20
                      :issues_url => auto_complete_issues_path(:project_id => @project, :q => '')
19
                      :auto_complete => true
21 20
                  }
22 21
%>
23 22

  
public/javascripts/application.js
1045 1045

  
1046 1046
function inlineAutoComplete(element) {
1047 1047
    'use strict';
1048

  
1048 1049
    // do not attach if Tribute is already initialized
1049
    if (element.dataset.tribute === 'true') {return;}
1050
    if (element.dataset.tribute === 'true') {return};
1051

  
1052
    const getDataSource = function(entity) {
1053
      const dataSources = JSON.parse(rm.AutoComplete.dataSources);
1050 1054

  
1051
    const issuesUrl = element.dataset.issuesUrl;
1052
    const usersUrl = element.dataset.usersUrl;
1055
      return dataSources[entity];
1056
    }
1053 1057

  
1054 1058
    const remoteSearch = function(url, cb) {
1055 1059
      const xhr = new XMLHttpRequest();
......
1074 1078
        if (event.target.type === 'text' && $(element).attr('autocomplete') != 'off') {
1075 1079
          $(element).attr('autocomplete', 'off');
1076 1080
        }
1077
        remoteSearch(issuesUrl + text, function (issues) {
1081
        remoteSearch(getDataSource('issues') + text, function (issues) {
1078 1082
          return cb(issues);
1079 1083
        });
1080 1084
      },
    (1-1/1)