@oncehub/knowledgeowl-angular 0.0.0
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.
- package/.eslintrc.json +82 -0
- package/.github/workflows/codeql-analysis.yml +71 -0
- package/.github/workflows/npm-publish.yml +23 -0
- package/.lighthouse/jenkins-x/Kptfile +11 -0
- package/.lighthouse/jenkins-x/pullrequest.yaml +278 -0
- package/.lighthouse/jenkins-x/release.yaml +269 -0
- package/.lighthouse/jenkins-x/triggers.yaml +19 -0
- package/.prettierignore +2 -0
- package/.prettierrc +3 -0
- package/.travis.yml +3 -0
- package/CHANGELOG.md +250 -0
- package/LICENSE +21 -0
- package/OWNERS +4 -0
- package/OWNERS_ALIASES +25 -0
- package/README.md +104 -0
- package/angular.json +47 -0
- package/deploy.sh +19 -0
- package/package-deploy/npm-login.js +14 -0
- package/package.json +61 -0
- package/src/README.md +5 -0
- package/src/lib/index.ts +2 -0
- package/src/lib/knowledge-owl-link/index.ts +1 -0
- package/src/lib/knowledge-owl-link/knowledge-owl-link-module.ts +10 -0
- package/src/lib/knowledge-owl-link/knowledge-owl-link.spec.ts +51 -0
- package/src/lib/knowledge-owl-link/knowledge-owl-link.ts +97 -0
- package/src/lib/knowledge-owl-link/knowledge-owl-widget-errors.ts +3 -0
- package/src/lib/knowledge-owl-link/public-api.ts +2 -0
- package/src/lib/knowledge-owl-widget/index.ts +1 -0
- package/src/lib/knowledge-owl-widget/knowledge-owl-widget-errors.ts +6 -0
- package/src/lib/knowledge-owl-widget/knowledge-owl-widget-module.ts +11 -0
- package/src/lib/knowledge-owl-widget/knowledge-owl-widget.spec.ts +66 -0
- package/src/lib/knowledge-owl-widget/knowledge-owl-widget.ts +162 -0
- package/src/lib/knowledge-owl-widget/public-api.ts +2 -0
- package/src/lib/tsconfig.json +10 -0
- package/src/ng-package.json +7 -0
- package/src/package-lock.json +117 -0
- package/src/package.json +14 -0
- package/src/polyfills.ts +76 -0
- package/src/public_api.ts +1 -0
- package/src/tsconfig.lib.json +31 -0
- package/src/tsconfig.spec.json +18 -0
- package/test/karma.conf.js +44 -0
- package/test/test.ts +24 -0
- package/tsconfig.json +34 -0
- package/tsconfig.lib.prod.json +9 -0
package/angular.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
|
|
3
|
+
"cli": {
|
|
4
|
+
"analytics": "47c16505-5eb3-4e8a-a186-dc68f7f3ea3b"
|
|
5
|
+
},
|
|
6
|
+
"version": 1,
|
|
7
|
+
"newProjectRoot": "projects",
|
|
8
|
+
"projects": {
|
|
9
|
+
"knowledgeowl-angular": {
|
|
10
|
+
"root": "",
|
|
11
|
+
"sourceRoot": "src",
|
|
12
|
+
"projectType": "library",
|
|
13
|
+
"prefix": "",
|
|
14
|
+
"architect": {
|
|
15
|
+
"build": {
|
|
16
|
+
"builder": "@angular-devkit/build-angular:ng-packagr",
|
|
17
|
+
"options": {
|
|
18
|
+
"tsConfig": "src/tsconfig.lib.json",
|
|
19
|
+
"project": "src/ng-package.json"
|
|
20
|
+
},
|
|
21
|
+
"configurations": {
|
|
22
|
+
"production": {
|
|
23
|
+
"tsConfig": "tsconfig.lib.prod.json"
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"test": {
|
|
28
|
+
"builder": "@angular-devkit/build-angular:karma",
|
|
29
|
+
"options": {
|
|
30
|
+
"main": "test/test.ts",
|
|
31
|
+
"tsConfig": "src/tsconfig.spec.json",
|
|
32
|
+
"karmaConfig": "test/karma.conf.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"lint": {
|
|
36
|
+
"builder": "@angular-eslint/builder:lint",
|
|
37
|
+
"options": {
|
|
38
|
+
"lintFilePatterns": [
|
|
39
|
+
"src/**/*.ts",
|
|
40
|
+
"src/**/*.html"
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
package/deploy.sh
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
set -e
|
|
3
|
+
|
|
4
|
+
node package-deploy/npm-login.js $(cat "/etc/npm-cred/NPM_AUTH_TOKEN")
|
|
5
|
+
npm whoami
|
|
6
|
+
npm i
|
|
7
|
+
chown root:root .
|
|
8
|
+
npm run package
|
|
9
|
+
filename="$(npm pack --dry-run | tail -n 1)"
|
|
10
|
+
echo "$1"
|
|
11
|
+
|
|
12
|
+
if [[ "$1" == "qa" ]] || [[ "$1" == "master" ]] || [[ "$1" == "staging" ]] || [[ "$1" == "staging-app2" ]]
|
|
13
|
+
then
|
|
14
|
+
npm publish dist/knowledgeowl-angular/$filename --registry=https://registry.npmjs.org/
|
|
15
|
+
else
|
|
16
|
+
npm publish --tag beta dist/knowledgeowl-angular/$filename --registry=https://registry.npmjs.org/
|
|
17
|
+
fi
|
|
18
|
+
|
|
19
|
+
echo "$filename package pushed to NPM successfully"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
let fs = require('fs');
|
|
2
|
+
let path = require('path');
|
|
3
|
+
|
|
4
|
+
let token = '';
|
|
5
|
+
|
|
6
|
+
process.argv.forEach(function(val, index, array) {
|
|
7
|
+
if (index == 2) token = val;
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
let registry = '//registry.npmjs.org/';
|
|
11
|
+
|
|
12
|
+
var configPath = configPath ? configPath : path.join(__dirname, '../', '.npmrc');
|
|
13
|
+
|
|
14
|
+
fs.writeFile(configPath, `${registry}:_authToken=${token}` + '\n', (err, message) => { if(err){ console.log(err , message)}});
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oncehub/knowledgeowl-angular",
|
|
3
|
+
"description": "KnowledgeOwl widget integration for Angular",
|
|
4
|
+
"author": "https://github.com/scheduleonce/knowledgeowl-angular/graphs/contributors",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/scheduleonce/knowledgeowl-angular.git"
|
|
8
|
+
},
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"version": "0.0.0",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"ng": "ng",
|
|
13
|
+
"build": "ng build --configuration production",
|
|
14
|
+
"test": "ng test",
|
|
15
|
+
"lint": "npm run test:lint",
|
|
16
|
+
"prettier": "prettier --list-different \"**/*.{ts,js,css,scss,md,html}\"",
|
|
17
|
+
"fix:prettier": "prettier --write \"**/*.{ts,js,css,scss,md,html}\"",
|
|
18
|
+
"npm-pack": "cd dist/knowledgeowl-angular && npm pack",
|
|
19
|
+
"package": "npm run build && cp README.md dist/knowledgeowl-angular/ && npm run npm-pack",
|
|
20
|
+
"publish": "cd dist/knowledgeowl-angular && npm publish --access public",
|
|
21
|
+
"test:lint": "ng lint knowledgeowl-angular",
|
|
22
|
+
"fix:lint": "ng lint knowledgeowl-angular --fix"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"tslib": "2.3.1"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@angular-devkit/build-angular": "14.2.12",
|
|
29
|
+
"@angular-eslint/builder": "14.4.0",
|
|
30
|
+
"@angular-eslint/eslint-plugin": "14.4.0",
|
|
31
|
+
"@angular-eslint/eslint-plugin-template": "14.4.0",
|
|
32
|
+
"@angular-eslint/schematics": "14.4.0",
|
|
33
|
+
"@angular-eslint/template-parser": "14.4.0",
|
|
34
|
+
"@angular/cli": "14.2.12",
|
|
35
|
+
"@angular/common": "14.2.12",
|
|
36
|
+
"@angular/compiler": "14.2.12",
|
|
37
|
+
"@angular/compiler-cli": "14.2.12",
|
|
38
|
+
"@angular/core": "14.2.12",
|
|
39
|
+
"@angular/platform-browser": "14.2.12",
|
|
40
|
+
"@angular/platform-browser-dynamic": "14.2.12",
|
|
41
|
+
"@types/jasmine": "3.10.3",
|
|
42
|
+
"@typescript-eslint/eslint-plugin": "^5.36.2",
|
|
43
|
+
"@typescript-eslint/parser": "^5.36.2",
|
|
44
|
+
"eslint": "^8.23.0",
|
|
45
|
+
"eslint-plugin-import": "2.25.4",
|
|
46
|
+
"eslint-plugin-jsdoc": "37.9.1",
|
|
47
|
+
"eslint-plugin-prefer-arrow": "1.2.3",
|
|
48
|
+
"jasmine-core": "4.0.0",
|
|
49
|
+
"karma": "6.3.16",
|
|
50
|
+
"karma-chrome-launcher": "3.1.0",
|
|
51
|
+
"karma-coverage-istanbul-reporter": "3.0.3",
|
|
52
|
+
"karma-jasmine": "4.0.1",
|
|
53
|
+
"karma-jasmine-html-reporter": "1.7.0",
|
|
54
|
+
"karma-junit-reporter": "2.0.1",
|
|
55
|
+
"ng-packagr": "14.2.2",
|
|
56
|
+
"prettier": "2.5.1",
|
|
57
|
+
"puppeteer": "13.3.2",
|
|
58
|
+
"typescript": "4.8.4",
|
|
59
|
+
"zone.js": "0.11.4"
|
|
60
|
+
}
|
|
61
|
+
}
|
package/src/README.md
ADDED
package/src/lib/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './public-api';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CommonModule } from '@angular/common';
|
|
2
|
+
import { NgModule } from '@angular/core';
|
|
3
|
+
import { KnowledgeOwlLink } from './knowledge-owl-link';
|
|
4
|
+
|
|
5
|
+
@NgModule({
|
|
6
|
+
imports: [CommonModule],
|
|
7
|
+
exports: [KnowledgeOwlLink],
|
|
8
|
+
declarations: [KnowledgeOwlLink],
|
|
9
|
+
})
|
|
10
|
+
export class KnowledgeOwlLinkModule {}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Component, Provider, Type } from '@angular/core';
|
|
2
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
3
|
+
import { KnowledgeOwlLinkModule } from './index';
|
|
4
|
+
import { getKnowledgeLinkArticleMissingError } from './knowledge-owl-widget-errors';
|
|
5
|
+
|
|
6
|
+
@Component({
|
|
7
|
+
template: `<a knowledgeOwlLink>Test link</a>`,
|
|
8
|
+
})
|
|
9
|
+
class KnowledgeOwlLinkWithoutArticle {}
|
|
10
|
+
|
|
11
|
+
@Component({
|
|
12
|
+
template: `
|
|
13
|
+
<a knowledgeOwlLink="https://knowledgeowl.article.com/help/test-article"
|
|
14
|
+
>Test link</a
|
|
15
|
+
>
|
|
16
|
+
`,
|
|
17
|
+
})
|
|
18
|
+
class KnowledgeOwlLinkWithValidInputs {}
|
|
19
|
+
|
|
20
|
+
describe('KnowledgeOwl link with missing credentials', () => {
|
|
21
|
+
it('should throw error without knowledgeOwlLink', () => {
|
|
22
|
+
const fixture = createComponent(KnowledgeOwlLinkWithoutArticle, [
|
|
23
|
+
{ provide: 'KOProjectURL', useValue: 'https://knowledgeowl.com' },
|
|
24
|
+
]);
|
|
25
|
+
expect(() => fixture.detectChanges()).toThrowError(
|
|
26
|
+
getKnowledgeLinkArticleMissingError().message
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('KnowledgeOwl link', () => {
|
|
32
|
+
it('should not throw error when given correct inputs', () => {
|
|
33
|
+
const fixture = createComponent(KnowledgeOwlLinkWithValidInputs, [
|
|
34
|
+
{ provide: 'KOProjectURL', useValue: 'https://knowledgeowl.com' },
|
|
35
|
+
]);
|
|
36
|
+
expect(() => fixture.detectChanges()).not.toThrowError();
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const createComponent = <T>(
|
|
41
|
+
component: Type<T>,
|
|
42
|
+
providers: Provider[] = []
|
|
43
|
+
): ComponentFixture<T> => {
|
|
44
|
+
TestBed.configureTestingModule({
|
|
45
|
+
imports: [KnowledgeOwlLinkModule],
|
|
46
|
+
declarations: [component],
|
|
47
|
+
providers,
|
|
48
|
+
}).compileComponents();
|
|
49
|
+
|
|
50
|
+
return TestBed.createComponent<T>(component);
|
|
51
|
+
};
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AfterContentInit,
|
|
3
|
+
ElementRef,
|
|
4
|
+
HostListener,
|
|
5
|
+
Inject,
|
|
6
|
+
Input,
|
|
7
|
+
Directive,
|
|
8
|
+
} from '@angular/core';
|
|
9
|
+
import { getKnowledgeLinkArticleMissingError } from './knowledge-owl-widget-errors';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Knowledge Owl Link
|
|
13
|
+
*/
|
|
14
|
+
@Directive({
|
|
15
|
+
selector: `a[knowledgeOwlLink]`,
|
|
16
|
+
exportAs: 'knowledgeOwlLink',
|
|
17
|
+
})
|
|
18
|
+
export class KnowledgeOwlLink implements AfterContentInit {
|
|
19
|
+
@Input()
|
|
20
|
+
knowledgeOwlLink: string;
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
protected elementRef: ElementRef,
|
|
24
|
+
@Inject('KOProjectURL') private projectURL: string
|
|
25
|
+
) {}
|
|
26
|
+
|
|
27
|
+
getHostElement(): HTMLElement {
|
|
28
|
+
return this.elementRef.nativeElement;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
ngAfterContentInit(): void {
|
|
32
|
+
this._validateLinkInputs();
|
|
33
|
+
this._initLink();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@HostListener('click', ['$event'])
|
|
37
|
+
openArticle(event: Event): void {
|
|
38
|
+
event.preventDefault();
|
|
39
|
+
// Convert the KO link into the widget link
|
|
40
|
+
let widgetHref;
|
|
41
|
+
if (this.knowledgeOwlLink.includes('#')) {
|
|
42
|
+
const widgetlink = this.knowledgeOwlLink.split('#');
|
|
43
|
+
const widgetlink1 =
|
|
44
|
+
widgetlink[0].replace('/help/', '/help/fetch-article/hash/') +
|
|
45
|
+
'?widget=true';
|
|
46
|
+
const widgetlink2 = '#' + widgetlink[1];
|
|
47
|
+
widgetHref = widgetlink1.concat(widgetlink2);
|
|
48
|
+
} else {
|
|
49
|
+
widgetHref =
|
|
50
|
+
this.knowledgeOwlLink.replace('/help/', '/help/fetch-article/hash/') +
|
|
51
|
+
'?widget=true';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (window['__ko16']) {
|
|
55
|
+
// Wait until widget loads completely
|
|
56
|
+
window['__ko16'].openArticle(widgetHref);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Private methods
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Initialize link
|
|
66
|
+
*
|
|
67
|
+
* @private
|
|
68
|
+
*/
|
|
69
|
+
private _initLink() {
|
|
70
|
+
this.knowledgeOwlLink = `${this.projectURL}/help/${this.knowledgeOwlLink}`; // Updating article link to have complete URL
|
|
71
|
+
const nativeElement = this.getHostElement();
|
|
72
|
+
if (nativeElement.tagName === 'A') {
|
|
73
|
+
// Updating link for <a>
|
|
74
|
+
nativeElement.setAttribute('href', this.knowledgeOwlLink);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validates all required inputs of knowledge-owl-link
|
|
80
|
+
*
|
|
81
|
+
* @private
|
|
82
|
+
*/
|
|
83
|
+
private _validateLinkInputs() {
|
|
84
|
+
this._validateArticlePresence();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Validates presence of article URL for link
|
|
89
|
+
*
|
|
90
|
+
* @private
|
|
91
|
+
*/
|
|
92
|
+
private _validateArticlePresence() {
|
|
93
|
+
if (!this.knowledgeOwlLink) {
|
|
94
|
+
throw getKnowledgeLinkArticleMissingError();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './public-api';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** @docs-private */
|
|
2
|
+
export const getKnowledgeWidgetPoductKeyMissingError = (): Error =>
|
|
3
|
+
Error('knowledge-owl-widget requires product key as input.');
|
|
4
|
+
|
|
5
|
+
export const getKnowledgeWidgetInvalidProductURLError = (): Error =>
|
|
6
|
+
Error('knowledge-owl-widget requires valid product URL as provider.');
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { NgModule } from '@angular/core';
|
|
2
|
+
import { CommonModule } from '@angular/common';
|
|
3
|
+
import { KnowledgeOwlWidget } from './knowledge-owl-widget';
|
|
4
|
+
|
|
5
|
+
@NgModule({
|
|
6
|
+
imports: [CommonModule],
|
|
7
|
+
exports: [KnowledgeOwlWidget],
|
|
8
|
+
declarations: [KnowledgeOwlWidget],
|
|
9
|
+
providers: [],
|
|
10
|
+
})
|
|
11
|
+
export class KnowledgeOwlWidgetModule {}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Component, Provider, Type } from '@angular/core';
|
|
2
|
+
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
|
3
|
+
import { KnowledgeOwlWidgetModule } from './index';
|
|
4
|
+
import {
|
|
5
|
+
getKnowledgeWidgetPoductKeyMissingError,
|
|
6
|
+
getKnowledgeWidgetInvalidProductURLError,
|
|
7
|
+
} from './knowledge-owl-widget-errors';
|
|
8
|
+
|
|
9
|
+
@Component({
|
|
10
|
+
template: `<knowledge-owl-widget></knowledge-owl-widget>`,
|
|
11
|
+
})
|
|
12
|
+
class KnowledgeOwlWidgetWithoutCredentials {}
|
|
13
|
+
|
|
14
|
+
@Component({
|
|
15
|
+
template: `<knowledge-owl-widget></knowledge-owl-widget>`,
|
|
16
|
+
})
|
|
17
|
+
class KnowledgeOwlWidgetWithoutProjectKey {}
|
|
18
|
+
|
|
19
|
+
@Component({
|
|
20
|
+
template: `
|
|
21
|
+
<knowledge-owl-widget [projectKey]="'testkey'"></knowledge-owl-widget>
|
|
22
|
+
`,
|
|
23
|
+
})
|
|
24
|
+
class KnowledgeOwlWidgetWithCredentials {}
|
|
25
|
+
|
|
26
|
+
describe('KnowledgeOwl widget with missing credentials', () => {
|
|
27
|
+
it('should throw KnowledgeWidgetProductURLMissingError error', () => {
|
|
28
|
+
const fixture = createComponent(KnowledgeOwlWidgetWithoutCredentials, [
|
|
29
|
+
{ provide: 'KOProjectURL', useValue: 'https://' },
|
|
30
|
+
]);
|
|
31
|
+
expect(() => fixture.detectChanges()).toThrowError(
|
|
32
|
+
getKnowledgeWidgetInvalidProductURLError().message
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should throw KnowledgeWidgetProductKeyMissingError error', () => {
|
|
37
|
+
const fixture = createComponent(KnowledgeOwlWidgetWithoutProjectKey, [
|
|
38
|
+
{ provide: 'KOProjectURL', useValue: 'https://knowledgeowl.com' },
|
|
39
|
+
]);
|
|
40
|
+
expect(() => fixture.detectChanges()).toThrowError(
|
|
41
|
+
getKnowledgeWidgetPoductKeyMissingError().message
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('KnowledgeOwl widget', () => {
|
|
47
|
+
it('should not throw error when given correct inputs', () => {
|
|
48
|
+
const fixture = createComponent(KnowledgeOwlWidgetWithCredentials, [
|
|
49
|
+
{ provide: 'KOProjectURL', useValue: 'https://knowledgeowl.com' },
|
|
50
|
+
]);
|
|
51
|
+
expect(() => fixture.detectChanges()).not.toThrowError();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const createComponent = <T>(
|
|
56
|
+
component: Type<T>,
|
|
57
|
+
providers: Provider[] = []
|
|
58
|
+
): ComponentFixture<T> => {
|
|
59
|
+
TestBed.configureTestingModule({
|
|
60
|
+
imports: [KnowledgeOwlWidgetModule],
|
|
61
|
+
declarations: [component],
|
|
62
|
+
providers,
|
|
63
|
+
}).compileComponents();
|
|
64
|
+
|
|
65
|
+
return TestBed.createComponent<T>(component);
|
|
66
|
+
};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AfterContentInit,
|
|
3
|
+
ChangeDetectionStrategy,
|
|
4
|
+
Component,
|
|
5
|
+
Inject,
|
|
6
|
+
Input,
|
|
7
|
+
OnInit,
|
|
8
|
+
ViewEncapsulation,
|
|
9
|
+
} from '@angular/core';
|
|
10
|
+
import {
|
|
11
|
+
getKnowledgeWidgetPoductKeyMissingError,
|
|
12
|
+
getKnowledgeWidgetInvalidProductURLError,
|
|
13
|
+
} from './knowledge-owl-widget-errors';
|
|
14
|
+
|
|
15
|
+
/** Regex to validate article URL is valid or not */
|
|
16
|
+
const urlValidatorRegex =
|
|
17
|
+
/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
|
|
18
|
+
|
|
19
|
+
@Component({
|
|
20
|
+
selector: 'knowledge-owl-widget',
|
|
21
|
+
template: '',
|
|
22
|
+
encapsulation: ViewEncapsulation.None,
|
|
23
|
+
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
24
|
+
exportAs: 'knowledgeOwlWidget',
|
|
25
|
+
providers: [],
|
|
26
|
+
})
|
|
27
|
+
export class KnowledgeOwlWidget implements OnInit, AfterContentInit {
|
|
28
|
+
/** Flag to check initial page location updated */
|
|
29
|
+
isPageLocationUpdated = false;
|
|
30
|
+
|
|
31
|
+
/** Product key to access KnowledgeOwl widget. */
|
|
32
|
+
@Input()
|
|
33
|
+
projectKey: string;
|
|
34
|
+
|
|
35
|
+
/** Current page location for widget context. */
|
|
36
|
+
private _pageLocation: string;
|
|
37
|
+
@Input() set pageLocation(value: string) {
|
|
38
|
+
this._pageLocation = value.trim();
|
|
39
|
+
this._updatePageLocation();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get pageLocation(): string {
|
|
43
|
+
return this._pageLocation;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
constructor(@Inject('KOProjectURL') private projectURL: string) {}
|
|
47
|
+
|
|
48
|
+
ngOnInit(): void {
|
|
49
|
+
this._initWidget();
|
|
50
|
+
this._loadScript();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
ngAfterContentInit(): void {
|
|
54
|
+
this._validateWidgetInputs();
|
|
55
|
+
this._initPageLocation();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Opens the widget. */
|
|
59
|
+
open(article = ''): void {
|
|
60
|
+
if (!window['__ko16']) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (article) {
|
|
64
|
+
const widgetHref = `${this.projectURL}/help/fetch-article/hash/${article}`;
|
|
65
|
+
window['__ko16'].openArticle(widgetHref);
|
|
66
|
+
} else {
|
|
67
|
+
window['__ko16']._toggleOpen();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Validates the widget's inputs.
|
|
73
|
+
*
|
|
74
|
+
* @private
|
|
75
|
+
*/
|
|
76
|
+
protected _validateWidgetInputs(): void {
|
|
77
|
+
this._validateProductURL();
|
|
78
|
+
this._validateProductKey();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Private methods
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Throws an error if the productKey input is missing.
|
|
87
|
+
*
|
|
88
|
+
* @private
|
|
89
|
+
*/
|
|
90
|
+
private _validateProductKey() {
|
|
91
|
+
if (!this.projectKey) {
|
|
92
|
+
throw getKnowledgeWidgetPoductKeyMissingError();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Throws an error if the productURL input is missing.
|
|
98
|
+
*
|
|
99
|
+
* @private
|
|
100
|
+
*/
|
|
101
|
+
private _validateProductURL() {
|
|
102
|
+
if (!this.projectURL || !urlValidatorRegex.test(this.projectURL)) {
|
|
103
|
+
throw getKnowledgeWidgetInvalidProductURLError();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Initializes the widget.
|
|
109
|
+
*
|
|
110
|
+
* @private
|
|
111
|
+
*/
|
|
112
|
+
private _initWidget() {
|
|
113
|
+
const ko16p = [];
|
|
114
|
+
ko16p.push(['_setProject', this.projectKey]);
|
|
115
|
+
window['_ko16_p'] = ko16p;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Loads script into page
|
|
120
|
+
*
|
|
121
|
+
* @private
|
|
122
|
+
*/
|
|
123
|
+
private _loadScript() {
|
|
124
|
+
const ko = document.createElement('script');
|
|
125
|
+
ko.type = 'text/javascript';
|
|
126
|
+
ko.async = true;
|
|
127
|
+
ko.src = `${this.projectURL}/javascript/ko-index?__pc=${this.projectKey}`;
|
|
128
|
+
document.head.appendChild(ko);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Init page location first time
|
|
133
|
+
*/
|
|
134
|
+
private _initPageLocation() {
|
|
135
|
+
const updatePageLocationInterval = setInterval(() => {
|
|
136
|
+
if (!this.pageLocation || this.isPageLocationUpdated) {
|
|
137
|
+
clearInterval(updatePageLocationInterval);
|
|
138
|
+
}
|
|
139
|
+
if (
|
|
140
|
+
!this.isPageLocationUpdated &&
|
|
141
|
+
window['__ko16'] &&
|
|
142
|
+
this.pageLocation
|
|
143
|
+
) {
|
|
144
|
+
// Updating page location once
|
|
145
|
+
window['__ko16'].updatePageLoc(this.pageLocation);
|
|
146
|
+
this.isPageLocationUpdated = true;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Update page location on change
|
|
153
|
+
*
|
|
154
|
+
* @private
|
|
155
|
+
*/
|
|
156
|
+
private _updatePageLocation() {
|
|
157
|
+
if (!window['__ko16'] || !this.pageLocation) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
window['__ko16'].updatePageLoc(this.pageLocation);
|
|
161
|
+
}
|
|
162
|
+
}
|