/** * sseinfo-menu-fun * 导航菜单/面包屑/网站地图 ---动态生成 * @version 1.0.2 * @project sseinfo opensseinfo site * @author tonyxiao@sse.com.cn * @copyright: sse infonet ltd reserve all rights * @example sseinfomenuobj.init(); **/ if(typeof sseinfomenuobj==='undefined'){ if (typeof (col_id) === 'undefined') { var col_id = 10286; // var col_id = 8318; } var sseinfomenuobj = { menu_tab_text: '', //主菜单 menu_con_text: '', //hover状态下显示的二三级菜单 mobile_menu_text: '', //手机版菜单 menu_type: '1', //菜单类型,默认为主菜单 siteobj: {},//站点obj act_id: col_id, //左侧菜单激活的id specialnodeobj: {},//专栏obj getnodechild: function (node) { //获取栏目子菜单 function compare(v1, v2) { return v1.order - v2.order; } if (typeof (node) !== 'object') { //如果找不到对应的栏目 return []; } var lv = [], childarr = node.children.split(";"); for (var i = 0, len = childarr.length; i < len; i++) { var thenode = this.formatnode(childarr[i]); if (typeof (thenode) == 'object') { lv.push(thenode); } } return lv.sort(compare); }, getnodespechild: function (node) { //获取快捷栏目子菜单 function compare_sorder(v1, v2) { return v1.sorder - v2.sorder; } if (typeof (node) != 'object') { //如果找不到对应的栏目 return []; } var lv = [[], [], [], [],[]], childarr = node.children.split(";"), haschild = false; for (var i = 0, len = childarr.length; i < len; i++) { var thenode = this.formatnode(childarr[i]); if (typeof (thenode) == 'object') { var col = parseint(thenode.sorder / 10) - 1; if (col >= 0 && col < lv.length) { haschild = true; lv[col].push(thenode); } } } if (!haschild) { return []; } for (var j = 0, len2 = lv.length; j < len2; j++) { lv[j] = lv[j].sort(compare_sorder); } var newlv = []; for (var j = 0, len2 = lv.length; j < len2; j++) { if(lv[j].length>0){ newlv.push(lv[j]); } } return newlv; }, formatnode: function (key) { //格式化栏目节点 var obj = sse_menu_32[key]; if (typeof (obj) != 'object') { //如果找不到对应的栏目 return false; } var checkflag = arguments[1] ? 1 : 0; if (!checkflag && key != '0') { if (obj.display == '0') { //如果不是栏目 return false; } if (this.menu_type == "1" && obj.type.split(";").indexof("1") < 0) { //判断菜单类型-主菜单 return false; } else if (this.menu_type == "2" && obj.type.split(";").indexof("2") < 0) { //判断菜单类型-左侧菜单 return false; } else if (this.menu_type == "3" && obj.type.split(";").indexof("3") < 0 ) { return false; } } return { code: key, //栏目id pcode: obj.parentcode, //父栏目id label: key == '0' ? '首页' : obj.chnlname, //标题 href: key == '0' ? '/home/' : obj.url, //链接 sorder: obj.shortcutorder, //快捷栏目类型序号 order: obj.order, //显示顺序 target: obj.target == '1' ? '_blank' : '_self', //打开窗口方式 children: obj.children, //子菜单 nodestyle: 'menu_style_' + obj.style, //显示样式 0=新栏目 active: key == sseinfomenuobj.act_id ? "active" : '' //是否是当前页面 }; }, checkspecialcookie:function(node){//检查当前专栏是否是前一页专栏的子级 var specialnode = sseinfocomm.cookie.get('ssemenuspecial'); function comparespecial(key){ var currnode = sse_menu_32[key]; if(currnode.parentcode == '0'){ sseinfocomm.cookie.set('ssemenuspecial',node,1); return node; } if(currnode.parentcode == specialnode){ return specialnode; } return comparespecial(currnode.parentcode); } if(specialnode === '') { sseinfocomm.cookie.set('ssemenuspecial',node,1); return node; }else{ return comparespecial(node); } }, findspecialnode: function (key) { //查找最近的专栏 var currnode = sse_menu_32[key]; if (typeof (currnode) != 'object') { //如果找不到对应的栏目 return false; } if ((currnode.display == "0" || currnode.type.split(";").indexof('2') < 0) && sseinfomenuobj.act_id == key) { sseinfomenuobj.act_id = currnode.parentcode; return this.findspecialnode(sseinfomenuobj.act_id); } if ( currnode.isspecialchannel == "1" || currnode.parentcode == '0') { //如果当前栏目是专栏或者一级菜单,直接返回当前节点 key = this.checkspecialcookie(key); var thenode = this.formatnode(key), lv = this.getnodechild(thenode); if (lv.length === 0) { //如果此专栏没有子菜单,则显示父级及其兄弟 thenode = this.formatnode(currnode.parentcode); sseinfocomm.cookie.set('ssemenuspecial',currnode.parentcode,1); lv = this.getnodechild(thenode); } return { node: thenode, lv: lv }; } return this.findspecialnode(currnode.parentcode); //如果当前栏目不是专栏,往上继续查找 }, findbreadnode: function (channel_id) { //查找面包屑 var lv = []; function findparent(key) { var currnode = sse_menu_32[key]; if (typeof (currnode) != 'object' || key === '') { //如果找不到对应的栏目 return false; } if(key == '0' || currnode.chnlname == "首页" || currnode.type != "0"){ lv.push(sseinfomenuobj.formatnode(key, 1)); } if (currnode.chnlname == "首页" && currnode.parentcode == "0") { return false; } findparent(currnode.parentcode); } findparent(channel_id); return lv; }, findmainactive: function (channel_id) { //查找主菜单激活状态id var activeidx = ''; var ischannel = true; function findparentmenu(key) { var currnode = sse_menu_32[key]; if (typeof (currnode) != 'object' || key === '') { //如果找不到对应的栏目 activeidx = 0; ischannel = false; return false; } if (currnode.parentcode === "0") { activeidx = key; return false; } findparentmenu(currnode.parentcode); } findparentmenu(channel_id); return { key:activeidx, name:sse_menu_32[activeidx].chnlname, ischannel:ischannel }; } }; //pc版主菜单 sseinfomenuobj.initpcmainmenu = function () { this.menu_type = '1'; var menu_tab = ''; this.menu_tab_text = menu_tab; //主菜单 this.menu_con_text = menu_con; //hover状态下显示的二三级菜单 }; //手机版菜单 sseinfomenuobj.initmobilemenu = function () { function getnodemenu(thenode, menu_lv) { var lv = sseinfomenuobj.getnodechild(thenode); var shtml = ''; var menulv = 'menu_lv_' + menu_lv; if (lv.length > 0) { var childmenu = '', displaystyle = ''; for (var k = 0, len = lv.length; k < len; k++) { currnode = lv[k]; var thechild = getnodemenu(currnode, menu_lv + 1); childmenu += thechild.html; } shtml += ''; return { html: shtml }; } this.menu_type = '1'; var lv1 = this.getnodechild(this.siteobj); var ret = ''; for (var k = 0, len = lv1.length; k < len; k++) { currnode = lv1[k]; var thechild = getnodemenu(currnode, 1); ret += thechild.html; } this.mobile_menu_text = ret; }; //pc版左侧菜单 sseinfomenuobj.initleftmenu = function () { var menu_lv = 0; this.specialnodeobj = this.findspecialnode(col_id); function getnodemenu(thenode, menu_lv) { var lv = sseinfomenuobj.getnodechild(thenode); var shtml = ''; var menulv = 'menu_lv_' + menu_lv; if (lv.length > 0) { var childmenu = '', displaystyle = ''; for (var k = 0, len = lv.length; k < len; k++) { currnode = lv[k]; var thechild = getnodemenu(currnode, menu_lv + 1); childmenu += thechild.html; if (thechild.active == 'active') { thenode.active = 'active'; } } shtml += ''; return { html: shtml, active: thenode.active }; } this.menu_type = '2'; var specialnodeobj = this.specialnodeobj; if (typeof (specialnodeobj) != 'object') { return ''; } var ret = ''; //专栏标题 var lv2 = specialnodeobj.lv; //如果有第二级菜单 for (var j = 0, len2 = lv2.length; j < len2; j++) { var currnode = lv2[j]; var childmenu = getnodemenu(currnode, 1); ret += childmenu.html; } return ret; }; //面包屑 sseinfomenuobj.initbreadmenu = function () { var currnode = null, lv = this.findbreadnode(col_id); var ret = ''; for (var i = lv.length - 1; i >= 0; i--) { currnode = lv[i]; if(i == lv.length - 1){ ret += '
  • ' + currnode.label + '
  • '; }else{ ret += '
  • ' + currnode.label + '
  • '; } } return ret; }; //搜索结果页面包屑 sseinfomenuobj.getsearchbreadmenu = function (channel_id) { var currnode = null, lv = this.findbreadnode(channel_id); var ret = ''; for (var i = lv.length - 1; i >= 0; i--) { currnode = lv[i]; if(i == lv.length - 1){ ret += '
  • ' + currnode.label + '
  • '; }else{ ret += '
  • ' + currnode.label + '
  • '; } } return { html:ret, channel:lv[0] }; }; //网站地图 sseinfomenuobj.initsitemap = function () { this.menu_type = '3'; var menu_con = '', //hover状态下显示的二三级菜单 currnode = null, haschild = 0, //有二三四级菜单的索引值 lv1 = this.getnodechild(this.siteobj); for (var i = 0, len = lv1.length; i < len ; i++) { var currnode_1 = lv1[i], lv2cls = 'col-sm-12', lv3cls = ''; lv2 = this.getnodespechild(currnode_1); menu_con += '
    '; menu_con += '
    ' + currnode_1.label + '
    '; //如果有第二级菜单 if (lv2.length > 0) { if(lv2.length == 2){ lv2cls = 'col-sm-6'; lv3cls = 'col-sm-4'; }else if(lv2.length == 5){ lv2cls = 'col-sm-3'; } for (var j = 0, len2 = lv2.length; j < len2; j++) { var the_col = lv2[j]; menu_con += '
    '; for (var k = 0, len3 = the_col.length; k < len3; k++) { currnode = the_col[k]; var lv3 = this.getnodechild(currnode); var lv2_url = lv3.length>0?'javascript:void(0);':currnode.href; menu_con += '

    ' + currnode.label + '

    '; //如果有第三级菜单 if (lv3.length > 0) { for (var m = 0, len4 = lv3.length; m < len4; m++) { currnode = lv3[m]; var lv4 = this.getnodechild(currnode); var lv3_url = lv4.length>0?'javascript:void(0);':currnode.href; menu_con += '

    ' + currnode.label + '

    '; //如果有第三级菜单 if (lv4.length > 0) { for (var n = 0, len5 = lv4.length; n < len5; n++) { currnode = lv4[n]; menu_con += '

    ' + currnode.label + '

    '; } } menu_con += '
    '; } } menu_con += '
    '; } menu_con += '
    '; } } menu_con += '
    '; } return menu_con; }; //菜单初始化 sseinfomenuobj.init = function () { if (!array.prototype.indexof) { array.prototype.indexof = function (elt /*, from*/ ) { var len = this.length >>> 0; var from = number(arguments[1]) || 0; from = (from < 0) ? math.ceil(from) : math.floor(from); if (from < 0) { from += len; } for (; from < len; from++) { if (from in this && this[from] === elt) { return from; } } return -1; }; } this.siteobj = this.formatnode('0'); //获取根节点对象 this.initpcmainmenu(); this.initmobilemenu(); }; sseinfomenuobj.init(); }