https://jsfiddle.net/jangwonwoong/nm7cLLm0/
var JOW_APP = JOW_APP || {}; JOW_APP._tree = [ { id : "home", name : "HOME", parentId : "root",order: 0 }, { id : 'sub_01', parentId : 'home', name : '감천문화마을',doc : 'sub_01', CssClass : 'top_color_01' ,order: 0}, { id : 'sub_01_02', parentId : 'sub_01', name : '마을소개',doc:'sub_01_02' ,order: 0 }, { id : 'sub_01_03', parentId : 'sub_01', name : '시설안내',doc:'sub_01_03' ,order: 1 }, { id : 'sub_01_04', parentId : 'sub_01', name : '마을 둘러보기',doc:'sub_01_04' ,order: 2 }, { id : 'sub_01_04', parentId : 'sub_01', name : '작품안내',doc:'sub_01_04' ,order: 3 }, { id : 'sub_01_05', parentId : 'sub_01', name : '오시는길',doc:'sub_01_05' ,order: 4 }, { id : 'sub_02', parentId : 'home', name : '체험 및 숙박',doc : 'sub_02' , CssClass : 'top_color_02' ,order: 1}, { id : 'sub_02_02', parentId: 'sub_02', name : '공방 소개',doc:'sub_02_02' ,order: 0 }, { id : 'sub_02_03', parentId: 'sub_02', name : '입주작가 공방',doc:'sub_02_03' ,order: 1 }, { id : 'sub_02_04', parentId: 'sub_02', name : '체험 신청',doc:'sub_02_04' ,order: 2 }, { id : 'sub_02_05', parentId: 'sub_02', name : '객실 소개',doc:'sub_02_05' ,order: 3 }, { id : 'sub_02_06', parentId: 'sub_02', name : '객실 예약',doc:'sub_02_06' ,order: 4 }, { id : 'sub_03', parentId : 'home', name : '문화쇼핑',doc : 'sub_03' , CssClass : 'top_color_03' ,order: 2}, { id : 'sub_02_02', parentId : 'sub_03', name : '마을기업 소개',doc : 'sub_02_03' ,order: 0}, { id : 'sub_02_03', parentId : 'sub_03', name : '관광상품',doc : 'sub_02_03' ,order: 1}, { id : 'sub_02_04', parentId : 'sub_03', name : '입주작가 작품',doc : 'sub_02_04' ,order: 2}, { id : 'sub_04', parentId : 'home', name : '정보광장',doc : 'sub_04' , CssClass : 'top_color_04' ,order: 3 }, { id : 'sub_04_02', parentId : 'sub_04', name : '주요 관광지',doc : 'sub_04_02' ,order: 0}, { id : 'sub_04_03', parentId : 'sub_04', name : '주요 문화재',doc : 'sub_04_03' ,order: 1}, { id : 'sub_04_04', parentId : 'sub_04', name : '전통시장' ,doc : 'sub_04_04' ,order: 2}, { id : 'sub_04_05', parentId : 'sub_04', name : '먹거리 정보',doc : 'sub_04_05' ,order: 3}, { id : 'sub_05', parentId : 'home', name : '마을소식',doc : 'sub_05' , CssClass : 'top_color_05' ,order: 4 }, { id : 'sub_05_02', parentId : 'sub_05', name : '공지사항',doc:'sub_05_02' ,order: 0 }, { id : 'sub_05_03', parentId : 'sub_05', name : '행사안내',doc:'sub_05_03' ,order: 1 }, { id : 'sub_05_04', parentId : 'sub_05', name : '언론보도',doc:'sub_05_04' ,order: 2 }, { id : 'sub_05_05', parentId : 'sub_05', name : '마을소식지',doc:'sub_05_05' ,order: 3 }, { id : 'sub_05_06', parentId : 'sub_05', name : '주민협의회',doc:'sub_05_06' ,order: 4 }, { id : 'sub_06', parentId : 'home', name : '소통공간',doc : 'sub_06' , CssClass : 'top_color_06' ,order: 5 }, { id : 'sub_06_02', parentId : 'sub_06', name : '문화마을 후기',doc:'sub_06_02' ,order: 0 }, { id : 'sub_06_03', parentId : 'sub_06', name : '감천 갤러리',doc:'sub_06_03' ,order: 1}, { id : 'sub_06_04', parentId : 'sub_06', name : '감천 동영상',doc:'sub_06_04' ,order: 2}, { id : 'sub_06_05', parentId : 'sub_06', name : '자주묻는질문',doc:'sub_06_05' ,order: 3} ]; JOW_APP.site_map = (function(_list, _rootId) { var _treeModel = []; var _listLength = _list.length; var _treeLength = 0; var _loopLength = 0; var _current = []; var _next = []; var _prev = []; var _set_doc = function (p_doc_id,c_doc_id) { $.each(_treeModel[0].children, function(i,v) { if(v.id == p_doc_id) { var _sub_tree = v.children; $.each(_sub_tree,function(i,v){ if(v.id == c_doc_id) { _current.length = 0; _next.length = 0; _prev.length = 0; _current.push(_sub_tree[i]); _next.push(_sub_tree[i+1]); _prev.push(_sub_tree[i-1]); return false; } }); } }); } var getParentNode = function ( _children, item ) { for ( var i=0, child; child = _children[i]; i++ ) { if ( child.id === item.parentId ) { var view = { "id" : item.id, "name" : item.name, "doc" : item.doc, "CssClass" : item.CssClass, "order" : item.order, "children" : [] }; child.children.push(view); _treeLength++; _list.splice( _list.indexOf( item ), 1 ); child.children.sort( function ( a, b ) { return a.order < b.order ? -1 : a.order > b.order ? 1 : 0; }); break; } else { if( child.children.length ) { arguments.callee( child.children, item ); } } } } while ( _treeLength != _listLength && _listLength != _loopLength++ ) { for ( var i=0, item; item = _list[i]; i++ ) { if ( item.parentId === _rootId ) { var view = { "id" : item.id, "name" : item.name, "doc" : item.doc, "CssClass" : item.CssClass, "order" : item.order, "children" : [] }; _treeModel.push(view); _treeLength++; _list.splice(i, 1); _treeModel.sort( function ( a, b ) { return a.order < b.order ? -1 : a.order > b.order ? 1 : 0; }); break; } else { getParentNode( _treeModel, item ); } } } return { treeModel : _treeModel, set_doc : _set_doc, next : _next, prev : _prev } })(JOW_APP._tree,'root'); var rs = JOW_APP.site_map.treeModel; JOW_APP.site_map.set_doc('sub_01','sub_01_02'); var next = JOW_APP.site_map.next; var prev = JOW_APP.site_map.prev; document.getElementById('result').innerHTML = JSON.stringify(next, null, ' ');
'JS > JavaScript' 카테고리의 다른 글
[펌]iFrame(서로다른 도메인) 간 메세지 전달하기 (1) | 2016.05.24 |
---|---|
클로저의 남용으로 인한 메모리 누수 (0) | 2016.04.21 |
스크립트 순서대로 로딩하기[펌] (0) | 2015.09.25 |
이미지 미리 보기 (0) | 2015.08.25 |
Editor 개발 관련하여 괜찮은 스크립트 (0) | 2015.08.04 |