scinote-web/app/assets/javascripts/dashboard/recent_work.js

61 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-03-09 22:10:51 +08:00
/* global I18n PerfectSb*/
2020-03-06 20:51:18 +08:00
/* eslint-disable no-param-reassign */
var DasboardRecentWorkWidget = (function() {
2020-03-09 22:10:51 +08:00
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);
2020-03-09 22:10:51 +08:00
container.append(recentWorkItem);
if (item.code) {
recentWorkItemType.attr('data-toggle', 'tooltip');
recentWorkItemType.attr('title', `${item.type} ID: ${item.code}`);
recentWorkItemType.tooltip();
}
recentWorkItemType.attr('data-e2e', `e2e-TL-dashRecentWork-${item.name}`);
2020-03-06 20:51:18 +08:00
});
}
function initRecentWork() {
2020-03-09 22:10:51 +08:00
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());
2020-03-06 20:51:18 +08:00
}
2020-03-09 22:10:51 +08:00
PerfectSb().update_all();
2020-03-06 20:51:18 +08:00
});
}
2020-03-09 22:10:51 +08:00
function initNavbar() {
$('.recent-work-navbar .navbar-link').on('click', function() {
$(this).parent().find('.navbar-link').removeClass('active');
$(this).addClass('active');
initRecentWork();
});
}
2020-03-06 20:51:18 +08:00
return {
init: () => {
if ($('.recent-work-widget').length) {
initNavbar();
initRecentWork();
}
}
};
}());
$(document).on('turbolinks:load', function() {
DasboardRecentWorkWidget.init();
});