Save an (yet unworking) version of user smart annotations

Used so other devs can start working on smart annotations.
This commit is contained in:
Luka Murn 2017-01-04 10:54:02 +01:00
parent 23e60e0d01
commit 3bee9d2e12
8 changed files with 155 additions and 1 deletions

View file

@ -0,0 +1,72 @@
(function() {
'use strict';
$(document).on(
'focus',
'[data-atwho-users-edit]',
function() {
if (_.isUndefined($(this).data('atwho'))) {
$(this)
.atwho({
at: '@',
callbacks: {
remoteFilter: function(query, callback) {
$.getJSON(
'/organizations/1/atwho_users.json',
{query: query},
function(data) {
callback(data.users);
}
);
},
tplEval: function(_tpl, map) {
var res;
try {
res = '';
res += '<li class="atwho-li atwho-li-user">';
res += '<img src="' + map.img_url + '" height="20" width="20" />';
res += '&nbsp;';
res += '<span data-full-name>';
res += map.full_name;
res += '</span>';
res += '&nbsp;';
res += '<i class="fa fa-circle" aria-hidden="true"></i>';
res += '&nbsp;';
res += '<small data-email>';
res += map.email;
res += '</small>';
res += '</li>';
} catch (_error) {
res = '';
}
return res;
},
highlighter: function(li, query) {
var li2 = $(li);
li2.addClass('highlighted');
var prevVal =
li2
.find('[data-full-name]')
.html();
var newVal =
prevVal
.replace(query, '<strong>' + query + '</strong>');
li2.find('[data-full-name]').html(newVal);
prevVal =
li2
.find('[data-email]')
.html();
newVal =
prevVal
.replace(query, '<strong>' + query + '</strong>');
li2.find('[data-email]').html(newVal);
return li2.html();
}
},
insertTpl: '[${atwho-at}${full_name}~${id}]',
limit: 5,
startWithSpace: true
});
}
});
})();

View file

@ -1704,3 +1704,15 @@ th.custom-field .modal-tooltiptext {
.disable-click {
pointer-events: none;
}
// AtWho (smart annotations)
.atwho-li-user {
.fa.fa-circle {
font-size: 4px;
}
&.highlighted {
background-color: #ff0000;
}
}

View file

@ -0,0 +1,39 @@
class AtWhoController < ApplicationController
before_action :load_vars
before_action :check_users_permissions, only: :users
def users
# Search users
res = @organization
.search_users(@query)
.select(:id, :full_name, :email)
.limit(Constants::ATWHO_SEARCH_LIMIT)
.as_json
# Add avatars, convert to JSON
res.each do |user_obj|
user_obj['img_url'] = avatar_path(user_obj['id'], :icon_small)
end
respond_to do |format|
format.json do
render json: {
users: res,
status: :ok
}
end
end
end
private
def load_vars
@organization = Organization.find_by_id(params[:id])
@query = params[:query]
render_404 unless @organization
end
def check_users_permissions
render_403 unless can_view_organization_users(@organization)
end
end

View file

@ -246,6 +246,11 @@ module PermissionHelper
# to "is project archived" or "is module archived" checks
# at the beginning of this file (via aspector).
# ---- ATWHO PERMISSIONS ----
def can_view_organization_users(organization)
is_member_of_organization(organization)
end
# ---- PROJECT PERMISSIONS ----
def can_view_projects(organization)

View file

@ -1,4 +1,6 @@
class Organization < ActiveRecord::Base
include SearchableModel
# Not really MVC-compliant, but we just use it for logger
# output in space_taken related functions
include ActionView::Helpers::NumberHelper
@ -51,6 +53,24 @@ class Organization < ActiveRecord::Base
end
end
def search_users(
query = nil,
attributes = [:full_name, :email]
)
if query
a_query = query
.strip
.gsub('_', '\\_')
.gsub('%', '\\%')
.split(/\s+/)
.map { |t| '%' + t + '%' }
else
a_query = query
end
users.where_attributes_like(attributes, a_query)
end
# Writes to user log
def log(message)
final = "[%s] %s" % [Time.current.to_s, message]

View file

@ -22,7 +22,7 @@
<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: '.' %>
<%= f.smart_text_area :message, single_line: true, hide_label: true, placeholder: t("general.comment_placeholder"), append: f.submit("+"), help: '.', data: { 'atwho-users-edit' => '' } %>
<% end %>
</li>
</ul>

View file

@ -48,6 +48,9 @@ class Constants
# Maximum number of users that can be invited in a single action
INVITE_USERS_LIMIT = 20
# Maximum nr. of search results for atwho (smart annotations)
ATWHO_SEARCH_LIMIT = 5
#=============================================================================
# File and data memory size
#=============================================================================

View file

@ -86,6 +86,9 @@ Rails.application.routes.draw do
post 'parse_sheet'
post 'import_samples'
post 'export_samples'
# Used for atwho (smart annotations)
get 'atwho_users', to: 'at_who#users'
end
match '*path', :to => 'organizations#routing_error', via: [:get, :post, :put, :patch]
end