+function($){
'use strict'
var AFFIX='#cps-main';
var TARGET='.posts-layout,.entry-content';
var CONTAINER=AFFIX+'>.cps-post-struction';
var is_single = $('.entry-content').length > 0;
var seed=1;
function create_node(el) {
var $el=$(el);
var level=1;
var title=null;
var isimg=false;
if ($el.is('img')) {
level=9;
isimg=true;
title=$el.prop('title')||$el.prop('alt')||$el.next().text()||$el.parent().text();
} else {
level=parseInt(/h(\d)/i.exec($el[0].tagName)[1]);
title=$el.text();
}
var id=$el.prop('id');
if (!id) {
id='post-struction-l'+level+'-'+(seed++);
$el.prop('id',id);
}
return {
id: id,
text: title,
level: level,
isimg: isimg,
nodes: [],
object: $el
};
}
function create_element(node, $parent) {
var href = null;
if (is_single) {
href = '#' + node.id;
} else {
if (node.isimg) return null;
var href=node.object.find('>a').prop('href');
if (!href) return null;
}
var $anchor=$('')
.text(node.text)
.prop('title', node.text)
.prop('href', href);
if (node.isimg) {
$anchor.prepend('');
}
var $res = $('
')
.append($anchor)
.appendTo($parent)
.addClass('cps-item level'+node.nlevel);
if (node.nodes) {
var $ul=$('').appendTo($res);
$(node.nodes).each(function(){
create_element(this, $ul);
});
}
}
$(function(){
var $affix=$(AFFIX)
var $container=$(CONTAINER);
if (!$container.is(':visible')) return;
var $postitle=$('.title-post');
var id=$postitle.prop('id');
if (!id) {
id='post-title-root';
$postitle.prop('id',id);
}
var $cpstitle=$('.cps-title');
if ($cpstitle.length) {
var text=$cpstitle.text();
$('')
.text(text)
.prop('title', text)
.prop('href','#'+id)
.appendTo($cpstitle.empty());
}
var $root=$('').appendTo($container.empty());
if ($container.data('title')) {
$root=$('').appendTo(
$('')
.append($('').text($postitle.text()).prop('href','#'+id))
.appendTo($root)
);
}
var seed=1;
var root={ level:0,nlevel:0, nodes:[], parent:null };
var curr=root;
$(TARGET).find('h1,h2,h3,h4,h5,h6,img').each(function() {
var node=create_node(this);
while(node.level<=curr.level) {
curr=curr.parent;
}
node.parent=curr;
node.nlevel=curr.nlevel+1;
curr.nodes.push(node);
curr=node;
});
$(root.nodes).each(function(){
create_element(this,$root);
});
if (is_single) $('body').scrollspy({target:CONTAINER,offset:$postitle.offset().top});
$affix.affix({
offset: {
top:$affix.offset().top-107,
bottom: function() {
return (this.bottom=$('body').outerHeight()-$(TARGET).offset().top-$(TARGET).outerHeight()+30);
}
}
}).on('mouseenter', '.cps-item', function() {
$(this).addClass('hover');
}).on('mouseleave', '.cps-item', function() {
$(this).removeClass('hover');
});
function refresh() {
if (is_single) $('body').scrollspy('refresh');
$affix.affix('checkPosition');
$affix.css('width',$affix.parent().width());
};
refresh();
var isfloat=false;
$('body').scroll(function() {
var curr=$('.site-header').is('.float-header');
if (curr!=isfloat) {
isfloat=curr;
refresh();
}
});
$(window).resize(refresh);
});
}(jQuery)