Files
NodeBB/public/src/widgets.js

91 lines
3.2 KiB
JavaScript
Raw Normal View History

"use strict";
(function (ajaxify) {
2014-03-28 15:35:07 -04:00
ajaxify.widgets = {};
2014-04-03 17:27:26 -04:00
ajaxify.widgets.reposition = function (location) {
2016-10-14 10:59:16 +03:00
$('body [has-widget-class]').each(function () {
var $this = $(this);
2016-10-14 10:59:16 +03:00
if ($this.attr('has-widget-target') === location) {
$this.removeClass();
2016-10-14 10:59:16 +03:00
$this.addClass($this.attr('has-widget-class'));
}
});
};
ajaxify.widgets.render = function (template, url, callback) {
callback = callback || function () {};
2015-06-29 15:16:36 -04:00
if (template.match(/^admin/)) {
return callback();
2015-06-29 15:16:36 -04:00
}
2015-09-10 17:13:36 -04:00
2016-10-14 10:59:16 +03:00
var widgetLocations = ['sidebar', 'footer', 'header'];
$('#content [widget-area]').each(function () {
var location = $(this).attr('widget-area');
if ($.inArray(location, widgetLocations) === -1) {
widgetLocations.push(location);
}
});
$.get(config.relative_path + '/api/widgets/render' + '?' + config['cache-buster'], {
2016-10-14 10:59:16 +03:00
locations: widgetLocations,
template: template + '.tpl',
url: url,
cid: ajaxify.data.cid,
2017-02-17 19:31:21 -07:00
isMobile: utils.isMobile(),
2016-10-14 10:59:16 +03:00
}, function (renderedAreas) {
for (var x = 0; x < renderedAreas.length; x += 1) {
2016-10-14 10:59:16 +03:00
var renderedWidgets = renderedAreas[x].widgets;
var location = renderedAreas[x].location;
var html = '';
for (var i = 0; i < renderedWidgets.length; i += 1) {
2016-10-14 10:59:16 +03:00
html += templates.parse(renderedWidgets[i].html, {});
}
2016-10-14 10:59:16 +03:00
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>'));
}
2016-10-14 10:59:16 +03:00
} else if (location === 'header' && !$('#content [widget-area="header"]').length) {
$('#content').prepend($('<div class="row"><div widget-area="header" class="col-xs-12"></div></div>'));
}
2014-03-28 16:01:33 -04:00
2016-10-14 10:59:16 +03:00
area = $('#content [widget-area="' + location + '"]');
}
2016-10-14 10:59:16 +03:00
area.html(html);
if (renderedWidgets.length) {
area.removeClass('hidden');
ajaxify.widgets.reposition(location);
}
2016-10-14 10:59:16 +03:00
}
2015-03-12 17:46:49 -04:00
2016-10-14 10:59:16 +03:00
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',
2017-02-17 19:31:21 -07:00
title: $(this).attr('title'),
2015-03-12 17:46:49 -04:00
});
});
2016-10-14 10:59:16 +03:00
$(window).trigger('action:widgets.loaded', {});
2016-10-14 10:59:16 +03:00
callback(renderedAreas);
});
};
2014-04-10 20:31:57 +01:00
}(ajaxify || {}));