patterns.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { __assign } from './_virtual/_tslib.js';
  2. import { toEventObject } from './utils.js';
  3. function toggle(onState, offState, eventType) {
  4. var _a, _b, _c;
  5. return _a = {}, _a[onState] = {
  6. on: (_b = {}, _b[eventType] = offState, _b)
  7. }, _a[offState] = {
  8. on: (_c = {}, _c[eventType] = onState, _c)
  9. }, _a;
  10. }
  11. var defaultSequencePatternOptions = {
  12. nextEvent: 'NEXT',
  13. prevEvent: 'PREV'
  14. };
  15. function sequence(items, options) {
  16. var resolvedOptions = __assign(__assign({}, defaultSequencePatternOptions), options);
  17. var states = {};
  18. var nextEventObject = resolvedOptions.nextEvent === undefined ? undefined : toEventObject(resolvedOptions.nextEvent);
  19. var prevEventObject = resolvedOptions.prevEvent === undefined ? undefined : toEventObject(resolvedOptions.prevEvent);
  20. items.forEach(function (item, i) {
  21. var state = {
  22. on: {}
  23. };
  24. if (i + 1 === items.length) {
  25. state.type = 'final';
  26. }
  27. if (nextEventObject && i + 1 < items.length) {
  28. state.on[nextEventObject.type] = items[i + 1];
  29. }
  30. if (prevEventObject && i > 0) {
  31. state.on[prevEventObject.type] = items[i - 1];
  32. }
  33. states[item] = state;
  34. });
  35. return {
  36. initial: items[0],
  37. states: states
  38. };
  39. }
  40. export { sequence, toggle };