ä¸è¨ã®è¨äºã§æ¸ããããã«ãã«ãã´ãªã¼ã®é層åã®ããã°ãã¼ãã¯ã注ç®è¨äºã®ã«ãã´ãªã¼ã®è¡¨ç¤ºã«ã¯ããããã対å¿ã§ãã¦ãã¾ããã
【解説】はてなブログ:カテゴリを階層化する(補足と問題点を追記) - daisukeの技術ブログ
ããã§ãæä¾ããã¦ããã½ã¼ã¹ã³ã¼ãã解æãã¦ï¼ãããããã¼ãã¼ã«ã§è§£æããæ¹æ³ãè¨è¼ããï¼ãç°¡åã«å¯¾å¿ã§ããããªãã注ç®è¨äºã®ã«ãã´ãªã¼ã®è¡¨ç¤ºã«å¯¾å¿ãã¦ããã¾ãã
ãªããJavaScriptã¯ãã»ã¨ãã©ä½¿ã£ããã¨ããªãã®ã§ãåºæ¬çãªãã¨ãã¡ã¢ããªãããã£ã¦ããã¾ãã
解æããçµæãçµè«ã¨ãã¦ã¯ãç¾æç¹ã§ã¯ã注ç®è¨äºã®ã«ãã´ãªã¼ã®é層åã¯ãä¸å¯è½ã ã¨æããã¾ãã
çç±ã¯ãã«ãã´ãªã¼é層åã®JavaScriptãã¤ã¾ããã¦ã¼ã¶ãé
ç½®ã§ããJavaScriptãå®è¡ãããã¿ã¤ãã³ã°ã§ã¯ã注ç®è¨äºã¯ã¾ã ä½ãæ¸ããã¦ããªãã£ãããã§ããããããããã¼ã¸è¡¨ç¤ºã®æå¾ã®æ¹ã®ã¿ã¤ãã³ã°ã§ãã¯ã¦ãªããã°ã®éå¶ãç¨æãã¦ããããã°ã©ã ã§ããã®ã¦ã¼ã¶ã¼ã®æ³¨ç®è¨äºãç¹å®ãã¦ã注ç®è¨äºã®å
容ãæ¸ãè¾¼ãã§ããã¨æããã¾ãã
ä½ãã«ä½¿ããã¨ãããããããããªãã®ã§ã解æããå
容ãæ¸ãã¦ããã¾ãã
åèæç®
ã¯ããã«
ããã°ã«ã¹ã¿ãã¤ãºã®ä¸è¦§ã§ããè¯ãã£ããåèã«ãã¦ãã ããã
ããã°ã«ã¹ã¿ãã¤ãºã®ã¾ã¨ã
JavaScriptã®åºæ¬
JavaScriptã«ã¤ãã¦ã¯ãéå»ã«å°ã使ã£ããã¨ããã£ããããã§ãå
¨ãè¦ãã¦ãªãã®ã§ãWebãé ¼ãã«åºæ¬çãªå
容ãã¡ã¢ãã¦ããã¾ãã
JavaScriptã®åºæ¬
JavaScriptã®è¨è¿°å ´æ
</body>
ã®ç´åã«ã<script>
JavaScriptã®ã½ã¼ã¹ã³ã¼ã</script>
ããããã¯ã<script type="text/javascript" src="JavaScriptã®ãã¡ã¤ã«ãã¹"></script>
ã§è¨è¿°ãã
<head>
ã¨</head>
ã®éã«ãä¸è¨ã¨åãå
容ã§è¨è¿°ãã
ãããã<body>
ã³ã³ãã³ã</body>
ã®è¡¨ç¤ºãåªå
ããããã<head>
ï½</head>
ã«æéããããå
容ã¯é¿ããæ¹ãããããã§ãããã£ã¦ãé常ã¯ãã1.ããé¸æããã¾ãã
ã«ãã´ãªã¼ã®é層åã®ã½ã¼ã¹ã³ã¼ãã解æãã
æ¬é¡ã®ã«ãã´ãªã¼ã®é層åã®ã½ã¼ã¹ã³ã¼ãã解æãã¦ããã¾ãã
JavaScriptã®ææ³ã¯ãã»ã¨ãã©åãããªãã¾ã¾ã§ãããã¨ããããè¦ã¦ããã¾ãã
ãããããã¼ãã¼ã«ã使ã£ã¦ãåãããªãããã½ã¼ã¹ã³ã¼ããç解ãã
ã¾ããminã®ã½ã¼ã¹ã§ã¯ãè¦ã«ããã®ã§ãGitHubãããé常ã®ã½ã¼ã¹ã³ã¼ããå
¥æãã¾ãã
github.com
public/v1.1
ã«å¯¾è±¡ã®ã½ã¼ã¹ã³ã¼ããå
¥ã£ã¦ãã¾ãã
解æã«å¿
è¦ãããªã®ã¯ããbreadcrumb.jsãã¨ãcategory_archive.jsãã§ãã
ããããããã¹ãç¨ã®ããã°ã§ãããã¿ã«è²¼ãä»ãã¦ãããbreadcrumb.min.jsãã¨ãcategory_archive.min.jsããåé¤ãã¦ãè²¼ãä»ãã¾ãã
å¤æ´ãä¿åãã¦ããã¼ã¸ã表示ãã¦ã¿ãã¨ãæ®éã«ã«ãã´ãªã¼ã®é層åãåä½ããã®ã§ã次ã¯ããããããã¼ãã¼ã«ã使ã£ã¦ãåããã¦ããã¾ãã
é©å½ãªè¨äºã表示ããã¦ããããããããã¼ãã¼ã«ãèµ·åãã¾ããã½ã¼ã¹ã¿ããéãã表示ãããè¨äºãã¯ãªãã¯ããã¨ãHTMLã½ã¼ã¹ã表示ãããã®ã§ãè¦ããã¨ããã§ãã¬ã¼ã¯ãã¤ã³ããè¨å®ãã¦ããã¼ã¸ããªãã¼ãããã¨ããã¬ã¼ã¯ãã¤ã³ãã§åæ¢ãã¾ãã
ãã¨ã¯ãã¹ãããã¤ã³ãã¹ããããªã¼ãã¼ãã¹ãããã¢ã¦ããªã©ã使ã£ã¦ãå¤æ°ã®å¤ã確èªããªããã解æãã¦ããã°OKã§ãã
ã«ãã´ãªã¼ã®é層åã®ã½ã¼ã¹ã³ã¼ãã®æ§æ
ã«ãã´ãªã¼ã®é層åã®ã½ã¼ã¹ã³ã¼ãã¯ã以ä¸ã®æ§æã§ãã
- ãããï¼
fulldisplay.min.css
ã追å â ä»åã®ä»¶ã«ã¯ç¡é¢ä¿
- ããã¿ï¼ä»¥ä¸ã®3ã¤ã®ãã¡ã¤ã«ã追å
https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
â å¤é¨ããæä¾ãããã©ã¤ãã©ãªãªã®ã§è§£æ対象å¤
breadcrumb.min.js
â ãã³ãããªã¹ãã®å¦çããã«ãã´ãªã¼åãå¤æããå¦çãè¡ã£ã¦ããï¼ä»åã®è§£æ対象ï¼
category_archive.min.js
â ãµã¤ããã¼ã®ã«ãã´ãªã¼ã¢ã¸ã¥ã¼ã«ã®é層åã®åã«ãã´ãªã®ééå¦çãè¡ã£ã¦ããï¼ä»åã®è§£æ対象å¤ï¼
breadcrumb.min.js
breadcrumb.min.jsã«ã¯ã5ã¤ã®é¢æ°ãå®è£
ããã¦ãã¾ãã
readyé¢æ°ã¯ããã¼ã¸ããã¼ããããã¨ãã«å¼ã°ããé¢æ°ã§ãããã¨ã³ããªã¼ãã¤ã³ãã§ãã
è¨äºã表示ãããå ´åã¨ãã«ãã´ãªã¼åãã¯ãªãã¯ããã¨ãã«è¡¨ç¤ºãããã«ãã´ãªã¼å
ã®è¨äºä¸è¦§ï¼ã¢ã¼ã«ã¤ãï¼ã表示ãããå ´åã¨ã§ã大ãã2ã¤ã«åããã¦ãã¾ãã
æåã®2ã¤ã®é¢æ°ã®ãremapBreadcrumbé¢æ°ãã¨ãremapArticleCategoryé¢æ°ãã¯ãè¨äºã表示ãããã¨ãã«å®è¡ãããé¢æ°ã§ãã
次ã®2ã¤ã®é¢æ°ã®ãremapCategoryBreadcrumbé¢æ°ãã¨ãremapArchiveCategoryé¢æ°ãã¯ãè¨äºä¸è¦§ã表示ãããã¨ãã«å®è¡ãããé¢æ°ã§ãã
以éã§ãå¦çãããé ã«ãããããã®é¢æ°ã®æ¦è¦ã説æããå¾ã詳細ãªè§£æããå
容ã¯ãã½ã¼ã¹ã³ã¼ãã«ã³ã¡ã³ããããã®ã示ãã¾ãã
readyé¢æ°
è¨äºã表示ãããå ´åã®å¦çã§ãè¨äºã¿ã¤ãã«ä¸ã®ãã³ãããªã¹ãï¼å¤æåã®ãããã°ã«ã¹ã¿ãã¤ãºãããã°ã«ã¹ã¿ãã¤ãº-JavaScriptããªã©ï¼ãåå¾ãã¦ããããå¼æ°ã«ãã¦ãremapArticleCategoryé¢æ°ï¼ãã³ãããªã¹ããåã«ãã´ãªã¼ã«å¤æããå¦çï¼ãå¼ã³åºãã¾ãããªããè¨äºä¸è¦§ã表示ãããå ´åã¯ããã®é¢æ°ã¯å¼ã³åºããã¾ããã
次ã¯ãè¨äºä¸è¦§ã表示ãããå ´åã®å¦çã§ãä¸ã¨åæ§ã«ãã³ãããªã¹ããåå¾ãã¦ããããå¼æ°ã«ãã¦ãremapCategoryBreadcrumbé¢æ°ï¼-
ã§çµåããããããã°ã«ã¹ã¿ãã¤ãº-JavaScriptããåã«ãã´ãªã¼ã ããJavaScriptãã«å¤æããå¦çï¼ãå¼ã³åºãã¾ãããã®å¾ãremapArchiveCategoryé¢æ°ï¼åè¨äºã«æ¸ããã¦ããã«ãã´ãªã¼åãå¤æããå¦çï¼ãå¼ã³åºãã¾ãããªããè¨äºã表示ãããå ´åã¯ããã®é¢æ°ã¯å¼ã³åºããã¾ããã
$(document).ready(function(){
var $entry_categories = $('#main-inner > article.entry > div.entry-inner > header > div.entry-categories > a');
if($entry_categories.length>0) {
remapArticleCategory($entry_categories);
}
var $archive_entries = $('#main-inner > div.archive-entries');
if($archive_entries.length > 0) {
$breadcrumb_child = $('#top-box > div.breadcrumb > div.breadcrumb-inner > span.breadcrumb-child:first');
breadcrumbs = $breadcrumb_child.find('span').text().split('-');
remapCategoryBreadcrumb(breadcrumbs);
$archive_entries.each(function(){
remapArchiveCategory($(this).find('section > div.categories > a'));
});
}
});
remapBreadcrumbé¢æ°
è¨äºã表示ãããæã®ãã³ãããªã¹ãå¤æå¦çã®ãµãé¢æ°ã§ãã
æåã¯ãã«ãã´ãªã¼åã'-'ã§çµåããã¦ãã¾ãããã®é¢æ°ã¯ãçµåããååã§URLãä½æããè¨äºã«è¡¨ç¤ºããæååã¯ãåã«ãã´ãªã¼ã®ååã ãã使ã£ã¦ãã¾ãã
var host = $(location).attr('host');
function remapBreadcrumb(breadcrumb){
new_breadcrumb_html='';
for(var i=0;i<breadcrumb.length;i++) {
url_category=[];
for(var j=0;j<=i;j++) {
url_category[j]=breadcrumb[j];
}
var category_url = 'https://'+host+'/archive/category/'+url_category.join('-');
new_breadcrumb_html += '<span class="breadcrumb-child""><a class="breadcrumb-child-link" href="'+category_url+'"><span>'+breadcrumb[i]+'</span></a></span>';
if(i+1<breadcrumb.length) {
new_breadcrumb_html += '<span class="breadcrumb-gt"> ></span>';
}
}
if(new_breadcrumb_html != '') {
$('#top-box > div.breadcrumb > div.breadcrumb-inner > span.breadcrumb-child:first').prop('outerHTML',new_breadcrumb_html);
}
}
remapArticleCategoryé¢æ°
è¨äºã表示ãããæã®ã¡ã¤ã³é¢æ°ã§ãã
æåã¯ãä¸çªé層ãæ·±ãååãåå¾ãã¦ããããåå²ããä¸ã§ç¤ºãããremapBreadcrumbé¢æ°ãå¼ã³åºãã¾ãã
ãã®å¾ã®å¦çã¯ãå¼æ°ã§æ¸¡ãããã«ãã´ãªã¼åãä¸æ¸ããã¦ãã¾ããããã®å¾ã使ãããªãã®ã§ãå¿
è¦ãªãå¦çã§ããå
¬å¼ã®ãã³ãããªã¹ãã®æ©è½ããã¾ã æä¾ããã¦ããªãã£ãã¨ãã®ãã³ãããªã¹ãã®æ©è½ãæµç¨ããã¨ã®ãã¨ãªã®ã§ãæåã«ä½ãããã¨ãã«ã¯ä½ãã«ä½¿ãããå¦çã ã£ãã®ããããã¾ããã
function remapArticleCategory(categories) {
var index=0;
while(typeof categories[index+1] != 'undefined' && categories[index+1].text.includes('-')) {
index+=1;
}
breadcrumb_array = categories[index].text.split('-');
remapBreadcrumb(breadcrumb_array)
category_num = categories.length;
for(var i=0;i<category_num;i++) {
category_branch = categories[i].text.split('-');
categories[i].text=category_branch[category_branch.length-1];
}
}
remapCategoryBreadcrumbé¢æ°
è¨äºä¸è¦§ã表示ãããå ´åã«å¼ã³åºãããå¦çã§ãremapBreadcrumbé¢æ°ã¨ãã£ã¦ããã¨ã¯ãã ãããåãã§ããããã³ãããªã¹ãã®ã«ãã´ãªã¼åã®å¤æå¦çã§ãã
function remapCategoryBreadcrumb(breadcrumb) {
new_breadcrumb_html='';
for(var i=0;i<breadcrumb.length;i++) {
if(i+1<breadcrumb.length) {
url_category=[];
for(var j=0;j<=i;j++) {
url_category[j]=breadcrumb[j];
}
var category_url = 'https://'+host+'/archive/category/'+url_category.join('-');
new_breadcrumb_html += '<span class="breadcrumb-child"><a class="breadcrumb-child-link" href="'+category_url+'">'+breadcrumb[i]+'</span></a></span>';
new_breadcrumb_html += '<span class="breadcrumb-gt"> ></span>';
} else {
new_breadcrumb_html += '<span class="breadcrumb-child">'+breadcrumb[i]+'</span>';
}
}
if(new_breadcrumb_html != '') {
$('#top-box > div.breadcrumb > div.breadcrumb-inner > span.breadcrumb-child:first').prop('outerHTML',new_breadcrumb_html);
}
}
remapArchiveCategoryé¢æ°
è¨äºä¸è¦§ã®åè¨äºã®ã«ãã´ãªã¼åãé
åã§æ¸¡ããã¾ããããã1ã¤ãã¤æä¸å±¤ã®ååã«ç½®ãæããå¦çã§ãã
function remapArchiveCategory(categories) {
for(var i=0;i<categories.length;i++) {
category_branch = categories[i].text.split('-');
categories[i].text=category_branch[category_branch.length-1];
}
}
category_archive.min.js
åé¢æ°ã®æ¦è¦ã¯ä»¥ä¸ã®éãã§ãããµã¤ããã¼ã®ã«ãã´ãªã¼ã¢ã¸ã¥ã¼ã«ã対象ã¨ããå¦çã§ããããâ¶ããæ¼ãããã¨ãã«ãé層åãããã«ãã´ãªã¼ã®åã«ãã´ãªã¼ã表示ãããããâ¼ããæ¼ãããã¨ãã«ã表示ããã¦ãåã«ãã´ãªã¼ãé表示ã«ããæ©è½ãå®ç¾ãã¦ãã¾ãã
- processArchiveé¢æ°ï¼ãµã¤ããã¼ã®ã«ãã´ãªã¼ã¢ã¸ã¥ã¼ã«å
ã«ãã
liã¿ã°
ãå帰çã«è¦ã¦ãããé層åãããã«ãã´ãªã¼ã®ééãå¶å¾¡ãã¦ãã
- toggleCategoryé¢æ°ï¼processArchiveé¢æ°ããå¼ã°ããé層åãããåã«ãã´ãªã¼ã®ééã®æ©è½ãæä¾ãã¦ãã
- readyé¢æ°ï¼processArchiveé¢æ°ãå¼ã³åºã
ã«ãã´ãªã¼ã®é層åã®ã½ã¼ã¹ã³ã¼ãã®è§£æçµæ
ã«ãã´ãªã¼ã®é層åã®ã½ã¼ã¹ã³ã¼ãããä¸éã確èªãããããã¯ãã注ç®è¨äºã®ã«ãã´ãªã¼ã«ã¤ãã¦ã¯èæ
®ãã¦ããªãããã§ãã
è¨äºã表示ããå ´åã¨ãã«ãã´ãªã¼ãã¯ãªãã¯ããã¨ãã®ã«ãã´ãªã¼å
ã®ä¸è¦§ã®å ´åã§ã2éãã®å¯¾å¿ãå¿
è¦ã«ãªãã¾ãã
ãããã«
æçµçãªçµè«ã¯ãåé ã«æ¸ããéããç¾ç¶ã¯ã注ç®è¨äºã®ã«ãã´ãªã¼ã«ã¤ãã¦ãé層åã«å¯¾å¿ããã®ã¯ä¸å¯è½ã§ãã
ä»åã¯ä»¥ä¸ã§ãï¼
æå¾ã¾ã§ãèªã¿ããã ãããããã¨ããããã¾ããã