refactor [fixes SCI-834] [fixes SCI-837] [fixes SCI-823] [fixes SCI-835] [fixes SCI-836] [fixes SCI-824]

This commit is contained in:
zmagod 2017-01-11 14:56:47 +01:00
parent a4aa933151
commit 3a1ec0eb5e
19 changed files with 170 additions and 138 deletions

View file

@ -86,8 +86,6 @@ var Comments = (function() {
function initCommentForm($el) {
var $form = $el.find('ul form');
// initialize smart annotation
SmartAnnotation.init($($form).find('#comment_message'));
$('.help-block', $form).addClass('hide');

View file

@ -18,8 +18,6 @@
validateMoveModal(id);
clearModal($(id));
validateExperimentForm($(id));
// initialize smart annotation
SmartAnnotation.init('#experiment-description');
})
.on("ajax:error", function() {
animateSpinner(null, false);

View file

@ -112,7 +112,6 @@ function applyEditCallBack() {
$("#step_name").focus();
});
openLinksInNewTab();
initSmartAnnotation();
});
}
@ -430,8 +429,6 @@ function reorderCheckboxData(checkboxUl) {
$posInput.attr("id", posId);
$posInput.val(itemPos);
$destroyLink.attr("data-delete-association-field-name", destroyLink);
debugger;
SmartAnnotation.init();
var $idInput = $this.find("> input");
if ($idInput.length) {
@ -567,27 +564,6 @@ function initHighlightjs() {
}
}
// initialize the smart annotations
function initSmartAnnotation() {
$('#new-step-checklists').on('click', function() {
setTimeout(function() {
SmartAnnotation.init('.checklist_name');
}, 300);
$('[data-object-class="checklist"]').on('click', function() {
setTimeout(function() {
SmartAnnotation.init('.checklist_name');
}, 300);
});
$('[data-object-class="checklist_item"]').on('click', function() {
setTimeout(function() {
$('.checklist-item-text').atwho('destroy');
SmartAnnotation.init('.checklist-item-text');
}, 300);
});
});
}
function initStepsComments() {
Comments.initialize();
Comments.initCommentOptions("ul.content-comments");

View file

@ -14,8 +14,6 @@ $("#new-result-text").on("ajax:success", function(e, data) {
toggleResultEditButtons(false);
$("#result_name").focus();
SmartAnnotation.init('.ql-editor');
});
$("#new-result-text").on("ajax:error", function(e, xhr, status, error) {

View file

@ -437,7 +437,6 @@ function onClickEdit() {
_.each($('[data-object="custom_fields"]'), function(el) {
SmartAnnotation.init(el);
});
// Adjust columns width in table header
table.columns.adjust();
},

View file

@ -48,29 +48,35 @@ var SmartAnnotation = (function() {
}
function _matchHighlighter(li, query, search_filter) {
var re, li2, prevVal, newVal;
var $li, re;
// search filter is not passed for users
if(search_filter){
resourcesChecker(query, search_filter);
}
function highlight(el, sel, re) {
var prevVal, newVal;
prevVal = el.find(sel).html();
newVal = prevVal.replace(re, '<strong>$&</strong>');
el.find(sel).html(newVal);
}
if (!query) {
return li;
}
resourcesChecker(query, search_filter);
li2 = $(li);
$li = $(li);
re = new RegExp(query, 'gi');
prevVal =
li2
.find('[data-val=name]')
.html();
newVal =
prevVal
.replace(re, '<strong>$&</strong>');
li2.find('[data-val=name]').html(newVal);
newVal =
prevVal
.replace(re, '<strong>$&</strong>');
li2.find('[data-val=email]').html(newVal);
return li2[0].outerHTML;
if(search_filter) {
highlight($li, '[data-val=name]', re);
} else {
highlight($li, '[data-val=full-name]', re);
highlight($li, '[data-val=email]', re);
}
return $li[0].outerHTML
}
function _genrateInputTag(value, li) {
function _generateInputTag(value, li) {
var res = '';
res += '[#' + li.attr('data-name');
res += '~' + li.attr('data-type');
@ -365,21 +371,7 @@ var SmartAnnotation = (function() {
return res;
},
highlighter: function(li, query) {
function highlight(el, sel, re) {
var prevVal = el.find(sel).html();
var newVal = prevVal.replace(re, '<strong>$&</strong>');
el.find(sel).html(newVal);
}
if (!query) {
return li;
}
var $li = $(li);
var re = new RegExp(query, 'gi');
highlight($li, '[data-val=full-name]', re);
highlight($li, '[data-val=email]', re);
return $li[0].outerHTML;
return _matchHighlighter(li, query);
},
beforeInsert: function(value, li) {
var res = '';
@ -434,7 +426,7 @@ var SmartAnnotation = (function() {
return _matchHighlighter(li, query, '#');
},
beforeInsert: function(value, li) {
return _genrateInputTag(value, li);
return _generateInputTag(value, li);
}
},
headerTpl: generateFilterMenu('sam'),
@ -463,7 +455,7 @@ var SmartAnnotation = (function() {
return _matchHighlighter(li, query, 'task#');
},
beforeInsert: function(value, li) {
return _genrateInputTag(value, li);
return _generateInputTag(value, li);
}
},
headerTpl: generateFilterMenu('tsk'),
@ -492,7 +484,7 @@ var SmartAnnotation = (function() {
return _matchHighlighter(li, query, 'project#');
},
beforeInsert: function(value, li) {
return _genrateInputTag(value, li);
return _generateInputTag(value, li);
}
},
headerTpl: generateFilterMenu('prj'),
@ -521,7 +513,7 @@ var SmartAnnotation = (function() {
return _matchHighlighter(li, query, 'experiment#');
},
beforeInsert: function(value, li) {
return _genrateInputTag(value, li);
return _generateInputTag(value, li);
}
},
headerTpl: generateFilterMenu('exp'),
@ -550,7 +542,7 @@ var SmartAnnotation = (function() {
return _matchHighlighter(li, query, 'sample#');
},
beforeInsert: function(value, li) {
return _genrateInputTag(value, li);
return _generateInputTag(value, li);
}
},
headerTpl: generateFilterMenu('sam'),
@ -578,3 +570,11 @@ var SmartAnnotation = (function() {
return publicApi;
})();
// initialize the smart annotations
(function initSmartAnnotation() {
$(document).on('focus', '[data-atwho-edit]', function() {
SmartAnnotation.init(this);
});
})();

View file

@ -30,7 +30,7 @@ class AtWhoController < ApplicationController
end
def menu_items
res = SmartAnnotation.new(current_user, @query)
res = SmartAnnotation.new(current_user, current_organization, @query)
respond_to do |format|
format.json do
@ -46,7 +46,7 @@ class AtWhoController < ApplicationController
end
def samples
res = SmartAnnotation.new(current_user, @query)
res = SmartAnnotation.new(current_user, current_organization, @query)
respond_to do |format|
format.json do
render json: {
@ -58,7 +58,7 @@ class AtWhoController < ApplicationController
end
def projects
res = SmartAnnotation.new(current_user, @query)
res = SmartAnnotation.new(current_user, current_organization, @query)
respond_to do |format|
format.json do
render json: {
@ -70,7 +70,7 @@ class AtWhoController < ApplicationController
end
def experiments
res = SmartAnnotation.new(current_user, @query)
res = SmartAnnotation.new(current_user, current_organization, @query)
respond_to do |format|
format.json do
render json: {
@ -82,7 +82,7 @@ class AtWhoController < ApplicationController
end
def my_modules
res = SmartAnnotation.new(current_user, @query)
res = SmartAnnotation.new(current_user, current_organization, @query)
respond_to do |format|
format.json do
render json: {

View file

@ -54,12 +54,12 @@ module ApplicationHelper
end
def smart_annotation_parser(text)
sa_reg = /\[\#(.*?)~(prj|exp|tsk|sam)~([0-9]+)\]/
sa_reg = /\[\#(.*?)~(prj|exp|tsk|sam)~([0-9a-zA-Z]+)\]/
new_text = text.gsub(sa_reg) do |el|
match = el.match(sa_reg)
case match[2]
when 'prj'
project = Project.find_by_id(match[3].to_i)
project = Project.find_by_id(match[3].base62_decode)
next unless project
if project.archived?
"<span class='sa-type'>#{sanitize(match[2])}</span> " \
@ -71,7 +71,7 @@ module ApplicationHelper
project_path(project)}"
end
when 'exp'
experiment = Experiment.find_by_id(match[3].to_i)
experiment = Experiment.find_by_id(match[3].base62_decode)
next unless experiment
if experiment.archived?
"<span class='sa-type'>#{sanitize(match[2])}</span> " \
@ -84,7 +84,7 @@ module ApplicationHelper
canvas_experiment_path(experiment)}"
end
when 'tsk'
my_module = MyModule.find_by_id(match[3].to_i)
my_module = MyModule.find_by_id(match[3].base62_decode)
next unless my_module
if my_module.archived?
"<span class='sa-type'>#{sanitize(match[2])}</span> " \
@ -97,7 +97,7 @@ module ApplicationHelper
protocols_my_module_path(my_module)}"
end
when 'sam'
sample = Sample.find_by_id(match[3])
sample = Sample.find_by_id(match[3].base62_decode)
if sample
"<span class='glyphicon glyphicon-tint'></span> " \
"#{link_to sample.name,
@ -112,12 +112,14 @@ module ApplicationHelper
end
end
sa_user = /\[\@(.*?)~([0-9]+)\]/
sa_user = /\[\@(.*?)~([0-9a-zA-Z]+)\]/
new_text = new_text.gsub(sa_user) do |el|
match = el.match(sa_user)
user = User.find_by_id(match[2].to_i)
"<span>#{image_tag avatar_path(user, :icon_small)} " \
"#{user.full_name}</span>" if user
user = User.find_by_id(match[2].base62_decode)
if user
"<span>#{image_tag avatar_path(user, :icon_small)} " \
"#{user.full_name}</span>"
end
end
new_text
end

View file

@ -38,7 +38,7 @@ class Experiment < ActiveRecord::Base
include_archived,
query = nil,
page = 1,
is_smart_annotation = false
current_organization = nil
)
project_ids =
Project
@ -55,10 +55,19 @@ class Experiment < ActiveRecord::Base
a_query = query
end
if is_smart_annotation
if current_organization
projects_ids =
Project
.search(user,
include_archived,
nil,
1,
current_organization)
.select('id')
new_query =
Experiment
.where(project: project_ids)
.where('experiments.project_id IN (?)', projects_ids)
.where_attributes_like([:name], a_query)
elsif include_archived
new_query =

View file

@ -46,7 +46,7 @@ class MyModule < ActiveRecord::Base
include_archived,
query = nil,
page = 1,
is_smart_annotation = false
current_organization = nil
)
exp_ids =
Experiment
@ -63,10 +63,17 @@ class MyModule < ActiveRecord::Base
a_query = query
end
if is_smart_annotation
if current_organization
experiemnts_ids = Experiment
.search(user,
include_archived,
nil,
1,
current_organization)
.select('id')
new_query = MyModule
.distinct
.where('my_modules.experiment_id IN (?)', exp_ids)
.where('my_modules.experiment_id IN (?)', experiemnts_ids)
.where_attributes_like([:name], a_query)
elsif include_archived
new_query = MyModule

View file

@ -26,7 +26,13 @@ class Project < ActiveRecord::Base
has_many :report_elements, inverse_of: :project, dependent: :destroy
belongs_to :organization, inverse_of: :projects
def self.search(user, include_archived, query = nil, page = 1)
def self.search(
user,
include_archived,
query = nil,
page = 1,
current_organization = nil
)
if query
a_query = query.strip
@ -38,30 +44,46 @@ class Project < ActiveRecord::Base
a_query = query
end
org_ids =
Organization
.joins(:user_organizations)
.where("user_organizations.user_id = ?", user.id)
.select("id")
.distinct
if include_archived
if current_organization
new_query = Project
.distinct
.joins(:user_projects)
.where("projects.organization_id IN (?)", org_ids)
.where("projects.visibility = 1 OR user_projects.user_id = ?", user.id)
.where_attributes_like(:name, a_query)
.distinct
.joins(:user_projects)
.where('projects.organization_id = ?',
current_organization.id)
.where('projects.visibility = 1 OR user_projects.user_id = ?',
user.id)
.where_attributes_like(:name, a_query)
else
new_query = Project
org_ids =
Organization
.joins(:user_organizations)
.where('user_organizations.user_id = ?', user.id)
.select('id')
.distinct
.joins(:user_projects)
.where("projects.organization_id IN (?)", org_ids)
.where("projects.visibility = 1 OR user_projects.user_id = ?", user.id)
.where_attributes_like(:name, a_query)
.where("projects.archived = ?", false)
if include_archived
new_query = Project
.distinct
.joins(:user_projects)
.where('projects.organization_id IN (?)', org_ids)
.where(
'projects.visibility = 1 OR user_projects.user_id = ?',
user.id
)
.where_attributes_like(:name, a_query)
else
new_query = Project
.distinct
.joins(:user_projects)
.where('projects.organization_id IN (?)', org_ids)
.where(
'projects.visibility = 1 OR user_projects.user_id = ?',
user.id
)
.where_attributes_like(:name, a_query)
.where('projects.archived = ?', false)
end
end
# Show all results if needed

View file

@ -24,7 +24,7 @@ class Sample < ActiveRecord::Base
include_archived,
query = nil,
page = 1,
is_smart_annotation = false
current_organization = nil
)
org_ids =
Organization
@ -43,17 +43,10 @@ class Sample < ActiveRecord::Base
a_query = query
end
if is_smart_annotation
if current_organization
new_query = Sample
.distinct
.joins(:user)
.joins('LEFT OUTER JOIN sample_types ON ' \
'samples.sample_type_id = sample_types.id')
.joins('LEFT OUTER JOIN sample_groups ON ' \
'samples.sample_group_id = sample_groups.id')
.joins('LEFT OUTER JOIN sample_custom_fields ON ' \
'samples.id = sample_custom_fields.sample_id')
.where('samples.organization_id IN (?)', org_ids)
.where('samples.organization_id = ?', current_organization.id)
.where_attributes_like(['samples.name'], a_query)
else
new_query = Sample

View file

@ -2,23 +2,24 @@ class SmartAnnotation
include ActionView::Helpers::SanitizeHelper
include ActionView::Helpers::TextHelper
attr_writer :current_user, :query
attr_writer :current_user, :current_organization, :query
def initialize(current_user, query)
def initialize(current_user, current_organization, query)
@current_user = current_user
@current_organization = current_organization
@query = query
end
def my_modules
# Search tasks
res = MyModule
.search(@current_user, true, @query)
.search(@current_user, true, @query, @current_organization)
.limit(Constants::ATWHO_SEARCH_LIMIT)
modules_list = []
res.each do |my_module_res|
my_mod = {}
my_mod['id'] = my_module_res.id
my_mod['id'] = my_module_res.id.base62_encode
my_mod['name'] = truncate(
sanitize(my_module_res.name,
length: Constants::NAME_TRUNCATION_LENGTH)
@ -42,13 +43,13 @@ class SmartAnnotation
def projects
# Search projects
res = Project
.search(@current_user, true, @query)
.search(@current_user, true, @query, 1, @current_organization)
.limit(Constants::ATWHO_SEARCH_LIMIT)
projects_list = []
res.each do |project_res|
prj = {}
prj['id'] = project_res.id
prj['id'] = project_res.id.base62_encode
prj['name'] = truncate(
sanitize(project_res.name,
length: Constants::NAME_TRUNCATION_LENGTH)
@ -62,13 +63,13 @@ class SmartAnnotation
def experiments
# Search experiments
res = Experiment
.search(@current_user, true, @query, 1, true)
.search(@current_user, true, @query, 1, @current_organization)
.limit(Constants::ATWHO_SEARCH_LIMIT)
experiments_list = []
res.each do |experiment_res|
exp = {}
exp['id'] = experiment_res.id
exp['id'] = experiment_res.id.base62_encode
exp['name'] = truncate(
sanitize(experiment_res.name,
length: Constants::NAME_TRUNCATION_LENGTH)
@ -86,13 +87,13 @@ class SmartAnnotation
def samples
# Search samples
res = Sample
.search(@current_user, true, @query, 1, true)
.search(@current_user, true, @query, 1, @current_organization)
.limit(Constants::ATWHO_SEARCH_LIMIT)
samples_list = []
res.each do |sample_res|
sam = {}
sam['id'] = sample_res.id
sam['id'] = sample_res.id.base62_encode
sam['name'] = truncate(
sanitize(sample_res.name,
length: Constants::NAME_TRUNCATION_LENGTH)

View file

@ -1,7 +1,12 @@
<%= bootstrap_form_for(@comment, url: @update_url, remote: true, html: { method: :put, class: 'comment-form edit-comment-form' }, data: { role: 'edit-comment-message-form' }) do |f| %>
<div class="form-group">
<div class="input-group">
<%= f.smart_text_area :message, single_line: true, autofocus: true, hide_label: true, data: { role: 'message-input' }, value: @comment.message %>
<%= f.smart_text_area :message,
single_line: true,
autofocus: true,
hide_label: true,
data: { role: 'message-input', 'atwho-edit' => '' },
value: @comment.message %>
<span class="input-group-btn">
<a class="btn btn-default" data-action="save">
<span class="glyphicon glyphicon-ok"></span>
@ -11,4 +16,4 @@
<span class="help-block hide"></span>
<a data-action="cancel" href="#"><%= t('general.cancel') %></a>
</div>
<% end %>
<% end %>

View file

@ -15,7 +15,8 @@
<div class="form-group">
<%= form.smart_text_area :description,
label: t('experiments.new.description'),
id: 'experiment-description' %>
id: 'experiment-description',
data: { 'atwho-edit' => '' } %>
</div>
</div>
</div>

View file

@ -18,7 +18,13 @@
<li>
<hr>
<%= bootstrap_form_for :comment, url: {format: :json}, method: :post, remote: true, html: { class: 'comment-form' } do |f| %>
<%= f.smart_text_area :message, single_line: true, hide_label: true, placeholder: t('projects.index.comment_placeholder'), append: f.submit('+'), help: '.' %>
<%= f.smart_text_area :message,
single_line: true,
hide_label: true,
placeholder: t('projects.index.comment_placeholder'),
append: f.submit('+'),
help: '.',
data: { 'atwho-edit' => '' } %>
<% end %>
</li>
</ul>

View file

@ -28,7 +28,7 @@
placeholder: t("general.comment_placeholder"),
append: f.submit("+", onclick: "processResult(event, ResultTypeEnum.COMMENT, false);"),
help: '.',
data: { 'atwho-res-edit' => '' } %>
data: { 'atwho-edit' => '' } %>
<% end %>
</li>
</ul>

View file

@ -22,7 +22,13 @@
<li>
<hr>
<%= bootstrap_form_for :comment, url: { format: :json }, html: { class: 'comment-form', id: "step-comment-#{@step.id}" }, method: :post, remote: true do |f| %>
<%= f.smart_text_area :message, single_line: true, hide_label: true, placeholder: t("general.comment_placeholder"), append: f.submit("+"), help: '.', data: { 'atwho-users-edit' => '' } %>
<%= f.smart_text_area :message,
single_line: true,
hide_label: true,
placeholder: t("general.comment_placeholder"),
append: f.submit("+"),
help: '.',
data: { 'atwho-edit' => '' } %>
<% end %>
</li>
</ul>

View file

@ -9,7 +9,12 @@
</div>
</div>
<div class="panel-body">
<%= ff.smart_text_area :name, label: t("protocols.steps.new.checklist_name"), class: "checklist_name", autofocus: true, placeholder: t("protocols.steps.new.checklist_name_placeholder") %>
<%= ff.smart_text_area :name,
label: t('protocols.steps.new.checklist_name'),
class: 'checklist_name',
autofocus: true,
placeholder: t('protocols.steps.new.checklist_name_placeholder'),
data: { 'atwho-edit' => '' } %>
<%= ff.label t("protocols.steps.new.checklist_items") %>
<ul>
<%= ff.nested_fields_for :checklist_items, ordered_checklist_items(ff.object) do |chkItems| %>
@ -18,7 +23,13 @@
<span class="glyphicon glyphicon-chevron-right handle-move pull-left"></span>
<div class="row">
<div class="col-xs-6">
<%= chkItems.smart_text_area :text, autofocus: true, placeholder: t("protocols.steps.new.checklist_item_placeholder"), hide_label: true, class: "checklist-item-text form-control", single_line: true %>
<%= chkItems.smart_text_area :text,
autofocus: true,
placeholder: t('protocols.steps.new.checklist_item_placeholder'),
hide_label: true,
class: 'checklist-item-text form-control',
single_line: true,
data: { 'atwho-edit' => '' } %>
<%= chkItems.hidden_field :position, class: "checklist-item-pos" %>
</div>
<div class="col-xs-1">