UNPKG

5.18 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.symbol.js");
4
5require("core-js/modules/es.symbol.description.js");
6
7require("core-js/modules/es.symbol.iterator.js");
8
9require("core-js/modules/es.array.iterator.js");
10
11require("core-js/modules/es.string.iterator.js");
12
13require("core-js/modules/web.dom-collections.iterator.js");
14
15require("core-js/modules/es.array.from.js");
16
17require("core-js/modules/es.array.slice.js");
18
19require("core-js/modules/es.function.name.js");
20
21Object.defineProperty(exports, "__esModule", {
22 value: true
23});
24exports.withLinks = exports.navigate = exports.linkTo = exports.hrefTo = void 0;
25
26require("core-js/modules/es.object.to-string.js");
27
28require("core-js/modules/es.promise.js");
29
30require("core-js/modules/es.regexp.exec.js");
31
32require("core-js/modules/es.string.search.js");
33
34require("core-js/modules/es.array.concat.js");
35
36require("core-js/modules/es.string.split.js");
37
38require("core-js/modules/es.object.assign.js");
39
40require("core-js/modules/es.string.match.js");
41
42var _global = _interopRequireDefault(require("global"));
43
44var _qs = _interopRequireDefault(require("qs"));
45
46var _addons = require("@storybook/addons");
47
48var _coreEvents = require("@storybook/core-events");
49
50var _csf = require("@storybook/csf");
51
52var _constants = require("./constants");
53
54function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
55
56function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
57
58function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
59
60function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
61
62function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
63
64function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
65
66function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
67
68var document = _global.default.document,
69 HTMLElement = _global.default.HTMLElement;
70
71var navigate = function navigate(params) {
72 return _addons.addons.getChannel().emit(_coreEvents.SELECT_STORY, params);
73};
74
75exports.navigate = navigate;
76
77var hrefTo = function hrefTo(title, name) {
78 return new Promise(function (resolve) {
79 var location = document.location;
80
81 var query = _qs.default.parse(location.search, {
82 ignoreQueryPrefix: true
83 });
84
85 var existingId = [].concat(query.id)[0];
86 var titleToLink = title || existingId.split('--', 2)[0];
87 var id = (0, _csf.toId)(titleToLink, name);
88 var url = "".concat(location.origin + location.pathname, "?").concat(_qs.default.stringify(Object.assign({}, query, {
89 id: id
90 }), {
91 encode: false
92 }));
93 resolve(url);
94 });
95};
96
97exports.hrefTo = hrefTo;
98
99var valueOrCall = function valueOrCall(args) {
100 return function (value) {
101 return typeof value === 'function' ? value.apply(void 0, _toConsumableArray(args)) : value;
102 };
103};
104
105var linkTo = function linkTo(idOrTitle, nameInput) {
106 return function () {
107 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
108 args[_key] = arguments[_key];
109 }
110
111 var resolver = valueOrCall(args);
112 var title = resolver(idOrTitle);
113 var name = resolver(nameInput);
114
115 if (title !== null && title !== void 0 && title.match(/--/) && !name) {
116 navigate({
117 storyId: title
118 });
119 } else {
120 navigate({
121 kind: title,
122 story: name
123 });
124 }
125 };
126};
127
128exports.linkTo = linkTo;
129
130var linksListener = function linksListener(e) {
131 var target = e.target;
132
133 if (!(target instanceof HTMLElement)) {
134 return;
135 }
136
137 var element = target;
138 var _element$dataset = element.dataset,
139 kind = _element$dataset.sbKind,
140 story = _element$dataset.sbStory;
141
142 if (kind || story) {
143 e.preventDefault();
144 navigate({
145 kind: kind,
146 story: story
147 });
148 }
149};
150
151var hasListener = false;
152
153var on = function on() {
154 if (!hasListener) {
155 hasListener = true;
156 document.addEventListener('click', linksListener);
157 }
158};
159
160var off = function off() {
161 if (hasListener) {
162 hasListener = false;
163 document.removeEventListener('click', linksListener);
164 }
165};
166
167var withLinks = (0, _addons.makeDecorator)({
168 name: 'withLinks',
169 parameterName: _constants.PARAM_KEY,
170 wrapper: function wrapper(getStory, context) {
171 on();
172
173 _addons.addons.getChannel().once(_coreEvents.STORY_CHANGED, off);
174
175 return getStory(context);
176 }
177});
178exports.withLinks = withLinks;
\No newline at end of file