Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- component autowire=true accessors=true singleton=true{
- // properties
- property name="assets" type="struct";
- property name="viewAssets" type="struct";
- property name="viewAppAssets" type="struct";
- property name="lookup" type="struct";
- property name="viewAssetLookup" type="struct";
- property name="viewAppAssetsLookup" type="struct";
- property name="viewJSLookup" type="struct";
- property name="types" type="struct";
- // dependency properties
- property name="minifiedDirectory" inject="coldbox:setting:minifiedDirectory";
- property name="requestService" inject="coldbox:requestService";
- property name="commonIncludesDirectory" inject="coldbox:setting:commonIncludesDirectory";
- property name="HTMLHelper" inject="model:HTMLHelper@coldbox";
- property name="environment" inject="coldbox:setting:environment";
- property name="applicationStorage" inject="model:applicationStorage@cbstorages";
- property name="utilities" inject="model:utilities@plugins";
- property name="appName" inject="coldbox:setting:appName";
- property name="wirebox" inject="wirebox";
- // constructor
- public AssetManager function init(){
- setTypes({
- "javascript" = {
- "extension" = "js",
- "directory" = "javascript"
- },
- "style" = {
- "extension" = "css",
- "directory" = "styles"
- }
- });
- return this;
- }
- public void function onDIComplete(){
- if(!directoryExists(getCoreMinifiedDirectory())){
- directoryCreate(getCoreMinifiedDirectory());
- }
- if(!directoryExists(getAPPMinifiedDirectory())){
- directoryCreate(getAPPMinifiedDirectory());
- }
- if(!directoryExists(getViewMinifiedDirectory())){
- directoryCreate(getViewMinifiedDirectory());
- }
- if(!directoryExists(getViewAppAssetMinifiedDirectory())){
- directoryCreate(getViewAppAssetMinifiedDirectory());
- }
- }
- // overrides
- public struct function getTypes(string name){
- if(!isNull(arguments.name)){
- if(structKeyExists(variables.types,arguments.name)){
- return variables.types[name];
- }
- }
- return variables.types;
- }
- public struct function getAssets(){
- return getApplicationStorage().getVar("assets",{});
- }
- public void function setAssets(required struct value){
- getApplicationStorage().setVar("assets",arguments.value);
- }
- public struct function getViewAssets(){
- return getApplicationStorage().getVar("viewAssets",{});
- }
- public void function setViewAssets(required struct value){
- getApplicationStorage().setVar("viewAssets",arguments.value);
- }
- public struct function getViewAppAssets(){
- return getApplicationStorage().getVar("viewAppAssets",{});
- }
- public void function setViewAppAssets(required struct value){
- getApplicationStorage().setVar("viewAppAssets",arguments.value);
- }
- public struct function getLookup(){
- return getApplicationStorage().getVar("assetLookup",{});
- }
- public void function setLookup(required struct value){
- getApplicationStorage().setVar("assetLookup",arguments.value);
- }
- public struct function getViewAssetLookup(){
- return getApplicationStorage().getVar("viewAssetLookup",{});
- }
- public void function setViewAssetLookup(required struct value){
- getApplicationStorage().setVar("viewAssetLookup",arguments.value);
- }
- public struct function getViewAppAssetLookup(){
- return getApplicationStorage().getVar("viewAppAssetLookup",{});
- }
- public void function setViewAppAssetLookup(required struct value){
- getApplicationStorage().setVar("viewAppAssetLookup",arguments.value);
- }
- public struct function getViewJSLookup(){
- return getApplicationStorage().getVar("viewJSLookup",{});
- }
- public void function setViewJSLookup(required struct value){
- getApplicationStorage().setVar("viewJSLookup",arguments.value);
- }
- // public functions
- public void function addViewAsset(required string view,string module=""){
- var viewQueue = getViewQueue();
- var viewAssetFullPath = getViewAssetFullPath(argumentCollection=arguments);
- if(!listFindNoCase(viewQueue,viewAssetFullPath)){
- marshallViewAssets(argumentCollection=arguments);
- setViewQueue(listAppend(viewQueue,viewAssetFullPath));
- }
- }
- public void function add(required string name,required string version,string direction="append",boolean local=false){
- var queue = getQueue();
- var _name = getName(name=arguments.name,local=arguments.local);
- var _version = lcase(trim(arguments.version));
- var key = getKey(name=_name,version=_version);
- if(!listFind(queue,key)){
- marshallAssets(name=_name,version=_version);
- if(arguments.direction == "append"){
- setQueue(listAppend(queue,key));
- }else{
- setQueue(listPrepend(queue,key));
- }
- }
- }
- public void function render(){
- toHeadAsUnminified("style");
- toHeadAsUnminified("javascript");
- writeViewAssetsToHead();
- resetAssets();
- }
- // private functions
- private void function resetAssets(){
- setViewAssetLookup({});
- setViewAppAssetLookup({});
- setViewAssets({});
- }
- private string function combineFiles(required string type){
- var queue = getQueue();
- var assetDirectory = "";
- var minifiedPath = getCoreMinifiedDirectory() & getHash() & ".#getTypes(arguments.type).extension#";
- var data = "";
- var assets = getAssets();
- var name = "";
- var version = "";
- var seperator = "";
- var lineFeed = getUtilities().getLineFeed();
- var basePath = getBasePath();
- var localBasePath = getBasePath(local=true);
- for(var y=1;y<=listLen(queue);y++){
- name = getNameFromKey(listGetAt(queue,y));
- version = getVersionFromKey(listGetAt(queue,y));
- if(isLocal(listGetAt(queue,y))){
- assetDirectory = localBasePath & "#getTypes(arguments.type).directory#\";
- }else{
- assetDirectory = basePath & "#getTypes(arguments.type).directory#\";
- }
- for(var x=1;x<=listLen(assets[listGetAt(queue,y)][arguments.type]);x++){
- if(fileExists(assetDirectory & name & "\" & version & "\" & listGetAt(assets[listGetAt(queue,y)][arguments.type],x))){
- seperator = LCase("/* #name & "|" & listGetAt(assets[listGetAt(queue,y)][arguments.type],x)# ");
- data = appendData(data=data,seperator=seperator,filePath=assetDirectory & name & "\" & version & "\" & listGetAt(assets[listGetAt(queue,y)][arguments.type],x));
- }
- }
- }
- if(!fileExists(minifiedPath)){
- fileWrite(minifiedPath,trim(data & lineFeed & getChecksum(data)));
- }else{
- marshallFile(data=data,existingPath=minifiedPath);
- }
- return listFirst(getFileFromPath(minifiedPath),".");
- }
- private string function combineViewJSFiles(){
- var viewQueue = getViewQueue();
- var viewAssets = getViewAssets();
- var viewJSHash = getViewJSHash();
- var minifiedDirectory = getViewMinifiedDirectory();
- var minifiedFile = viewJSHash & ".js";
- var minifiedPath = minifiedDirectory & minifiedFile;
- var data = "";
- var lineFeed = getUtilities().getLineFeed();
- var seperator = "";
- var viewAsset = "";
- for(var y=1;y<=listLen(viewQueue);y++){
- viewAsset = viewAssets[listGetAt(viewQueue,y)];
- if(structKeyExists(viewAsset,"file")){
- seperator = LCase("/* [#listFirst(viewAsset.name,"\")#] #listLast(viewAsset.name,"\")#|#viewAsset.file.lastModified# ");
- data = appendData(data=data,seperator=seperator,filePath=viewAsset.file.path);
- }
- }
- if(!len(data)){
- return "";
- }
- if(!fileExists(minifiedPath)){
- fileWrite(minifiedPath,trim(data & lineFeed & getChecksum(data)));
- }else{
- marshallFile(data=data,existingPath=minifiedPath);
- }
- return minifiedFile;
- }
- private function combineViewAppAssets(){
- var data = "";
- var seperator = "";
- var item = "";
- var viewAppAssetQueue = getViewAppAssetQueue();
- var minifiedFile = getViewAppAssetHash() & ".js";
- var minifiedDirectory = getViewAppAssetMinifiedDirectory();
- var minifiedPath = minifiedDirectory & minifiedFile;
- var viewAppAssets = getViewAppAssets();
- for(var x=1;x<=listLen(viewAppAssetQueue);x++){
- item = structFindValue(viewAppAssets,listGetAt(viewAppAssetQueue,x));
- seperator = LCase("/* #getFileFromPath(item[1].owner.path)#|#item[1].owner.lastModified# ");
- data = appendData(data=data,seperator=seperator,filePath=item[1].owner.path);
- }
- if(!len(data)){
- return "";
- }
- if(!fileExists(minifiedPath)){
- fileWrite(minifiedPath,trim(data & getUtilities().getLineFeed() & getChecksum(data)));
- }else{
- marshallFile(data=data,existingPath=minifiedPath);
- }
- return minifiedFile;
- }
- private struct function combineViewAssetFiles(required struct asset){
- var viewAssetHash = getViewAssetHash(arguments.asset);
- var minifiedPath = getAPPMinifiedDirectory();
- var lineFeed = getUtilities().getLineFeed();
- var filePath = "";
- var javascriptData = "";
- var styleData = "";
- var javascriptWritePath = "";
- var styleWritePath = "";
- var data = "";
- var directory = "";
- var HTMLSegmentPath = "";
- var result = {};
- for(var y=1;y<=arguments.asset.app.recordCount;y++){
- if(arguments.asset.app["type"][y] == "file"){
- data = "";
- if(listFindNoCase("js,css",listLast(arguments.asset.app["name"][y],"."))){
- filePath = arguments.asset.app["directory"][y] & "\" & arguments.asset.app["name"][y];
- seperator = getViewAssetSeperator(asset=arguments.asset,row=y);
- data = appendData(data=data,seperator=seperator,filePath=filePath);
- }
- switch(listLast(arguments.asset.app["name"][y],".")){
- case "js":{
- javascriptData = javascriptData & data;
- break;
- }
- case "css":{
- styleData = styleData & data;
- break;
- }
- case "htm":
- case "html":{
- directory = getViewAssetAppDirectory(asset,y);
- if(!directoryExists(directory)){
- directoryCreate(directory);
- }
- fileCopy(arguments.asset.app["directory"][y] & "\" & arguments.asset.app["name"][y],directory & "\" & arguments.asset.app["name"][y]);
- HTMLSegmentPath = getLocalIncludesDirectory() & "temp/" & getViewAssetFileLocation(asset,y) & "/" & arguments.asset.app["name"][y];
- javascriptData = javascriptData & linefeed & "/* APP HTML Segment @ #HTMLSegmentPath#" & " */" & lineFeed;
- styleData = styleData & linefeed & "/* APP HTML Segment @ #HTMLSegmentPath#" & " */" & lineFeed;
- break;
- }
- }
- }
- }
- javascriptWritePath = minifiedPath & viewAssetHash & ".js";
- styleWritePath = minifiedPath & viewAssetHash & ".css";
- if(!fileExists(javascriptWritePath)){
- if(len(javascriptData)){
- fileWrite(javascriptWritePath,javascriptData & lineFeed & getChecksum(javascriptData));
- }
- }else{
- if(len(javascriptData)){
- marshallFile(data=javascriptData,existingPath=javascriptWritePath);
- }else{
- fileDelete(javascriptWritePath);
- }
- }
- if(!fileExists(styleWritePath)){
- if(len(styleData)){
- fileWrite(styleWritePath,styleData & lineFeed & getChecksum(styleData));
- }
- }else{
- if(len(styleData)){
- marshallFile(data=styleData,existingPath=styleWritePath);
- }else{
- fileDelete(styleWritePath);
- }
- }
- if(len(javascriptData)){
- result.javascript = {
- name = viewAssetHash & ".js"
- };
- }
- if(len(styleData)){
- result.style = {
- name = viewAssetHash & ".css"
- };
- }
- return result;
- }
- private string function getMinifiedFile(required string type){
- var lookup = getLookup();
- var hash = getHash();
- if(!structKeyExists(lookup,hash)){
- lock type="exclusive" timeout=20 name=hash{
- combineFiles(type="style");
- lookup[hash] = combineFiles(type="javascript");
- setLookup(lookup);
- }
- }
- return lookup[hash] & ".#getTypes(arguments.type).extension#";
- }
- private struct function getViewAssetsMinifiedFiles(required struct asset){
- var viewAssetLookup = getViewAssetLookup();
- var viewAssetHash = getViewAssetHash(arguments.asset);
- if(!structKeyExists(viewAssetLookup,viewAssetHash)){
- lock type="exclusive" timeout=20 name=viewAssetHash{
- viewAssetLookup[viewAssetHash] = combineViewAssetFiles(arguments.asset);
- setViewAssetLookup(viewAssetLookup);
- }
- }
- return viewAssetLookup[viewAssetHash];
- }
- private string function getViewAppAssetMinifiedFile(){
- var viewAppAssetLookup = getViewAppAssetLookup();
- var viewAppAssetHash = getViewAppAssetHash();
- if(!structKeyExists(viewAppAssetLookup,viewAppAssetHash)){
- lock type="exclusive" timeout=20 name=viewAppAssetHash{
- viewAppAssetLookup[viewAppAssetHash] = combineViewAppAssets();
- setViewAppAssetLookup(viewAppAssetLookup);
- }
- }
- return viewAppAssetLookup[viewAppAssetHash];
- }
- private string function getViewJSMinifiedFile(){
- var viewJSLookup = getViewJSLookup();
- var viewJSHash = getViewJSHash();
- if(!structKeyExists(viewJSLookup,viewJSHash)){
- lock type="exclusive" timeout=20 name=viewJSHash{
- viewJSLookup[viewJSHash] = combineViewJSFiles();
- setViewJSLookup(viewJSLookup);
- }
- }
- return viewJSLookup[viewJSHash];
- }
- private void function toHeadAsMinified(required string type){
- getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/core/",file=getMinifiedFile(type=arguments.type),type=arguments.type));
- }
- private void function viewAssetsToHeadAsMinified(){
- var viewQueue = getViewQueue();
- var viewAssets = getViewAssets();
- var viewAsset = "";
- var viewAssetsMinifiedFiles = "";
- var viewAppAssetMinifiedFile = "";
- var viewJSMinifiedFile = "";
- for(var x=1;x<=listLen(viewQueue);x++){
- viewAsset = viewAssets[listGetAt(viewQueue,x)];
- if(structKeyExists(viewAsset,"app")){
- viewAssetsMinifiedFiles = getViewAssetsMinifiedFiles(viewAsset);
- if(structKeyExists(viewAssetsMinifiedFiles,"style")){
- getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/app/",file=viewAssetsMinifiedFiles.style.name,type="style"));
- }
- if(structKeyExists(viewAssetsMinifiedFiles,"javascript")){
- getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/app/",file=viewAssetsMinifiedFiles.javascript.name,type="javascript"));
- }
- }
- }
- viewAppAssetMinifiedFile = getViewAppAssetMinifiedFile();
- viewJSMinifiedFile = getViewJSMinifiedFile();
- if(len(viewAppAssetMinifiedFile)){
- getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/app/asset/",file=viewAppAssetMinifiedFile,type="javascript"));
- }
- if(len(viewJSMinifiedFile)){
- getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/view/",file=viewJSMinifiedFile,type="javascript"));
- }
- }
- private string function appendData(required string data,required string seperator,required string filePath,boolean compress=true){
- var result = arguments.data;
- var lineFeed = getUtilities().getLineFeed();
- var newData = fileRead(arguments.filePath);
- newData = replace(newData,"/* ================================================================================================================================================= */"," ","ALL");
- result = result & lineFeed;
- result = result & "/* " & repeatString("=",130) & " */#lineFeed#";
- result = result & arguments.seperator & repeatString("=",133 - len(arguments.seperator)) & " */#lineFeed#";
- result = result & "/* " & repeatString("=",130) & " */#lineFeed#";
- result = result & lineFeed;
- result = result & trim(newData);
- result = result & lineFeed;
- return result;
- }
- private string function getViewAssetSeperator(required struct asset,required numeric row){
- return LCase("/* [#listFirst(asset.name,"\")#] #getViewAssetFileLocation(argumentCollection=arguments)#\#arguments.asset.app["name"][arguments.row]#|#arguments.asset.app["dateLastModified"][arguments.row]# ");
- }
- private boolean function overrideCompress(required string data){
- var lineFeed = getUtilities().getLineFeed();
- var overrideCompress = listFirst(arguments.data,linefeed);
- var result = false;
- if(findNoCase("compress",overrideCompress)){
- if(listLast(listGetAt(overrideCompress,2," "),":") == false){
- result = true;
- }
- }
- return result;
- }
- private void function toHeadAsUnminified(required string type){
- var includesDirectory = "";
- var HTMLHelper = getHTMLHelper();
- var queue = getQueue();
- var assets = getAssets();
- var list = "";
- var name = "";
- var version = "";
- HTMLHelper.$HTMLHead(chr(13));
- for(var y=1;y<=listLen(queue);y++){
- name = getNameFromKey(listGetAt(queue,y));
- version = getVersionFromKey(listGetAt(queue,y));
- list = assets[listGetAt(queue,y)][arguments.type];
- for(var x=1;x<=listLen(list);x++){
- if(assets[listGetAt(queue,y)].local){
- includesDirectory = getLocalIncludesDirectory();
- }else{
- includesDirectory = "/" & getCommonIncludesDirectory();
- }
- HTMLHelper.$HTMLHead(getHeadTag(directory=includesDirectory & getTypes(arguments.type).directory & "/" & name & "/" & version & "/",file=listGetAt(list,x),type=arguments.type) & chr(13));
- }
- }
- }
- private void function writeViewAssetsToHead(){
- var viewQueue = getViewQueue();
- var viewAssets = getViewAssets();
- var filePath = "";
- var directory = "";
- var name = "";
- var asset = "";
- var HTMLHelper = getHTMLHelper();
- HTMLHelper.$HTMLHead(chr(13));
- for(var x=1;x<=listLen(viewQueue);x++){
- if(structKeyExists(viewAssets,listGetAt(viewQueue,x))){
- asset = viewAssets[listGetAt(viewQueue,x)];
- if(structKeyExists(asset,"file")){
- name = REReplacenocase(asset.name,"[^a-z0-9]","_","all") & ".js";
- directory = getDirectoryFromPath(getTemplatePath()) & getLocalIncludesDirectory() & "temp\";
- if(!directoryExists(directory)){
- directoryCreate(directory);
- }
- filePath = directory & name;
- fileWrite(filePath,fileRead(asset.file.path));
- HTMLHelper.$HTMLHead(getHeadTag(directory="#getLocalIncludesDirectory()#temp/",file=name,type="javascript") & chr(13));
- }
- if(structKeyExists(asset,"app")){
- HTMLHelper.$HTMLHead(chr(13));
- HTMLHelper.$HTMLHead("<!-- [app] " & asset.name & " " & repeatString("=",103 - len(asset.name)) & " -->" & chr(13));
- for(var y=1;y<=asset.app.recordCount;y++){
- if(asset.app["type"][y] == "file"){
- directory = getViewAssetAppDirectory(asset,y);
- viewAssetFileLocation = getViewAssetFileLocation(asset=asset,row=y);
- if(!directoryExists(directory)){
- directoryCreate(directory);
- }
- filePath = directory & asset.app["name"][y];
- fileWrite(filePath,fileRead(asset.app["directory"][y] & "\" & asset.app["name"][y]));
- switch(listLast(asset.app["name"][y],".")){
- case "js":{
- HTMLHelper.$HTMLHead(getHeadTag(directory="#getLocalIncludesDirectory()#temp/#viewAssetFileLocation#/",file=asset.app["name"][y],type="javascript") & chr(13));
- break;
- }
- case "css":{
- HTMLHelper.$HTMLHead(getHeadTag(directory="#getLocalIncludesDirectory()#temp/#viewAssetFileLocation#/",file=asset.app["name"][y],type="style") & chr(13));
- break;
- }
- case "htm":
- case "html":{
- HTMLHelper.$HTMLHead("<!-- app HTML segment @ #getLocalIncludesDirectory()#temp/#viewAssetFileLocation#/#asset.app["name"][y]# -->" & chr(13));
- break;
- }
- }
- }
- }
- HTMLHelper.$HTMLHead("<!-- " & repeatString("=",110) & " -->" & chr(13));
- HTMLHelper.$HTMLHead(chr(13));
- }
- }
- }
- writeViewAppAssetToHead();
- }
- private void function writeViewAppAssetToHead(){
- var viewAppAssetQueue = getViewAppAssetQueue();
- var HTMLHelper = getHTMLHelper();
- var name = "";
- var directory = "";
- var item = "";
- HTMLHelper.$HTMLHead("<!-- [assets] " & repeatString("=",109 - len("[Assets]")) & " -->" & chr(13));
- for(var x=1;x<=listLen(viewAppAssetQueue);x++){
- item = listGetAt(viewAppAssetQueue,x);
- name = REReplacenocase(getFileFromPath(item),"[^a-z0-9]","_","all") & ".js";
- directory = getDirectoryFromPath(getTemplatePath()) & getLocalIncludesDirectory() & "temp\asset\";
- if(!directoryExists(directory)){
- directoryCreate(directory);
- }
- fileWrite(directory & name,fileRead(item));
- HTMLHelper.$HTMLHead(getHeadTag(directory="#getLocalIncludesDirectory()#temp/asset/",file=name,type="javascript") & chr(13));
- }
- HTMLHelper.$HTMLHead("<!-- " & repeatString("=",110) & " -->" & chr(13));
- HTMLHelper.$HTMLHead(chr(13));
- }
- private void function marshallFile(required string data,required string existingPath){
- var checksum = getChecksum(arguments.data);
- var comparedFile = fileRead(arguments.existingPath);
- var linefeed = getUtilities().getLineFeed();
- if(compare(listLast(comparedFile,linefeed),checksum) != 0){
- fileDelete(arguments.existingPath);
- fileWrite(arguments.existingPath,trim(arguments.data & linefeed & checksum));
- }
- }
- private void function marshallAssets(required string name,required string version){
- var assets = getAssets();
- var key = getKey(name=arguments.name,version=arguments.version);
- var types = {
- "local" = isLocal(arguments.name)
- };
- var type = "";
- if(!structKeyExists(assets,key)){
- for(type IN getTypes()){
- types[type] = getFiles(name=arguments.name,version=arguments.version,type=type);
- }
- assets[key] = types;
- setAssets(assets);
- }
- }
- private void function marshallViewAssets(required string view,string module=""){
- var viewAssets = getViewAssets();
- var appDirectoryPath = "";
- var JSFilePath = "";
- var basePath = getViewAssetBasePath(argumentCollection=arguments);
- var viewAssetFullPath= getViewAssetFullPath(argumentCollection=arguments);
- var fileInfo = "";
- if(!structKeyExists(viewAssets,viewAssetFullPath)){
- viewAssets[viewAssetFullPath] = {
- isModule = (len(arguments.module))?true:false,
- name = (len(arguments.module))?"#arguments.module#\views\#arguments.view#":"views\#arguments.view#"
- };
- JSFilePath = basePath & "\" & listLast(arguments.view,"/") & ".js";
- appDirectoryPath = basePath & "\" & listLast(arguments.view,"/") & "App";
- if(fileExists(JSFilePath)){
- fileInfo = getFileInfo(JSFilePath);
- viewAssets[viewAssetFullPath].file = {
- lastModified = fileInfo.lastModified,
- path = lCase(JSFilePath)
- };
- }
- if(directoryExists(appDirectoryPath)){
- viewAssets[viewAssetFullPath].app = directoryList(appDirectoryPath,true,"query","","directory asc, name asc");
- for(var y=1;y<=viewAssets[viewAssetFullPath].app.recordCount;y++){
- if(viewAssets[viewAssetFullPath].app["name"][y] == "app.js"){
- marshallViewAppAssets(viewAssets[viewAssetFullPath].app["directory"][y] & "\" & viewAssets[viewAssetFullPath].app["name"][y]);
- break;
- }
- }
- }
- setViewAssets(viewAssets);
- }
- }
- private void function marshallViewAppAssets(required string filePath){
- var viewAppAssets = getViewAppAssets();
- var lineFeed = getUtilities().getLineFeed();
- var data = fileRead(arguments.filePath);
- var assetName = "";
- var fileInfo = "";
- var line = "";
- var appName = lCase(arguments.filePath);
- var jsFilePath = "";
- var metaData = "";
- for(var x=1;x<=listLen(data,lineFeed);x++){
- line = replace(replace(listGetAt(data,x,lineFeed),"/*",""),"*/","");
- if(trim(listFirst(line,":")) == "inject"){
- assetName = lCase(trim(listLast(line,":")));
- if(!structKeyExists(viewAppAssets,appName)){
- viewAppAssets[appName] = {};
- }
- if(!structKeyExists(viewAppAssets[appName],assetName)){
- metaData = getMetaData(getWirebox().getInstance(assetName));
- jsFilePath = getUtilities().removeFileExtensionFromFilePath(metaData.path) & ".js";
- if(fileExists(jsFilePath)){
- fileInfo = getFileInfo(jsFilePath);
- viewAppAssets[appName][assetName] = {
- "path" = lCase(jsFilePath),
- "lastModified" = fileInfo.lastModified
- };
- }
- }
- }
- }
- setViewAppAssets(viewAppAssets);
- }
- private string function getFiles(required string name,required string version,required string type){
- var files = [];
- var path = getBasePath() & "#getTypes(arguments.type).directory#" & "\" & arguments.name & "\" & arguments.version;
- var result = "";
- if(isLocal(arguments.name)){
- path = getBasePath(local=true) & "#getTypes(arguments.type).directory#" & "\" & listFirst(arguments.name,getLocalDelimiter()) & "\" & arguments.version;
- }
- if(directoryExists(path)){
- files = directoryList(path,false,"name","*.#getTypes(arguments.type).extension#","asc");
- }
- for(var x=1;x<=arrayLen(files);x++){
- result = listAppend(result,files[x]);
- }
- return result;
- }
- private string function getViewAssetFullPath(required string view,string module=""){
- return getViewAssetBasePath(argumentCollection=arguments) & "\" & listLast(arguments.view,"/");
- }
- private string function getViewAssetBasePath(required string view,string module=""){
- var basePath = "";
- if(len(arguments.module)){
- basePath = expandPath("../../") & "modules\#arguments.module#\views\#listDeleteAt(arguments.view,listLen(arguments.view,"/"),"/")#";
- }else{
- basePath = getDirectoryFromPath(getTemplatePath()) & "views\#listDeleteAt(arguments.view,listLen(arguments.view,"/"),"/")#";
- }
- return basePath;
- }
- private string function getViewAssetSegment(required string view){
- return listDeleteAt(arguments.view,listLast(arguments.view,"/"),"/");
- }
- private string function getViewAssetFileLocation(required struct asset,required numeric row){
- var name = listLast(arguments.asset.name,"/") & "APP";
- var index = listFindNoCase(arguments.asset.app["directory"][arguments.row],name,"\");
- var items = listToArray(arguments.asset.app["directory"][arguments.row],"\");
- var path = arrayToList(items.sublist(index-1,arrayLen(items)),"/");
- return path;
- }
- private string function getViewAssetAppDirectory(required struct asset,required numeric row){
- return getDirectoryFromPath(getTemplatePath()) & getLocalIncludesDirectory() & "temp\" & getViewAssetFileLocation(argumentCollection=arguments) & "\";
- }
- private boolean function isLocal(required string name){
- if(listLen(arguments.name,getLocalDelimiter()) == 2){
- return true;
- }
- return false;
- }
- private string function getChecksum(data){
- return "/* #hash(trim(arguments.data))# */";
- }
- private string function getQueue(){
- var prc = getRequestService().getContext().getCollection(private=true);
- if(!structKeyExists(prc,"assetQueue")){
- prc.assetQueue = "";
- }
- return prc.assetQueue;
- }
- private string function getViewQueue(){
- var prc = getRequestService().getContext().getCollection(private=true);
- if(!structKeyExists(prc,"viewAssetQueue")){
- prc.viewAssetQueue = "";
- }
- return prc.viewAssetQueue;
- }
- private void function setQueue(required string value){
- var prc = getRequestService().getContext().getCollection(private=true);
- prc.assetQueue = arguments.value;
- }
- private void function setViewQueue(required string value){
- var prc = getRequestService().getContext().getCollection(private=true);
- prc.viewAssetQueue = arguments.value;
- }
- private string function getNameFromKey(required string key){
- name = listFirst(arguments.key,getKeyDelimiter());
- if(isLocal(name)){
- name = listFirst(name,getLocalDelimiter());
- }
- return name;
- }
- private string function getName(required string name,required boolean local){
- var _name = lcase(trim(arguments.name));
- if(arguments.local){
- _name = _name & "#getLocalDelimiter()##getAppName()#-local";
- }
- return _name;
- }
- private string function getVersionFromKey(required string key){
- return listLast(arguments.key,getKeyDelimiter());
- }
- private string function getBasePath(boolean local=false){
- if(arguments.local){
- return getDirectoryFromPath(getTemplatePath()) & getLocalIncludesDirectory();
- }
- return expandPath("../../") & getCommonIncludesDirectory();
- }
- private string function getLocalIncludesDirectory(){
- return "includes/";
- }
- private string function getViewAssetHash(required struct asset){
- var list = "";
- if(structKeyExists(arguments.asset,"app")){
- for(var x=1;x<=arguments.asset.app.recordCount;x++){
- if(arguments.asset.app["type"][x] == "file"){
- list = listAppend(list,LCase(trim(arguments.asset.app["name"][x]) & "$" & trim(arguments.asset.app["directory"][x]) & "$" & arguments.asset.app["dateLastModified"][x]));
- }
- }
- }
- return hash(list);
- }
- private string function getViewAppAssetHash(){
- var viewAppAssetQueue = getViewAppAssetQueue();
- var viewAppAssets = getViewAppAssets();
- var list = "";
- var item = "";
- for(var x=1;x<=listlen(viewAppAssetQueue);x++){
- item = structFindValue(viewAppAssets,listGetAt(viewAppAssetQueue,x));
- if(!listFind(list,item[1].owner.path & "|" & item[1].owner.lastModified)){
- list = listAppend(list,item[1].owner.path & "|" & item[1].owner.lastModified);
- }
- }
- return hash(list);
- }
- private string function getViewAppAssetQueue(){
- var viewQueue = getViewQueue();
- var viewAssets = getViewAssets();
- var viewAppAssets = getViewAppAssets();
- var viewAsset = "";
- var key = "";
- var result = "";
- for(var x=1;x<=listLen(viewQueue);x++){
- viewAsset = viewAssets[listGetAt(viewQueue,x)];
- if(structKeyExists(viewAsset,"app")){
- for(var y=1;y<=viewAsset.app.recordCount;y++){
- if(viewAsset.app["name"][y] == "app.js"){
- key = lCase(viewAsset.app["directory"][y] & "\" & viewAsset.app["name"][y]);
- if(structKeyExists(viewAppAssets,key)){
- for(var item IN viewAppAssets[key]){
- if(!listFind(result,viewAppAssets[key][item].path)){
- result = listAppend(result,viewAppAssets[key][item].path);
- }
- }
- }
- break;
- }
- }
- }
- }
- return listSort(result,"text","desc");
- }
- private string function getViewJSHash(){
- var viewQueue = getViewQueue();
- var viewAssets = getViewAssets();
- var viewAsset = "";
- var list = "";
- for(var x=1;x<=listLen(viewQueue);x++){
- viewAsset = viewAssets[listGetAt(viewQueue,x)];
- if(structKeyExists(viewAsset,"file")){
- list = listAppend(list,LCase(trim(viewAsset.file.path) & "|" & viewAsset.file.lastModified));
- }
- }
- return hash(list);
- }
- private string function getHash(){
- var queue = getQueue();
- var assets = getAssets();
- var list = "";
- var queueItem = "";
- for(var x=1;x<=listLen(queue);x++){
- queueItem = listGetAt(queue,x);
- for(var type IN assets[queueItem]){
- if((type == "javascript" || type == "style") && len(assets[queueItem][type])){
- list = listAppend(list,LCase(trim(queueItem) & "|" & LCase(trim(assets[queueItem][type]))));
- }
- }
- }
- return hash(list);
- }
- private string function getHeadTag(required string directory,required string file,required string type){
- switch(arguments.type){
- case "javascript":{
- return "<script src=""" & arguments.directory & arguments.file & """ type=""text/javascript""></script>";
- }
- case "style":{
- return "<link href=""" & arguments.directory & arguments.file & """ type=""text/css"" rel=""stylesheet"" />";
- }
- }
- }
- private string function getLocalDelimiter(){
- return "||";
- }
- private string function getKey(required string name,required string version){
- return arguments.name & getKeyDelimiter() & arguments.version;
- }
- private string function getKeyDelimiter(){
- return "___";
- }
- private string function getCoreMinifiedDirectory(){
- return getMinifiedDirectory() & "core\";
- }
- private string function getAppMinifiedDirectory(){
- return getMinifiedDirectory() & "app\";
- }
- private string function getViewAppAssetMinifiedDirectory(){
- return getMinifiedDirectory() & "app\asset\";
- }
- private string function getViewMinifiedDirectory(){
- return getMinifiedDirectory() & "view\";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement