'use strict';
(function (ajaxify) {
ajaxify.widgets = {};
ajaxify.widgets.reposition = function (location) {
$('body [has-widget-class]').each(function () {
var $this = $(this);
if ($this.attr('has-widget-target') === location) {
$this.removeClass();
$this.addClass($this.attr('has-widget-class'));
}
});
};
ajaxify.widgets.render = function (template, callback) {
callback = callback || function () {};
if (template.match(/^admin/)) {
return callback();
}
var locations = Object.keys(ajaxify.data.widgets);
locations.forEach(function (location) {
var area = $('#content [widget-area="' + location + '"]');
if (area.length) {
return;
}
var widgetsAtLocation = ajaxify.data.widgets[location] || [];
var html = '';
widgetsAtLocation.forEach(function (widget) {
html += widget.html;
if (location === 'footer' && !$('#content [widget-area="footer"]').length) {
$('#content').append($('
'));
} else if (location === 'sidebar' && !$('#content [widget-area="sidebar"]').length) {
if ($('[component="account/cover"]').length) {
$('[component="account/cover"]').nextAll().wrapAll($(''));
} else if ($('[component="groups/cover"]').length) {
$('[component="groups/cover"]').nextAll().wrapAll($(''));
} else {
$('#content > *').wrapAll($(''));
}
} else if (location === 'header' && !$('#content [widget-area="header"]').length) {
$('#content').prepend($(''));
}
});
area = $('#content [widget-area="' + location + '"]');
if (html && area.length) {
area.html(html);
}
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 || {}));