|
| 1 | +--- |
| 2 | +--- |
| 3 | +<!DOCTYPE html> |
| 4 | +<html> |
| 5 | + <head> |
| 6 | + {% include head.html %} |
| 7 | + {% include title.html %} |
| 8 | + <link rel="stylesheet" href="{{ site.baseurl }}/css/tag.css"> |
| 9 | + </head> |
| 10 | + <body class="tag"> |
| 11 | + {% include header.html %} |
| 12 | + |
| 13 | + <main class="tag main" role="main"> |
| 14 | + <div class="tag__inner"> |
| 15 | + |
| 16 | + <h1 class="tag__title">タグを含むページの一覧</h1> |
| 17 | + |
| 18 | + <dl class="tag__tag-state tag-state"> |
| 19 | + <dt class="tag-state__title">指定されているタグ</dt> |
| 20 | + <dd class="tag-state__list"> |
| 21 | + <ul id="selected-tags" class="tag-state__tags tags"></ul> |
| 22 | + </dd> |
| 23 | + </dl> |
| 24 | + |
| 25 | + <dl class="tag__tag-state tag-state"> |
| 26 | + <dt class="tag-state__title">選択できるタグ</dt> |
| 27 | + <dd class="tag-state__list"> |
| 28 | + <ul id="unselect-tags" class="tag-state__tags tags"></ul> |
| 29 | + </dd> |
| 30 | + </dl> |
| 31 | + |
| 32 | + <nav> |
| 33 | + <ul id="articles" class="tag__articles articles"></ul> |
| 34 | + </nav> |
| 35 | + |
| 36 | + </div> |
| 37 | + </main> |
| 38 | + |
| 39 | + {% include footer.html %} |
| 40 | + |
| 41 | + <script id="html-find-tags" type="text/html"> |
| 42 | + <li class="tags__tag tag"> |
| 43 | + <a class="tag__link" href="<%- obj.uri %>"> |
| 44 | + <span class="tag__text">#<%- obj.name %></span><!-- |
| 45 | + --></a> |
| 46 | + </li> |
| 47 | + </script> |
| 48 | + |
| 49 | + <script id="html-list-item" type="text/html"> |
| 50 | + <li class="articles__article article"> |
| 51 | + <a class="article__link" href="<%- obj.uri %>"> |
| 52 | + <h1 class="article__title"><%- obj.title %></h1> |
| 53 | + <time class="article__pubdate"><%- obj.date %></time> |
| 54 | + <% if (obj.author) { %> |
| 55 | + <span class="article__author"><%- obj.author %></span> |
| 56 | + <% } %> |
| 57 | + <ul class="article__tags tags"> |
| 58 | + <% _.forEach(obj.tags, function(tag) { %> |
| 59 | + <li class="tags__tag tag"> |
| 60 | + <span class="tag__text">#<%- tag %></span> |
| 61 | + </li> |
| 62 | + <% }); %> |
| 63 | + </ul> |
| 64 | + </a> |
| 65 | + </li> |
| 66 | + </script> |
| 67 | + |
| 68 | + <script id="json-site-tags" type="application/json"> |
| 69 | + { |
| 70 | + "tags": [{% assign tags = site.tags | sort %} |
| 71 | + {% for tag in tags %}{% assign name = tag[0] %} |
| 72 | + "{{ name }}"{% unless forloop.last %},{% endunless %} |
| 73 | + {% endfor %} |
| 74 | + ] |
| 75 | + } |
| 76 | + </script> |
| 77 | + |
| 78 | + <script id="json-post-data" type="application/json"> |
| 79 | + { |
| 80 | + "articles": [ |
| 81 | + {% for post in site.posts %} |
| 82 | + { |
| 83 | + "uri": "{{ site.baseurl }}{{ post.url | remove_first:'index.html' }}", |
| 84 | + "title": "{{ post.title | escape }}", |
| 85 | + "date": "{{ post.date | split:' ' | first }}", |
| 86 | + "author": "{{ post.author }}", |
| 87 | + "tags": [{% assign tags = post.tags | sort %} |
| 88 | + {% for tag in tags %} |
| 89 | + "{{ tag }}"{% unless forloop.last %},{% endunless %} |
| 90 | + {% endfor %} |
| 91 | + ] |
| 92 | + }{% unless forloop.last %},{% endunless %} |
| 93 | + {% endfor %} |
| 94 | + ] |
| 95 | + } |
| 96 | + </script> |
| 97 | + |
| 98 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script> |
| 99 | + <script> |
| 100 | + (function(){ |
| 101 | + 'use strict'; |
| 102 | + |
| 103 | + //---------------------------------------------------------------------- |
| 104 | + |
| 105 | + /** |
| 106 | + * parse query string |
| 107 | + * |
| 108 | + * @param {String} text |
| 109 | + * @return {String[]} |
| 110 | + */ |
| 111 | + function parseQueryString(text) { |
| 112 | + var result = {}, |
| 113 | + params; |
| 114 | + |
| 115 | + if (/^\?/.test(text)) { |
| 116 | + params = text.slice(1).split('&'); |
| 117 | + } else { |
| 118 | + params = text.split('&'); |
| 119 | + } |
| 120 | + |
| 121 | + _.forEach(params, function(string) { |
| 122 | + var key, value; |
| 123 | + |
| 124 | + /=/.exec(string); |
| 125 | + |
| 126 | + key = RegExp.leftContext; |
| 127 | + value = RegExp.rightContext; |
| 128 | + |
| 129 | + result[key] || (result[key] = []); |
| 130 | + result[key].push( |
| 131 | + decodeURIComponent(value) || null |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + return result; |
| 136 | + } |
| 137 | + |
| 138 | + var queries = parseQueryString(location.search), |
| 139 | + queryTags = _.map(queries.q, _.toLower); |
| 140 | + |
| 141 | + //---------------------------------------------------------------------- |
| 142 | + |
| 143 | + (function(){ |
| 144 | + var compile, data; |
| 145 | + |
| 146 | + compile = _.template( |
| 147 | + document.getElementById('html-find-tags').innerHTML |
| 148 | + ); |
| 149 | + data = JSON.parse( |
| 150 | + document.getElementById('json-site-tags').innerHTML |
| 151 | + ); |
| 152 | + |
| 153 | + var selectedTags, unselectTags; |
| 154 | + |
| 155 | + selectedTags = _.pullAll(data.tags, queryTags); |
| 156 | + unselectTags = _.pullAll(queryTags, data.tags); |
| 157 | + |
| 158 | + /** |
| 159 | + * return querystring |
| 160 | + * |
| 161 | + * @param {String[]} |
| 162 | + * @param {String} |
| 163 | + * @return {String} |
| 164 | + */ |
| 165 | + function removeTag(sourceTags, removeTag) { |
| 166 | + var result = _.pull(_.clone(sourceTags), removeTag); |
| 167 | + |
| 168 | + return (result.length > 0) ? '?q=' + result.join('&q=') : ''; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * return querystring |
| 173 | + * |
| 174 | + * @param {String[]} |
| 175 | + * @param {String} |
| 176 | + * @return {String} |
| 177 | + */ |
| 178 | + function appendTag(sourceTags, appendTag) { |
| 179 | + var result = _.clone(sourceTags).concat([appendTag]); |
| 180 | + |
| 181 | + return (result.length > 0) ? '?q=' + result.join('&q=') : ''; |
| 182 | + } |
| 183 | + |
| 184 | + var selectedTagsElement = document.getElementById('selected-tags'), |
| 185 | + unselectTagsElement = document.getElementById('unselect-tags'); |
| 186 | + |
| 187 | + selectedTagsElement.innerHTML = |
| 188 | + _(unselectTags) |
| 189 | + .map(function(tag) { |
| 190 | + return { |
| 191 | + name: tag, |
| 192 | + uri: location.pathname + removeTag(queryTags, tag) |
| 193 | + }; |
| 194 | + }) |
| 195 | + .map(compile) |
| 196 | + .value() |
| 197 | + .join(''); |
| 198 | + |
| 199 | + unselectTagsElement.innerHTML = |
| 200 | + _(selectedTags) |
| 201 | + .map(function(tag) { |
| 202 | + return { |
| 203 | + name: tag, |
| 204 | + uri: location.pathname + appendTag(queryTags, tag) |
| 205 | + }; |
| 206 | + }) |
| 207 | + .map(compile) |
| 208 | + .value() |
| 209 | + .join(''); |
| 210 | + }()); |
| 211 | + |
| 212 | + //---------------------------------------------------------------------- |
| 213 | + |
| 214 | + (function(){ |
| 215 | + var compile, data, articles; |
| 216 | + |
| 217 | + compile = _.template( |
| 218 | + document.getElementById('html-list-item').innerHTML |
| 219 | + ); |
| 220 | + data = JSON.parse( |
| 221 | + document.getElementById('json-post-data').innerHTML |
| 222 | + ); |
| 223 | + |
| 224 | + articles = _.filter(data.articles, function(post) { |
| 225 | + var iteratee = _.partial(_.includes, post.tags), |
| 226 | + includes = _.map(queryTags, iteratee); |
| 227 | + |
| 228 | + return _.every(includes, function(value) { |
| 229 | + return (value === true); |
| 230 | + }); |
| 231 | + }); |
| 232 | + |
| 233 | + var articlesElement = document.getElementById('articles'); |
| 234 | + |
| 235 | + articlesElement.innerHTML = _.map(articles, compile).join(''); |
| 236 | + }()); |
| 237 | + }()); |
| 238 | + </script> |
| 239 | + </body> |
| 240 | +</html> |
0 commit comments