/**
* @author NAVER (developers@xpressengine.com)
* @version 0.1
* @brief 에디터 관련 스크립트
*/
/**
* 에디터 사용시 사용되는 이벤트 연결 함수 호출
**/
/**
* 에디터의 상태나 객체를 구하기 위한 함수
**/
// editor_sequence값에 해당하는 textarea object를 return
function editorGetTextArea(editor_sequence) {
return jQuery('#editor_textarea_' + editor_sequence)[0];
}
function editorGetPreviewArea(editor_sequence) {
return jQuery( '#editor_preview_' + editor_sequence )[0];
}
// editor_sequence에 해당하는 form문 구함
function editorGetForm(editor_sequence) {
var iframe_obj = editorGetIFrame(editor_sequence);
if(!iframe_obj) return;
var fo_obj = iframe_obj.parentNode;
while(fo_obj.nodeName != 'FORM') { fo_obj = fo_obj.parentNode; }
if(fo_obj.nodeName == 'FORM') return fo_obj;
return;
}
// 에디터의 전체 내용 return
function editorGetContent_xe(editor_sequence) {
var html = "";
if(editorMode[editor_sequence]=='html') {
var textarea_obj = editorGetTextArea(editor_sequence);
if(!textarea_obj) return "";
html = textarea_obj.value;
} else {
var iframe_obj = editorGetIFrame(editor_sequence);
if(!iframe_obj) return "";
html = jQuery(iframe_obj.contentWindow.document.body).html().replace(/^
]*)>$/i,'');
}
return html;
}
// 에디터 내의 선택된 부분의 NODE를 return
function editorGetSelectedNode(editor_sequence) {
var iframe_obj = editorGetIFrame(editor_sequence), w, range;
w = iframe_obj.contentWindow;
if(w.document.selection) {
range = w.document.selection.createRange();
return jQuery('
",e.target); xPreventDefault(evt); xStopPropagation(evt); break; // ie에서 ctrlKey + enter일 경우 P 태그 입력 case 13 : if(xIE4Up) { if(e.target.parentElement.document.designMode!="On") return; obj = e.target.parentElement.document.selection.createRange(); obj.pasteHTML('
');
obj.select();
evt.cancelBubble = true;
evt.returnValue = false;
return;
}
break;
// bold
case 98 :
editorDo('Bold',null,e.target);
xPreventDefault(evt);
xStopPropagation(evt);
break;
// italic
case 105 :
editorDo('Italic',null,e.target);
xPreventDefault(evt);
xStopPropagation(evt);
break;
// underline
case 117 :
editorDo('Underline',null,e.target);
xPreventDefault(evt);
xStopPropagation(evt);
break;
//RemoveFormat
case 100 :
editorDo('RemoveFormat',null,e.target);
xPreventDefault(evt);
xStopPropagation(evt);
break;
// strike
/*
case 83 :
case 115 :
editorDo('StrikeThrough',null,e.target);
xPreventDefault(evt);
xStopPropagation(evt);
break;
*/
}
}
}
// 편집 기능 실행
function editorDo(command, value, target) {
var doc = null;
// target이 object인지 editor_sequence인지에 따라 document를 구함
if(typeof(target)=="object") {
if(xIE4Up) doc = target.parentElement.document;
else doc = target.parentNode;
} else {
var iframe_obj = editorGetIFrame(target);
doc = iframe_obj.contentWindow.document;
}
var editor_sequence = doc.body.getAttribute('editor_sequence');
if(editorMode[editor_sequence]) return;
// 포커스
if(typeof(target)=="object") target.focus();
else editorFocus(target);
// 실행
doc.execCommand(command, false, value);
// 포커스
if(typeof(target)=="object") target.focus();
else editorFocus(target);
}
// 폰트를 변경
function editorChangeFontName(obj,srl) {
var value = obj.options[obj.selectedIndex].value;
if(!value) return;
editorDo('FontName',value,srl);
obj.selectedIndex = 0;
}
function editorChangeFontSize(obj,srl) {
var value = obj.options[obj.selectedIndex].value;
if(!value) return;
editorDo('FontSize',value,srl);
obj.selectedIndex = 0;
}
function editorUnDo(obj,srl) {
editorDo('undo','',srl);
obj.selectedIndex = 0;
}
function editorReDo(obj,srl) {
editorDo('redo','',srl);
obj.selectedIndex = 0;
}
function editorChangeHeader(obj,srl) {
var value = obj.options[obj.selectedIndex].value;
if(!value) return;
value = "<"+value+">";
editorDo('formatblock',value,srl);
obj.selectedIndex = 0;
}
/**
* HTML 편집 기능 활성/비활성
**/
function editorChangeMode(mode, editor_sequence) {
/* jshint -W041 */
if(mode == 'html' || mode == ''){
var expire = new Date();
expire.setTime(expire.getTime()+ (7000 * 24 * 3600000));
xSetCookie('editor_mode', mode, expire);
}
var iframe_obj = editorGetIFrame(editor_sequence);
if(!iframe_obj) return;
var textarea_obj = editorGetTextArea(editor_sequence);
var preview_obj = editorGetPreviewArea(editor_sequence);
var contentDocument = iframe_obj.contentWindow.document;
var html = null;
if(editorMode[editor_sequence]=='html') {
html = textarea_obj.value;
contentDocument.body.innerHTML = textarea_obj.value;
} else if (editorMode[editor_sequence]=='preview') {
// html = xInnerHtml(preview_obj);
html = textarea_obj.value;
preview_obj.contentWindow.document.body.innerHTML = '';
// xAddEventListener(xGetElementById('editor_preview_'+editor_sequence), 'load', function(){setPreviewHeight(editor_sequence)});
} else {
html = contentDocument.body.innerHTML;
textarea_obj.value = html;
html = html.replace(/
/ig,"
\n");
html = html.replace(/
\n\n/ig,"
\n");
}
// html 편집 사용시
if(mode == 'html' && textarea_obj) {
preview_obj.style.display='none';
if(xGetElementById('fileUploader_'+editor_sequence)) xGetElementById('fileUploader_'+editor_sequence).style.display='block';
textarea_obj.value = html;
xWidth(textarea_obj, xWidth(iframe_obj.parentNode));
xHeight(textarea_obj, xHeight(iframe_obj.parentNode));
editorMode[editor_sequence] = 'html';
if(xGetElementById('xeEditor_'+editor_sequence)) {
xGetElementById('xeEditor_'+editor_sequence).className = 'xeEditor html';
xGetElementById('use_rich_'+editor_sequence).className = '';
xGetElementById('preview_html_'+editor_sequence).className = '';
xGetElementById('use_html_'+editor_sequence).className = 'active';
}
// 미리보기
} else if(mode == 'preview' && preview_obj) {
preview_obj.style.display='';
if(xGetElementById('fileUploader_'+editor_sequence)) xGetElementById('fileUploader_'+editor_sequence).style.display='none';
var fo_obj = xGetElementById("preview_form");
if(!fo_obj) {
fo_obj = xCreateElement('form');
fo_obj.id = "preview_form";
fo_obj.method = "post";
fo_obj.action = request_uri;
fo_obj.target = "editor_preview_"+editor_sequence;
xInnerHtml(fo_obj,'');
document.body.appendChild(fo_obj);
}
fo_obj.content.value = html;
fo_obj.submit();
xWidth(preview_obj, xWidth(iframe_obj.parentNode));
editorMode[editor_sequence] = 'preview';
if(xGetElementById('xeEditor_'+editor_sequence)) {
xGetElementById('xeEditor_'+editor_sequence).className = 'xeEditor preview';
xGetElementById('use_rich_'+editor_sequence).className = '';
xGetElementById('preview_html_'+editor_sequence).className = 'active';
if(xGetElementById('use_html_'+editor_sequence)) xGetElementById('use_html_'+editor_sequence).className = '';
}
// 위지윅 모드 사용시
} else {
preview_obj.style.display='none';
if(xGetElementById('fileUploader_'+editor_sequence)) xGetElementById('fileUploader_'+editor_sequence).style.display='block';
contentDocument.body.innerHTML = html;
editorMode[editor_sequence] = null;
if(xGetElementById('xeEditor_'+editor_sequence)) {
xGetElementById('xeEditor_'+editor_sequence).className = 'xeEditor rich';
xGetElementById('use_rich_'+editor_sequence).className = 'active';
xGetElementById('preview_html_'+editor_sequence).className = '';
if(xGetElementById('use_html_'+editor_sequence)) xGetElementById('use_html_'+editor_sequence).className = '';
}
}
}
// Editor Info Close
function closeEditorInfo(editor_sequence) {
xGetElementById('editorInfo_'+editor_sequence).style.display='none';
var expire = new Date();
expire.setTime(expire.getTime()+ (7000 * 24 * 3600000));
xSetCookie('EditorInfo', '1', expire);
}
function showEditorHelp(e,editor_sequence){
jQuery('#helpList_'+editor_sequence).toggleClass('open');
}
function showEditorExtension(evt,editor_sequence){
var oid = '#editorExtension_'+editor_sequence;
var e = new xEvent(evt);
if(jQuery(oid).hasClass('extension2')){
jQuery(oid).addClass('open');
if(e.pageX <= xWidth('editor_component_'+editor_sequence)){
jQuery('#editor_component_'+editor_sequence).css('right','auto').css('left', 0);
}else{
jQuery('#editor_component_'+editor_sequence).css('right', 0).css('left', 'auto');
}
}else{
jQuery(oid).attr('class', 'extension2');
}
}
function showPreviewContent(editor_sequence) {
if(typeof(editor_sequence)=='undefined') return;
if(typeof(_editorFontColor[editor_sequence])=='undefined') return;
var preview_obj = editorGetPreviewArea(editor_sequence);
preview_obj.contentWindow.document.body.style.color = _editorFontColor[editor_sequence];
}
function setPreviewHeight(editor_sequence){
var h = xGetElementById('editor_preview_'+editor_sequence).contentWindow.document.body.scrollHeight;
if(h < 400) h=400;
xHeight('editor_preview_'+editor_sequence,h+20);
}
function getAutoSavedSrl(ret_obj, response_tags, c) {
var editor_sequence = ret_obj.editor_sequence;
var primary_key = ret_obj.key;
var fo_obj = editorGetForm(editor_sequence);
fo_obj[primary_key].value = ret_obj.document_srl;
if(uploadSettingObj[editor_sequence]) editorUploadInit(uploadSettingObj[editor_sequence], true);
}