@telia-ace/knowledge-widget-plugins 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ import { Container } from '@webprovisions/platform';
2
+
3
+ export type FavoritePlatform = {
4
+ add: (guideId: string) => Promise<string[]>;
5
+ remove: (guideId: string) => Promise<string[]>;
6
+ clear: () => Promise<string[]>;
7
+ get: () => string[];
8
+ };
9
+ declare const createFavoritePlatform: (container: Container) => FavoritePlatform;
10
+ export default createFavoritePlatform;
@@ -0,0 +1,10 @@
1
+ import { Plugin } from '../../../core/src/index.ts';
2
+ import { Container } from '@webprovisions/platform';
3
+ import { FavoritePlatform } from './favorite-platform';
4
+
5
+ export type FavoritePluginSettings = {};
6
+ export default class FavoritePlugin extends Plugin {
7
+ settings: FavoritePluginSettings;
8
+ handler: FavoritePlatform;
9
+ constructor(container: Container, settings: FavoritePluginSettings);
10
+ }
@@ -0,0 +1,3 @@
1
+ import { default as FavoritePlugin } from './favorite';
2
+
3
+ export default FavoritePlugin;
@@ -0,0 +1,9 @@
1
+ import { Container } from '@webprovisions/platform';
2
+
3
+ export declare const addFavoriteToStorage: (guideId: string, container: Container) => Promise<string[]>;
4
+ export declare const removeFavoriteFromStorage: (guideId: string, container: Container) => Promise<string[]>;
5
+ export declare const readFavoritesFromStorage: (container: Container) => Promise<string[]>;
6
+ export declare const removeAllFavoritesFromStorage: (container: Container) => Promise<never[]>;
7
+ export declare const notifyListeners: (container: Container, data: {
8
+ updated: string[];
9
+ }) => void;
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { default as FavoritePlugin } from './favorite';
2
+ export { default as LegacyResourcesPlugin } from './legacy-resources';
3
+ export { default as MiscBehaviorPlugin } from './misc-behavior';
package/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("@telia-ace/knowledge-widget-core"),u=require("@telia-ace/widget-services"),P=require("@telia-ace/knowledge-resource-loader"),A=require("@telia-ace/knowledge-data-client"),L="favorites",E=async n=>await u.createStorageWriter(n,L,u.StorageCategory.Necessary,{medium:u.StorageMedium.Local}),k=async n=>await u.readStorage(n,L,u.StorageMedium.Local)||[],b=async(n,e)=>{const s=await E(e),o=await k(e);if(o.indexOf(n)>-1)return o;const a=o.concat(n);return s(a),a},x=async(n,e)=>{const s=await E(e),a=(await k(e)).filter(i=>i!==n);return s(a),a},M=async n=>{try{return await k(n)}catch{return[]}},T=async n=>((await E(n))([]),[]),v=(n,e)=>{const{updated:s}=e,{events:o}=n.get("$widget");o.dispatch("widget:favorites-updated",{updated:s})},B=n=>{let e=[];const s=()=>{M(n).then(t=>{e=t})},o=()=>e,a=async t=>{const r=await b(t,n);return e=r,v(n,{updated:r}),r},i=async()=>{const t=await T(n);return e=t,v(n,{updated:t}),t},c=async t=>{const r=await x(t,n);return e=r,v(n,{updated:r}),r};return s(),{add:a,get:o,clear:i,remove:c}};class O extends w.Plugin{constructor(e,s){super(e),this.settings=s,this.handler=B(e),e.register("favorites",this.handler)}}const D=n=>{const e=document.createElement("a");return e.href=n,`//${e.hostname}/`};class N extends w.Plugin{constructor(e,s={}){super(e),this.widget.events.subscribeOnce("widget:attached",()=>{var i,c;const{data:{projection:o}}=this.container.get("$settings");if(o){const t=D(o),r=new P.ResourceLoader;r.add(`${t}ClientLibraries/Supplementary/font-awesome-4.7.0/css/font-awesome.min.css`),r.add(`${t}${(i=this.container.parent)==null?void 0:i.name}/widgets.css`),r.load()}const{initialCss:a=""}=s;if(a.length){const t=document.createElement("style"),r=((c=window.webprovisionsEnvironmentVariables)==null?void 0:c.cspNonce)||void 0;r&&(t.nonce=r,t.setAttribute("nonce",r)),t.innerHTML=a,document.head.appendChild(t)}})}}const $=async(n,e)=>{const{denyAutoExpandContactMethods:s}=e;if(s){const o=new RegExp(s);w.extendComponent(n,"contact-list",a=>{a.properties(({contactMethods:i=[]})=>{const c=i.filter(({clientName:t})=>o.test(t));if(c.length)return c.forEach(t=>{t.expanded=!1,t.inline=!1}),{contactMethods:i}})})}return[]},C="misc-behavior:hideBackValue",q=async(n,e)=>{const s=(a,i)=>A.DataClient.getInstance(n).then(c=>c.fetch(A.DataType.ContactMethodCategories,{}).then(t=>{if(t){const{categories:r=[],contactMethods:g=[]}=t;if(i===null)return i=r.length===0&&g.length===1,u.createStorageWriter(a.container,C,u.StorageCategory.Functional).then(f=>f(i))}})),{events:o}=n.get("$widget");return o.subscribe("widget:render-state-changed",async(a,i)=>{if(a.target.state==="activated"&&i.next==="open"){const{hideBackOnSingleContactMethod:c}=e;let t;const r=n.get("$widget"),{name:g,implementation:f}=r;if(g.endsWith("_contact")?t=f.widgets.get(g.replace("_contact","")):g.endsWith("_contact-method")||(t=r),t){let d=null;const l=await u.readStorage(t.container,C),h=(await t.container.getAsync("components")).components().ofType("widget-header").select();typeof l=="boolean"?d=l:c&&await s(t,d),(c||typeof d=="boolean")&&(d&&h.actions.dispatch("show-back",!1),h.actions.watch("show-back",(m,p)=>p(d&&m?!1:m)))}}}),[]},H=n=>n.reduce((e,s)=>{if(e)return e;const[o]=Array.from((s==null?void 0:s.getElementsByClassName("humany-quick-filter-dropdown"))||[]);return!!o},!1),W=n=>n.reduce((e,s)=>{if(e)return e;const[o]=Array.from((s==null?void 0:s.getElementsByTagName("input"))||[]);return!!(o&&o===document.activeElement)},!1),R=()=>Array.from(document.querySelectorAll('[data-component-type="guide-list"]')).reduce((n,e)=>(Array.from(e.getElementsByTagName("a")).forEach(o=>n.push(o)),n),[]),G=async(n,e)=>{const{searchToGuideListArrowKeyNavigation:s=!1}=e;return s?n.getAsync("components").then(o=>{const{nodes:{all:a}}=o,i=a.filter(t=>t.type==="search").map(t=>t.id),c=[];return c.push({parent:window,type:"keydown",handler:t=>{var y,h,m,p;const r=[];i.forEach(F=>{const S=document.getElementById(F);S&&r.push(S)});const g=H(r),f=W(r),d=R(),l=d.indexOf(document.activeElement);(t.key==="ArrowDown"||t.key==="ArrowUp")&&(f||l>-1)&&t.preventDefault(),t.key==="ArrowDown"&&f&&!g?d[0].focus():t.key==="ArrowUp"&&l===0?(h=(y=r[0])==null?void 0:y.getElementsByTagName("input")[0])==null||h.focus():t.key==="ArrowUp"&&l>-1?(m=d[l-1])==null||m.focus():t.key==="ArrowDown"&&l>-1&&((p=d[l+1])==null||p.focus())}}),c}):Promise.resolve([])};class I extends w.Plugin{constructor(e,s={}){super(e),this.settings=s,this.listeners=[]}async initialize(){this.listeners=await Promise.all([Promise.resolve(q(this.container,this.settings)),Promise.resolve($(this.container,this.settings)),Promise.resolve(G(this.container,this.settings))]).then(e=>{const s=[];return e.forEach(o=>o.forEach(a=>s.push(a))),s.forEach(({parent:o,type:a,handler:i})=>{o.addEventListener(a,i)}),s})}deactivate(){this.listeners.forEach(e=>{const{parent:s,handler:o,type:a}=e;s.removeEventListener(a,o)})}}exports.FavoritePlugin=O;exports.LegacyResourcesPlugin=N;exports.MiscBehaviorPlugin=I;
package/index.mjs ADDED
@@ -0,0 +1,202 @@
1
+ import { Plugin as y, extendComponent as P } from "@telia-ace/knowledge-widget-core";
2
+ import { createStorageWriter as F, StorageCategory as L, StorageMedium as C, readStorage as S } from "@telia-ace/widget-services";
3
+ import { ResourceLoader as T } from "@telia-ace/knowledge-resource-loader";
4
+ import { DataClient as B, DataType as M } from "@telia-ace/knowledge-data-client";
5
+ const b = "favorites", v = async (n) => await F(n, b, L.Necessary, {
6
+ medium: C.Local
7
+ }), E = async (n) => await S(n, b, C.Local) || [], N = async (n, e) => {
8
+ const s = await v(e), o = await E(e);
9
+ if (o.indexOf(n) > -1)
10
+ return o;
11
+ const a = o.concat(n);
12
+ return s(a), a;
13
+ }, O = async (n, e) => {
14
+ const s = await v(e), a = (await E(e)).filter((i) => i !== n);
15
+ return s(a), a;
16
+ }, $ = async (n) => {
17
+ try {
18
+ return await E(n);
19
+ } catch {
20
+ return [];
21
+ }
22
+ }, D = async (n) => ((await v(n))([]), []), w = (n, e) => {
23
+ const { updated: s } = e, { events: o } = n.get("$widget");
24
+ o.dispatch("widget:favorites-updated", { updated: s });
25
+ }, H = (n) => {
26
+ let e = [];
27
+ const s = () => {
28
+ $(n).then((t) => {
29
+ e = t;
30
+ });
31
+ }, o = () => e, a = async (t) => {
32
+ const r = await N(t, n);
33
+ return e = r, w(n, { updated: r }), r;
34
+ }, i = async () => {
35
+ const t = await D(n);
36
+ return e = t, w(n, { updated: t }), t;
37
+ }, c = async (t) => {
38
+ const r = await O(t, n);
39
+ return e = r, w(n, { updated: r }), r;
40
+ };
41
+ return s(), {
42
+ add: a,
43
+ get: o,
44
+ clear: i,
45
+ remove: c
46
+ };
47
+ };
48
+ class Q extends y {
49
+ constructor(e, s) {
50
+ super(e), this.settings = s, this.handler = H(e), e.register("favorites", this.handler);
51
+ }
52
+ }
53
+ const W = (n) => {
54
+ const e = document.createElement("a");
55
+ return e.href = n, `//${e.hostname}/`;
56
+ };
57
+ class Y extends y {
58
+ constructor(e, s = {}) {
59
+ super(e), this.widget.events.subscribeOnce("widget:attached", () => {
60
+ var i, c;
61
+ const {
62
+ data: { projection: o }
63
+ } = this.container.get("$settings");
64
+ if (o) {
65
+ const t = W(o), r = new T();
66
+ r.add(
67
+ `${t}ClientLibraries/Supplementary/font-awesome-4.7.0/css/font-awesome.min.css`
68
+ ), r.add(`${t}${(i = this.container.parent) == null ? void 0 : i.name}/widgets.css`), r.load();
69
+ }
70
+ const { initialCss: a = "" } = s;
71
+ if (a.length) {
72
+ const t = document.createElement("style"), r = ((c = window.webprovisionsEnvironmentVariables) == null ? void 0 : c.cspNonce) || void 0;
73
+ r && (t.nonce = r, t.setAttribute("nonce", r)), t.innerHTML = a, document.head.appendChild(t);
74
+ }
75
+ });
76
+ }
77
+ }
78
+ const G = async (n, e) => {
79
+ const { denyAutoExpandContactMethods: s } = e;
80
+ if (s) {
81
+ const o = new RegExp(s);
82
+ P(n, "contact-list", (a) => {
83
+ a.properties(({ contactMethods: i = [] }) => {
84
+ const c = i.filter(
85
+ ({ clientName: t }) => o.test(t)
86
+ );
87
+ if (c.length)
88
+ return c.forEach((t) => {
89
+ t.expanded = !1, t.inline = !1;
90
+ }), { contactMethods: i };
91
+ });
92
+ });
93
+ }
94
+ return [];
95
+ }, A = "misc-behavior:hideBackValue", I = async (n, e) => {
96
+ const s = (a, i) => B.getInstance(n).then((c) => c.fetch(M.ContactMethodCategories, {}).then((t) => {
97
+ if (t) {
98
+ const { categories: r = [], contactMethods: u = [] } = t;
99
+ if (i === null)
100
+ return i = r.length === 0 && u.length === 1, F(
101
+ a.container,
102
+ A,
103
+ L.Functional
104
+ ).then((f) => f(i));
105
+ }
106
+ })), { events: o } = n.get("$widget");
107
+ return o.subscribe(
108
+ "widget:render-state-changed",
109
+ async (a, i) => {
110
+ if (a.target.state === "activated" && i.next === "open") {
111
+ const { hideBackOnSingleContactMethod: c } = e;
112
+ let t;
113
+ const r = n.get("$widget"), { name: u, implementation: f } = r;
114
+ if (u.endsWith("_contact") ? t = f.widgets.get(u.replace("_contact", "")) : u.endsWith("_contact-method") || (t = r), t) {
115
+ let d = null;
116
+ const l = await S(
117
+ t.container,
118
+ A
119
+ ), m = (await t.container.getAsync("components")).components().ofType("widget-header").select();
120
+ typeof l == "boolean" ? d = l : c && await s(t, d), (c || typeof d == "boolean") && (d && m.actions.dispatch("show-back", !1), m.actions.watch("show-back", (h, g) => g(d && h ? !1 : h)));
121
+ }
122
+ }
123
+ }
124
+ ), [];
125
+ }, K = (n) => n.reduce((e, s) => {
126
+ if (e)
127
+ return e;
128
+ const [o] = Array.from(
129
+ (s == null ? void 0 : s.getElementsByClassName("humany-quick-filter-dropdown")) || []
130
+ );
131
+ return !!o;
132
+ }, !1), R = (n) => n.reduce((e, s) => {
133
+ if (e)
134
+ return e;
135
+ const [o] = Array.from((s == null ? void 0 : s.getElementsByTagName("input")) || []);
136
+ return !!(o && o === document.activeElement);
137
+ }, !1), U = () => Array.from(
138
+ document.querySelectorAll('[data-component-type="guide-list"]')
139
+ ).reduce((n, e) => (Array.from(e.getElementsByTagName("a")).forEach((o) => n.push(o)), n), []), _ = async (n, e) => {
140
+ const { searchToGuideListArrowKeyNavigation: s = !1 } = e;
141
+ return s ? n.getAsync("components").then((o) => {
142
+ const {
143
+ nodes: { all: a }
144
+ } = o, i = a.filter((t) => t.type === "search").map((t) => t.id), c = [];
145
+ return c.push({
146
+ parent: window,
147
+ type: "keydown",
148
+ handler: (t) => {
149
+ var p, m, h, g;
150
+ const r = [];
151
+ i.forEach((x) => {
152
+ const k = document.getElementById(x);
153
+ k && r.push(k);
154
+ });
155
+ const u = K(
156
+ r
157
+ ), f = R(
158
+ r
159
+ ), d = U(), l = d.indexOf(
160
+ document.activeElement
161
+ );
162
+ (t.key === "ArrowDown" || t.key === "ArrowUp") && (f || l > -1) && t.preventDefault(), t.key === "ArrowDown" && f && !u ? d[0].focus() : t.key === "ArrowUp" && l === 0 ? (m = (p = r[0]) == null ? void 0 : p.getElementsByTagName("input")[0]) == null || m.focus() : t.key === "ArrowUp" && l > -1 ? (h = d[l - 1]) == null || h.focus() : t.key === "ArrowDown" && l > -1 && ((g = d[l + 1]) == null || g.focus());
163
+ }
164
+ }), c;
165
+ }) : Promise.resolve([]);
166
+ };
167
+ class J extends y {
168
+ constructor(e, s = {}) {
169
+ super(e), this.settings = s, this.listeners = [];
170
+ }
171
+ async initialize() {
172
+ this.listeners = await Promise.all([
173
+ Promise.resolve(
174
+ I(this.container, this.settings)
175
+ ),
176
+ Promise.resolve(
177
+ G(this.container, this.settings)
178
+ ),
179
+ Promise.resolve(
180
+ _(this.container, this.settings)
181
+ )
182
+ ]).then((e) => {
183
+ const s = [];
184
+ return e.forEach(
185
+ (o) => o.forEach((a) => s.push(a))
186
+ ), s.forEach(({ parent: o, type: a, handler: i }) => {
187
+ o.addEventListener(a, i);
188
+ }), s;
189
+ });
190
+ }
191
+ deactivate() {
192
+ this.listeners.forEach((e) => {
193
+ const { parent: s, handler: o, type: a } = e;
194
+ s.removeEventListener(a, o);
195
+ });
196
+ }
197
+ }
198
+ export {
199
+ Q as FavoritePlugin,
200
+ Y as LegacyResourcesPlugin,
201
+ J as MiscBehaviorPlugin
202
+ };
@@ -0,0 +1,3 @@
1
+ import { default as LegacyResources } from './legacy-resources';
2
+
3
+ export default LegacyResources;
@@ -0,0 +1,15 @@
1
+ import { Plugin } from '../../../core/src/index.ts';
2
+ import { Container } from '@webprovisions/platform';
3
+
4
+ type Settings = {
5
+ initialCss?: string;
6
+ };
7
+ /**
8
+ * Downloads legacy resources for a widget. These resources are to be treated as legacy. When the
9
+ * Webprovisions bundling service is available these resources should be included as their own
10
+ * plugins and be part of the output.
11
+ */
12
+ export default class LegacyResourcesPlugin extends Plugin {
13
+ constructor(container: Container, settings?: Settings);
14
+ }
15
+ export {};
@@ -0,0 +1,10 @@
1
+ import { Container } from '@webprovisions/platform';
2
+ import { Settings } from './plugin';
3
+
4
+ /**
5
+ * Sets contactMethod.expanded and contactMethod.inline for contactMethods
6
+ * with a clientName that match the regular expression passed in
7
+ * the denyAutoExpandContactMethods-setting
8
+ */
9
+ declare const _default: (container: Container, settings: Settings) => Promise<never[]>;
10
+ export default _default;
@@ -0,0 +1,9 @@
1
+ import { Container } from '@webprovisions/platform';
2
+ import { Settings } from './plugin';
3
+
4
+ /**
5
+ * Will hide the back-button in the widget-header back-button if there
6
+ * is only a single contact method in the contact-list.
7
+ */
8
+ declare const _default: (container: Container, settings: Settings) => Promise<never[]>;
9
+ export default _default;
@@ -0,0 +1,3 @@
1
+ import { default as plugin } from './plugin';
2
+
3
+ export default plugin;
@@ -0,0 +1,20 @@
1
+ import { Plugin } from '../../../core/src/index.ts';
2
+ import { Container } from '@webprovisions/platform';
3
+
4
+ export type Settings = {
5
+ denyAutoExpandContactMethods?: string;
6
+ hideBackOnSingleContactMethod?: boolean;
7
+ searchToGuideListArrowKeyNavigation?: boolean;
8
+ };
9
+ export type Listerners = {
10
+ parent: HTMLElement | Window;
11
+ handler: (...args: any[]) => void;
12
+ type: string;
13
+ }[];
14
+ export default class MiscBehaviorPlugin extends Plugin {
15
+ settings: Settings;
16
+ listeners: Listerners;
17
+ constructor(container: Container, settings?: Settings);
18
+ initialize(): Promise<void>;
19
+ deactivate(): void;
20
+ }
@@ -0,0 +1,8 @@
1
+ import { Container } from '@webprovisions/platform';
2
+ import { Listerners, Settings } from './plugin';
3
+
4
+ /**
5
+ * Will focus guide-list when focusing search-input and pressing arrow-down key
6
+ */
7
+ declare const _default: (container: Container, settings: Settings) => Promise<Listerners>;
8
+ export default _default;
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@telia-ace/knowledge-widget-plugins",
3
+ "version": "0.0.1",
4
+ "dependencies": {
5
+ "@telia-ace/knowledge-data-client": "*",
6
+ "@telia-ace/widget-services": "*",
7
+ "@webprovisions/platform": "^1.1.4",
8
+ "@telia-ace/knowledge-widget-core": "*",
9
+ "@telia-ace/knowledge-resource-loader": "*"
10
+ },
11
+ "peerDependencies": {
12
+ "@emotion/react": "11.11.1"
13
+ },
14
+ "main": "./index.js",
15
+ "module": "./index.mjs",
16
+ "typings": "./index.d.ts"
17
+ }