scinote-web/app/assets/javascripts/sitewide/atwho_res.js

270 lines
9.7 KiB
JavaScript
Raw Normal View History

/* global PerfectScrollbar MyModuleRepositories HelperModule _ */
2020-09-15 17:56:44 +08:00
2017-01-10 00:16:39 +08:00
var SmartAnnotation = (function() {
2017-01-05 17:52:00 +08:00
'use strict';
// stop the user annotation popover on click propagation
function atwhoStopPropagation(element) {
$(element).on('click', function(e) {
e.stopPropagation();
e.preventDefault();
});
}
function SetAtWho(field, deferred, assignableMyModuleId) {
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') }
});
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);
var $liText = $html.find('.item-text, .sa-type');
2020-09-15 17:56:44 +08:00
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>'));
});
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 = '';
$.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;
}
});
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) {
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');
params.assignable_my_module_id = assignableMyModuleId;
2020-09-04 22:48:53 +08:00
if (activeRepository.length) {
2020-09-15 17:56:44 +08:00
params.repository_id = activeRepository.data('object-id');
}
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({
tag: filterType.tag,
repository: data.repository
}));
2020-09-07 16:46:51 +08:00
callback(data.res);
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');
}
if ($('.atwho-scroll-container').last()[0]) {
// eslint-disable-next-line no-new
new PerfectScrollbar($('.atwho-scroll-container').last()[0]);
}
});
2020-09-15 17:56:44 +08:00
return true;
},
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;
},
2020-09-15 17:56:44 +08:00
highlighter: function(li, query) {
2020-09-15 18:04:23 +08:00
return matchHighlighter(li, query);
},
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')}]`;
},
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;
}
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');
match = regexp.exec(subtext);
if (match) {
2020-09-23 17:13:59 +08:00
return match[1] || '';
}
2020-09-15 17:56:44 +08:00
return null;
}
},
2020-09-15 18:04:23 +08:00
headerTpl: generateFilterMenu(),
startWithSpace: true,
acceptSpaceBar: true,
2020-09-15 18:04:23 +08:00
displayTimeout: 120000
2020-09-15 17:56:44 +08:00
};
}
function initAtwho() {
if ($(this).data('atwho-initialized')) return;
$(field).on('shown.atwho', function() {
var $currentAtWho = $('.atwho-view[style]:not(.old)');
var atWhoId = $currentAtWho.find('.atwho-header-res').data('at-who-key');
$currentAtWho.addClass('old').attr('data-at-who-id', atWhoId);
$(field).attr('data-smart-annotation', atWhoId);
$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();
});
$currentAtWho.on('click', '.atwho-assign-button', function() {
let el = $(this);
$.ajax({
method: 'POST',
url: el.data('assign-url'),
data: { repository_row_id: el.data('repository-row-id') },
dataType: 'json',
success: function(data) {
if (typeof MyModuleRepositories !== 'undefined') {
MyModuleRepositories.reloadRepositoriesList(el.data('repository-id'));
}
HelperModule.flashAlertMsg(data.flash, 'success');
},
error: function(response) {
HelperModule.flashAlertMsg(response.responseJSON.flash, 'danger');
}
});
});
if ($currentAtWho.find('.tab-pane.active').length === 0) {
let filterType = DEFAULT_SEARCH_FILTER.tag;
let teamId = $currentAtWho.find('.atwho-header-res').data('team-id');
let remeberedState = localStorage.getItem('smart_annotation_states/teams/' + teamId);
if (remeberedState) {
try {
remeberedState = JSON.parse(remeberedState);
filterType = remeberedState.tag;
$currentAtWho.find(`.repository-object[data-object-id=${remeberedState.repository}]`)
.addClass('btn-primary');
} catch (error) {
console.error(error);
2020-09-15 17:56:44 +08:00
}
}
$currentAtWho.find(`.${filterType}`).click();
}
}).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();
2017-01-11 00:05:32 +08:00
}
query.$el.find('.atwho-view').css('left', leftPosition + 'px');
}
if ($('.repository-show').length) {
query.$el.find('.atwho-view').css('top', flag.top + 'px');
}
}).atwho({
at: '@',
callbacks: {
remoteFilter: function(query, callback) {
$.getJSON(FilterTypeEnum.USER.dataUrl, { query: query }, function(data) {
callback(data.users);
});
2020-09-15 17:56:44 +08:00
},
tplEval: function(_tpl, items) {
var $items = $(items.name);
$items.find('li').data('item-data', { 'atwho-at': '@' }); // Emulate at.js insertContentFor method
return $items;
},
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
})
.atwho(atWhoSettings('#'));
$(this).data('atwho-initialized', true);
}
function init() {
if (deferred) {
$(field).on('focus', initAtwho);
} else {
initAtwho();
}
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
}
// Closes the atwho popup * needed in repositories to close the popup
// if nothing is selected and the user leaves the form *
function closePopup() {
$('.atwho-header-res').find('.fa-times').click();
}
2017-01-05 17:52:00 +08:00
function initialize(field, deferred, assignableMyModuleId) {
var atWho = new SetAtWho(field, deferred, assignableMyModuleId);
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({
init: initialize,
preventPropagation: atwhoStopPropagation,
closePopup: closePopup
});
2020-09-15 17:56:44 +08:00
}());
// initialize the smart annotations
2020-09-15 17:56:44 +08:00
(function() {
$(document).on('focus', '[data-atwho-edit]', function() {
2020-09-15 17:56:44 +08:00
if (_.isUndefined($(this).data('atwho'))) {
SmartAnnotation.init(this, false);
2017-01-12 16:52:29 +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
}());