var SmartAnnotation = (function() { 'use strict'; // 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; })(); function setAtWho(field) { var dataUserUrl, dataTaskUrl, dataProjectUrl, dataExperimentUrl, dataSampleUrl, dataMenuItemsUrl, prevAt, DEFAULT_SEARCH_FILTER, atWhoUpdating; dataUserUrl = $(document.body).attr('data-atwho-users-url'); dataTaskUrl = $(document.body).attr('data-atwho-task-url'); dataProjectUrl = $(document.body).attr('data-atwho-project-url'); dataExperimentUrl = $(document.body).attr('data-atwho-experiment-url'); dataSampleUrl = $(document.body).attr('data-atwho-sample-url'); dataMenuItemsUrl = $(document.body).attr('data-atwho-menu-items'); // Default selected filter when using '#' DEFAULT_SEARCH_FILTER = 'sam'; atWhoUpdating = false; // helper methods for AtWho callback function _templateEval(_tpl, map) { var res; try { res = generateTemplate(map); } catch (_error) { res = ''; } return res; } function _matchHighlighter(li, query, searchFilter) { var $li, re; function highlight(el, sel, re) { var prevVal, newVal; prevVal = el.find(sel).html(); newVal = prevVal.replace(re, '$&'); el.find(sel).html(newVal); } if (!query) { return li; } $li = $(li); re = new RegExp(query, 'gi'); // search_filter is not passed for the user if(searchFilter) { highlight($li, '[data-val=name]', re); } else { highlight($li, '[data-val=full-name]', re); highlight($li, '[data-val=email]', re); } return $li[0].outerHTML } function _generateInputTag(value, li) { var res = ''; res += '[#' + li.attr('data-name'); res += '~' + li.attr('data-type'); res += '~' + li.attr('data-id') + ']'; return res; } // check if query has some hits and disables the buttons without it function resourcesChecker(query, searchFilter) { var $element; $.getJSON( dataMenuItemsUrl, {query: query}, function(data){ if(data) { _.each($('.atwho-header-res .title .btn'), function(el) { $element = $(el); if(data[$element.data('filter')].length === 0) { $element.prop('disabled', true); $element .removeClass('btn-primary') .addClass('btn-default'); $('[data-filter="' + searchFilter +'"]') .removeClass('btn-default') .addClass('btn-primary'); } else { $element.prop('disabled', false); if($element.data('filter') == searchFilter) { $element .removeClass('btn-default') .addClass('btn-primary'); } else { $element .removeClass('btn-primary') .addClass('btn-default'); } } }); } }); } // initialise dropdown dismiss button function initDismissButton() { $('.atwho-header-res .dismiss').off('click'); $('.atwho-header-res .dismiss').on('click', function() { $(field).atwho('destroy'); init(); }); } // Initialize or update filter buttons function initButtons(query, searchFilter) { initDismissButton(); resourcesChecker(query, searchFilter); $('.atwho-header-res .title button').off(); $('.atwho-header-res .title button').on('click', function(e) { // Update the selected filter button var $selectedBtn = $(this); var $prevBtn = $selectedBtn.closest('.title').children('.btn-primary'); $selectedBtn.removeClass('btn-default').addClass('btn-primary'); $prevBtn.removeClass('btn-primary').addClass('btn-default'); // Updates query and dropdown elements; focuses input $(field).click().focus(); }); } // Generates suggestion dropdown filter function generateFilterMenu(active, res_data) { var header = '
' + '
' + '' + '' + '' + '' + '
' + '
' + '
' + '<%= I18n.t("atwho.users.navigate_1") %> ' + '<%= I18n.t("atwho.users.navigate_2") %>' + '
' + '
<%= I18n.t("atwho.users.confirm_1") %> ' + '<%= I18n.t("atwho.users.confirm_2") %>' + '
' + '
' + '<%= I18n.t("atwho.users.dismiss_1") %> ' + '<%= I18n.t("atwho.users.dismiss_2") %>' + '
' + '
' + '' + '
' + '
' + '
'; return header; } // Generates resources list items function generateTemplate(map) { var res = ''; res += '
  • '; switch(map.type) { case 'tsk': res += '' + map.type + ''; break; case 'prj': res += '' + map.type + ''; break; case 'exp': res += '' + map.type + ''; break; case 'sam': res += ''; break; } res += ' '; res += ''; res += truncateLongString(map.name, <%= Constants::NAME_TRUNCATION_LENGTH %>); res += ''; if(map.archived) { res += '<%= I18n.t("atwho.res.archived") %>'; } else { res += ''; } res += ' '; switch (map.type) { case 'tsk': res += '< ' + map.experimentName + ' < ' + map.projectName + ''; break; case 'exp': res += '< ' + map.projectName + ''; break; case 'sam': res += '' + map.description + ''; break; } res += '
  • '; return res; } /** * Hackish wrapper function to make AtWho work when switching between * multiple AtWho instances (e.g. from # to task#). * * Prevents second execution of AtWho update callback, triggered when user * switches to different AtWho instance (e.g. from # to task#), which causes * both of them to be called. In such case, AtWhO modal needs to be * rerendered. */ function atWhoSwitchHack(searchFilter, remoteFilterCb) { if(atWhoUpdating || (!$(field).length && _.isUndefined(searchFilter))) { setTimeout(function() { $(field).atwho('run'); }, 100); return; } atWhoUpdating = true; setTimeout(function() { remoteFilterCb(); atWhoUpdating = false; }, 100); } function atWhoSettings(at, defaultSearchFilter) { return { at: at, callbacks: { remoteFilter: function(query, callback) { var searchFilter = $('.atwho-view[style] .btn-primary') .data('filter'); atWhoSwitchHack(searchFilter, function() { if (_.isUndefined(searchFilter)) { searchFilter = defaultSearchFilter; } if(prevAt != at) { prevAt = at; $('.atwho-view[style]').removeAttr("style"); searchFilter = defaultSearchFilter; } 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( dataUrl, {query: query}, function(data) { callback(data.res); initButtons(query, searchFilter); } ); }); }, tplEval: function(_tpl, map) { return _templateEval(_tpl, map); }, highlighter: function(li, query) { return _matchHighlighter(li, query, true); }, beforeInsert: function(value, li) { return _generateInputTag(value, li); } }, headerTpl: generateFilterMenu(DEFAULT_SEARCH_FILTER), limit: <%= Constants::ATWHO_SEARCH_LIMIT %>, startWithSpace: true, acceptSpaceBar: true, displayTimeout: 120000 } } function init() { $(field) .atwho({ at: '@', callbacks: { remoteFilter: function(query, callback) { $.getJSON( dataUserUrl, {query: query}, function(data) { callback(data.users); initDismissButton(); } ); }, sorter: function(query, items, _searchKey) { // Sorting is already done on server-side return items; }, tplEval: function(_tpl, map) { var res; try { res = ''; res += '
  • '; res += ''; res += ''; res += map.full_name; res += ''; res += ''; res += ' '; res += '·'; res += ' '; res += ''; res += map.email; res += ''; res += ''; res += '
  • '; } catch (_error) { res = ''; } return res; }, highlighter: function(li, query) { return _matchHighlighter(li, query); }, beforeInsert: function(value, li) { var res = ''; res += '[@' + li.attr('data-full-name'); res += '~' + li.attr('data-id') + ']'; return res; } }, headerTpl: '
    ' + '
    <%= I18n.t("atwho.users.title") %>
    ' + '
    ' + '
    ' + '<%= I18n.t("atwho.users.navigate_1") %> ' + '<%= I18n.t("atwho.users.navigate_2") %>' + '
    ' + '
    ' + '<%= I18n.t("atwho.users.confirm_1") %> ' + '<%= I18n.t("atwho.users.confirm_2") %>' + '
    ' + '
    ' + '<%= I18n.t("atwho.users.dismiss_1") %> ' + '<%= I18n.t("atwho.users.dismiss_2") %>' + '
    ' + '
    ' + '' + '
    ' + '
    ' + '
    ', limit: <%= Constants::ATWHO_SEARCH_LIMIT %>, startsWithSpace: true, acceptSpaceBar: true, displayTimeout: 120000 }) .atwho(atWhoSettings('#', DEFAULT_SEARCH_FILTER)) .atwho(atWhoSettings('task#', 'tsk')) .atwho(atWhoSettings('project#', 'prj')) .atwho(atWhoSettings('experiment#', 'exp')) .atwho(atWhoSettings('sample#', 'sam')); } return { init: init }; } function initialize(field) { var atWho = new setAtWho(field); atWho.init(); } var publicApi = { init: initialize }; return publicApi; })(); // initialize the smart annotations (function initSmartAnnotation() { $(document).on('focus', '[data-atwho-edit]', function() { if(_.isUndefined($(this).data('atwho'))) { SmartAnnotation.init(this); } }); })();