mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 11:05:54 +01:00 
			
		
		
		
	refactored widgets api call to render all at once rather than one area at a time, closes https://github.com/NodeBB/NodeBB/issues/2062
This commit is contained in:
		| @@ -30,60 +30,56 @@ | |||||||
| 			ajaxify.widgets.reposition(); | 			ajaxify.widgets.reposition(); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		function renderWidgets(location) { | 		function renderWidgets(locations) { | ||||||
| 			var area = $('#content [widget-area="' + location + '"]'), | 			var areaDatas = []; | ||||||
| 				areaData = { |  | ||||||
| 					location: location, |  | ||||||
| 					template: template + '.tpl', |  | ||||||
| 					url: url |  | ||||||
| 				}; |  | ||||||
|  |  | ||||||
| 			$.get(RELATIVE_PATH + '/api/widgets/render', areaData, function(renderedWidgets) { | 			$.get(RELATIVE_PATH + '/api/widgets/render', { | ||||||
| 				var html = ''; | 				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) { | 					for (var i=0; i<renderedWidgets.length; ++i) { | ||||||
| 					html += templates.parse(renderedWidgets[i].html, {}); | 						html += templates.parse(renderedWidgets[i].html, {}); | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				if (!area.length && window.location.pathname.indexOf('/admin') === -1 && renderedWidgets.length) { |  | ||||||
| 					if (location === 'footer' && !$('#content [widget-area="footer"]').length) { |  | ||||||
| 						$('#content').append($('<div class="col-xs-12"><div widget-area="footer"></div></div>')); |  | ||||||
| 					} 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>')); |  | ||||||
| 					} else if (location === 'header' && !$('#content [widget-area="header"]').length) { |  | ||||||
| 						$('#content').prepend($('<div class="col-xs-12"><div widget-area="header"></div></div>')); |  | ||||||
| 					} | 					} | ||||||
|  |  | ||||||
| 					area = $('#content [widget-area="' + location + '"]'); | 					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="col-xs-12"><div widget-area="footer"></div></div>')); | ||||||
|  | 						} 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>')); | ||||||
|  | 						} else if (location === 'header' && !$('#content [widget-area="header"]').length) { | ||||||
|  | 							$('#content').prepend($('<div class="col-xs-12"><div widget-area="header"></div></div>')); | ||||||
|  | 						} | ||||||
|  |  | ||||||
|  | 						area = $('#content [widget-area="' + location + '"]'); | ||||||
|  | 					} | ||||||
|  |  | ||||||
|  | 					area.html(html); | ||||||
|  |  | ||||||
|  | 					if (!renderedWidgets.length) { | ||||||
|  | 						area.addClass('hidden'); | ||||||
|  | 						ajaxify.widgets.reposition(location); | ||||||
|  | 					} | ||||||
|  |  | ||||||
|  | 					$('#content [widget-area] img:not(.user-img)').addClass('img-responsive');	 | ||||||
| 				} | 				} | ||||||
|  | 				 | ||||||
| 				area.html(html); |  | ||||||
|  |  | ||||||
| 				if (!renderedWidgets.length) { |  | ||||||
| 					area.addClass('hidden'); |  | ||||||
| 					ajaxify.widgets.reposition(location); |  | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				$('#content [widget-area] img:not(.user-img)').addClass('img-responsive'); |  | ||||||
| 				checkCallback(); |  | ||||||
| 			}); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		function checkCallback() { |  | ||||||
| 			numLocations--; |  | ||||||
| 			if (numLocations < 0) { |  | ||||||
| 				$(window).trigger('action:widgets.loaded', {}); | 				$(window).trigger('action:widgets.loaded', {}); | ||||||
|  | 				 | ||||||
| 				if (typeof callback === 'function') { | 				if (typeof callback === 'function') { | ||||||
| 					callback(); | 					callback(); | ||||||
| 				} | 				} | ||||||
| 			} | 			}); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		for (var i=0; i<widgetLocations.length; ++i) { | 		renderWidgets(widgetLocations); | ||||||
| 			renderWidgets(widgetLocations[i]); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		checkCallback(); |  | ||||||
| 	}; | 	}; | ||||||
| }(ajaxify || {})); | }(ajaxify || {})); | ||||||
|   | |||||||
| @@ -84,19 +84,34 @@ apiController.getConfig = function(req, res, next) { | |||||||
|  |  | ||||||
|  |  | ||||||
| apiController.renderWidgets = function(req, res, next) { | apiController.renderWidgets = function(req, res, next) { | ||||||
| 	var uid = req.user ? req.user.uid : 0, | 	var async = require('async'), | ||||||
| 		area = { | 		uid = req.user ? req.user.uid : 0, | ||||||
|  | 		areas = { | ||||||
| 			template: req.query.template, | 			template: req.query.template, | ||||||
| 			location: req.query.location, | 			locations: req.query.locations, | ||||||
| 			url: req.query.url | 			url: req.query.url | ||||||
| 		}; | 		}, | ||||||
|  | 		renderedWidgets = []; | ||||||
|  |  | ||||||
| 	if (!area.template || !area.location) { | 	if (!areas.template || !areas.locations) { | ||||||
| 		return res.json(200, {}); | 		return res.json(200, {}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	widgets.render(uid, area, function(err, data) { | 	async.each(areas.locations, function(location, next) { | ||||||
| 		res.json(200, data); | 		widgets.render(uid, { | ||||||
|  | 			template: areas.template, | ||||||
|  | 			url: areas.url, | ||||||
|  | 			location: location | ||||||
|  | 		}, function(err, widgets) { | ||||||
|  | 			renderedWidgets.push({ | ||||||
|  | 				location: location, | ||||||
|  | 				widgets: widgets | ||||||
|  | 			}); | ||||||
|  |  | ||||||
|  | 			next(); | ||||||
|  | 		}); | ||||||
|  | 	}, function(err) { | ||||||
|  | 		res.json(200, renderedWidgets); | ||||||
| 	}); | 	}); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user