scinote-web/app/assets/javascripts/dashboard/recent_work.js
2023-03-15 11:14:52 +01:00

60 lines
1.7 KiB
JavaScript

/* global I18n PerfectSb*/
/* eslint-disable no-param-reassign */
var DasboardRecentWorkWidget = (function() {
function renderRecentWorkItem(data, container) {
$.each(data, (i, item) => {
var recentWorkItem = $($('#recent-work-item-template').html());
var recentWorkItemType = recentWorkItem.find('.object-type span');
recentWorkItem.attr('href', item.url);
recentWorkItem.find('.object-name').html(item.name);
recentWorkItemType.text(item.code || item.type);
recentWorkItem.find('.object-changed').text(item.last_change);
container.append(recentWorkItem);
if (item.code) {
recentWorkItemType.attr('data-toggle', 'tooltip');
recentWorkItemType.attr('title', `${item.type} ID: ${item.code}`);
recentWorkItemType.tooltip();
}
});
}
function initRecentWork() {
var container = $('.recent-work-container');
$('.recent-work-container').empty();
$.get(container.data('url'), {
mode: $('.recent-work-navbar .active').data('mode')
}, function(result) {
if (result.length) {
renderRecentWorkItem(result, container);
} else {
container.append($('#recent-work-no-results-template').html());
}
PerfectSb().update_all();
});
}
function initNavbar() {
$('.recent-work-navbar .navbar-link').on('click', function() {
$(this).parent().find('.navbar-link').removeClass('active');
$(this).addClass('active');
initRecentWork();
});
}
return {
init: () => {
if ($('.recent-work-widget').length) {
initNavbar();
initRecentWork();
}
}
};
}());
$(document).on('turbolinks:load', function() {
DasboardRecentWorkWidget.init();
});