(function($) { "use strict"; var MAX_ITEMS = 5; /* 表示順はカテゴリー一覧と揃える。 ただし法人FAQはインポート時に SortOrder が空のまま入っているため、 空のものは後ろへ回して公開日の新しい順にする。 */ function compareItems(a, b) { if (a.sortOrder && b.sortOrder) { if (a.sortOrder < b.sortOrder) { return -1; } if (a.sortOrder > b.sortOrder) { return 1; } } else if (a.sortOrder) { return -1; } else if (b.sortOrder) { return 1; } if (a.published > b.published) { return -1; } if (a.published < b.published) { return 1; } return 0; } function buildItem(record) { var $link = $("", { "class": "p-faqRelated__link", href: record.url }); $link.append($("", { "class": "p-faqRelated__mark", "aria-hidden": "true" })); $link.append($("", { "class": "p-faqRelated__title", text: record.title })); return $("
  • ", { "class": "p-faqRelated__item" }).append($link); } function initFaqRelated() { var $section = $(".js-faq-related"); if (!$section.length) { return; } var dataUrl = $.trim($section.attr("data-related-data") || ""); var currentId = $.trim($section.attr("data-entry-id") || ""); var $list = $section.find(".p-faqRelated__list"); if (!dataUrl || !currentId || !$list.length) { return; } $.ajax({ url: dataUrl, dataType: "json", cache: false }).done(function(data) { var items = $.isArray(data) ? data : []; var currentKey = ""; var siblings = []; $.each(items, function(index, item) { if (item && String(item.id) === currentId) { currentKey = $.trim(String(item.categoryKey || "")); return false; } return true; }); if (!currentKey) { return; } $.each(items, function(index, item) { if (!item || String(item.id) === currentId) { return true; } if ($.trim(String(item.categoryKey || "")) !== currentKey) { return true; } siblings.push({ title: String(item.title || ""), url: String(item.url || ""), sortOrder: $.trim(String(item.sortOrder || "")), published: String(item.published || "") }); return true; }); if (!siblings.length) { return; } siblings.sort(compareItems); $.each(siblings.slice(0, MAX_ITEMS), function(index, record) { $list.append(buildItem(record)); }); $section.removeAttr("hidden"); }); } $(initFaqRelated); })(jQuery);