1 | "use strict";
|
2 |
|
3 | require("core-js/modules/es.symbol.js");
|
4 |
|
5 | require("core-js/modules/es.symbol.description.js");
|
6 |
|
7 | require("core-js/modules/es.symbol.iterator.js");
|
8 |
|
9 | require("core-js/modules/es.array.iterator.js");
|
10 |
|
11 | require("core-js/modules/es.string.iterator.js");
|
12 |
|
13 | require("core-js/modules/web.dom-collections.iterator.js");
|
14 |
|
15 | require("core-js/modules/es.array.from.js");
|
16 |
|
17 | require("core-js/modules/es.array.slice.js");
|
18 |
|
19 | require("core-js/modules/es.function.name.js");
|
20 |
|
21 | Object.defineProperty(exports, "__esModule", {
|
22 | value: true
|
23 | });
|
24 | exports.withLinks = exports.navigate = exports.linkTo = exports.hrefTo = void 0;
|
25 |
|
26 | require("core-js/modules/es.object.to-string.js");
|
27 |
|
28 | require("core-js/modules/es.promise.js");
|
29 |
|
30 | require("core-js/modules/es.regexp.exec.js");
|
31 |
|
32 | require("core-js/modules/es.string.search.js");
|
33 |
|
34 | require("core-js/modules/es.array.concat.js");
|
35 |
|
36 | require("core-js/modules/es.string.split.js");
|
37 |
|
38 | require("core-js/modules/es.object.assign.js");
|
39 |
|
40 | require("core-js/modules/es.string.match.js");
|
41 |
|
42 | var _global = _interopRequireDefault(require("global"));
|
43 |
|
44 | var _qs = _interopRequireDefault(require("qs"));
|
45 |
|
46 | var _addons = require("@storybook/addons");
|
47 |
|
48 | var _coreEvents = require("@storybook/core-events");
|
49 |
|
50 | var _csf = require("@storybook/csf");
|
51 |
|
52 | var _constants = require("./constants");
|
53 |
|
54 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
55 |
|
56 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
57 |
|
58 | function _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 |
|
60 | function _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 |
|
62 | function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
63 |
|
64 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
65 |
|
66 | function _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 |
|
68 | var document = _global.default.document,
|
69 | HTMLElement = _global.default.HTMLElement;
|
70 |
|
71 | var navigate = function navigate(params) {
|
72 | return _addons.addons.getChannel().emit(_coreEvents.SELECT_STORY, params);
|
73 | };
|
74 |
|
75 | exports.navigate = navigate;
|
76 |
|
77 | var 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 |
|
97 | exports.hrefTo = hrefTo;
|
98 |
|
99 | var valueOrCall = function valueOrCall(args) {
|
100 | return function (value) {
|
101 | return typeof value === 'function' ? value.apply(void 0, _toConsumableArray(args)) : value;
|
102 | };
|
103 | };
|
104 |
|
105 | var 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 |
|
128 | exports.linkTo = linkTo;
|
129 |
|
130 | var 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 |
|
151 | var hasListener = false;
|
152 |
|
153 | var on = function on() {
|
154 | if (!hasListener) {
|
155 | hasListener = true;
|
156 | document.addEventListener('click', linksListener);
|
157 | }
|
158 | };
|
159 |
|
160 | var off = function off() {
|
161 | if (hasListener) {
|
162 | hasListener = false;
|
163 | document.removeEventListener('click', linksListener);
|
164 | }
|
165 | };
|
166 |
|
167 | var 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 | });
|
178 | exports.withLinks = withLinks; |
\ | No newline at end of file |