joshwhatk
8/14/2014 - 1:23 PM

portal.js

portal.js

(function() {
	var $portal = {};
	$portal.config = {
		'body_element': '#app-content',
		'nav_element': '#timesheetNav fieldset',
		'modal_element': '#page',
		'modal_form': '#modal_form',
		'api_url': '<?php echo $Config->Gatekeeper_url ?>/',
		'loadingButton': '',
		'loadingButtonContent': ''
	};

	$portal.fn = {
		'addLoading': function(currentObject) {
			$portal.config.loadingButtonContent = $(currentObject).html();
			$portal.config.loadingButton = $(currentObject);
			$portal.config.loadingButton.click(function(event){
				event.preventDefault();
			});

			$portal.config.loadingButton.html('<i class="fa fa-spinner fa-spin fa-lg"></i> Loading');
		}

		'removeLoading': function() {
			$portal.config.loadingButton.html($portal.config.loadingButtonContent);
		}
	};
	$(document)
		/* Update both body and navigation
		-------------------------------------------------- */
		
		.on('click', '.navigate', function() {
			
			$portal.fn.addLoading(this);

			if($(this).attr('data-val')!== undefined) {
				var $dataVal = '&'+$(this).attr('data-val');
			} else {
				var $dataVal = '';
			}
			
			var params = [];
			// pass in the 'view' as the name and the view you are directing it towards as the value
			params = $($portal.config.body_element).serialize()+"&"+$(this).attr('name')+"="+$(this).attr('value')+$dataVal;
			
			
			//-- Begin Transition
			$($portal.config.body_element).fadeOut(300);
			
			
			//-- Reset #timesheetContent
			setTimeout(function() {
				$.post($portal.config.api_url, params, function(e){
						$($portal.config.body_element).empty();
						$($portal.config.body_element).append(e);
						$($portal.config.body_element).fadeIn(300);

						// reset the params array
						params.length = 0;
					}, "html");
			}, 300);
				
			$.post($portal.config.api_url, params, function(e){
					$($portal.config.nav_element).empty();
					$($portal.config.nav_element).append(e);
					// reset the params array
					params.length = 0;
				}, "html");

			$portal.fn.removeLoading();

		})
		
		/* Update just body
		-------------------------------------------------- */
		
		.on('click', '.interact', function() {
			
			$portal.fn.addLoading(this);

			if($(this).attr('data-val')!== undefined) {
				var $dataVal = '&'+$(this).attr('data-val');
			} else {
				var $dataVal = '';
			}
			
			var params = [];
			// pass in the 'view' as the name and the view you are directing it towards as the value
			params = $($portal.config.body_element).serialize()+"&"+$(this).attr('name')+"="+$(this).attr('value')+$dataVal;
			
			
			//-- Begin Transition
			$($portal.config.body_element).fadeOut(300);
			
			
			//-- Reset #timesheetContent
			setTimeout(function() {
				$.post($portal.config.api_url, params, function(e){
						$($portal.config.body_element).empty();
						$($portal.config.body_element).append(e);
						$($portal.config.body_element).fadeIn(300);

						// reset the params array
						params.length = 0;
					}, "html");
			}, 300);
			
			$portal.fn.removeLoading();

		})
		
		/* Update just body (no transition)
		-------------------------------------------------- */
		
		.on('click', '.interact-static', function() {
			
			$portal.fn.addLoading(this);

			if($(this).attr('data-val')!== undefined) {
				var $dataVal = '&'+$(this).attr('data-val');
			} else {
				var $dataVal = '';
			}
			
			var params = [];
			// pass in the 'view' as the name and the view you are directing it towards as the value
			params = $($portal.config.body_element).serialize()+"&"+$(this).attr('name')+"="+$(this).attr('value')+$dataVal;
			
			//-- Reset #timesheetContent
			$.post($portal.config.api_url, params, function(e){
					$($portal.config.body_element).empty();
					$($portal.config.body_element).append(e);
					
					// reset the params array
					params.length = 0;
				}, "html"
			);

			$portal.fn.removeLoading();

		})
		
		/* Update Modal Box
		-------------------------------------------------- */
		
		.on('click', '.call-modal', function() {
			
			$portal.fn.addLoading(this);

			if($(this).attr('data-val')!== undefined) {
				var $dataVal = '&'+$(this).attr('data-val');
			} else {
				var $dataVal = '';
			}
			
			//-- Set up the modal
			var $dataModalID = 'myModal';
			
			if($('#'+$dataModalID).length==0) {
			$($portal.config.modal_element).prepend('<div class="modal fade" id="'+$dataModalID+'" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true"></div>');
			}
			var $dataModal = $('#'+$dataModalID);
			
			
			//-- Set parameters
			var params = [];
			// pass in the 'view' as the name and the view you are directing it towards as the value
			params = $($portal.config.modal_form).serialize()+"&"+$(this).attr('name')+"="+$(this).attr('value')+$dataVal;

			//-- Reset the modal
			$.post($portal.config.api_url, params, function(e){
					$dataModal.empty();
					$dataModal.append(e);

					// reset the params array
					params.length = 0;
				}, "html");
			
			$dataModal.modal({
				keyboard: true,
				backdrop: true
			}).css("position", "absolute").css("z-index", "1541");

			$portal.fn.removeLoading();

		});
})();