UNPKG

5.5 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var Vue = _interopDefault(require('vue'));
8
9var canNavigate = function (event) {
10 return (!event.defaultPrevented &&
11 (event.button !== undefined && event.button === 0) &&
12 !(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey));
13};
14var Link = {
15 name: "curi-link",
16 props: ["to", "params", "hash", "query", "state", "click"],
17 computed: {
18 location: function () {
19 var pathname = this.to
20 ? this.$router.route.pathname(this.to, this.params)
21 : this.$curi.response.location.pathname;
22 return {
23 hash: this.hash,
24 query: this.query,
25 state: this.state,
26 pathname: pathname
27 };
28 },
29 href: function () {
30 return this.$router.history.toHref(this.location);
31 }
32 },
33 data: function () {
34 return {
35 navigating: false
36 };
37 },
38 methods: {
39 clickHandler: function (event) {
40 var _this = this;
41 if (this.click) {
42 this.click(event);
43 }
44 if (canNavigate(event)) {
45 event.preventDefault();
46 var cancelled = void 0, finished = void 0;
47 if (this.$scopedSlots.default) {
48 cancelled = finished = function () {
49 _this.navigating = false;
50 };
51 this.navigating = true;
52 }
53 this.$router.navigate({
54 name: this.to,
55 params: this.params,
56 hash: this.hash,
57 query: this.query,
58 state: this.state,
59 cancelled: cancelled,
60 finished: finished
61 });
62 }
63 }
64 },
65 render: function (h) {
66 return h("a", {
67 attrs: { href: this.href },
68 on: { click: this.clickHandler }
69 }, this.$scopedSlots.default
70 ? this.$scopedSlots.default({
71 navigating: this.navigating
72 })
73 : this.$slots.default);
74 }
75};
76
77var Block = {
78 name: 'curi-block',
79 props: {
80 active: {
81 type: Boolean,
82 default: true
83 },
84 confirm: {
85 type: Function,
86 required: true
87 }
88 },
89 methods: {
90 on: function () {
91 this.$router.history.confirmWith(this.confirm);
92 },
93 off: function () {
94 this.$router.history.removeConfirmation();
95 },
96 update: function () {
97 this.off();
98 if (this.active) {
99 this.on();
100 }
101 }
102 },
103 beforeMount: function () {
104 if (this.active) {
105 this.on();
106 }
107 },
108 watch: {
109 active: function () {
110 this.update();
111 },
112 confirm: function () {
113 this.update();
114 }
115 },
116 beforeDestroy: function () {
117 this.off();
118 },
119 render: function (h) {
120 return null;
121 }
122};
123
124function focus(el, options) {
125 var _a = options.preserve, preserve = _a === void 0 ? false : _a, _b = options.preventScroll, preventScroll = _b === void 0 ? false : _b;
126 if (preserve && el.contains(document.activeElement)) {
127 return;
128 }
129 setTimeout(function () {
130 // @ts-ignore
131 el.focus({ preventScroll: preventScroll });
132 });
133}
134var focusDirection = {
135 inserted: function (el, binding) {
136 if (process.env.NODE_ENV !== "production") {
137 if (!el.hasAttribute("tabIndex") && el.tabIndex === -1) {
138 console.warn('The element that is passed the "v-curi-focus" directive must have a "tabIndex" prop or ' +
139 "be focusable by default in order to be focused. " +
140 "Otherwise, the document's <body> will be focused instead.");
141 }
142 }
143 focus(el, binding.value);
144 },
145 update: function (el, binding) {
146 if (binding.value.key !== binding.oldValue.key) {
147 focus(el, binding.value);
148 }
149 }
150};
151
152var CuriPlugin = {
153 install: function (_Vue, options) {
154 _Vue.component(Link.name, Link);
155 _Vue.component(Block.name, Block);
156 _Vue.directive("curi-focus", focusDirection);
157 // create a reactive object so that components will receive
158 // the new response/navigation when a new response is emitted
159 var reactive = new Vue({
160 data: { response: null, navigation: null }
161 });
162 options.router.observe(function (_a) {
163 var response = _a.response, navigation = _a.navigation;
164 reactive.response = response;
165 reactive.navigation = navigation;
166 });
167 _Vue.mixin({
168 beforeCreate: function () {
169 this.$curi = reactive;
170 }
171 });
172 Object.defineProperty(_Vue.prototype, "$router", {
173 get: function () {
174 return options.router;
175 }
176 });
177 }
178};
179
180exports.CuriPlugin = CuriPlugin;