This repository has been archived by the owner. It is now read-only.
Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
227 lines (218 sloc) 11.1 KB
## Macro: loadResource()
## Author: Michael Fienen
## Date: 12.10.22
## Version: 1.0.121022
## E-mail: fienen@gmail.com
## Website: http://learndotcms.com/
## Description: Loads a set of files to an HTML page based on a library ID. Supports JavaScript, CSS, or VTL.
## Supports shared hosts by setting the host's ID in the plugin's plugin.properties file. Has
## built in support for all Google Hosted Libraries without needing to upload any files to your
## server. Also enables you to use the BoxJS/CSS proxy CDN service for minifying and combining
## your files. Use the file's Sort Order field to control loading priority.
##
## Required Parameters:
## $library - The name of the library you wish to load. (str)
##
## Optional Parameters:
## $commentBreaks - Show HTML comments before and after loading the library files. (bool; default: true)
## $htmlDebug - Prints debug info. (bool; default: false)
## $resourceCache - Cache time for resources, in seconds (int; default: 86400)
## $forceSsl - Set how External/CDN links should behave regarding security. Can be: none, never,
## always, or relative. (str; default: none)
## $boxCss - Enables BoxCSS (http://boxcss.com/) to create a single, minified, CDN cached version
## of your CSS. (bool; default:false)
## $boxCssMinify - Enable or disable minifying along with combining of CSS files. (bool; default:true)
## $boxCssCache - Enable or disable CDN caching along with combining of CSS files. (bool; default:true)
## $boxCssVersion - Set a version number for your combined CSS file (str; default:'1')
## $boxJs - Enables BoxJS (http://boxjs.com/) to create a single, minified, CDN cached version
## of your JS. (bool; default:false)
## $boxJsDefer - Normally, Boxjs will fetch your scripts as soon as you call it. But there may be times
## when you'd like to control when the scripts are requested, perhaps based on some kind
## of condition for example. You must manually add <script>box.get();</script> where you
## want the code executed. (bool; default:false)
## $boxJsAppend - By default, Boxjs will append the script element to the bottom of the page. Setting
## this to false will add your scripts to the DOM using document.write.
## (bool; default:true)
## $boxJsMinify - Enable or disable minifying along with combining of JS files. (bool; default:true)
## $boxJsCache - Enable or disable CDN caching along with combining of JS files. (bool; default:true)
## $boxJsVersion - Set a version number for your combined JS file (str; default:'1')
##
#macro(loadResource $library)
## SET DEFAULT CACHE TIME
#if(!$UtilMethods.isSet($resourceCache))
#set($resourceCache = 86400)
#end
## CACHE RESOURCE CALLS
#dotcache("resourceLoader-${library}",$resourceCache)
## INITIALIZE ALL OUR DEFAULT SETTINGS
## DEFAULT TO SHOWING COMMENTS BREAKS FOR THE RESOURCE UNLESS OTHERWISE SET
#if(!$UtilMethods.isSet($commentBreaks))
#set($commentBreaks = true)
#end
## DEFAULT TO NOT SHOWING DEBUG DATA
#if(!$UtilMethods.isSet($htmlDebug))
#set($htmlDebug = false)
#end
## DEFAULT TO DISABLE BOXCSS FOR CSS FILES
#if(!$UtilMethods.isSet($boxCss))
#set($boxCss = false)
#end
## DEFAULT TO DISABLE BOXJS FOR JS FILES
#if(!$UtilMethods.isSet($boxJs))
#set($boxJs = false)
#end
## CHECK TO SEE IF WE'VE STARTED TRACKING LOADED LIBRARIES. IF NOT, START NOW.
#if(!$UtilMethods.isSet($loadedLibraries))
#set($loadedLibraries = $!contents.getEmptyList())
#end
## SET UP THE SHARED HOST FOR THE FILE QUERY, IF PROVIDED
#if($UtilMethods.isSet($pluginapitool.loadProperty("com.learndotcms.plugins.resourceloader","sharedHostId")))
#set($sharedHost = " conhost:${pluginapitool.loadProperty('com.learndotcms.plugins.resourceloader','sharedHostId')}")
#end
## DEFAULT TO NOT CHANGE EXTERNAL LINK SECURITY
#if(!$UtilMethods.isSet($forceSsl))
#set($forceSsl = 'none')
#elseif($forceSsl == 'never')
## USED BELOW IN $defaultResources TO MAKE SURE THEY RESPECT THE SETTING AS WELL AS USER DEFINED OPTIONS
#set($defaultSsl = 'http:')
#elseif($forceSsl == 'always')
#set($defaultSsl = 'https:')
#end
## ESTABLISH DEFAULT REMOTE RESOURCE SET
#if(!$UtilMethods.isSet($defaultResources))
#set($defaultResources = $!contents.getEmptyMap())
#set($_temp = $!defaultResources.put("angularjs", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"))
## chromeframe WAS A LEGACY NAME, NOT THE FORMAL GOOGLE NAME. PROBABLY SAFE TO REMOVE. USE chrome-frame.
#set($_temp = $!defaultResources.put("chromeframe", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/chrome-frame/1.0.3/CFInstall.min.js"))
#set($_temp = $!defaultResources.put("chrome-frame", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/chrome-frame/1.0.3/CFInstall.min.js"))
#set($_temp = $!defaultResources.put("dojo", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/dojo/1.8.1/dojo/dojo.js"))
## extjs WAS A LEGACY NAME, NOT THE FORMAL GOOGLE NAME. PROBABLY SAFE TO REMOVE. USE ext-core.
#set($_temp = $!defaultResources.put("extjs", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/ext-core/3.1.0/ext-core.js"))
#set($_temp = $!defaultResources.put("ext-core", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/ext-core/3.1.0/ext-core.js"))
#set($_temp = $!defaultResources.put("jquery", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"))
#set($_temp = $!defaultResources.put("jqueryui", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/jquery-ui.min.js"))
#set($_temp = $!defaultResources.put("mootools", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"))
#set($_temp = $!defaultResources.put("prototype", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"))
#set($_temp = $!defaultResources.put("scriptaculous", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/scriptaculous.js"))
#set($_temp = $!defaultResources.put("swfobjcet", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"))
#set($_temp = $!defaultResources.put("webfont", "$!{defaultSsl}//ajax.googleapis.com/ajax/libs/webfont/1.0.31/webfont.js"))
#end
## IF THIS LIBRARY HASN'T BEEN LOADED ALREADY, DO IT (PREVENTS REDUNDANT LOADS)
#if(!$loadedLibraries.contains($library))
#if($commentBreaks)
<!-- Begin Resource: ${library} -->
#end
## GET THE FILES ASSOCIATED WITH THIS RESOURCE LIBRARY
#set($libraryQuery = "+structureName:${pluginapitool.loadProperty('com.learndotcms.plugins.resourceloader','webResourceVarName')} +(conhost:${host.identifier}$!{sharedHost}) +WebResource.resourceId:${library}")
#if($dotcontent.count($libraryQuery) > 0)
#foreach($file in $dotcontent.pull($libraryQuery,0,'WebResource.sortOrder asc'))
## SET THE APPROPRIATE FILE SOURCE
#if($file.source.selectValue == 'cdn')
## CHECK SECURITY OF EXTERNAL LINKS
#if($forceSsl != 'none')
#if($forceSsl == 'never')
#set($src = $file.cdn.replaceAll('https','http'))
#elseif($forceSsl == 'always')
#set($src = $file.cdn.replaceAll('http:','https:'))
#elseif($forceSsl == 'relative')
#set($src = $file.cdn.replaceAll('http(s?):',''))
#end
#else
#set($src = $file.cdn)
#end
#else
## THIS RETURNS AN UGLY /dotAsset PATH, NEED TO FIND THE HUMAN-READABLE URI
#set($src = $filetool.getURI($filetool.getFile($file.identifier, true)))
#end
## SET UP DIFFERENT MARKUP FOR FILE TYPES
#if($file.fileName.contains('js'))
## CHECK FOR CDN CACHING/MINIFYING VIA BOXJS
#if($boxJs)
#set($boxJsSrc = "$!{boxJsSrc},'dotAsset/${file.identifier}.js'")
#else
<script src="${src}"></script>
#end
#elseif($file.fileName.contains('css'))
## CHECK FOR CDN CACHING/MINIFYING VIA BOXCSS
#if($boxCss)
#set($boxCssSrc = "$!{boxCssSrc},${file.identifier}.css")
#else
<link rel="stylesheet" type="text/css" href="${src}" />
#end
#elseif($file.fileName.contains('vtl'))
#dotParse($src)
#end
## RUN ANY SUPPORTING CODE IF DETECTED
#if($UtilMethods.isSet($file.code))
${render.eval($file.code)}
#end
#end
## SPIT OUT THE BOXCSS PROCESSED CSS IF IT WAS REQUESTED
#if($boxCss)
## SET UP BOXCSS CUSTOMIZATIONS
#if($UtilMethods.isSet($boxCssMinify))
#set($boxCssVars = "&amp;minify=${boxCssMinify}")
#end
#if($UtilMethods.isSet($boxCssCache))
#set($boxCssVars = "$!{boxCssVars}&amp;cache=${boxCssCache}")
#end
#if($UtilMethods.isSet($boxCssVersion))
#set($boxCssVars = "$!{boxCssVars}&amp;version=${boxCssVersion}")
#end
<link rel="stylesheet" type="text/css" href="http://www.boxcss.com/boxme?host=http://${request.getServerName()}/dotAsset/&amp;include=${boxCssSrc.substring(1)}$!{boxCssVars}" />
#end
## SPIT OUT THE BOXJS PROCESSED JS IF IT WAS REQUESTED
#if($boxJs)
## SET UP BOXJS CUSTOMIZATIONS
#if($UtilMethods.isSet($boxJsDefer))
#set($boxJsVars = "$!{boxJsVars}, defer: ${boxJsDefer}")
#end
#if($UtilMethods.isSet($boxJsAppend))
#set($boxJsVars = "$!{boxJsVars}, append: ${boxJsAppend}")
#end
#if($UtilMethods.isSet($boxJsMinify))
#set($boxJsVars = "$!{boxJsVars}, minify: ${boxJsMinify}")
#end
#if($UtilMethods.isSet($boxJsCache))
#set($boxJsVars = "$!{boxJsVars}, cache: ${boxJsCache}")
#end
#if($UtilMethods.isSet($boxJsVersion))
#set($boxJsVars = "$!{boxJsVars}, version: ${boxJsVersion}")
#end
#if($UtilMethods.isSet($boxJsVars))
#set($boxJsVars = ", {${boxJsVars.substring(1)} }")
#end
<script src="http://www.boxjs.com/box.js"></script>
<script>
Box('http://${request.getServerName()}/', [
${boxJsSrc.substring(1)}
]$!{boxJsVars});
</script>
#end
## TRY TO GET IT FROM OUR DEFAULT CDN SOURCES
#else
## IS THE REQUESTED LIBRARY IN THE DEFAULT LIST?
#if($defaultResources.get($library))
<script src="${defaultResources.get($library)}"></script>
#end
#end
#if($commentBreaks)
<!-- End Resource: ${library} -->
#end
## NOW ADD THIS LIBRARY TO THE LIST OF LOADED ONES
#set($_temp = $loadedLibraries.add($library))
#end
## SHOW DEBUG INFO
#if($htmlDebug)
<!-- DEBUG INFO (library: ${library})
${esc.d}commentBreaks: ${commentBreaks}
Shared Host ID: $!{pluginapitool.loadProperty("com.learndotcms.plugins.resourceloader","sharedHostId")}
Enable BoxJS: ${boxJs}
Enable BoxCSS: ${boxCss}
Cache for: ${resourceCache} seconds
Files in library: ${dotcontent.count("+structureName:WebResource +(conhost:${host.identifier}$!{sharedHost}) +WebResource.resourceId:${library}")}
Currently loaded: ${loadedLibraries}
-->
#end
#end
#end