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

363 lines
12 KiB
Plaintext
Raw Normal View History

2017-01-10 00:16:39 +08:00
var SmartAnnotation = (function() {
2017-01-05 17:52:00 +08:00
'use strict';
2017-01-09 18:33:28 +08:00
// utilities
var Util = (function() {
// helper method that binds show/hidden action
function showHideBinding() {
$.each(['show', 'hide'], function (i, ev) {
var el = $.fn[ev];
$.fn[ev] = function () {
this.trigger(ev);
return el.apply(this, arguments);
};
});
}
var publicApi = {
showHideBinding: showHideBinding
};
return publicApi;
})();
// stop the user annotation popover on click propagation
function atwhoStopPropagation(element) {
$(element).on('click', function(e) {
e.stopPropagation();
e.preventDefault();
});
}
2017-01-10 00:16:39 +08:00
function setAtWho(field) {
var FilterTypeEnum = Object.freeze({
USER: {tag: "users",
dataUrl: $(document.body).attr('data-atwho-users-url')},
2020-09-04 22:48:53 +08:00
TASK: {tag: "sa-tasks",
dataUrl: $(document.body).attr('data-atwho-task-url')},
2020-09-04 22:48:53 +08:00
PROJECT: {tag: "sa-projects",
dataUrl: $(document.body).attr('data-atwho-project-url')},
2020-09-04 22:48:53 +08:00
EXPERIMENT: {tag: "sa-experiments",
dataUrl: $(document.body).attr('data-atwho-experiment-url')},
2020-09-04 22:48:53 +08:00
REPOSITORY: {tag: "sa-repositories",
dataUrl: $(document.body).attr('data-atwho-rep-items-url')},
MENU: {tag: "menu",
dataUrl: $(document.body).attr('data-atwho-menu-items')}
});
var prevAt,
// Default selected filter when using '#'
DEFAULT_SEARCH_FILTER = FilterTypeEnum.REPOSITORY,
atWhoUpdating = false;
2017-01-05 17:52:00 +08:00
var defaultRepId;
2017-01-09 18:33:28 +08:00
// helper methods for AtWho callback
function _templateEval(_tpl, map) {
var res;
try {
2020-09-07 16:46:51 +08:00
res = generateTemplate(map);
2017-01-09 18:33:28 +08:00
} catch (_error) {
res = '';
}
return res;
}
2017-01-05 20:29:21 +08:00
function _matchHighlighter(li, query, filterType) {
var $li, re;
2017-01-12 16:52:29 +08:00
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 || $(li).data('no-results')) {
2017-01-09 18:33:28 +08:00
return li;
}
$li = $(li);
2017-01-09 18:33:28 +08:00
re = new RegExp(query, 'gi');
2017-01-12 16:52:29 +08:00
// search_filter is not passed for the user
if(filterType) {
highlight($li, '[data-val=name]', re);
} else {
highlight($li, '[data-val=full-name]', re);
highlight($li, '[data-val=email]', re);
}
return $li[0].outerHTML
2017-01-09 18:33:28 +08:00
}
function _generateInputTag(value, li) {
2020-09-04 22:48:53 +08:00
return `[#${li.attr('data-name')}~${li.attr('data-type')}~${li.attr('data-id')}]`;
2017-01-09 18:33:28 +08:00
}
// initialise dropdown dismiss button
function initDismissButton($currentAtWho) {
$currentAtWho.find('.dismiss').off('click')
.on('click', function() {
2017-01-11 00:05:32 +08:00
$(field).atwho('destroy');
init();
});
}
2017-01-09 18:33:28 +08:00
// Generates suggestion dropdown filter
function generateFilterMenu(active, res_data) {
2020-09-04 22:48:53 +08:00
var menu = '';
var rep_buttons = '';
$.ajax({
async: false,
dataType: 'json',
url: $(document.body).attr('data-atwho-repositories-url'),
success: function(data) {
2020-09-04 22:48:53 +08:00
menu = data.html
}
});
2020-09-04 22:48:53 +08:00
return menu;
2017-01-05 20:29:21 +08:00
}
function noResultsTemplate() {
2020-09-07 16:46:51 +08:00
return `<div class="atwho-no-results" data-no-results="1">
<span><%= I18n.t("atwho.no_results") %></span>
</div>`;
}
2017-01-05 17:52:00 +08:00
// Generates resources list items
function generateTemplate(map) {
2020-09-04 22:48:53 +08:00
return map.name;
}
function atWhoSettings(at, defaultFilterType) {
return {
at: at,
callbacks: {
remoteFilter: function(query, callback) {
var $currentAtWho = $('.atwho-view[style]');
2020-09-04 22:48:53 +08:00
var filterType;
var params = { query: query };
filterType = FilterTypeEnum[$currentAtWho.find('.tab-pane.active').data('object-type')]
if (!filterType) {
2020-09-07 16:46:51 +08:00
callback([{name: noResultsTemplate()}]);
2020-09-04 22:48:53 +08:00
return false
}
2020-09-04 22:48:53 +08:00
if(filterType.tag === 'sa-repositories') {
let repositoryTab = $currentAtWho.find('[data-object-type="REPOSITORY"]')
let activeRepository = repositoryTab.find('.btn-primary');
if (activeRepository.length) {
params['repository_id'] = activeRepository.data('object-id')
}
2020-09-04 22:48:53 +08:00
}
$.getJSON(filterType.dataUrl, params, function(data) {
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}"]`)
.addClass('btn-primary').removeClass('btn-light')
}
});
},
sorter: function(query, items, _searchKey) {
// Sorting is already done on server-side
return items;
},
tplEval: function(_tpl, map) {
return _templateEval(_tpl, map);
},
highlighter: function(li, query) {
2020-09-07 16:46:51 +08:00
//return _matchHighlighter(li, query, true);
return li;
},
beforeInsert: function(value, li) {
return _generateInputTag(value, li);
},
matcher:function(flag, subtext, should_startWithSpace, acceptSpaceBar) {
var _a, _y, match, regexp, space;
flag = flag.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
if (should_startWithSpace) {
flag = '(?:^|\\s)' + flag;
}
_a = decodeURI("%C3%80");
_y = decodeURI("%C3%BF");
regexp = new RegExp(flag + "([A-Za-z" + _a + "-" + _y + "0-9_/:\\s\+\-\]*)$|" + flag + "([^\\x00-\\xff]*)$", 'gi');
match = regexp.exec(subtext);
if (match) {
return match[2] || match[1];
} else {
return null;
}
},
},
headerTpl: generateFilterMenu(defaultFilterType),
limit: <%= Constants::ATWHO_SEARCH_LIMIT %>,
startWithSpace: true,
acceptSpaceBar: true,
displayTimeout: 120000
}
}
function init() {
2017-01-09 18:33:28 +08:00
$(field)
2020-09-04 22:48:53 +08:00
.on("shown.atwho", function() {
var $currentAtWho = $('.atwho-view[style]');
$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) {
let filterType = DEFAULT_SEARCH_FILTER;
$currentAtWho.find(`.${filterType.tag}`).click();
}
})
2020-01-16 00:19:56 +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-01-16 23:28:51 +08:00
}
2020-01-17 20:55:43 +08:00
if ($('.repository-show').length) {
query.$el.find('.atwho-view').css('top', flag.top + 'px');
}
2020-01-16 00:19:56 +08:00
})
2017-01-11 00:05:32 +08:00
.atwho({
at: '@',
callbacks: {
remoteFilter: function(query, callback) {
$.getJSON(
FilterTypeEnum.USER.dataUrl,
2017-01-11 00:05:32 +08:00
{query: query},
function(data) {
if (data.users.length < 1) {
callback([{no_results: 1}]);
} else {
callback(data.users);
}
initDismissButton($('.atwho-view[style]'));
2017-01-11 00:05:32 +08:00
}
);
},
sorter: function(query, items, _searchKey) {
// Sorting is already done on server-side
return items;
},
tplEval: function(_tpl, map) {
var res;
try {
if (map.no_results) {
res = noResultsTemplate();
} else {
res = '';
res += '<li class="atwho-li atwho-li-user" ';
res += 'data-id="' + map.id + '" ';
res += 'data-full-name="' + map.full_name + '">';
res += '<span class="global-avatar-container"><img src="' + map.img_url + '" class="avatar" /></span>';
res += '<span data-val="full-name">';
res += map.full_name;
res += '</span>';
res += '<small>';
res += '&nbsp;';
res += '&#183;';
res += '&nbsp;';
res += '<span data-val="email">';
res += map.email;
res += '</span>';
res += '</small>';
res += '</li>';
}
2017-01-11 00:05:32 +08:00
} catch (_error) {
res = '';
}
return res;
},
highlighter: function(li, query) {
return _matchHighlighter(li, query);
2017-01-11 00:05:32 +08:00
},
beforeInsert: function(value, li) {
var res = '';
res += '[@' + li.attr('data-full-name');
res += '~' + li.attr('data-id') + ']';
return res;
}
},
headerTpl:
'<div class="atwho-header-res">' +
'<div class="title-user"><%= I18n.t("atwho.users.title") %></div>' +
2017-01-11 00:05:32 +08:00
'<div class="help">' +
'<div>' +
'<strong><%= I18n.t("atwho.users.navigate_1") %></strong> ' +
'<%= I18n.t("atwho.users.navigate_2") %>' +
'</div>' +
'<div>' +
'<strong><%= I18n.t("atwho.users.confirm_1") %></strong> ' +
'<%= I18n.t("atwho.users.confirm_2") %>' +
'</div>' +
'<div>' +
'<strong><%= I18n.t("atwho.users.dismiss_1") %></strong> ' +
'<%= I18n.t("atwho.users.dismiss_2") %>' +
'</div>' +
'</div>' +
2017-01-11 00:05:32 +08:00
'<div class="dismiss">' +
'<span class="fas fa-times"></span>' +
2017-01-11 00:05:32 +08:00
'</div>' +
'</div>',
limit: <%= Constants::ATWHO_SEARCH_LIMIT %>,
startsWithSpace: true,
acceptSpaceBar: true,
displayTimeout: 120000
})
.atwho(atWhoSettings('#', DEFAULT_SEARCH_FILTER))
// .atwho(atWhoSettings('task#', FilterTypeEnum.TASK)) Waiting for better times
// .atwho(atWhoSettings('project#', FilterTypeEnum.PROJECT))
// .atwho(atWhoSettings('experiment#', FilterTypeEnum.EXPERIMENT))
// .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
}
// 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
2017-01-10 00:16:39 +08:00
function initialize(field) {
var atWho = new setAtWho(field);
atWho.init();
}
2017-01-05 17:52:00 +08:00
var publicApi = Object.freeze({
init: initialize,
preventPropagation: atwhoStopPropagation,
closePopup: closePopup
});
2017-01-05 17:52:00 +08:00
2017-01-10 00:16:39 +08:00
return publicApi;
2017-01-05 17:52:00 +08:00
})();
// initialize the smart annotations
(function initSmartAnnotation() {
$(document).on('focus', '[data-atwho-edit]', function() {
2017-01-12 16:52:29 +08:00
if(_.isUndefined($(this).data('atwho'))) {
SmartAnnotation.init(this);
}
});
})();