ds/UrlSettings.js

/**
 * @fileoverview Exposed data type for use outside of DsClient.
 *               All properties should be exposed to allow use from uncompiled
 *               code.
 *
 * @author anders.rejdebrant@spark-vision.com (Anders Rejdebrant)
 */

goog.provide('spv.ds.UrlSettings');




/**
 * Base URLs needed for most GET operations for images etc.
 *
 * @struct
 * @constructor
 * @param {string} gui_url
 * @param {string} icon_url
 * @param {string} composed_url
 * @param {string} readmore_content_url
 * @param {string} summary_content_url
 */
spv.ds.UrlSettings = function(
		gui_url,
		icon_url,
		composed_url,
		readmore_content_url,
		summary_content_url)
{
	/**
	 * @nocollapse
	 * @type {string}
	 */
	this.gui_url = spv.ds.UrlSettings.urlFix( gui_url );

	/**
	 * @nocollapse
	 * @type {string}
	 */
	this.icon_url = spv.ds.UrlSettings.urlFix( icon_url );

	/**
	 * @nocollapse
	 * @type {string}
	 */
	this.composed_url = spv.ds.UrlSettings.urlFix( composed_url );

	/**
	 * @nocollapse
	 * @type {string}
	 */
	this.readmore_content_url = spv.ds.UrlSettings.urlFix( readmore_content_url );

	/**
	 * @nocollapse
	 * @type {string}
	 */
	this.summary_content_url = spv.ds.UrlSettings.urlFix( summary_content_url );
};


/**
 * @param {string} url
 * @return {string}
 */
spv.ds.UrlSettings.urlFix = function( url )
{
	// A bit of a hack. The backend can't always return https due to the proxy.
	if( location.protocol == "https:" )
	{
		var protocol_i = url.indexOf(":");
		if( protocol_i != -1 )
			url = "https:" + url.substring( protocol_i+1 );
	}
	return spv.ds.UrlSettings.enforceEndSlash( url );
}


/**
 * @param {string} url
 * @return {string}
 */
spv.ds.UrlSettings.enforceEndSlash = function( url )
{
	var ends_with_slash = url.length > 0 && url[ url.length - 1 ] === '/';
	if( ends_with_slash )
	{
		return url;
	}
	else
	{
		return url + '/';
	}
};