Merge pull request #427 from mz3944/mz-sci-908

Some smart annotations refactoring [SCI-908]
This commit is contained in:
mz3944 2017-01-19 12:15:20 +01:00 committed by GitHub
commit da604d2619

View file

@ -22,25 +22,24 @@ var SmartAnnotation = (function() {
})(); })();
function setAtWho(field) { function setAtWho(field) {
var dataUserUrl, var FilterTypeEnum = Object.freeze({
dataTaskUrl, USER: {tag: "users",
dataProjectUrl, dataUrl: $(document.body).attr('data-atwho-users-url')},
dataExperimentUrl, TASK: {tag: "tsk",
dataSampleUrl, dataUrl: $(document.body).attr('data-atwho-task-url')},
dataMenuItemsUrl, PROJECT: {tag: "prj",
prevAt, dataUrl: $(document.body).attr('data-atwho-project-url')},
DEFAULT_SEARCH_FILTER, EXPERIMENT: {tag: "exp",
atWhoUpdating; dataUrl: $(document.body).attr('data-atwho-experiment-url')},
SAMPLE: {tag: "sam",
dataUserUrl = $(document.body).attr('data-atwho-users-url'); dataUrl: $(document.body).attr('data-atwho-sample-url')},
dataTaskUrl = $(document.body).attr('data-atwho-task-url'); MENU: {tag: "menu",
dataProjectUrl = $(document.body).attr('data-atwho-project-url'); dataUrl: $(document.body).attr('data-atwho-menu-items')}
dataExperimentUrl = $(document.body).attr('data-atwho-experiment-url'); });
dataSampleUrl = $(document.body).attr('data-atwho-sample-url'); var prevAt,
dataMenuItemsUrl = $(document.body).attr('data-atwho-menu-items'); // Default selected filter when using '#'
// Default selected filter when using '#' DEFAULT_SEARCH_FILTER = FilterTypeEnum.SAMPLE,
DEFAULT_SEARCH_FILTER = 'sam'; atWhoUpdating = false;
atWhoUpdating = false;
// helper methods for AtWho callback // helper methods for AtWho callback
function _templateEval(_tpl, map) { function _templateEval(_tpl, map) {
@ -53,7 +52,7 @@ var SmartAnnotation = (function() {
return res; return res;
} }
function _matchHighlighter(li, query, searchFilter) { function _matchHighlighter(li, query, filterType) {
var $li, re; var $li, re;
function highlight(el, sel, re) { function highlight(el, sel, re) {
@ -70,7 +69,7 @@ var SmartAnnotation = (function() {
$li = $(li); $li = $(li);
re = new RegExp(query, 'gi'); re = new RegExp(query, 'gi');
// search_filter is not passed for the user // search_filter is not passed for the user
if(searchFilter) { if(filterType) {
highlight($li, '[data-val=name]', re); highlight($li, '[data-val=name]', re);
} else { } else {
highlight($li, '[data-val=full-name]', re); highlight($li, '[data-val=full-name]', re);
@ -89,11 +88,11 @@ var SmartAnnotation = (function() {
} }
// check if query has some hits and disables the buttons without it // check if query has some hits and disables the buttons without it
function resourcesChecker(query, searchFilter) { function resourcesChecker(query, filterTypeTag) {
var $element; var $element;
$.getJSON( $.getJSON(
dataMenuItemsUrl, FilterTypeEnum.MENU.dataUrl,
{query: query}, {query: query},
function(data){ function(data){
if(data) { if(data) {
@ -104,12 +103,12 @@ var SmartAnnotation = (function() {
$element $element
.removeClass('btn-primary') .removeClass('btn-primary')
.addClass('btn-default'); .addClass('btn-default');
$('[data-filter="' + searchFilter +'"]') $('[data-filter="' + filterTypeTag +'"]')
.removeClass('btn-default') .removeClass('btn-default')
.addClass('btn-primary'); .addClass('btn-primary');
} else { } else {
$element.prop('disabled', false); $element.prop('disabled', false);
if($element.data('filter') == searchFilter) { if($element.data('filter') == filterTypeTag) {
$element $element
.removeClass('btn-default') .removeClass('btn-default')
.addClass('btn-primary'); .addClass('btn-primary');
@ -134,9 +133,9 @@ var SmartAnnotation = (function() {
} }
// Initialize or update filter buttons // Initialize or update filter buttons
function initButtons(query, searchFilter) { function initButtons(query, filterTypeTag) {
initDismissButton(); initDismissButton();
resourcesChecker(query, searchFilter); resourcesChecker(query, filterTypeTag);
$('.atwho-header-res .title button').off(); $('.atwho-header-res .title button').off();
$('.atwho-header-res .title button').on('click', function(e) { $('.atwho-header-res .title button').on('click', function(e) {
@ -247,8 +246,8 @@ var SmartAnnotation = (function() {
* both of them to be called. In such case, AtWhO modal needs to be * both of them to be called. In such case, AtWhO modal needs to be
* rerendered. * rerendered.
*/ */
function atWhoSwitchHack(searchFilter, remoteFilterCb) { function atWhoSwitchHack(filterTypeTag, remoteFilterCb) {
if(atWhoUpdating || (!$(field).length && _.isUndefined(searchFilter))) { if(atWhoUpdating || (!$(field).length && _.isUndefined(filterTypeTag))) {
setTimeout(function() { setTimeout(function() {
$(field).atwho('run'); $(field).atwho('run');
}, 100); }, 100);
@ -262,46 +261,43 @@ var SmartAnnotation = (function() {
}, 100); }, 100);
} }
function atWhoSettings(at, defaultSearchFilter) { function atWhoSettings(at, defaultFilterType) {
return { return {
at: at, at: at,
callbacks: { callbacks: {
remoteFilter: function(query, callback) { remoteFilter: function(query, callback) {
var searchFilter = $('.atwho-view[style] .btn-primary') var filterTypeTag = $('.atwho-view[style] .btn-primary')
.data('filter'); .data('filter');
atWhoSwitchHack(searchFilter, function() { atWhoSwitchHack(filterTypeTag, function() {
if (_.isUndefined(searchFilter)) { var filterType;
searchFilter = defaultSearchFilter; if (_.isUndefined(filterTypeTag)) {
// Switched smart annotation type (i.e. changed input)
filterType = defaultFilterType;
} else {
// Switched filtering type (i.e. different filter button
// pressed; works also for specific annotation types, e.g.
// task#, and coverts to the correct annotation type on confirm)
$.each(FilterTypeEnum, function(k, v) {
if (v.tag == filterTypeTag) {
filterType = FilterTypeEnum[k];
return false;
}
});
} }
if(prevAt != at) { if (prevAt != at) {
// Switching smart annotation type (i.e. chaned input)
prevAt = at; prevAt = at;
$('.atwho-view[style]').removeAttr("style"); $('.atwho-view[style]').removeAttr("style");
searchFilter = defaultSearchFilter; filterType = defaultFilterType;
}
var dataUrl;
switch(searchFilter) {
case 'tsk':
dataUrl = dataTaskUrl;
break;
case 'prj':
dataUrl = dataProjectUrl;
break;
case 'exp':
dataUrl = dataExperimentUrl;
break;
case 'sam':
dataUrl = dataSampleUrl;
break;
} }
$.getJSON( $.getJSON(
dataUrl, filterType.dataUrl,
{query: query}, {query: query},
function(data) { function(data) {
callback(data.res); callback(data.res);
initButtons(query, searchFilter); initButtons(query, filterType.tag);
} }
); );
}); });
@ -316,7 +312,7 @@ var SmartAnnotation = (function() {
return _generateInputTag(value, li); return _generateInputTag(value, li);
} }
}, },
headerTpl: generateFilterMenu(DEFAULT_SEARCH_FILTER), headerTpl: generateFilterMenu(defaultFilterType),
limit: <%= Constants::ATWHO_SEARCH_LIMIT %>, limit: <%= Constants::ATWHO_SEARCH_LIMIT %>,
startWithSpace: true, startWithSpace: true,
acceptSpaceBar: true, acceptSpaceBar: true,
@ -331,7 +327,7 @@ var SmartAnnotation = (function() {
callbacks: { callbacks: {
remoteFilter: function(query, callback) { remoteFilter: function(query, callback) {
$.getJSON( $.getJSON(
dataUserUrl, FilterTypeEnum.USER.dataUrl,
{query: query}, {query: query},
function(data) { function(data) {
callback(data.users); callback(data.users);
@ -405,10 +401,10 @@ var SmartAnnotation = (function() {
displayTimeout: 120000 displayTimeout: 120000
}) })
.atwho(atWhoSettings('#', DEFAULT_SEARCH_FILTER)) .atwho(atWhoSettings('#', DEFAULT_SEARCH_FILTER))
.atwho(atWhoSettings('task#', 'tsk')) .atwho(atWhoSettings('task#', FilterTypeEnum.TASK))
.atwho(atWhoSettings('project#', 'prj')) .atwho(atWhoSettings('project#', FilterTypeEnum.PROJECT))
.atwho(atWhoSettings('experiment#', 'exp')) .atwho(atWhoSettings('experiment#', FilterTypeEnum.EXPERIMENT))
.atwho(atWhoSettings('sample#', 'sam')); .atwho(atWhoSettings('sample#', FilterTypeEnum.SAMPLE));
} }
return { return {