Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.79 KB | None | 0 0
  1. component autowire=true accessors=true singleton=true{
  2.  
  3. // properties
  4.  
  5. property name="assets" type="struct";
  6.  
  7. property name="viewAssets" type="struct";
  8.  
  9. property name="viewAppAssets" type="struct";
  10.  
  11. property name="lookup" type="struct";
  12.  
  13. property name="viewAssetLookup" type="struct";
  14.  
  15. property name="viewAppAssetsLookup" type="struct";
  16.  
  17. property name="viewJSLookup" type="struct";
  18.  
  19. property name="types" type="struct";
  20.  
  21. // dependency properties
  22.  
  23. property name="minifiedDirectory" inject="coldbox:setting:minifiedDirectory";
  24.  
  25. property name="requestService" inject="coldbox:requestService";
  26.  
  27. property name="commonIncludesDirectory" inject="coldbox:setting:commonIncludesDirectory";
  28.  
  29. property name="HTMLHelper" inject="model:HTMLHelper@coldbox";
  30.  
  31. property name="environment" inject="coldbox:setting:environment";
  32.  
  33. property name="applicationStorage" inject="model:applicationStorage@cbstorages";
  34.  
  35. property name="utilities" inject="model:utilities@plugins";
  36.  
  37. property name="appName" inject="coldbox:setting:appName";
  38.  
  39. property name="wirebox" inject="wirebox";
  40.  
  41. // constructor
  42.  
  43. public AssetManager function init(){
  44.  
  45. setTypes({
  46. "javascript" = {
  47. "extension" = "js",
  48. "directory" = "javascript"
  49. },
  50. "style" = {
  51. "extension" = "css",
  52. "directory" = "styles"
  53. }
  54. });
  55.  
  56. return this;
  57. }
  58.  
  59. public void function onDIComplete(){
  60. if(!directoryExists(getCoreMinifiedDirectory())){
  61. directoryCreate(getCoreMinifiedDirectory());
  62. }
  63.  
  64. if(!directoryExists(getAPPMinifiedDirectory())){
  65. directoryCreate(getAPPMinifiedDirectory());
  66. }
  67.  
  68. if(!directoryExists(getViewMinifiedDirectory())){
  69. directoryCreate(getViewMinifiedDirectory());
  70. }
  71.  
  72. if(!directoryExists(getViewAppAssetMinifiedDirectory())){
  73. directoryCreate(getViewAppAssetMinifiedDirectory());
  74. }
  75. }
  76.  
  77. // overrides
  78.  
  79. public struct function getTypes(string name){
  80. if(!isNull(arguments.name)){
  81. if(structKeyExists(variables.types,arguments.name)){
  82. return variables.types[name];
  83. }
  84. }
  85.  
  86. return variables.types;
  87. }
  88.  
  89. public struct function getAssets(){
  90. return getApplicationStorage().getVar("assets",{});
  91. }
  92.  
  93. public void function setAssets(required struct value){
  94. getApplicationStorage().setVar("assets",arguments.value);
  95. }
  96.  
  97. public struct function getViewAssets(){
  98. return getApplicationStorage().getVar("viewAssets",{});
  99. }
  100.  
  101. public void function setViewAssets(required struct value){
  102. getApplicationStorage().setVar("viewAssets",arguments.value);
  103. }
  104.  
  105. public struct function getViewAppAssets(){
  106. return getApplicationStorage().getVar("viewAppAssets",{});
  107. }
  108.  
  109. public void function setViewAppAssets(required struct value){
  110. getApplicationStorage().setVar("viewAppAssets",arguments.value);
  111. }
  112.  
  113. public struct function getLookup(){
  114. return getApplicationStorage().getVar("assetLookup",{});
  115. }
  116.  
  117. public void function setLookup(required struct value){
  118. getApplicationStorage().setVar("assetLookup",arguments.value);
  119. }
  120.  
  121. public struct function getViewAssetLookup(){
  122. return getApplicationStorage().getVar("viewAssetLookup",{});
  123. }
  124.  
  125. public void function setViewAssetLookup(required struct value){
  126. getApplicationStorage().setVar("viewAssetLookup",arguments.value);
  127. }
  128.  
  129. public struct function getViewAppAssetLookup(){
  130. return getApplicationStorage().getVar("viewAppAssetLookup",{});
  131. }
  132.  
  133. public void function setViewAppAssetLookup(required struct value){
  134. getApplicationStorage().setVar("viewAppAssetLookup",arguments.value);
  135. }
  136.  
  137. public struct function getViewJSLookup(){
  138. return getApplicationStorage().getVar("viewJSLookup",{});
  139. }
  140.  
  141. public void function setViewJSLookup(required struct value){
  142. getApplicationStorage().setVar("viewJSLookup",arguments.value);
  143. }
  144.  
  145. // public functions
  146.  
  147. public void function addViewAsset(required string view,string module=""){
  148. var viewQueue = getViewQueue();
  149.  
  150. var viewAssetFullPath = getViewAssetFullPath(argumentCollection=arguments);
  151.  
  152. if(!listFindNoCase(viewQueue,viewAssetFullPath)){
  153. marshallViewAssets(argumentCollection=arguments);
  154.  
  155. setViewQueue(listAppend(viewQueue,viewAssetFullPath));
  156. }
  157. }
  158.  
  159. public void function add(required string name,required string version,string direction="append",boolean local=false){
  160. var queue = getQueue();
  161.  
  162. var _name = getName(name=arguments.name,local=arguments.local);
  163.  
  164. var _version = lcase(trim(arguments.version));
  165.  
  166. var key = getKey(name=_name,version=_version);
  167.  
  168. if(!listFind(queue,key)){
  169. marshallAssets(name=_name,version=_version);
  170.  
  171. if(arguments.direction == "append"){
  172. setQueue(listAppend(queue,key));
  173. }else{
  174. setQueue(listPrepend(queue,key));
  175. }
  176. }
  177. }
  178.  
  179. public void function render(){
  180.  
  181. toHeadAsUnminified("style");
  182.  
  183. toHeadAsUnminified("javascript");
  184.  
  185. writeViewAssetsToHead();
  186.  
  187. resetAssets();
  188.  
  189. }
  190.  
  191. // private functions
  192.  
  193. private void function resetAssets(){
  194. setViewAssetLookup({});
  195.  
  196. setViewAppAssetLookup({});
  197.  
  198. setViewAssets({});
  199. }
  200.  
  201. private string function combineFiles(required string type){
  202. var queue = getQueue();
  203.  
  204. var assetDirectory = "";
  205.  
  206. var minifiedPath = getCoreMinifiedDirectory() & getHash() & ".#getTypes(arguments.type).extension#";
  207.  
  208. var data = "";
  209.  
  210. var assets = getAssets();
  211.  
  212. var name = "";
  213.  
  214. var version = "";
  215.  
  216. var seperator = "";
  217.  
  218. var lineFeed = getUtilities().getLineFeed();
  219.  
  220. var basePath = getBasePath();
  221.  
  222. var localBasePath = getBasePath(local=true);
  223.  
  224. for(var y=1;y<=listLen(queue);y++){
  225. name = getNameFromKey(listGetAt(queue,y));
  226.  
  227. version = getVersionFromKey(listGetAt(queue,y));
  228.  
  229. if(isLocal(listGetAt(queue,y))){
  230. assetDirectory = localBasePath & "#getTypes(arguments.type).directory#\";
  231. }else{
  232. assetDirectory = basePath & "#getTypes(arguments.type).directory#\";
  233. }
  234.  
  235. for(var x=1;x<=listLen(assets[listGetAt(queue,y)][arguments.type]);x++){
  236. if(fileExists(assetDirectory & name & "\" & version & "\" & listGetAt(assets[listGetAt(queue,y)][arguments.type],x))){
  237. seperator = LCase("/* #name & "|" & listGetAt(assets[listGetAt(queue,y)][arguments.type],x)# ");
  238.  
  239. data = appendData(data=data,seperator=seperator,filePath=assetDirectory & name & "\" & version & "\" & listGetAt(assets[listGetAt(queue,y)][arguments.type],x));
  240. }
  241. }
  242. }
  243.  
  244. if(!fileExists(minifiedPath)){
  245. fileWrite(minifiedPath,trim(data & lineFeed & getChecksum(data)));
  246. }else{
  247. marshallFile(data=data,existingPath=minifiedPath);
  248. }
  249.  
  250. return listFirst(getFileFromPath(minifiedPath),".");
  251. }
  252.  
  253. private string function combineViewJSFiles(){
  254. var viewQueue = getViewQueue();
  255.  
  256. var viewAssets = getViewAssets();
  257.  
  258. var viewJSHash = getViewJSHash();
  259.  
  260. var minifiedDirectory = getViewMinifiedDirectory();
  261.  
  262. var minifiedFile = viewJSHash & ".js";
  263.  
  264. var minifiedPath = minifiedDirectory & minifiedFile;
  265.  
  266. var data = "";
  267.  
  268. var lineFeed = getUtilities().getLineFeed();
  269.  
  270. var seperator = "";
  271.  
  272. var viewAsset = "";
  273.  
  274. for(var y=1;y<=listLen(viewQueue);y++){
  275. viewAsset = viewAssets[listGetAt(viewQueue,y)];
  276.  
  277. if(structKeyExists(viewAsset,"file")){
  278. seperator = LCase("/* [#listFirst(viewAsset.name,"\")#] #listLast(viewAsset.name,"\")#|#viewAsset.file.lastModified# ");
  279.  
  280. data = appendData(data=data,seperator=seperator,filePath=viewAsset.file.path);
  281. }
  282. }
  283.  
  284. if(!len(data)){
  285. return "";
  286. }
  287.  
  288. if(!fileExists(minifiedPath)){
  289. fileWrite(minifiedPath,trim(data & lineFeed & getChecksum(data)));
  290. }else{
  291. marshallFile(data=data,existingPath=minifiedPath);
  292. }
  293.  
  294. return minifiedFile;
  295. }
  296.  
  297. private function combineViewAppAssets(){
  298. var data = "";
  299.  
  300. var seperator = "";
  301.  
  302. var item = "";
  303.  
  304. var viewAppAssetQueue = getViewAppAssetQueue();
  305.  
  306. var minifiedFile = getViewAppAssetHash() & ".js";
  307.  
  308. var minifiedDirectory = getViewAppAssetMinifiedDirectory();
  309.  
  310. var minifiedPath = minifiedDirectory & minifiedFile;
  311.  
  312. var viewAppAssets = getViewAppAssets();
  313.  
  314. for(var x=1;x<=listLen(viewAppAssetQueue);x++){
  315. item = structFindValue(viewAppAssets,listGetAt(viewAppAssetQueue,x));
  316.  
  317. seperator = LCase("/* #getFileFromPath(item[1].owner.path)#|#item[1].owner.lastModified# ");
  318.  
  319. data = appendData(data=data,seperator=seperator,filePath=item[1].owner.path);
  320. }
  321.  
  322. if(!len(data)){
  323. return "";
  324. }
  325.  
  326. if(!fileExists(minifiedPath)){
  327. fileWrite(minifiedPath,trim(data & getUtilities().getLineFeed() & getChecksum(data)));
  328. }else{
  329. marshallFile(data=data,existingPath=minifiedPath);
  330. }
  331.  
  332. return minifiedFile;
  333. }
  334.  
  335. private struct function combineViewAssetFiles(required struct asset){
  336. var viewAssetHash = getViewAssetHash(arguments.asset);
  337.  
  338. var minifiedPath = getAPPMinifiedDirectory();
  339.  
  340. var lineFeed = getUtilities().getLineFeed();
  341.  
  342. var filePath = "";
  343.  
  344. var javascriptData = "";
  345.  
  346. var styleData = "";
  347.  
  348. var javascriptWritePath = "";
  349.  
  350. var styleWritePath = "";
  351.  
  352. var data = "";
  353.  
  354. var directory = "";
  355.  
  356. var HTMLSegmentPath = "";
  357.  
  358. var result = {};
  359.  
  360. for(var y=1;y<=arguments.asset.app.recordCount;y++){
  361. if(arguments.asset.app["type"][y] == "file"){
  362. data = "";
  363.  
  364. if(listFindNoCase("js,css",listLast(arguments.asset.app["name"][y],"."))){
  365. filePath = arguments.asset.app["directory"][y] & "\" & arguments.asset.app["name"][y];
  366.  
  367. seperator = getViewAssetSeperator(asset=arguments.asset,row=y);
  368.  
  369. data = appendData(data=data,seperator=seperator,filePath=filePath);
  370. }
  371.  
  372. switch(listLast(arguments.asset.app["name"][y],".")){
  373. case "js":{
  374. javascriptData = javascriptData & data;
  375.  
  376. break;
  377. }
  378.  
  379. case "css":{
  380. styleData = styleData & data;
  381.  
  382. break;
  383. }
  384.  
  385. case "htm":
  386. case "html":{
  387. directory = getViewAssetAppDirectory(asset,y);
  388.  
  389. if(!directoryExists(directory)){
  390. directoryCreate(directory);
  391. }
  392.  
  393. fileCopy(arguments.asset.app["directory"][y] & "\" & arguments.asset.app["name"][y],directory & "\" & arguments.asset.app["name"][y]);
  394.  
  395. HTMLSegmentPath = getLocalIncludesDirectory() & "temp/" & getViewAssetFileLocation(asset,y) & "/" & arguments.asset.app["name"][y];
  396.  
  397. javascriptData = javascriptData & linefeed & "/* APP HTML Segment @ #HTMLSegmentPath#" & " */" & lineFeed;
  398.  
  399. styleData = styleData & linefeed & "/* APP HTML Segment @ #HTMLSegmentPath#" & " */" & lineFeed;
  400.  
  401. break;
  402. }
  403. }
  404. }
  405. }
  406.  
  407. javascriptWritePath = minifiedPath & viewAssetHash & ".js";
  408.  
  409. styleWritePath = minifiedPath & viewAssetHash & ".css";
  410.  
  411. if(!fileExists(javascriptWritePath)){
  412. if(len(javascriptData)){
  413. fileWrite(javascriptWritePath,javascriptData & lineFeed & getChecksum(javascriptData));
  414. }
  415. }else{
  416. if(len(javascriptData)){
  417. marshallFile(data=javascriptData,existingPath=javascriptWritePath);
  418. }else{
  419. fileDelete(javascriptWritePath);
  420. }
  421. }
  422.  
  423. if(!fileExists(styleWritePath)){
  424. if(len(styleData)){
  425. fileWrite(styleWritePath,styleData & lineFeed & getChecksum(styleData));
  426. }
  427. }else{
  428. if(len(styleData)){
  429. marshallFile(data=styleData,existingPath=styleWritePath);
  430. }else{
  431. fileDelete(styleWritePath);
  432. }
  433. }
  434.  
  435. if(len(javascriptData)){
  436. result.javascript = {
  437. name = viewAssetHash & ".js"
  438. };
  439. }
  440.  
  441. if(len(styleData)){
  442. result.style = {
  443. name = viewAssetHash & ".css"
  444. };
  445. }
  446.  
  447. return result;
  448. }
  449.  
  450. private string function getMinifiedFile(required string type){
  451. var lookup = getLookup();
  452.  
  453. var hash = getHash();
  454.  
  455. if(!structKeyExists(lookup,hash)){
  456. lock type="exclusive" timeout=20 name=hash{
  457. combineFiles(type="style");
  458.  
  459. lookup[hash] = combineFiles(type="javascript");
  460.  
  461. setLookup(lookup);
  462. }
  463. }
  464.  
  465. return lookup[hash] & ".#getTypes(arguments.type).extension#";
  466. }
  467.  
  468. private struct function getViewAssetsMinifiedFiles(required struct asset){
  469. var viewAssetLookup = getViewAssetLookup();
  470.  
  471. var viewAssetHash = getViewAssetHash(arguments.asset);
  472.  
  473. if(!structKeyExists(viewAssetLookup,viewAssetHash)){
  474. lock type="exclusive" timeout=20 name=viewAssetHash{
  475. viewAssetLookup[viewAssetHash] = combineViewAssetFiles(arguments.asset);
  476.  
  477. setViewAssetLookup(viewAssetLookup);
  478. }
  479. }
  480.  
  481. return viewAssetLookup[viewAssetHash];
  482. }
  483.  
  484. private string function getViewAppAssetMinifiedFile(){
  485. var viewAppAssetLookup = getViewAppAssetLookup();
  486.  
  487. var viewAppAssetHash = getViewAppAssetHash();
  488.  
  489. if(!structKeyExists(viewAppAssetLookup,viewAppAssetHash)){
  490. lock type="exclusive" timeout=20 name=viewAppAssetHash{
  491. viewAppAssetLookup[viewAppAssetHash] = combineViewAppAssets();
  492.  
  493. setViewAppAssetLookup(viewAppAssetLookup);
  494. }
  495. }
  496.  
  497. return viewAppAssetLookup[viewAppAssetHash];
  498. }
  499.  
  500. private string function getViewJSMinifiedFile(){
  501. var viewJSLookup = getViewJSLookup();
  502.  
  503. var viewJSHash = getViewJSHash();
  504.  
  505. if(!structKeyExists(viewJSLookup,viewJSHash)){
  506. lock type="exclusive" timeout=20 name=viewJSHash{
  507. viewJSLookup[viewJSHash] = combineViewJSFiles();
  508.  
  509. setViewJSLookup(viewJSLookup);
  510. }
  511. }
  512.  
  513. return viewJSLookup[viewJSHash];
  514. }
  515.  
  516. private void function toHeadAsMinified(required string type){
  517. getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/core/",file=getMinifiedFile(type=arguments.type),type=arguments.type));
  518. }
  519.  
  520. private void function viewAssetsToHeadAsMinified(){
  521. var viewQueue = getViewQueue();
  522.  
  523. var viewAssets = getViewAssets();
  524.  
  525. var viewAsset = "";
  526.  
  527. var viewAssetsMinifiedFiles = "";
  528.  
  529. var viewAppAssetMinifiedFile = "";
  530.  
  531. var viewJSMinifiedFile = "";
  532.  
  533. for(var x=1;x<=listLen(viewQueue);x++){
  534. viewAsset = viewAssets[listGetAt(viewQueue,x)];
  535.  
  536. if(structKeyExists(viewAsset,"app")){
  537. viewAssetsMinifiedFiles = getViewAssetsMinifiedFiles(viewAsset);
  538.  
  539. if(structKeyExists(viewAssetsMinifiedFiles,"style")){
  540. getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/app/",file=viewAssetsMinifiedFiles.style.name,type="style"));
  541. }
  542.  
  543. if(structKeyExists(viewAssetsMinifiedFiles,"javascript")){
  544. getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/app/",file=viewAssetsMinifiedFiles.javascript.name,type="javascript"));
  545. }
  546. }
  547. }
  548.  
  549. viewAppAssetMinifiedFile = getViewAppAssetMinifiedFile();
  550.  
  551. viewJSMinifiedFile = getViewJSMinifiedFile();
  552.  
  553. if(len(viewAppAssetMinifiedFile)){
  554. getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/app/asset/",file=viewAppAssetMinifiedFile,type="javascript"));
  555. }
  556.  
  557. if(len(viewJSMinifiedFile)){
  558. getHTMLHelper().$htmlhead(getHeadTag(directory="/" & getCommonIncludesDirectory() & "minified/view/",file=viewJSMinifiedFile,type="javascript"));
  559. }
  560. }
  561.  
  562. private string function appendData(required string data,required string seperator,required string filePath,boolean compress=true){
  563. var result = arguments.data;
  564.  
  565. var lineFeed = getUtilities().getLineFeed();
  566.  
  567.  
  568. var newData = fileRead(arguments.filePath);
  569.  
  570. newData = replace(newData,"/* ================================================================================================================================================= */"," ","ALL");
  571.  
  572. result = result & lineFeed;
  573.  
  574. result = result & "/* " & repeatString("=",130) & " */#lineFeed#";
  575.  
  576. result = result & arguments.seperator & repeatString("=",133 - len(arguments.seperator)) & " */#lineFeed#";
  577.  
  578. result = result & "/* " & repeatString("=",130) & " */#lineFeed#";
  579.  
  580. result = result & lineFeed;
  581.  
  582. result = result & trim(newData);
  583.  
  584. result = result & lineFeed;
  585.  
  586. return result;
  587. }
  588.  
  589. private string function getViewAssetSeperator(required struct asset,required numeric row){
  590. return LCase("/* [#listFirst(asset.name,"\")#] #getViewAssetFileLocation(argumentCollection=arguments)#\#arguments.asset.app["name"][arguments.row]#|#arguments.asset.app["dateLastModified"][arguments.row]# ");
  591. }
  592.  
  593. private boolean function overrideCompress(required string data){
  594. var lineFeed = getUtilities().getLineFeed();
  595.  
  596. var overrideCompress = listFirst(arguments.data,linefeed);
  597.  
  598. var result = false;
  599.  
  600. if(findNoCase("compress",overrideCompress)){
  601. if(listLast(listGetAt(overrideCompress,2," "),":") == false){
  602. result = true;
  603. }
  604. }
  605.  
  606. return result;
  607. }
  608.  
  609. private void function toHeadAsUnminified(required string type){
  610. var includesDirectory = "";
  611.  
  612. var HTMLHelper = getHTMLHelper();
  613.  
  614. var queue = getQueue();
  615.  
  616. var assets = getAssets();
  617.  
  618. var list = "";
  619.  
  620. var name = "";
  621.  
  622. var version = "";
  623.  
  624. HTMLHelper.$HTMLHead(chr(13));
  625.  
  626. for(var y=1;y<=listLen(queue);y++){
  627. name = getNameFromKey(listGetAt(queue,y));
  628.  
  629. version = getVersionFromKey(listGetAt(queue,y));
  630.  
  631. list = assets[listGetAt(queue,y)][arguments.type];
  632.  
  633. for(var x=1;x<=listLen(list);x++){
  634. if(assets[listGetAt(queue,y)].local){
  635. includesDirectory = getLocalIncludesDirectory();
  636. }else{
  637. includesDirectory = "/" & getCommonIncludesDirectory();
  638. }
  639.  
  640. HTMLHelper.$HTMLHead(getHeadTag(directory=includesDirectory & getTypes(arguments.type).directory & "/" & name & "/" & version & "/",file=listGetAt(list,x),type=arguments.type) & chr(13));
  641. }
  642. }
  643. }
  644.  
  645. private void function writeViewAssetsToHead(){
  646. var viewQueue = getViewQueue();
  647.  
  648. var viewAssets = getViewAssets();
  649.  
  650. var filePath = "";
  651.  
  652. var directory = "";
  653.  
  654. var name = "";
  655.  
  656. var asset = "";
  657.  
  658. var HTMLHelper = getHTMLHelper();
  659.  
  660. HTMLHelper.$HTMLHead(chr(13));
  661.  
  662. for(var x=1;x<=listLen(viewQueue);x++){
  663. if(structKeyExists(viewAssets,listGetAt(viewQueue,x))){
  664. asset = viewAssets[listGetAt(viewQueue,x)];
  665.  
  666. if(structKeyExists(asset,"file")){
  667. name = REReplacenocase(asset.name,"[^a-z0-9]","_","all") & ".js";
  668.  
  669. directory = getDirectoryFromPath(getTemplatePath()) & getLocalIncludesDirectory() & "temp\";
  670.  
  671. if(!directoryExists(directory)){
  672. directoryCreate(directory);
  673. }
  674.  
  675. filePath = directory & name;
  676.  
  677. fileWrite(filePath,fileRead(asset.file.path));
  678.  
  679. HTMLHelper.$HTMLHead(getHeadTag(directory="#getLocalIncludesDirectory()#temp/",file=name,type="javascript") & chr(13));
  680. }
  681.  
  682. if(structKeyExists(asset,"app")){
  683. HTMLHelper.$HTMLHead(chr(13));
  684.  
  685. HTMLHelper.$HTMLHead("<!-- [app] " & asset.name & " " & repeatString("=",103 - len(asset.name)) & " -->" & chr(13));
  686.  
  687. for(var y=1;y<=asset.app.recordCount;y++){
  688. if(asset.app["type"][y] == "file"){
  689. directory = getViewAssetAppDirectory(asset,y);
  690.  
  691. viewAssetFileLocation = getViewAssetFileLocation(asset=asset,row=y);
  692.  
  693. if(!directoryExists(directory)){
  694. directoryCreate(directory);
  695. }
  696.  
  697. filePath = directory & asset.app["name"][y];
  698.  
  699. fileWrite(filePath,fileRead(asset.app["directory"][y] & "\" & asset.app["name"][y]));
  700.  
  701. switch(listLast(asset.app["name"][y],".")){
  702. case "js":{
  703. HTMLHelper.$HTMLHead(getHeadTag(directory="#getLocalIncludesDirectory()#temp/#viewAssetFileLocation#/",file=asset.app["name"][y],type="javascript") & chr(13));
  704.  
  705. break;
  706. }
  707.  
  708. case "css":{
  709. HTMLHelper.$HTMLHead(getHeadTag(directory="#getLocalIncludesDirectory()#temp/#viewAssetFileLocation#/",file=asset.app["name"][y],type="style") & chr(13));
  710.  
  711. break;
  712. }
  713.  
  714. case "htm":
  715. case "html":{
  716. HTMLHelper.$HTMLHead("<!-- app HTML segment @ #getLocalIncludesDirectory()#temp/#viewAssetFileLocation#/#asset.app["name"][y]# -->" & chr(13));
  717.  
  718. break;
  719. }
  720. }
  721. }
  722. }
  723.  
  724. HTMLHelper.$HTMLHead("<!-- " & repeatString("=",110) & " -->" & chr(13));
  725. HTMLHelper.$HTMLHead(chr(13));
  726. }
  727. }
  728. }
  729.  
  730. writeViewAppAssetToHead();
  731. }
  732.  
  733. private void function writeViewAppAssetToHead(){
  734. var viewAppAssetQueue = getViewAppAssetQueue();
  735.  
  736. var HTMLHelper = getHTMLHelper();
  737.  
  738. var name = "";
  739.  
  740. var directory = "";
  741.  
  742. var item = "";
  743.  
  744. HTMLHelper.$HTMLHead("<!-- [assets] " & repeatString("=",109 - len("[Assets]")) & " -->" & chr(13));
  745.  
  746. for(var x=1;x<=listLen(viewAppAssetQueue);x++){
  747. item = listGetAt(viewAppAssetQueue,x);
  748.  
  749. name = REReplacenocase(getFileFromPath(item),"[^a-z0-9]","_","all") & ".js";
  750.  
  751. directory = getDirectoryFromPath(getTemplatePath()) & getLocalIncludesDirectory() & "temp\asset\";
  752.  
  753. if(!directoryExists(directory)){
  754. directoryCreate(directory);
  755. }
  756.  
  757. fileWrite(directory & name,fileRead(item));
  758.  
  759. HTMLHelper.$HTMLHead(getHeadTag(directory="#getLocalIncludesDirectory()#temp/asset/",file=name,type="javascript") & chr(13));
  760. }
  761.  
  762. HTMLHelper.$HTMLHead("<!-- " & repeatString("=",110) & " -->" & chr(13));
  763. HTMLHelper.$HTMLHead(chr(13));
  764. }
  765.  
  766. private void function marshallFile(required string data,required string existingPath){
  767. var checksum = getChecksum(arguments.data);
  768.  
  769. var comparedFile = fileRead(arguments.existingPath);
  770.  
  771. var linefeed = getUtilities().getLineFeed();
  772.  
  773. if(compare(listLast(comparedFile,linefeed),checksum) != 0){
  774. fileDelete(arguments.existingPath);
  775.  
  776. fileWrite(arguments.existingPath,trim(arguments.data & linefeed & checksum));
  777. }
  778. }
  779.  
  780. private void function marshallAssets(required string name,required string version){
  781. var assets = getAssets();
  782.  
  783. var key = getKey(name=arguments.name,version=arguments.version);
  784.  
  785. var types = {
  786. "local" = isLocal(arguments.name)
  787. };
  788.  
  789. var type = "";
  790.  
  791. if(!structKeyExists(assets,key)){
  792. for(type IN getTypes()){
  793. types[type] = getFiles(name=arguments.name,version=arguments.version,type=type);
  794. }
  795.  
  796. assets[key] = types;
  797.  
  798. setAssets(assets);
  799. }
  800. }
  801.  
  802. private void function marshallViewAssets(required string view,string module=""){
  803. var viewAssets = getViewAssets();
  804.  
  805. var appDirectoryPath = "";
  806.  
  807. var JSFilePath = "";
  808.  
  809. var basePath = getViewAssetBasePath(argumentCollection=arguments);
  810.  
  811. var viewAssetFullPath= getViewAssetFullPath(argumentCollection=arguments);
  812.  
  813. var fileInfo = "";
  814.  
  815. if(!structKeyExists(viewAssets,viewAssetFullPath)){
  816. viewAssets[viewAssetFullPath] = {
  817. isModule = (len(arguments.module))?true:false,
  818. name = (len(arguments.module))?"#arguments.module#\views\#arguments.view#":"views\#arguments.view#"
  819. };
  820.  
  821. JSFilePath = basePath & "\" & listLast(arguments.view,"/") & ".js";
  822.  
  823. appDirectoryPath = basePath & "\" & listLast(arguments.view,"/") & "App";
  824.  
  825. if(fileExists(JSFilePath)){
  826. fileInfo = getFileInfo(JSFilePath);
  827.  
  828. viewAssets[viewAssetFullPath].file = {
  829. lastModified = fileInfo.lastModified,
  830. path = lCase(JSFilePath)
  831. };
  832. }
  833.  
  834. if(directoryExists(appDirectoryPath)){
  835. viewAssets[viewAssetFullPath].app = directoryList(appDirectoryPath,true,"query","","directory asc, name asc");
  836.  
  837. for(var y=1;y<=viewAssets[viewAssetFullPath].app.recordCount;y++){
  838. if(viewAssets[viewAssetFullPath].app["name"][y] == "app.js"){
  839. marshallViewAppAssets(viewAssets[viewAssetFullPath].app["directory"][y] & "\" & viewAssets[viewAssetFullPath].app["name"][y]);
  840.  
  841. break;
  842. }
  843. }
  844. }
  845.  
  846. setViewAssets(viewAssets);
  847. }
  848. }
  849.  
  850. private void function marshallViewAppAssets(required string filePath){
  851. var viewAppAssets = getViewAppAssets();
  852.  
  853. var lineFeed = getUtilities().getLineFeed();
  854.  
  855. var data = fileRead(arguments.filePath);
  856.  
  857. var assetName = "";
  858.  
  859. var fileInfo = "";
  860.  
  861. var line = "";
  862.  
  863. var appName = lCase(arguments.filePath);
  864.  
  865. var jsFilePath = "";
  866.  
  867. var metaData = "";
  868.  
  869. for(var x=1;x<=listLen(data,lineFeed);x++){
  870. line = replace(replace(listGetAt(data,x,lineFeed),"/*",""),"*/","");
  871.  
  872. if(trim(listFirst(line,":")) == "inject"){
  873. assetName = lCase(trim(listLast(line,":")));
  874.  
  875. if(!structKeyExists(viewAppAssets,appName)){
  876. viewAppAssets[appName] = {};
  877. }
  878.  
  879. if(!structKeyExists(viewAppAssets[appName],assetName)){
  880. metaData = getMetaData(getWirebox().getInstance(assetName));
  881.  
  882. jsFilePath = getUtilities().removeFileExtensionFromFilePath(metaData.path) & ".js";
  883.  
  884. if(fileExists(jsFilePath)){
  885. fileInfo = getFileInfo(jsFilePath);
  886.  
  887. viewAppAssets[appName][assetName] = {
  888. "path" = lCase(jsFilePath),
  889. "lastModified" = fileInfo.lastModified
  890. };
  891. }
  892. }
  893. }
  894. }
  895.  
  896. setViewAppAssets(viewAppAssets);
  897. }
  898.  
  899. private string function getFiles(required string name,required string version,required string type){
  900. var files = [];
  901.  
  902. var path = getBasePath() & "#getTypes(arguments.type).directory#" & "\" & arguments.name & "\" & arguments.version;
  903.  
  904. var result = "";
  905.  
  906. if(isLocal(arguments.name)){
  907. path = getBasePath(local=true) & "#getTypes(arguments.type).directory#" & "\" & listFirst(arguments.name,getLocalDelimiter()) & "\" & arguments.version;
  908. }
  909.  
  910. if(directoryExists(path)){
  911. files = directoryList(path,false,"name","*.#getTypes(arguments.type).extension#","asc");
  912. }
  913.  
  914. for(var x=1;x<=arrayLen(files);x++){
  915. result = listAppend(result,files[x]);
  916. }
  917.  
  918. return result;
  919. }
  920.  
  921. private string function getViewAssetFullPath(required string view,string module=""){
  922. return getViewAssetBasePath(argumentCollection=arguments) & "\" & listLast(arguments.view,"/");
  923. }
  924.  
  925. private string function getViewAssetBasePath(required string view,string module=""){
  926. var basePath = "";
  927.  
  928. if(len(arguments.module)){
  929. basePath = expandPath("../../") & "modules\#arguments.module#\views\#listDeleteAt(arguments.view,listLen(arguments.view,"/"),"/")#";
  930. }else{
  931. basePath = getDirectoryFromPath(getTemplatePath()) & "views\#listDeleteAt(arguments.view,listLen(arguments.view,"/"),"/")#";
  932. }
  933.  
  934. return basePath;
  935. }
  936.  
  937. private string function getViewAssetSegment(required string view){
  938. return listDeleteAt(arguments.view,listLast(arguments.view,"/"),"/");
  939. }
  940.  
  941. private string function getViewAssetFileLocation(required struct asset,required numeric row){
  942. var name = listLast(arguments.asset.name,"/") & "APP";
  943.  
  944. var index = listFindNoCase(arguments.asset.app["directory"][arguments.row],name,"\");
  945.  
  946. var items = listToArray(arguments.asset.app["directory"][arguments.row],"\");
  947.  
  948. var path = arrayToList(items.sublist(index-1,arrayLen(items)),"/");
  949.  
  950. return path;
  951. }
  952.  
  953. private string function getViewAssetAppDirectory(required struct asset,required numeric row){
  954. return getDirectoryFromPath(getTemplatePath()) & getLocalIncludesDirectory() & "temp\" & getViewAssetFileLocation(argumentCollection=arguments) & "\";
  955. }
  956.  
  957. private boolean function isLocal(required string name){
  958. if(listLen(arguments.name,getLocalDelimiter()) == 2){
  959. return true;
  960. }
  961.  
  962. return false;
  963. }
  964.  
  965. private string function getChecksum(data){
  966. return "/* #hash(trim(arguments.data))# */";
  967. }
  968.  
  969. private string function getQueue(){
  970. var prc = getRequestService().getContext().getCollection(private=true);
  971.  
  972. if(!structKeyExists(prc,"assetQueue")){
  973. prc.assetQueue = "";
  974. }
  975.  
  976. return prc.assetQueue;
  977. }
  978.  
  979. private string function getViewQueue(){
  980. var prc = getRequestService().getContext().getCollection(private=true);
  981.  
  982. if(!structKeyExists(prc,"viewAssetQueue")){
  983. prc.viewAssetQueue = "";
  984. }
  985.  
  986. return prc.viewAssetQueue;
  987. }
  988.  
  989. private void function setQueue(required string value){
  990. var prc = getRequestService().getContext().getCollection(private=true);
  991.  
  992. prc.assetQueue = arguments.value;
  993. }
  994.  
  995. private void function setViewQueue(required string value){
  996. var prc = getRequestService().getContext().getCollection(private=true);
  997.  
  998. prc.viewAssetQueue = arguments.value;
  999. }
  1000.  
  1001. private string function getNameFromKey(required string key){
  1002. name = listFirst(arguments.key,getKeyDelimiter());
  1003.  
  1004. if(isLocal(name)){
  1005. name = listFirst(name,getLocalDelimiter());
  1006. }
  1007.  
  1008. return name;
  1009. }
  1010.  
  1011. private string function getName(required string name,required boolean local){
  1012. var _name = lcase(trim(arguments.name));
  1013.  
  1014. if(arguments.local){
  1015. _name = _name & "#getLocalDelimiter()##getAppName()#-local";
  1016. }
  1017.  
  1018. return _name;
  1019. }
  1020.  
  1021. private string function getVersionFromKey(required string key){
  1022. return listLast(arguments.key,getKeyDelimiter());
  1023. }
  1024.  
  1025. private string function getBasePath(boolean local=false){
  1026. if(arguments.local){
  1027. return getDirectoryFromPath(getTemplatePath()) & getLocalIncludesDirectory();
  1028. }
  1029.  
  1030. return expandPath("../../") & getCommonIncludesDirectory();
  1031. }
  1032.  
  1033. private string function getLocalIncludesDirectory(){
  1034. return "includes/";
  1035. }
  1036.  
  1037. private string function getViewAssetHash(required struct asset){
  1038. var list = "";
  1039.  
  1040. if(structKeyExists(arguments.asset,"app")){
  1041. for(var x=1;x<=arguments.asset.app.recordCount;x++){
  1042. if(arguments.asset.app["type"][x] == "file"){
  1043. list = listAppend(list,LCase(trim(arguments.asset.app["name"][x]) & "$" & trim(arguments.asset.app["directory"][x]) & "$" & arguments.asset.app["dateLastModified"][x]));
  1044. }
  1045. }
  1046. }
  1047.  
  1048. return hash(list);
  1049. }
  1050.  
  1051. private string function getViewAppAssetHash(){
  1052. var viewAppAssetQueue = getViewAppAssetQueue();
  1053.  
  1054. var viewAppAssets = getViewAppAssets();
  1055.  
  1056. var list = "";
  1057.  
  1058. var item = "";
  1059.  
  1060. for(var x=1;x<=listlen(viewAppAssetQueue);x++){
  1061. item = structFindValue(viewAppAssets,listGetAt(viewAppAssetQueue,x));
  1062.  
  1063. if(!listFind(list,item[1].owner.path & "|" & item[1].owner.lastModified)){
  1064. list = listAppend(list,item[1].owner.path & "|" & item[1].owner.lastModified);
  1065. }
  1066. }
  1067.  
  1068. return hash(list);
  1069. }
  1070.  
  1071. private string function getViewAppAssetQueue(){
  1072. var viewQueue = getViewQueue();
  1073.  
  1074. var viewAssets = getViewAssets();
  1075.  
  1076. var viewAppAssets = getViewAppAssets();
  1077.  
  1078. var viewAsset = "";
  1079.  
  1080. var key = "";
  1081.  
  1082. var result = "";
  1083.  
  1084. for(var x=1;x<=listLen(viewQueue);x++){
  1085. viewAsset = viewAssets[listGetAt(viewQueue,x)];
  1086.  
  1087. if(structKeyExists(viewAsset,"app")){
  1088. for(var y=1;y<=viewAsset.app.recordCount;y++){
  1089. if(viewAsset.app["name"][y] == "app.js"){
  1090. key = lCase(viewAsset.app["directory"][y] & "\" & viewAsset.app["name"][y]);
  1091.  
  1092. if(structKeyExists(viewAppAssets,key)){
  1093. for(var item IN viewAppAssets[key]){
  1094. if(!listFind(result,viewAppAssets[key][item].path)){
  1095. result = listAppend(result,viewAppAssets[key][item].path);
  1096. }
  1097. }
  1098. }
  1099.  
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. }
  1105.  
  1106. return listSort(result,"text","desc");
  1107. }
  1108.  
  1109. private string function getViewJSHash(){
  1110. var viewQueue = getViewQueue();
  1111.  
  1112. var viewAssets = getViewAssets();
  1113.  
  1114. var viewAsset = "";
  1115.  
  1116. var list = "";
  1117.  
  1118. for(var x=1;x<=listLen(viewQueue);x++){
  1119. viewAsset = viewAssets[listGetAt(viewQueue,x)];
  1120.  
  1121. if(structKeyExists(viewAsset,"file")){
  1122. list = listAppend(list,LCase(trim(viewAsset.file.path) & "|" & viewAsset.file.lastModified));
  1123. }
  1124. }
  1125.  
  1126. return hash(list);
  1127. }
  1128.  
  1129. private string function getHash(){
  1130. var queue = getQueue();
  1131.  
  1132. var assets = getAssets();
  1133.  
  1134. var list = "";
  1135.  
  1136. var queueItem = "";
  1137.  
  1138. for(var x=1;x<=listLen(queue);x++){
  1139. queueItem = listGetAt(queue,x);
  1140.  
  1141. for(var type IN assets[queueItem]){
  1142. if((type == "javascript" || type == "style") && len(assets[queueItem][type])){
  1143. list = listAppend(list,LCase(trim(queueItem) & "|" & LCase(trim(assets[queueItem][type]))));
  1144. }
  1145. }
  1146. }
  1147.  
  1148. return hash(list);
  1149. }
  1150.  
  1151. private string function getHeadTag(required string directory,required string file,required string type){
  1152. switch(arguments.type){
  1153. case "javascript":{
  1154. return "<script src=""" & arguments.directory & arguments.file & """ type=""text/javascript""></script>";
  1155. }
  1156.  
  1157. case "style":{
  1158. return "<link href=""" & arguments.directory & arguments.file & """ type=""text/css"" rel=""stylesheet"" />";
  1159. }
  1160. }
  1161. }
  1162.  
  1163. private string function getLocalDelimiter(){
  1164. return "||";
  1165. }
  1166.  
  1167. private string function getKey(required string name,required string version){
  1168. return arguments.name & getKeyDelimiter() & arguments.version;
  1169. }
  1170.  
  1171. private string function getKeyDelimiter(){
  1172. return "___";
  1173. }
  1174.  
  1175. private string function getCoreMinifiedDirectory(){
  1176. return getMinifiedDirectory() & "core\";
  1177. }
  1178.  
  1179. private string function getAppMinifiedDirectory(){
  1180. return getMinifiedDirectory() & "app\";
  1181. }
  1182.  
  1183. private string function getViewAppAssetMinifiedDirectory(){
  1184. return getMinifiedDirectory() & "app\asset\";
  1185. }
  1186.  
  1187. private string function getViewMinifiedDirectory(){
  1188. return getMinifiedDirectory() & "view\";
  1189. }
  1190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement