JS Tools

The tools provides you some very common functions you will use in your modules

FrontendTools.getDataModules

Returns an array of all the DOM elements with te data-fc-modules attributes as sData.

FrontendTools.getDataModules = function(sData) {
	return $('[data-fc-modules*="'+ sData +'"]')
};

FrontendTools.loadCSS

Attach the sPathCss to the dom if it's not already attached.

/*!
 loadCSS: load a CSS file asynchronously.
 [c]2015 @scottjehl, Filament Group, Inc.
 Licensed MIT
 */
(function(w){
	"use strict";
	/* exported loadCSS */
	var loadCSS = function( href, media, before ){
		// Arguments explained:
		// `href` [REQUIRED] is the URL for your CSS file.
		// `before` [OPTIONAL] is the element the script should use as a reference for injecting our stylesheet  before
		// By default, loadCSS attempts to inject the link after the last stylesheet or script in the DOM. However, you might desire a more specific location in your document.
		// `media` [OPTIONAL] is the media type or query of the stylesheet. By default it will be 'all'
		var doc = w.document;
		var ss = doc.createElement( "link" );
		var ref;
		if( before ){
			ref = before;
		}
		else {
			var refs = ( doc.body || doc.getElementsByTagName( "head" )[ 0 ] ).childNodes;
			ref = refs[ refs.length - 1];
		}

		var sheets = doc.styleSheets;
		ss.rel = "stylesheet";
		ss.href = href;
		// temporarily set media to something inapplicable to ensure it'll fetch without blocking render
		ss.media = "only x";

		// Inject link
		// Note: the ternary preserves the existing behavior of "before" argument, but we could choose to change the argument to "after" in a later release and standardize on ref.nextSibling for all refs
		// Note: `insertBefore` is used instead of `appendChild`, for safety re: http://www.paulirish.com/2011/surefire-dom-element-insertion/
		ref.parentNode.insertBefore( ss, ( before ? ref : ref.nextSibling ) );
		// A method (exposed on return object for external use) that mimics onload by polling until document.styleSheets until it includes the new sheet.
		var onloadcssdefined = function( cb ){
			var resolvedHref = ss.href;
			var i = sheets.length;
			while( i-- ){
				if( sheets[ i ].href === resolvedHref ){
					return cb();
				}
			}
			setTimeout(function() {
				onloadcssdefined( cb );
			});
		};

		// once loaded, set link's media back to `all` so that the stylesheet applies once it loads
		ss.onloadcssdefined = onloadcssdefined;
		onloadcssdefined(function() {
			ss.media = media || "all";
		});
		return ss;
	};
	// commonjs
	if( typeof module !== "undefined" ){
		module.exports = loadCSS;
	}
	else {
		w.loadCSS = loadCSS;
	}
}( typeof global !== "undefined" ? global : this ));

FrontendTools.loadCSS = function ( sCssPath, sCssMedia ) {

	var sId = sCssPath.replace('/',''),
		fMatchMedia = window.matchMedia,
		sCssMedia = sCssMedia ? sCssMedia : 'all',
		oCss = {};

	// If the CSS is not already loaded
	if ( !fMatchMedia || window.matchMedia(sCssMedia).matches ) {

		if ( sCssPath && !document.getElementById(sId) ) {
			loadCSS(sCssPath, sCssMedia);
		}

	} else if ( !window.matchMedia(sCssMedia).matches ) {

		oCss[sCssPath] = sCssMedia;

		window.addEventListener("resize", function() {

			for ( var property in oCss) {

				if  (window.matchMedia(oCss[property]).matches) {
					loadCSS( property, oCss[property] );
				}
			}
		} );
	}



}

Remove Loading class from an object

To remove the loading class once the module is initialized

FrontendTools.removeLoading = function ( oTarget, sClass, callback ) {

	var oLoading = oTarget;

	if (sClass === undefined) {
		sClass = '';
	}

	if ( oTarget.parentNode.className.indexOf('loading') !== -1  ) {
		oLoading = oTarget.parentNode;
	}

	$(oLoading).removeClass('loading');
	$(oLoading).addClass('animated fade-in');

	if (sClass !== '' & oTarget.className.indexOf(sClass) === -1 ) {
		$(oTarget).addClass(sClass);
	}

	if (callback !== undefined) {
		callback();
	}

}

FrontendTools.mergeJSON

Merges to JSON into one overwriting the first with the second.

FrontendTools.mergeJSON = function ( oJson1, oJson2 ){

	// Properties from the Source1 object will be copied to Source2 Object.
	// Note: This method will return a new merged object, Source1 and Source2 original values will not be replaced.

	var oMergedJson = {};

	for (var attrname in oJson1) { oMergedJson[attrname] = oJson1[attrname]; }

	for (var attrname in oJson2) { oMergedJson[attrname] = oJson2[attrname]; }

	// Returns the merged JSON
	return oMergedJson;
}

FrontendTools.attributeToArray

Gets a string sepparated by comma or a string with an array format and returns an array


FrontendTools.attributeToArray = function( sValue ) {
	return sValue.replace('[','').replace(']','').split(',');
};

Track to Google Analytics

To avoid dependencies from the api of google analytics use this wrapper to send your trackings.

if ( _gaq === undefined ) var _gaq = null;

FrontendTools.trackModule = function( sCategory, sAction, sLabel, sValue ) {
	if ( _gaq !== null && oGlobalSettings.bTrackModules === true ) {
		FrontendTools.trackEvent( sCategory, sAction, sLabel, sValue );
	}
}

FrontendTools.trackEvent = function( sCategory, sAction, sLabel, sValue ) {
	if ( _gaq !== null ) {
		_gaq.push(['_trackEvent', sCategory, sAction, sLabel, sValue]);
	}
}

FrontendTools.trackPage = function( sPage ) {
	if ( _gaq !== null ) {
		_gaq.push(['_trackPageview', sPage]);
	}
}