You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/public/src/widgets.js

87 lines
2.7 KiB
JavaScript

"use strict";
/*global ajaxify, templates, config, RELATIVE_PATH*/
(function(ajaxify) {
11 years ago
ajaxify.widgets = {};
11 years ago
ajaxify.widgets.reposition = function(location) {
$('body [no-widget-class]').each(function() {
var $this = $(this);
if ($this.attr('no-widget-target') === location) {
$this.removeClass();
11 years ago
$this.addClass($this.attr('no-widget-class'));
}
});
};
ajaxify.widgets.render = function(template, url, callback) {
var widgetLocations = ['sidebar', 'footer', 'header'], numLocations;
$('#content [widget-area]').each(function() {
var location = $(this).attr('widget-area');
if ($.inArray(location, widgetLocations) === -1) {
widgetLocations.push(location);
}
});
numLocations = widgetLocations.length;
if (!numLocations) {
ajaxify.widgets.reposition();
}
11 years ago
function renderWidgets(locations) {
var areaDatas = [];
10 years ago
$.get(RELATIVE_PATH + '/api/widgets/render' + (config['cache-buster'] ? '?v=' + config['cache-buster'] : ''), {
locations: locations,
template: template + '.tpl',
url: url
}, function(renderedAreas) {
for (var x=0; x<renderedAreas.length; ++x) {
var renderedWidgets = renderedAreas[x].widgets,
location = renderedAreas[x].location,
html = '';
for (var i=0; i<renderedWidgets.length; ++i) {
html += templates.parse(renderedWidgets[i].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) {
10 years ago
$('#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>'));
}
11 years ago
area = $('#content [widget-area="' + location + '"]');
}
area.html(html);
if (!renderedWidgets.length) {
area.addClass('hidden');
ajaxify.widgets.reposition(location);
}
}
10 years ago
var widgetAreas = $('#content [widget-area]');
widgetAreas.find('img:not(.user-img)').addClass('img-responsive');
widgetAreas.find('span.timeago').timeago();
10 years ago
$(window).trigger('action:widgets.loaded', {});
10 years ago
if (typeof callback === 'function') {
callback();
}
});
}
renderWidgets(widgetLocations);
};
}(ajaxify || {}));