1. JS/CSS 파일 제거 문법 추가

: Context.class.php에 unloadCSSFile, unloadJsFile 추가 (파일명과 옵션이 일치해야 제거됨)
: templateHandler에 <!--%unload(..)--> 추가
2. optmized된 JS/CSS 통합 파일을 제일 먼저 불러오도록 순서 변경


git-svn-id: http://xe-core.googlecode.com/svn/sandbox@4580 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
zero 2008-10-01 07:25:31 +00:00
parent 2f44b9c980
commit 81ac933a7a
3 changed files with 97 additions and 4 deletions

View file

@ -152,7 +152,7 @@
$this->addJsFile("./common/js/xml_handler.js");
$this->addJsFile("./common/js/xml_js_filter.js");
$this->addCSSFile("./common/css/default.css");
$this->addCSSFile("./common/css/button.css");
$this->addCSSFile("./common/css/button.css",false);
// 관리자 페이지일 경우 관리자 공용 CSS 추가
if(Context::get('module')=='admin' || strpos(Context::get('act'),'Admin')>0) $this->addCssFile("./modules/admin/tpl/css/admin.css", false);
@ -849,10 +849,29 @@
**/
function _addJsFile($file, $optimized, $targetie) {
if(in_array($file, $this->js_files)) return;
//if(!preg_match('/^http:\/\//i',$file)) $file = str_replace(realpath("."), ".", realpath($file));
$this->js_files[] = array('file' => $file, 'optimized' => $optimized, 'targetie' => $targetie);
}
/**
* @brief js file을 제거
**/
function unloadJsFile($file, $optimized = true, $targetie = '') {
$oContext = &Context::getInstance();
return $oContext->_unloadJsFile($file, $optimized, $targetie);
}
/**
* @brief js file을 제거
**/
function _unloadJsFile($file, $optimized, $targetie) {
foreach($this->js_files as $key => $val) {
if(realpath($val['file'])==realpath($file) && $val['optimized'] == $optimized && $val['targetie'] == $targetie) {
unset($this->js_files[$key]);
return;
}
}
}
/**
* @brief array_unique와 동작은 동일하나 file 첨자에 대해서만 동작함
**/
@ -903,6 +922,26 @@
$this->css_files[] = array('file' => $file, 'optimized' => $optimized, 'media' => $media, 'targetie' => $targetie);
}
/**
* @brief css file을 제거
**/
function unloadCSSFile($file, $optimized = true, $media = 'all', $targetie = '') {
$oContext = &Context::getInstance();
return $oContext->_unloadCSSFile($file, $optimized, $media, $targetie);
}
/**
* @brief css file을 제거
**/
function _unloadCSSFile($file, $optimized, $media, $targetie) {
foreach($this->css_files as $key => $val) {
if(realpath($val['file'])==realpath($file) && $val['optimized'] == $optimized && $val['media'] == $media && $val['targetie'] == $targetie) {
unset($this->css_files[$key]);
return;
}
}
}
/**
* @brief CSS file 목록 return
**/

View file

@ -46,10 +46,12 @@
// 캐시 디렉토리가 없으면 실행하지 않음
if(!is_dir($this->cache_path)) return $this->_getOptimizedRemoved($source_files);
$files = array();
if(!count($source_files)) return;
foreach($source_files as $file) {
if(!$file || !$file['file']) continue;
if(empty($file['optimized']) || preg_match('/^https?:\/\//i', $file['file']) || $file['file'] == './common/css/button.css') $files[] = $file;
if(empty($file['optimized']) || preg_match('/^https?:\/\//i', $file['file']) ) $files[] = $file;
else $targets[] = $file;
}
@ -62,7 +64,7 @@
$this->doOptimizedFile($path, $filename, $targets, $type);
$files[] = array('file' => $path.'/'.$filename, 'media' => 'all');
array_unshift($files, array('file' => $path.'/'.$filename, 'media' => 'all'));
return $this->_getOptimizedRemoved($files);
}

View file

@ -121,6 +121,9 @@
// import xml filter/ css/ js/ 언어파일 <!--%import("filename"[,optimized=true|false][,media="media"][,targetie="lt IE 6|IE 7|gte IE 8|..."])--> (media는 css에만 적용)
$buff = preg_replace_callback('!<\!--%import\(\"([^\"]*?)\"(,optimized\=(true|false))?(,media\=\"([^\"]*)\")?(,targetie=\"([^\"]*)\")?\)-->!is', array($this, '_compileImportCode'), $buff);
// unload css/ js <!--%unload("filename"[,optimized=true|false][,media="media"][,targetie="lt IE 6|IE 7|gte IE 8|..."])--> (media는 css에만 적용)
$buff = preg_replace_callback('!<\!--%unload\(\"([^\"]*?)\"(,optimized\=(true|false))?(,media\=\"([^\"]*)\")?(,targetie=\"([^\"]*)\")?\)-->!is', array($this, '_compileUnloadCode'), $buff);
// 파일에 쓰기 전에 직접 호출되는 것을 방지
$buff = sprintf('%s%s%s','<?php if(!defined("__ZBXE__")) exit();?>',"\n",$buff);
@ -350,6 +353,55 @@
return $output;
}
/**
* @brief <!--%filename--> 확장자를 봐서 css/ js 파일을 제거하도록 수정
**/
function _compileUnloadCode($matches) {
// 현재 tpl 파일의 위치를 구해서 $base_path에 저장하여 적용하려는 xml file을 찾음
//$base_path = dirname($this->tpl_file).'/';
$base_path = $this->tpl_path;
$given_file = trim($matches[1]);
if(!$given_file) return;
if(isset($matches[3]))
$optimized = strtolower(trim($matches[3]));
if(!$optimized) $optimized = 'true';
if(isset($matches[5]))
$media = trim($matches[5]);
if(!$media) $media = 'all';
if(isset($matches[7]))
$targetie = trim($matches[7]);
if(!$targetie) $targetie = '';
else $optimized = 'false';
$filename = sprintf("%s%s",$base_path, $given_file);
// path와 파일이름을 구함
$tmp_arr = explode("/",$filename);
$filename = array_pop($tmp_arr);
$base_path = implode("/",$tmp_arr)."/";
// 확장자를 구함
$tmp_arr = explode(".",$filename);
$ext = strtolower(array_pop($tmp_arr));
// 확장자에 따라서 파일 import를 별도로
switch($ext) {
// css file
case 'css' :
$meta_file = sprintf('%s%s', $base_path, $filename);
$output = sprintf('<?php Context::unloadCSSFile("%s%s", %s, "%s", "%s"); ?>', $base_path, $filename, $optimized, $media, $targetie);
break;
// js file
case 'js' :
$meta_file = sprintf('%s%s', $base_path, $filename);
$output = sprintf('<?php Context::unloadJsFile("%s%s", %s, "%s"); ?>', $base_path, $filename, $optimized, $targetie);
break;
}
return $output;
}
/**
* @brief $tpl_file로 compiled_tpl_file이름을 return
**/