Files
NodeBB/public/src/widgets.js

90 lines
2.6 KiB
JavaScript
Raw Normal View History

"use strict";
/*global ajaxify, templates*/
(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) {
$('body [no-widget-class]').each(function() {
var $this = $(this);
if ($this.attr('no-widget-target') === location) {
$this.removeClass();
2014-04-03 17:27:26 -04:00
$this.addClass($this.attr('no-widget-class'));
}
});
};
ajaxify.widgets.render = function(template, url, callback) {
2014-05-28 16:30:16 -04:00
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();
}
2014-07-20 14:35:16 -04:00
function renderWidgets(location) {
var area = $('#content [widget-area="' + location + '"]'),
areaData = {
location: location,
template: template + '.tpl',
url: url
};
$.get(RELATIVE_PATH + '/api/widgets/render', areaData, function(renderedWidgets) {
2014-03-28 16:01:33 -04:00
var html = '';
2014-06-22 17:08:55 -04:00
for (var i=0; i<renderedWidgets.length; ++i) {
html += templates.parse(renderedWidgets[i].html, {});
}
if (!area.length && window.location.pathname.indexOf('/admin') === -1 && renderedWidgets.length) {
2014-07-20 14:35:16 -04:00
if (location === 'footer' && !$('#content [widget-area="footer"]').length) {
$('#content').append($('<div class="col-xs-12"><div widget-area="footer"></div></div>'));
2014-07-20 14:35:16 -04:00
} else if (location === 'sidebar' && !$('#content [widget-area="sidebar"]').length) {
$('#content > *').wrapAll($('<div class="col-lg-9 col-xs-12"></div>'));
$('#content').append($('<div class="col-lg-3 col-xs-12"><div widget-area="sidebar"></div></div>'));
2014-07-20 14:35:16 -04:00
} else if (location === 'header' && !$('#content [widget-area="header"]').length) {
2014-05-28 16:30:16 -04:00
$('#content').prepend($('<div class="col-xs-12"><div widget-area="header"></div></div>'));
}
area = $('#content [widget-area="' + location + '"]');
}
2014-05-28 16:24:10 -04:00
area.html(html);
2014-06-22 17:08:55 -04:00
2014-03-28 16:01:33 -04:00
if (!renderedWidgets.length) {
2014-05-28 16:24:10 -04:00
area.addClass('hidden');
ajaxify.widgets.reposition(location);
2014-03-28 16:01:33 -04:00
}
$('#content [widget-area] img:not(.user-img)').addClass('img-responsive');
checkCallback();
});
}
function checkCallback() {
numLocations--;
if (numLocations < 0) {
2014-05-26 11:37:50 -04:00
$(window).trigger('action:widgets.loaded', {});
if (typeof callback === 'function') {
callback();
}
}
}
2014-06-22 17:08:55 -04:00
for (var i=0; i<widgetLocations.length; ++i) {
renderWidgets(widgetLocations[i]);
}
checkCallback();
};
2014-04-10 20:31:57 +01:00
}(ajaxify || {}));