From 4f69d40ca8681243f5092e0b0db93e8ac9ebad8f Mon Sep 17 00:00:00 2001 From: tohosaku Date: Fri, 5 Aug 2022 22:12:26 +0900 Subject: [PATCH] Use meta element instead of javascript object --- app/helpers/application_helper.rb | 19 ++----------------- app/views/issues/_form.html.erb | 9 ++++++--- app/views/layouts/base.html.erb | 6 +++++- app/views/wiki/edit.html.erb | 3 ++- public/javascripts/application.js | 15 +++++---------- 5 files changed, 20 insertions(+), 32 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e2eebfa41..d8e859bc5 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1826,21 +1826,6 @@ module ApplicationHelper end end - def heads_for_auto_complete(project) - data_sources = autocomplete_data_sources(project) - javascript_tag( - "rm = window.rm || {};" \ - "rm.AutoComplete = rm.AutoComplete || {};" \ - "rm.AutoComplete.dataSources = JSON.parse('#{data_sources.to_json}');" - ) - end - - def update_data_sources_for_auto_complete(data_sources) - javascript_tag( - "rm.AutoComplete.dataSources = Object.assign(rm.AutoComplete.dataSources, JSON.parse('#{data_sources.to_json}'));" - ) - end - def copy_object_url_link(url) link_to_function( l(:button_copy_link), 'copyTextToClipboard(this);', @@ -1875,8 +1860,8 @@ module ApplicationHelper def autocomplete_data_sources(project) { - issues: auto_complete_issues_path(project_id: project, q: ''), - wiki_pages: auto_complete_wiki_pages_path(project_id: project, q: ''), + autocomplete_issues: auto_complete_issues_path(project_id: project, q: ''), + autocomplete_wiki_pages: auto_complete_wiki_pages_path(project_id: project, q: ''), } end end diff --git a/app/views/issues/_form.html.erb b/app/views/issues/_form.html.erb index 3993b9ebf..1fa054f47 100644 --- a/app/views/issues/_form.html.erb +++ b/app/views/issues/_form.html.erb @@ -53,11 +53,14 @@ <% end %> <% heads_for_wiki_formatter %> -<%= heads_for_auto_complete(@issue.project) %> + +<% autocomplete_data_sources(@issue.project).each do |name, content| %> +<% content_for name, content %> +<% end %> <% if User.current.allowed_to?(:add_issue_watchers, @issue.project)%> - <%= update_data_sources_for_auto_complete({users: watchers_autocomplete_for_mention_path(project_id: @issue.project, q: '', object_type: 'issue', - object_id: @issue.id)}) %> + <% content = watchers_autocomplete_for_mention_path(project_id: @issue.project, q: '', object_type: 'issue', object_id: @issue.id) %> + <%= tag.meta name: :autocomplete_users, content: content %> <% end %> <%= javascript_tag do %> diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb index 0a1f2ae38..94b7b545d 100644 --- a/app/views/layouts/base.html.erb +++ b/app/views/layouts/base.html.erb @@ -13,13 +13,17 @@ <%= stylesheet_link_tag 'rtl', :media => 'all' if l(:direction) == 'rtl' %> <%= javascript_heads %> <%= heads_for_theme %> -<%= heads_for_auto_complete(@project) %> <%= call_hook :view_layouts_base_html_head %> <%= yield :header_tags -%> <%= call_hook :view_layouts_base_body_top %> + +<% autocomplete_data_sources(@project).each do |name, content| %> + <%= tag.meta name: name, content: (content_for?(name) ? yield(name) : content) %> +<% end %> +
diff --git a/app/views/wiki/edit.html.erb b/app/views/wiki/edit.html.erb index 7e9cf42af..dabe65d8c 100644 --- a/app/views/wiki/edit.html.erb +++ b/app/views/wiki/edit.html.erb @@ -66,7 +66,8 @@ <%= wikitoolbar_for 'content_text', preview_project_wiki_page_path(:project_id => @project, :id => @page.title) %> <% if User.current.allowed_to?(:add_wiki_page_watchers, @project)%> - <%= update_data_sources_for_auto_complete({users: watchers_autocomplete_for_mention_path(project_id: @project, q: '', object_type: 'wiki_page', object_id: @page.id)}) %> + <% content = watchers_autocomplete_for_mention_path(project_id: @project, q: '', object_type: 'wiki_page', object_id: @page.id) %> + <%= tag.meta name: :autocomplete_users, content: content %> <% end %> <% end %> diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 91da19229..8581526c5 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -1127,13 +1127,8 @@ function inlineAutoComplete(element) { if (element.dataset.tribute === 'true') {return}; const getDataSource = function(entity) { - const dataSources = rm.AutoComplete.dataSources; - - if (dataSources[entity]) { - return dataSources[entity]; - } else { - return false; - } + const element = document.body.querySelector(`meta[name="${entity}"]`) + return element && element.content } const remoteSearch = function(url, cb) { @@ -1161,7 +1156,7 @@ function inlineAutoComplete(element) { if (event.target.type === 'text' && $(element).attr('autocomplete') != 'off') { $(element).attr('autocomplete', 'off'); } - remoteSearch(getDataSource('issues') + text, function (issues) { + remoteSearch(getDataSource('autocomplete_issues') + text, function (issues) { return cb(issues); }); }, @@ -1178,7 +1173,7 @@ function inlineAutoComplete(element) { { trigger: '[[', values: function (text, cb) { - remoteSearch(getDataSource('wiki_pages') + text, function (wikiPages) { + remoteSearch(getDataSource('autocomplete_wiki_pages') + text, function (wikiPages) { return cb(wikiPages); }); }, @@ -1198,7 +1193,7 @@ function inlineAutoComplete(element) { return user.name + user.firstname + user.lastname + user.login; }, values: function (text, cb) { - const url = getDataSource('users'); + const url = getDataSource('autocomplete_users'); if (url) { remoteSearch(url + text, function (users) { return cb(users); -- 2.30.2