選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

bs-stepper.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*!
  2. * bsStepper v1.7.0 (https://github.com/Johann-S/bs-stepper)
  3. * Copyright 2018 - 2019 Johann-S <johann.servoire@gmail.com>
  4. * Licensed under MIT (https://github.com/Johann-S/bs-stepper/blob/master/LICENSE)
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  8. typeof define === 'function' && define.amd ? define(factory) :
  9. (global = global || self, global.Stepper = factory());
  10. }(this, function () {
  11. 'use strict';
  12. function _extends() {
  13. _extends = Object.assign || function (target) {
  14. for (var i = 1; i < arguments.length; i++) {
  15. var source = arguments[i];
  16. for (var key in source) {
  17. if (Object.prototype.hasOwnProperty.call(source, key)) {
  18. target[key] = source[key];
  19. }
  20. }
  21. }
  22. return target;
  23. };
  24. return _extends.apply(this, arguments);
  25. }
  26. var matches = window.Element.prototype.matches;
  27. var closest = function closest(element, selector) {
  28. return element.closest(selector);
  29. };
  30. var WinEvent = function WinEvent(inType, params) {
  31. return new window.Event(inType, params);
  32. };
  33. var createCustomEvent = function createCustomEvent(eventName, params) {
  34. var cEvent = new window.CustomEvent(eventName, params);
  35. return cEvent;
  36. };
  37. /* istanbul ignore next */
  38. function polyfill() {
  39. if (!window.Element.prototype.matches) {
  40. matches = window.Element.prototype.msMatchesSelector || window.Element.prototype.webkitMatchesSelector;
  41. }
  42. if (!window.Element.prototype.closest) {
  43. closest = function closest(element, selector) {
  44. if (!document.documentElement.contains(element)) {
  45. return null;
  46. }
  47. do {
  48. if (matches.call(element, selector)) {
  49. return element;
  50. }
  51. element = element.parentElement || element.parentNode;
  52. } while (element !== null && element.nodeType === 1);
  53. return null;
  54. };
  55. }
  56. if (!window.Event || typeof window.Event !== 'function') {
  57. WinEvent = function WinEvent(inType, params) {
  58. params = params || {};
  59. var e = document.createEvent('Event');
  60. e.initEvent(inType, Boolean(params.bubbles), Boolean(params.cancelable));
  61. return e;
  62. };
  63. }
  64. if (typeof window.CustomEvent !== 'function') {
  65. var originPreventDefault = window.Event.prototype.preventDefault;
  66. createCustomEvent = function createCustomEvent(eventName, params) {
  67. var evt = document.createEvent('CustomEvent');
  68. params = params || {
  69. bubbles: false,
  70. cancelable: false,
  71. detail: null
  72. };
  73. evt.initCustomEvent(eventName, params.bubbles, params.cancelable, params.detail);
  74. evt.preventDefault = function () {
  75. if (!this.cancelable) {
  76. return;
  77. }
  78. originPreventDefault.call(this);
  79. Object.defineProperty(this, 'defaultPrevented', {
  80. get: function get() {
  81. return true;
  82. }
  83. });
  84. };
  85. return evt;
  86. };
  87. }
  88. }
  89. polyfill();
  90. var MILLISECONDS_MULTIPLIER = 1000;
  91. var ClassName = {
  92. ACTIVE: 'active',
  93. LINEAR: 'linear',
  94. BLOCK: 'dstepper-block',
  95. NONE: 'dstepper-none',
  96. FADE: 'fade',
  97. VERTICAL: 'vertical'
  98. };
  99. var transitionEndEvent = 'transitionend';
  100. var customProperty = 'bsStepper';
  101. var show = function show(stepperNode, indexStep, options, done) {
  102. var stepper = stepperNode[customProperty];
  103. if (stepper._steps[indexStep].classList.contains(ClassName.ACTIVE) || stepper._stepsContents[indexStep].classList.contains(ClassName.ACTIVE)) {
  104. return;
  105. }
  106. var showEvent = createCustomEvent('show.bs-stepper', {
  107. cancelable: true,
  108. detail: {
  109. from: stepper._currentIndex,
  110. to: indexStep,
  111. indexStep: indexStep
  112. }
  113. });
  114. stepperNode.dispatchEvent(showEvent);
  115. var activeStep = stepper._steps.filter(function (step) {
  116. return step.classList.contains(ClassName.ACTIVE);
  117. });
  118. var activeContent = stepper._stepsContents.filter(function (content) {
  119. return content.classList.contains(ClassName.ACTIVE);
  120. });
  121. if (showEvent.defaultPrevented) {
  122. return;
  123. }
  124. if (activeStep.length) {
  125. activeStep[0].classList.remove(ClassName.ACTIVE);
  126. }
  127. if (activeContent.length) {
  128. activeContent[0].classList.remove(ClassName.ACTIVE);
  129. if (!stepperNode.classList.contains(ClassName.VERTICAL) && !stepper.options.animation) {
  130. activeContent[0].classList.remove(ClassName.BLOCK);
  131. }
  132. }
  133. showStep(stepperNode, stepper._steps[indexStep], stepper._steps, options);
  134. showContent(stepperNode, stepper._stepsContents[indexStep], stepper._stepsContents, activeContent, done);
  135. };
  136. var showStep = function showStep(stepperNode, step, stepList, options) {
  137. stepList.forEach(function (step) {
  138. var trigger = step.querySelector(options.selectors.trigger);
  139. trigger.setAttribute('aria-selected', 'false'); // if stepper is in linear mode, set disabled attribute on the trigger
  140. if (stepperNode.classList.contains(ClassName.LINEAR)) {
  141. trigger.setAttribute('disabled', 'disabled');
  142. }
  143. });
  144. step.classList.add(ClassName.ACTIVE);
  145. var currentTrigger = step.querySelector(options.selectors.trigger);
  146. currentTrigger.setAttribute('aria-selected', 'true'); // if stepper is in linear mode, remove disabled attribute on current
  147. if (stepperNode.classList.contains(ClassName.LINEAR)) {
  148. currentTrigger.removeAttribute('disabled');
  149. }
  150. };
  151. var showContent = function showContent(stepperNode, content, contentList, activeContent, done) {
  152. var stepper = stepperNode[customProperty];
  153. var toIndex = contentList.indexOf(content);
  154. var shownEvent = createCustomEvent('shown.bs-stepper', {
  155. cancelable: true,
  156. detail: {
  157. from: stepper._currentIndex,
  158. to: toIndex,
  159. indexStep: toIndex
  160. }
  161. });
  162. function complete() {
  163. content.classList.add(ClassName.BLOCK);
  164. content.removeEventListener(transitionEndEvent, complete);
  165. stepperNode.dispatchEvent(shownEvent);
  166. done();
  167. }
  168. if (content.classList.contains(ClassName.FADE)) {
  169. content.classList.remove(ClassName.NONE);
  170. var duration = getTransitionDurationFromElement(content);
  171. content.addEventListener(transitionEndEvent, complete);
  172. if (activeContent.length) {
  173. activeContent[0].classList.add(ClassName.NONE);
  174. }
  175. content.classList.add(ClassName.ACTIVE);
  176. emulateTransitionEnd(content, duration);
  177. } else {
  178. content.classList.add(ClassName.ACTIVE);
  179. content.classList.add(ClassName.BLOCK);
  180. stepperNode.dispatchEvent(shownEvent);
  181. done();
  182. }
  183. };
  184. var getTransitionDurationFromElement = function getTransitionDurationFromElement(element) {
  185. if (!element) {
  186. return 0;
  187. } // Get transition-duration of the element
  188. var transitionDuration = window.getComputedStyle(element).transitionDuration;
  189. var floatTransitionDuration = parseFloat(transitionDuration); // Return 0 if element or transition duration is not found
  190. if (!floatTransitionDuration) {
  191. return 0;
  192. } // If multiple durations are defined, take the first
  193. transitionDuration = transitionDuration.split(',')[0];
  194. return parseFloat(transitionDuration) * MILLISECONDS_MULTIPLIER;
  195. };
  196. var emulateTransitionEnd = function emulateTransitionEnd(element, duration) {
  197. var called = false;
  198. var durationPadding = 5;
  199. var emulatedDuration = duration + durationPadding;
  200. function listener() {
  201. called = true;
  202. element.removeEventListener(transitionEndEvent, listener);
  203. }
  204. element.addEventListener(transitionEndEvent, listener);
  205. window.setTimeout(function () {
  206. if (!called) {
  207. element.dispatchEvent(WinEvent(transitionEndEvent));
  208. }
  209. element.removeEventListener(transitionEndEvent, listener);
  210. }, emulatedDuration);
  211. };
  212. var detectAnimation = function detectAnimation(contentList, options) {
  213. if (options.animation) {
  214. contentList.forEach(function (content) {
  215. content.classList.add(ClassName.FADE);
  216. content.classList.add(ClassName.NONE);
  217. });
  218. }
  219. };
  220. var buildClickStepLinearListener = function buildClickStepLinearListener() {
  221. return function clickStepLinearListener(event) {
  222. event.preventDefault();
  223. };
  224. };
  225. var buildClickStepNonLinearListener = function buildClickStepNonLinearListener(options) {
  226. return function clickStepNonLinearListener(event) {
  227. event.preventDefault();
  228. var step = closest(event.target, options.selectors.steps);
  229. var stepperNode = closest(step, options.selectors.stepper);
  230. var stepper = stepperNode[customProperty];
  231. var stepIndex = stepper._steps.indexOf(step);
  232. show(stepperNode, stepIndex, options, function () {
  233. stepper._currentIndex = stepIndex;
  234. });
  235. };
  236. };
  237. var DEFAULT_OPTIONS = {
  238. linear: true,
  239. animation: false,
  240. selectors: {
  241. steps: '.step',
  242. trigger: '.step-trigger',
  243. stepper: '.bs-stepper'
  244. }
  245. };
  246. var Stepper =
  247. /*#__PURE__*/
  248. function () {
  249. function Stepper(element, _options) {
  250. var _this = this;
  251. if (_options === void 0) {
  252. _options = {};
  253. }
  254. this._element = element;
  255. this._currentIndex = 0;
  256. this._stepsContents = [];
  257. this.options = _extends({}, DEFAULT_OPTIONS, {}, _options);
  258. this.options.selectors = _extends({}, DEFAULT_OPTIONS.selectors, {}, this.options.selectors);
  259. if (this.options.linear) {
  260. this._element.classList.add(ClassName.LINEAR);
  261. }
  262. this._steps = [].slice.call(this._element.querySelectorAll(this.options.selectors.steps));
  263. this._steps.filter(function (step) {
  264. return step.hasAttribute('data-target');
  265. }).forEach(function (step) {
  266. _this._stepsContents.push(_this._element.querySelector(step.getAttribute('data-target')));
  267. });
  268. detectAnimation(this._stepsContents, this.options);
  269. this._setLinkListeners();
  270. Object.defineProperty(this._element, customProperty, {
  271. value: this,
  272. writable: true
  273. });
  274. if (this._steps.length) {
  275. show(this._element, this._currentIndex, this.options, function () {
  276. });
  277. }
  278. } // Private
  279. var _proto = Stepper.prototype;
  280. _proto._setLinkListeners = function _setLinkListeners() {
  281. var _this2 = this;
  282. this._steps.forEach(function (step) {
  283. var trigger = step.querySelector(_this2.options.selectors.trigger);
  284. if (_this2.options.linear) {
  285. _this2._clickStepLinearListener = buildClickStepLinearListener(_this2.options);
  286. trigger.addEventListener('click', _this2._clickStepLinearListener);
  287. } else {
  288. _this2._clickStepNonLinearListener = buildClickStepNonLinearListener(_this2.options);
  289. trigger.addEventListener('click', _this2._clickStepNonLinearListener);
  290. }
  291. });
  292. } // Public
  293. ;
  294. _proto.next = function next() {
  295. var _this3 = this;
  296. var nextStep = this._currentIndex + 1 <= this._steps.length - 1 ? this._currentIndex + 1 : this._steps.length - 1;
  297. show(this._element, nextStep, this.options, function () {
  298. _this3._currentIndex = nextStep;
  299. });
  300. };
  301. _proto.previous = function previous() {
  302. var _this4 = this;
  303. var previousStep = this._currentIndex - 1 >= 0 ? this._currentIndex - 1 : 0;
  304. show(this._element, previousStep, this.options, function () {
  305. _this4._currentIndex = previousStep;
  306. });
  307. };
  308. _proto.to = function to(stepNumber) {
  309. var _this5 = this;
  310. var tempIndex = stepNumber - 1;
  311. var nextStep = tempIndex >= 0 && tempIndex < this._steps.length ? tempIndex : 0;
  312. show(this._element, nextStep, this.options, function () {
  313. _this5._currentIndex = nextStep;
  314. });
  315. };
  316. _proto.reset = function reset() {
  317. var _this6 = this;
  318. show(this._element, 0, this.options, function () {
  319. _this6._currentIndex = 0;
  320. });
  321. };
  322. _proto.destroy = function destroy() {
  323. var _this7 = this;
  324. this._steps.forEach(function (step) {
  325. var trigger = step.querySelector(_this7.options.selectors.trigger);
  326. if (_this7.options.linear) {
  327. trigger.removeEventListener('click', _this7._clickStepLinearListener);
  328. } else {
  329. trigger.removeEventListener('click', _this7._clickStepNonLinearListener);
  330. }
  331. });
  332. this._element[customProperty] = undefined;
  333. this._element = undefined;
  334. this._currentIndex = undefined;
  335. this._steps = undefined;
  336. this._stepsContents = undefined;
  337. this._clickStepLinearListener = undefined;
  338. this._clickStepNonLinearListener = undefined;
  339. };
  340. return Stepper;
  341. }();
  342. return Stepper;
  343. }));
  344. //# sourceMappingURL=bs-stepper.js.map