| 
									
										
										
										
											2014-03-28 14:18:42 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | /*global ajaxify, socket, templates*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | (function(ajaxify) { | 
					
						
							| 
									
										
										
										
											2014-03-28 15:35:07 -04:00
										 |  |  | 	ajaxify.widgets = {}; | 
					
						
							| 
									
										
										
										
											2014-04-03 17:27:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-31 12:54:27 -04:00
										 |  |  | 	ajaxify.widgets.reposition = function(location) { | 
					
						
							| 
									
										
										
										
											2014-03-28 14:18:42 -04:00
										 |  |  | 		$('body [no-widget-class]').each(function() { | 
					
						
							|  |  |  | 			var $this = $(this); | 
					
						
							| 
									
										
										
										
											2014-03-31 12:54:27 -04:00
										 |  |  | 			if ($this.attr('no-widget-target') === location) { | 
					
						
							|  |  |  | 				$this.removeClass(); | 
					
						
							| 
									
										
										
										
											2014-04-03 17:27:26 -04:00
										 |  |  | 				$this.addClass($this.attr('no-widget-class')); | 
					
						
							| 
									
										
										
										
											2014-03-31 12:54:27 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-03-28 14:18:42 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ajaxify.widgets.render = function(tpl_url, url, callback) { | 
					
						
							| 
									
										
										
										
											2014-03-31 12:27:24 -04:00
										 |  |  | 		var widgetLocations = ['sidebar', 'footer'], numLocations; | 
					
						
							| 
									
										
										
										
											2014-03-28 14:18:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$('#content [widget-area]').each(function() { | 
					
						
							|  |  |  | 			widgetLocations.push($(this).attr('widget-area')); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		numLocations = widgetLocations.length; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!numLocations) { | 
					
						
							|  |  |  | 			ajaxify.widgets.reposition(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function renderWidgets(location) { | 
					
						
							|  |  |  | 			var area = $('#content [widget-area="' + location + '"]'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			socket.emit('widgets.render', {template: tpl_url + '.tpl', url: url, location: location}, function(err, renderedWidgets) { | 
					
						
							| 
									
										
										
										
											2014-03-28 16:01:33 -04:00
										 |  |  | 				var html = ''; | 
					
						
							| 
									
										
										
										
											2014-03-28 14:18:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 16:01:33 -04:00
										 |  |  | 				for (var widget in renderedWidgets) { | 
					
						
							|  |  |  | 					if (renderedWidgets.hasOwnProperty(widget)) { | 
					
						
							| 
									
										
										
										
											2014-03-29 17:16:41 -04:00
										 |  |  | 						html += templates.parse(renderedWidgets[widget].html, {}); | 
					
						
							| 
									
										
										
										
											2014-03-28 14:18:42 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-01 11:22:03 -04:00
										 |  |  | 				if (!area.length && window.location.pathname.indexOf('/admin') === -1 && renderedWidgets.length) { | 
					
						
							|  |  |  | 					if (location === 'footer') { | 
					
						
							|  |  |  | 						$('#content').append($('<div class="col-xs-12"><div widget-area="footer"></div></div>')); | 
					
						
							|  |  |  | 					} else if (location === 'sidebar') { | 
					
						
							| 
									
										
										
										
											2014-04-01 14:49:30 -04:00
										 |  |  | 						$('#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-04-01 11:22:03 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					area = $('#content [widget-area="' + location + '"]'); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 16:01:33 -04:00
										 |  |  | 				area.html(html).removeClass('hidden'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (!renderedWidgets.length) { | 
					
						
							| 
									
										
										
										
											2014-03-31 12:54:27 -04:00
										 |  |  | 					ajaxify.widgets.reposition(location); | 
					
						
							| 
									
										
										
										
											2014-03-28 16:01:33 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 14:18:42 -04:00
										 |  |  | 				$('#content [widget-area] img:not(.user-img)').addClass('img-responsive'); | 
					
						
							|  |  |  | 				checkCallback(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		function checkCallback() { | 
					
						
							|  |  |  | 			numLocations--; | 
					
						
							|  |  |  | 			if (numLocations < 0 && callback) { | 
					
						
							|  |  |  | 				callback(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (var i in widgetLocations) { | 
					
						
							|  |  |  | 			if (widgetLocations.hasOwnProperty(i)) { | 
					
						
							|  |  |  | 				renderWidgets(widgetLocations[i]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		checkCallback(); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | }(ajaxify || {})); |