2020-09-15 17:56:44 +08:00
|
|
|
/* global _ */
|
|
|
|
|
2017-01-10 00:16:39 +08:00
|
|
|
var SmartAnnotation = (function() {
|
2017-01-05 17:52:00 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-01-23 16:47:04 +08:00
|
|
|
// stop the user annotation popover on click propagation
|
|
|
|
function atwhoStopPropagation(element) {
|
|
|
|
$(element).on('click', function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-15 17:56:44 +08:00
|
|
|
function SetAtWho(field) {
|
2017-01-19 00:25:33 +08:00
|
|
|
var FilterTypeEnum = Object.freeze({
|
2020-09-15 17:56:44 +08:00
|
|
|
USER: { tag: 'users', dataUrl: $(document.body).attr('data-atwho-users-url') },
|
|
|
|
TASK: { tag: 'sa-tasks', dataUrl: $(document.body).attr('data-atwho-task-url') },
|
|
|
|
PROJECT: { tag: 'sa-projects', dataUrl: $(document.body).attr('data-atwho-project-url') },
|
|
|
|
EXPERIMENT: { tag: 'sa-experiments', dataUrl: $(document.body).attr('data-atwho-experiment-url') },
|
|
|
|
REPOSITORY: { tag: 'sa-repositories', dataUrl: $(document.body).attr('data-atwho-rep-items-url') },
|
|
|
|
MENU: { tag: 'menu', dataUrl: $(document.body).attr('data-atwho-menu-items') }
|
2017-01-19 00:25:33 +08:00
|
|
|
});
|
2020-09-15 17:56:44 +08:00
|
|
|
var DEFAULT_SEARCH_FILTER = FilterTypeEnum.REPOSITORY;
|
2017-01-05 17:52:00 +08:00
|
|
|
|
2020-09-15 17:56:44 +08:00
|
|
|
function matchHighlighter(html, query) {
|
2020-09-09 20:17:19 +08:00
|
|
|
var $html = $(html);
|
2020-09-15 17:56:44 +08:00
|
|
|
var $liText = $html.find('.item-text');
|
|
|
|
if ($liText.length === 0 || !query) return html;
|
2017-01-12 16:52:29 +08:00
|
|
|
|
2020-09-15 17:56:44 +08:00
|
|
|
$.each($liText, function(i, item) {
|
|
|
|
$(item).html($(item).text().replace(new RegExp(query.split(' ').join('|'), 'gi'),
|
|
|
|
'<span class="atwho-highlight">$&</span>'));
|
|
|
|
});
|
2017-01-11 21:56:47 +08:00
|
|
|
|
2020-09-09 20:17:19 +08:00
|
|
|
return $html;
|
2017-01-09 18:33:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generates suggestion dropdown filter
|
2020-09-15 17:56:44 +08:00
|
|
|
function generateFilterMenu() {
|
2020-09-04 22:48:53 +08:00
|
|
|
var menu = '';
|
2018-04-03 02:38:56 +08:00
|
|
|
$.ajax({
|
|
|
|
async: false,
|
|
|
|
dataType: 'json',
|
|
|
|
url: $(document.body).attr('data-atwho-repositories-url'),
|
|
|
|
success: function(data) {
|
2020-09-15 17:56:44 +08:00
|
|
|
menu = data.html;
|
|
|
|
}
|
2018-04-03 02:38:56 +08:00
|
|
|
});
|
2020-09-04 22:48:53 +08:00
|
|
|
return menu;
|
2017-01-05 20:29:21 +08:00
|
|
|
}
|
|
|
|
|
2020-09-15 18:04:23 +08:00
|
|
|
function atWhoSettings(at) {
|
2017-01-17 16:31:01 +08:00
|
|
|
return {
|
|
|
|
at: at,
|
|
|
|
callbacks: {
|
|
|
|
remoteFilter: function(query, callback) {
|
2020-09-17 19:11:54 +08:00
|
|
|
var $currentAtWho = $(`.atwho-view[data-at-who-id=${$(field).attr('data-smart-annotation')}]`);
|
2020-09-04 22:48:53 +08:00
|
|
|
var filterType;
|
|
|
|
var params = { query: query };
|
2020-09-15 17:56:44 +08:00
|
|
|
filterType = FilterTypeEnum[$currentAtWho.find('.tab-pane.active').data('object-type')];
|
2020-09-04 22:48:53 +08:00
|
|
|
if (!filterType) {
|
2020-09-15 17:56:44 +08:00
|
|
|
callback([{ name: '' }]);
|
|
|
|
return false;
|
2020-09-04 22:48:53 +08:00
|
|
|
}
|
2020-09-15 17:56:44 +08:00
|
|
|
if (filterType.tag === 'sa-repositories') {
|
|
|
|
let repositoryTab = $currentAtWho.find('[data-object-type="REPOSITORY"]');
|
2020-09-04 22:48:53 +08:00
|
|
|
let activeRepository = repositoryTab.find('.btn-primary');
|
|
|
|
if (activeRepository.length) {
|
2020-09-15 17:56:44 +08:00
|
|
|
params.repository_id = activeRepository.data('object-id');
|
2017-01-18 22:29:48 +08:00
|
|
|
}
|
2020-09-04 22:48:53 +08:00
|
|
|
}
|
|
|
|
$.getJSON(filterType.dataUrl, params, function(data) {
|
2020-09-17 19:11:54 +08:00
|
|
|
localStorage.setItem('smart_annotation_states/teams/' + data.team, JSON.stringify({
|
2020-09-15 18:34:51 +08:00
|
|
|
tag: filterType.tag,
|
|
|
|
repository: data.repository
|
|
|
|
}));
|
|
|
|
|
2020-09-07 16:46:51 +08:00
|
|
|
callback(data.res);
|
2017-01-18 22:29:48 +08:00
|
|
|
|
2020-09-04 22:48:53 +08:00
|
|
|
if (data.repository) {
|
|
|
|
$currentAtWho.find(`.repository-object[data-object-id="${data.repository}"]`)
|
2020-09-15 17:56:44 +08:00
|
|
|
.addClass('btn-primary').removeClass('btn-light');
|
2018-04-03 02:38:56 +08:00
|
|
|
}
|
2017-01-18 23:04:39 +08:00
|
|
|
});
|
2020-09-15 17:56:44 +08:00
|
|
|
return true;
|
2017-01-17 16:31:01 +08:00
|
|
|
},
|
2020-09-09 20:17:19 +08:00
|
|
|
tplEval: function(_tpl, items) {
|
2020-09-22 16:15:01 +08:00
|
|
|
var $items = $(items.name);
|
2020-09-24 19:32:43 +08:00
|
|
|
$items.find('li').data('item-data', { 'atwho-at': at }); // Emulate at.js insertContentFor method
|
2020-09-22 16:15:01 +08:00
|
|
|
return $items;
|
2017-01-17 16:31:01 +08:00
|
|
|
},
|
2020-09-15 17:56:44 +08:00
|
|
|
highlighter: function(li, query) {
|
2020-09-15 18:04:23 +08:00
|
|
|
return matchHighlighter(li, query);
|
2017-01-17 16:31:01 +08:00
|
|
|
},
|
|
|
|
beforeInsert: function(value, li) {
|
2020-09-15 18:04:23 +08:00
|
|
|
return `[#${li.attr('data-name')}~${li.attr('data-type')}~${li.attr('data-id')}]`;
|
2018-07-06 20:26:20 +08:00
|
|
|
},
|
2020-09-15 17:56:44 +08:00
|
|
|
matcher: function(flag, subtext, shouldStartWithSpace) {
|
|
|
|
var a;
|
|
|
|
var y;
|
|
|
|
var match;
|
|
|
|
var regexp;
|
|
|
|
var cleanedFlag = flag.replace(/[-[]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
|
|
|
|
if (shouldStartWithSpace) {
|
|
|
|
cleanedFlag = '(?:^|\\s)' + cleanedFlag;
|
2018-07-06 20:26:20 +08:00
|
|
|
}
|
2020-09-15 17:56:44 +08:00
|
|
|
a = decodeURI('%C3%80');
|
|
|
|
y = decodeURI('%C3%BF');
|
2020-09-23 17:13:59 +08:00
|
|
|
regexp = new RegExp(`${cleanedFlag}$|${cleanedFlag}(\\S[A-Za-z${a}-${y}0-9_/:\\s+-]*)$|${cleanedFlag}(\\S[^\\x00-\\xff]*)$`, 'gi');
|
2018-07-06 20:26:20 +08:00
|
|
|
match = regexp.exec(subtext);
|
|
|
|
if (match) {
|
2020-09-23 17:13:59 +08:00
|
|
|
return match[1] || '';
|
2018-07-06 20:26:20 +08:00
|
|
|
}
|
2020-09-15 17:56:44 +08:00
|
|
|
return null;
|
|
|
|
}
|
2017-01-17 16:31:01 +08:00
|
|
|
},
|
2020-09-15 18:04:23 +08:00
|
|
|
headerTpl: generateFilterMenu(),
|
2017-01-17 16:31:01 +08:00
|
|
|
startWithSpace: true,
|
|
|
|
acceptSpaceBar: true,
|
2020-09-15 18:04:23 +08:00
|
|
|
displayTimeout: 120000
|
2020-09-15 17:56:44 +08:00
|
|
|
};
|
2017-01-17 16:31:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
2017-01-09 18:33:28 +08:00
|
|
|
$(field)
|
2020-09-15 17:56:44 +08:00
|
|
|
.on('shown.atwho', function() {
|
2020-09-17 19:11:54 +08:00
|
|
|
var $currentAtWho = $('.atwho-view[style]:not(.old)');
|
2020-09-22 16:15:01 +08:00
|
|
|
var atWhoId = $currentAtWho.find('.atwho-header-res').data('at-who-key');
|
2020-09-17 19:11:54 +08:00
|
|
|
$currentAtWho.addClass('old').attr('data-at-who-id', atWhoId);
|
|
|
|
$(field).attr('data-smart-annotation', atWhoId);
|
|
|
|
|
2020-09-15 17:56:44 +08:00
|
|
|
$currentAtWho.find('.tab-button').off().on('shown.bs.tab', function() {
|
|
|
|
$(field).click().focus();
|
|
|
|
$(this).closest('.nav-tabs').find('.tab-button').removeClass('active');
|
|
|
|
$(this).addClass('active');
|
|
|
|
});
|
|
|
|
$currentAtWho.find('.repository-object').off().on('click', function() {
|
|
|
|
$(this).parent().find('.repository-object').removeClass('btn-primary')
|
|
|
|
.addClass('btn-light');
|
|
|
|
$(this).addClass('btn-primary').removeClass('btn-light');
|
|
|
|
$(field).click().focus();
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($currentAtWho.find('.tab-pane.active').length === 0) {
|
2020-09-22 16:15:01 +08:00
|
|
|
let filterType = DEFAULT_SEARCH_FILTER.tag;
|
2020-09-15 18:34:51 +08:00
|
|
|
let teamId = $currentAtWho.find('.atwho-header-res').data('team-id');
|
2020-09-17 19:11:54 +08:00
|
|
|
let remeberedState = localStorage.getItem('smart_annotation_states/teams/' + teamId);
|
2020-09-15 18:34:51 +08:00
|
|
|
if (remeberedState) {
|
2020-09-17 19:11:54 +08:00
|
|
|
try {
|
|
|
|
remeberedState = JSON.parse(remeberedState);
|
|
|
|
filterType = remeberedState.tag;
|
|
|
|
$currentAtWho.find(`.repository-object[data-object-id=${remeberedState.repository}]`)
|
|
|
|
.addClass('btn-primary');
|
2020-09-17 21:35:12 +08:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
2020-09-15 18:34:51 +08:00
|
|
|
}
|
|
|
|
$currentAtWho.find(`.${filterType}`).click();
|
2020-09-15 17:56:44 +08:00
|
|
|
}
|
2020-09-04 22:48:53 +08:00
|
|
|
})
|
2020-09-15 17:56:44 +08:00
|
|
|
.on('reposition.atwho', function(event, flag, query) {
|
|
|
|
let inputFieldLeft = query.$inputor.offset().left;
|
|
|
|
if (inputFieldLeft > $(window).width()) {
|
|
|
|
let leftPosition;
|
|
|
|
if (inputFieldLeft < flag.left + $(window).scrollLeft()) {
|
|
|
|
leftPosition = inputFieldLeft;
|
|
|
|
} else {
|
|
|
|
leftPosition = flag.left + $(window).scrollLeft();
|
|
|
|
}
|
|
|
|
query.$el.find('.atwho-view').css('left', leftPosition + 'px');
|
2020-05-13 18:45:22 +08:00
|
|
|
}
|
2020-09-15 17:56:44 +08:00
|
|
|
if ($('.repository-show').length) {
|
|
|
|
query.$el.find('.atwho-view').css('top', flag.top + 'px');
|
2017-01-11 00:05:32 +08:00
|
|
|
}
|
2020-09-15 17:56:44 +08:00
|
|
|
})
|
|
|
|
.atwho({
|
|
|
|
at: '@',
|
|
|
|
callbacks: {
|
|
|
|
remoteFilter: function(query, callback) {
|
|
|
|
$.getJSON(FilterTypeEnum.USER.dataUrl, { query: query }, function(data) {
|
|
|
|
callback(data.users);
|
|
|
|
});
|
|
|
|
},
|
2020-09-22 16:15:01 +08:00
|
|
|
tplEval: function(_tpl, items) {
|
|
|
|
var $items = $(items.name);
|
2020-09-24 19:32:43 +08:00
|
|
|
$items.find('li').data('item-data', { 'atwho-at': '@' }); // Emulate at.js insertContentFor method
|
2020-09-22 16:15:01 +08:00
|
|
|
return $items;
|
2020-09-15 17:56:44 +08:00
|
|
|
},
|
|
|
|
highlighter: function(li, query) {
|
|
|
|
return matchHighlighter(li, query);
|
|
|
|
},
|
|
|
|
beforeInsert: function(value, li) {
|
|
|
|
return `[@${li.attr('data-full-name')}~${li.attr('data-id')}]`;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
startsWithSpace: true,
|
|
|
|
acceptSpaceBar: true,
|
|
|
|
displayTimeout: 120000
|
|
|
|
})
|
2020-09-15 18:04:23 +08:00
|
|
|
.atwho(atWhoSettings('#'));
|
2017-01-20 18:33:10 +08:00
|
|
|
// .atwho(atWhoSettings('task#', FilterTypeEnum.TASK)) Waiting for better times
|
|
|
|
// .atwho(atWhoSettings('project#', FilterTypeEnum.PROJECT))
|
|
|
|
// .atwho(atWhoSettings('experiment#', FilterTypeEnum.EXPERIMENT))
|
2018-04-03 02:38:56 +08:00
|
|
|
// .atwho(atWhoSettings('sample#', FilterTypeEnum.REPOSITORY));
|
2017-01-05 17:52:00 +08:00
|
|
|
}
|
2017-01-09 18:33:28 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
init: init
|
|
|
|
};
|
2017-01-05 17:52:00 +08:00
|
|
|
}
|
2017-07-10 22:22:32 +08:00
|
|
|
// Closes the atwho popup * needed in repositories to close the popup
|
|
|
|
// if nothing is selected and the user leaves the form *
|
|
|
|
function closePopup() {
|
2018-07-09 19:13:44 +08:00
|
|
|
$('.atwho-header-res').find('.fa-times').click();
|
2017-07-10 22:22:32 +08:00
|
|
|
}
|
2017-01-05 17:52:00 +08:00
|
|
|
|
2017-01-10 00:16:39 +08:00
|
|
|
function initialize(field) {
|
2020-09-15 17:56:44 +08:00
|
|
|
var atWho = new SetAtWho(field);
|
2017-01-10 00:16:39 +08:00
|
|
|
atWho.init();
|
|
|
|
}
|
2017-01-05 17:52:00 +08:00
|
|
|
|
2020-09-15 17:56:44 +08:00
|
|
|
return Object.freeze({
|
2017-01-23 16:47:04 +08:00
|
|
|
init: initialize,
|
2017-07-10 22:22:32 +08:00
|
|
|
preventPropagation: atwhoStopPropagation,
|
|
|
|
closePopup: closePopup
|
2017-01-23 16:47:04 +08:00
|
|
|
});
|
2020-09-15 17:56:44 +08:00
|
|
|
}());
|
2017-01-11 21:56:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
// initialize the smart annotations
|
2020-09-15 17:56:44 +08:00
|
|
|
(function() {
|
2017-01-11 21:56:47 +08:00
|
|
|
$(document).on('focus', '[data-atwho-edit]', function() {
|
2020-09-15 17:56:44 +08:00
|
|
|
if (_.isUndefined($(this).data('atwho'))) {
|
2017-01-12 16:52:29 +08:00
|
|
|
SmartAnnotation.init(this);
|
|
|
|
}
|
2017-01-11 21:56:47 +08:00
|
|
|
});
|
2020-09-22 17:38:32 +08:00
|
|
|
|
|
|
|
$(document).on('click', '.atwho-view .dismiss', function() {
|
|
|
|
$(this).closest('.atwho-view').hide();
|
|
|
|
});
|
2020-09-15 17:56:44 +08:00
|
|
|
}());
|