Source:header-block.component.js

(function () {
	'use strict';
	// Header Block
	angular
		.module('mohistory')
		.component('headerBlock', {
			templateUrl: 'app/components/header-block/header-block.component.html',
			controller: headerBlockCtrl,
			controllerAs: 'headerBlock',
			bindings: {
				data: '<',
			}
		});
	headerBlockCtrl.$inject = ['$location', '$document'];
	/**
	 * This component can render the mhs:dataClass headerBlock when combined with any of
	 * the following mhs:displayClasses self-hero, self-hero-title-top,
	 * history-happens-here-header, or homepage-hero.
	 * @memberof mohistory
     * @name headerBlock
     * @ngdoc component
	 * @param {object} $location AngularJS wrapper for window.location
	 * @param {object} $document AngularJS wrapper for document
	 */
	function headerBlockCtrl($location, $document) {
		var vm = this;
		/* ----- Variables ----- */
		vm.urlPath = '';
		/* ----- Function Bindings ----- */
		vm.$onInit = onInit;
		vm.getURLPath = getURLPath;
		/* ----- Function Definitions ----- */
		/**
		 * Initiated when component is created. Runs code necessary for block
		 * configuration.
		 * @function onInit
		 * @memberof headerBlock
		 */
		function onInit() {
			vm.urlPath = $location.path();
			if (vm.urlPath.slice(vm.urlPath.length - 1) === '/') {
				vm.urlPath = vm.urlPath.slice(0, vm.urlPath.length - 1);
			}
		}
		/**
		 * Hide the homepage-hero if the button's link points to the current page.
		 * WILL NOT WORK OUTSIDE OF BROWSER. (source: https://stackoverflow.com/a/13405933)
		 * @function getURLPath
		 * @memberof headerBlock
		 * @param {string} href link provided from the API 
		 */
		function getURLPath(href) {
			var loc = document.createElement('a');
			loc.href = href;
			// IE doesn't populate all link properties when setting .href with a relative URL,
			// however .href will return an absolute URL which then can be used on itself
			// to populate these additional fields.
			if (loc.host == '') {
				loc.href = loc.href;
			}
			var path = loc.pathname;
			if (path.slice(path.length - 1) === '/') {
				path = path.slice(0, path.length - 1);
			}
			return path;
		};
	};
})();