From 76de2d1380c4123e799c58407a895e1c62564ac7 Mon Sep 17 00:00:00 2001 From: Martin Artnik Date: Tue, 29 Nov 2022 13:47:52 +0100 Subject: [PATCH] Make deferred SmartAnnotation loading optional [SCI-7534] --- .../repositories/renderers/edit_renderers.js | 2 +- .../javascripts/repositories/row_editor.js | 2 +- app/assets/javascripts/sitewide/atwho_res.js | 164 +++++++++--------- 3 files changed, 87 insertions(+), 81 deletions(-) diff --git a/app/assets/javascripts/repositories/renderers/edit_renderers.js b/app/assets/javascripts/repositories/renderers/edit_renderers.js index b90443c0f..e61065bc3 100644 --- a/app/assets/javascripts/repositories/renderers/edit_renderers.js +++ b/app/assets/javascripts/repositories/renderers/edit_renderers.js @@ -40,7 +40,7 @@ $.fn.dataTable.render.editRepositoryTextValue = function(formId, columnId, cell) data-type="RepositoryTextValue"> `); $cell.find('input').val(text); - SmartAnnotation.init($cell.find('input')); + SmartAnnotation.init($cell.find('input'), true); }; $.fn.dataTable.render.editRepositoryListValue = function(formId, columnId, cell) { diff --git a/app/assets/javascripts/repositories/row_editor.js b/app/assets/javascripts/repositories/row_editor.js index 93c916292..c2cb1aee5 100644 --- a/app/assets/javascripts/repositories/row_editor.js +++ b/app/assets/javascripts/repositories/row_editor.js @@ -16,7 +16,7 @@ var RepositoryDatatableRowEditor = (function() { function initSmartAnnotation($row) { $row.find('[data-object="repository_cell"]').each(function(el) { if (el.data('atwho')) { - SmartAnnotation.init(el); + SmartAnnotation.init(el, true); } }); } diff --git a/app/assets/javascripts/sitewide/atwho_res.js b/app/assets/javascripts/sitewide/atwho_res.js index 69f64d3ba..b9ce45dc0 100644 --- a/app/assets/javascripts/sitewide/atwho_res.js +++ b/app/assets/javascripts/sitewide/atwho_res.js @@ -11,7 +11,7 @@ var SmartAnnotation = (function() { }); } - function SetAtWho(field) { + function SetAtWho(field, deferred) { var FilterTypeEnum = Object.freeze({ USER: { tag: 'users', dataUrl: $(document.body).attr('data-atwho-users-url') }, TASK: { tag: 'sa-tasks', dataUrl: $(document.body).attr('data-atwho-task-url') }, @@ -121,90 +121,96 @@ var SmartAnnotation = (function() { }; } - function init() { - $(field).on('focus', function() { - if ($(this).data('atwho-initialized')) return; + 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); + $(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.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.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); - } + 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); } - $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(); - } - query.$el.find('.atwho-view').css('left', leftPosition + 'px'); + $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(); } - 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); - }); - }, - 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')}]`; - } + 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); + }); }, - startsWithSpace: true, - acceptSpaceBar: true, - displayTimeout: 120000 - }) - .atwho(atWhoSettings('#')); - // .atwho(atWhoSettings('task#', FilterTypeEnum.TASK)) Waiting for better times - // .atwho(atWhoSettings('project#', FilterTypeEnum.PROJECT)) - // .atwho(atWhoSettings('experiment#', FilterTypeEnum.EXPERIMENT)) - // .atwho(atWhoSettings('sample#', FilterTypeEnum.REPOSITORY)); + 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('#')); + // .atwho(atWhoSettings('task#', FilterTypeEnum.TASK)) Waiting for better times + // .atwho(atWhoSettings('project#', FilterTypeEnum.PROJECT)) + // .atwho(atWhoSettings('experiment#', FilterTypeEnum.EXPERIMENT)) + // .atwho(atWhoSettings('sample#', FilterTypeEnum.REPOSITORY)); - $(this).data('atwho-initialized', true); - }); + $(this).data('atwho-initialized', true); + } + + function init() { + if (deferred) { + $(field).on('focus', initAtwho); + } else { + initAtwho(); + } } return { @@ -217,8 +223,8 @@ var SmartAnnotation = (function() { $('.atwho-header-res').find('.fa-times').click(); } - function initialize(field) { - var atWho = new SetAtWho(field); + function initialize(field, deferred) { + var atWho = new SetAtWho(field, deferred); atWho.init(); }