mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 20:16:04 +01:00
Widgets refactor (#5817)
* widgets refactor render widgets server side widgets can use all the data the template can use * fix tests
This commit is contained in:
committed by
GitHub
parent
65329b9bd7
commit
923bd86662
@@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
|
||||
(function (ajaxify) {
|
||||
ajaxify.widgets = {};
|
||||
|
||||
@@ -14,77 +13,63 @@
|
||||
});
|
||||
};
|
||||
|
||||
ajaxify.widgets.render = function (template, url, callback) {
|
||||
ajaxify.widgets.render = function (template, callback) {
|
||||
callback = callback || function () {};
|
||||
|
||||
if (template.match(/^admin/)) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
var widgetLocations = ['sidebar', 'footer', 'header'];
|
||||
var locations = Object.keys(ajaxify.data.widgets);
|
||||
|
||||
$('#content [widget-area]').each(function () {
|
||||
var location = $(this).attr('widget-area');
|
||||
if ($.inArray(location, widgetLocations) === -1) {
|
||||
widgetLocations.push(location);
|
||||
locations.forEach(function (location) {
|
||||
var area = $('#content [widget-area="' + location + '"]');
|
||||
if (area.length) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
$.get(config.relative_path + '/api/widgets/render?' + config['cache-buster'], {
|
||||
locations: widgetLocations,
|
||||
template: template + '.tpl',
|
||||
url: url,
|
||||
cid: ajaxify.data.cid,
|
||||
isMobile: utils.isMobile(),
|
||||
}, function (renderedAreas) {
|
||||
for (var x = 0; x < renderedAreas.length; x += 1) {
|
||||
var renderedWidgets = renderedAreas[x].widgets;
|
||||
var location = renderedAreas[x].location;
|
||||
var html = '';
|
||||
var widgetsAtLocation = ajaxify.data.widgets[location] || [];
|
||||
var html = '';
|
||||
|
||||
for (var i = 0; i < renderedWidgets.length; i += 1) {
|
||||
html += templates.parse(renderedWidgets[i].html, {});
|
||||
}
|
||||
widgetsAtLocation.forEach(function (widget) {
|
||||
html += widget.html;
|
||||
|
||||
var area = $('#content [widget-area="' + location + '"]');
|
||||
|
||||
if (!area.length && window.location.pathname.indexOf('/admin') === -1 && renderedWidgets.length) {
|
||||
if (location === 'footer' && !$('#content [widget-area="footer"]').length) {
|
||||
$('#content').append($('<div class="row"><div widget-area="footer" class="col-xs-12"></div></div>'));
|
||||
} else if (location === 'sidebar' && !$('#content [widget-area="sidebar"]').length) {
|
||||
if ($('[component="account/cover"]').length) {
|
||||
$('[component="account/cover"]').nextAll().wrapAll($('<div class="row"><div class="col-lg-9 col-xs-12"></div><div widget-area="sidebar" class="col-lg-3 col-xs-12"></div></div></div>'));
|
||||
} else if ($('[component="groups/cover"]').length) {
|
||||
$('[component="groups/cover"]').nextAll().wrapAll($('<div class="row"><div class="col-lg-9 col-xs-12"></div><div widget-area="sidebar" class="col-lg-3 col-xs-12"></div></div></div>'));
|
||||
} else {
|
||||
$('#content > *').wrapAll($('<div class="row"><div class="col-lg-9 col-xs-12"></div><div widget-area="sidebar" class="col-lg-3 col-xs-12"></div></div></div>'));
|
||||
}
|
||||
} else if (location === 'header' && !$('#content [widget-area="header"]').length) {
|
||||
$('#content').prepend($('<div class="row"><div widget-area="header" class="col-xs-12"></div></div>'));
|
||||
if (location === 'footer' && !$('#content [widget-area="footer"]').length) {
|
||||
$('#content').append($('<div class="row"><div widget-area="footer" class="col-xs-12"></div></div>'));
|
||||
} else if (location === 'sidebar' && !$('#content [widget-area="sidebar"]').length) {
|
||||
if ($('[component="account/cover"]').length) {
|
||||
$('[component="account/cover"]').nextAll().wrapAll($('<div class="row"><div class="col-lg-9 col-xs-12"></div><div widget-area="sidebar" class="col-lg-3 col-xs-12"></div></div></div>'));
|
||||
} else if ($('[component="groups/cover"]').length) {
|
||||
$('[component="groups/cover"]').nextAll().wrapAll($('<div class="row"><div class="col-lg-9 col-xs-12"></div><div widget-area="sidebar" class="col-lg-3 col-xs-12"></div></div></div>'));
|
||||
} else {
|
||||
$('#content > *').wrapAll($('<div class="row"><div class="col-lg-9 col-xs-12"></div><div widget-area="sidebar" class="col-lg-3 col-xs-12"></div></div></div>'));
|
||||
}
|
||||
|
||||
area = $('#content [widget-area="' + location + '"]');
|
||||
} else if (location === 'header' && !$('#content [widget-area="header"]').length) {
|
||||
$('#content').prepend($('<div class="row"><div widget-area="header" class="col-xs-12"></div></div>'));
|
||||
}
|
||||
});
|
||||
|
||||
area = $('#content [widget-area="' + location + '"]');
|
||||
if (html && area.length) {
|
||||
area.html(html);
|
||||
|
||||
if (renderedWidgets.length) {
|
||||
area.removeClass('hidden');
|
||||
ajaxify.widgets.reposition(location);
|
||||
}
|
||||
}
|
||||
|
||||
var widgetAreas = $('#content [widget-area]');
|
||||
widgetAreas.find('img:not(.not-responsive)').addClass('img-responsive');
|
||||
widgetAreas.find('.timeago').timeago();
|
||||
widgetAreas.find('img[title].teaser-pic,img[title].user-img').each(function () {
|
||||
$(this).tooltip({
|
||||
placement: 'top',
|
||||
title: $(this).attr('title'),
|
||||
});
|
||||
});
|
||||
$(window).trigger('action:widgets.loaded', {});
|
||||
|
||||
callback(renderedAreas);
|
||||
if (widgetsAtLocation.length) {
|
||||
area.removeClass('hidden');
|
||||
ajaxify.widgets.reposition(location);
|
||||
}
|
||||
});
|
||||
|
||||
var widgetAreas = $('#content [widget-area]');
|
||||
widgetAreas.find('img:not(.not-responsive)').addClass('img-responsive');
|
||||
widgetAreas.find('.timeago').timeago();
|
||||
widgetAreas.find('img[title].teaser-pic,img[title].user-img').each(function () {
|
||||
$(this).tooltip({
|
||||
placement: 'top',
|
||||
title: $(this).attr('title'),
|
||||
});
|
||||
});
|
||||
$(window).trigger('action:widgets.loaded', {});
|
||||
callback();
|
||||
};
|
||||
}(ajaxify || {}));
|
||||
|
||||
Reference in New Issue
Block a user