"use strict"; var FF; (function (FF) { var Top = (function () { function Top() { var _this = this; window.addEventListener("scroll", function () { return _this.onScroll(); }, !document.documentMode ? { passive: true } : false); window.addEventListener("blur", function () { return Top.pause = true; }); window.addEventListener("focus", function () { return Top.pause = false; }); } Top.prototype.onScroll = function () { if (!Top.panels.length) return; var v = document.documentElement.scrollTop || document.body.scrollTop; var windowCenter = v + (window.innerHeight / 2); var near; var nearV = -1; for (var _i = 0, _a = Top.panels; _i < _a.length; _i++) { var panel = _a[_i]; var offsetCenter = panel.self.$.offsetTop + (panel.self.$.offsetHeight / 2); var n = Math.abs(offsetCenter - windowCenter); if (nearV < 0 || n < nearV) { near = panel; nearV = n; } } for (var _b = 0, _c = Top.panels; _b < _c.length; _b++) { var panel = _c[_b]; panel.activate(panel == near); } }; Top.panels = []; Top.pause = false; return Top; }()); FF.Top = Top; var TopPanel = (function () { function TopPanel(self) { this.weitList = []; this.current = 0; this.imgs = []; this.time = 4.5; this.isActive = false; this.self = new FF.AttrNode(self, "data-js-panel", false); this.init(); Top.panels.push(this); window.dispatchEvent(new CustomEvent("scroll")); } TopPanel.prototype.init = function () { var _this = this; var str; var opt; var $body = this.self.$.querySelector(".panel__body"); try { var script = this.self.$.querySelector("script"); str = script.innerHTML; FF.Utils.remove(script); } catch (e) { alert("エラー:scriptタグが見つかりませんでした"); return; } try { opt = JSON.parse(str); } catch (e) { alert("JSONパースエラー:書式を確認してください" + str); return; } if (!TopPanel.temp) { TopPanel.temp = document.querySelector("[data-js=\"panel-template\"]"); TopPanel.elementTemp = document.querySelector("[data-js=\"panel-element-template\"]"); } try { $body.innerHTML = TopPanel.temp.innerHTML .replace("{{ title }}", opt.title) .replace("{{ labelledby }}", opt.labelledby) .replace("{{ subtitle }}", opt.subtitle); if (!opt.subtitle) { FF.Utils.remove($body.querySelector(".panel__subtitle")); } } catch (e) { alert("JSONエラー:title/labelledbyの値に問題があります。" + str); } var $list = $body.querySelector("[data-js=\"body\"]"); try { var _loop_1 = function (ele) { TopPanel.bord.innerHTML = TopPanel.elementTemp.innerHTML; var element = new FF.AttrNode(TopPanel.bord.firstElementChild, "data-js-element", false); element.v = "hidden"; this_1.weitList.push(function () { var $img = element.$.querySelector("img"); $img.onload = function () { var m = _this.weitList.shift(); if (m) m(); }; element.$.querySelector("source").srcset = ele.sp; $img.src = ele.pc; }); element.$.querySelector("img").alt = ele.alt; $list.appendChild(element.$); this_1.imgs.push(element); }; var this_1 = this; for (var _i = 0, _a = opt.imgs; _i < _a.length; _i++) { var ele = _a[_i]; _loop_1(ele); } this.weitList.shift()(); this.imgs[0].v = "current"; } catch (e) { alert("データエラー:imgsの記述に問題があります"); } }; TopPanel.prototype.activate = function (isActive) { if (isActive) { this.self.v = "current"; this.play(); } else { this.self.v = this.self.v.replace("current", ""); this.stop(); } }; TopPanel.prototype.play = function () { if (this.isActive) return; this.isActive = true; this.imgs[this.current].v = "current|animation"; this.loop(60 * (this.time - 2.5)); }; TopPanel.prototype.loop = function (count) { var _this = this; if (!this.isActive) return; if (!Top.pause && count-- < 0) { count = 60 * this.time; this.change(); } requestAnimationFrame(function () { return _this.loop(count); }); }; TopPanel.prototype.change = function () { var before = this.imgs[this.current]; before.$.addEventListener("transitionend", transitionend); before.v = "hidden|animation"; this.current++; if (this.current >= this.imgs.length) this.current = 0; this.imgs[this.current].v = "current|animation"; function transitionend() { before.v = "hidden"; before.$.removeEventListener("transitionend", transitionend); } }; TopPanel.prototype.stop = function () { this.isActive = false; this.imgs[this.current].v = "current"; }; TopPanel.bord = document.createElement("div"); return TopPanel; }()); FF.TopPanel = TopPanel; })(FF || (FF = {})); new FF.Top();