Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit 4ba4ea8

Browse files
authored
Merge pull request #56 from Notalib/feat/is-enabled-prop
feat: add isEnabled property
2 parents d777e82 + 1dbf232 commit 4ba4ea8

File tree

9 files changed

+139
-81
lines changed

9 files changed

+139
-81
lines changed

src/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"demo.reset": "git clean -d -x -f ../demo && npm install",
2828
"format": "prettier --write --config ../.prettierrc.json angular/index.ts vue/index.ts *.ts",
2929
"format:check": "prettier -c --config ../.prettierrc.json angular/index.ts vue/index.ts *.ts",
30-
"lint": "npm-run-all format:check tslint",
30+
"lint:www": "cd ../www-src && npm run lint",
31+
"lint": "npm-run-all format:check tslint lint:www",
3132
"make-bridge-loader": "ts-node ./make-bridge-loader.ts",
3233
"ngc": "ngc --project tsconfig.aot.json",
3334
"plugin.link": "npm link && cd ../demo && npm link nativescript-webview-ext && cd ../src",
@@ -43,7 +44,7 @@
4344
"test.ios": "npm run build && npm-run-all test.install --parallel build:webview-bridge:watch plugin.tscwatch run.test.ios",
4445
"test.reset": "git clean -d -x -f ../unittest && npm install",
4546
"tsc": "npx tsc -skipLibCheck",
46-
"tslint": "cd .. && tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\" --exclude \"**/*.d.ts\" --exclude \"**/platforms/**\""
47+
"tslint": "tslint '**/*.ts' --config ../tslint.json --exclude \"**/node_modules/**\" --exclude \"**/*.d.ts\" --exclude \"**/platforms/**\""
4748
},
4849
"keywords": [
4950
"NativeScript",

src/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compileOnSave": false,
33
"compilerOptions": {
4+
"strictNullChecks": true,
45
"allowUnreachableCode": false,
56
"allowUnusedLabels": false,
67
"declaration": true,

src/webview-ext-common.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as fs from "@nativescript/core/file-system";
44
import { booleanConverter, ContainerView, CSSType, EventData, Property, traceEnabled, traceMessageType, traceWrite } from "@nativescript/core/ui/core/view";
5+
import { isEnabledProperty } from "tns-core-modules/ui/core/view/view";
56
import * as URL from "url";
67
import { fetchPolyfill, metadataViewPort, promisePolyfill, webViewBridge } from "./nativescript-webview-bridge-loader";
78

@@ -519,7 +520,7 @@ export class WebViewExtBase extends ContainerView {
519520

520521
this.notify(args);
521522

522-
this.getTitle().then((title) => this._titleChanged(title));
523+
this.getTitle().then((title) => title && this._titleChanged(title));
523524

524525
return args;
525526
}
@@ -618,7 +619,7 @@ export class WebViewExtBase extends ContainerView {
618619
return true;
619620
}
620621

621-
public _webConfirm(message: string, callback: (response: boolean) => void) {
622+
public _webConfirm(message: string, callback: (response: boolean | null) => void) {
622623
if (!this.hasListeners(WebViewExtBase.webConfirmEvent)) {
623624
return false;
624625
}
@@ -636,7 +637,7 @@ export class WebViewExtBase extends ContainerView {
636637
return true;
637638
}
638639

639-
public _webPrompt(message: string, defaultText: string, callback: (response: string) => void) {
640+
public _webPrompt(message: string, defaultText: string, callback: (response: string | null) => void) {
640641
if (!this.hasListeners(WebViewExtBase.webPromptEvent)) {
641642
return false;
642643
}
@@ -836,7 +837,7 @@ export class WebViewExtBase extends ContainerView {
836837
/**
837838
* Resolve a "x-local://{name}" to file-path.
838839
*/
839-
public getRegisteredLocalResource(name: string): string {
840+
public getRegisteredLocalResource(name: string): string | void {
840841
throw new Error("Method not implemented.");
841842
}
842843

@@ -870,7 +871,7 @@ export class WebViewExtBase extends ContainerView {
870871
/**
871872
* Load a JavaScript file on the current page in the webview.
872873
*/
873-
public loadJavaScriptFile(scriptName: string, filepath?: string) {
874+
public loadJavaScriptFile(scriptName: string, filepath: string) {
874875
return this.loadJavaScriptFiles([
875876
{
876877
resourceName: scriptName,
@@ -1335,7 +1336,7 @@ export class WebViewExtBase extends ContainerView {
13351336
* Get document.title
13361337
* NOTE: On Android, if empty returns filename
13371338
*/
1338-
public getTitle(): Promise<string> {
1339+
public getTitle(): Promise<string | void> {
13391340
throw new Error("Method not implemented.");
13401341
}
13411342

@@ -1361,6 +1362,10 @@ export class WebViewExtBase extends ContainerView {
13611362

13621363
return resourceName;
13631364
}
1365+
1366+
[isEnabledProperty.getDefault]() {
1367+
return true;
1368+
}
13641369
}
13651370

13661371
export interface WebViewExtBase {

src/webview-ext.android.ts

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
debugModeProperty,
1010
displayZoomControlsProperty,
1111
domStorageProperty,
12+
isEnabledProperty,
1213
supportZoomProperty,
1314
traceMessageType,
1415
UnsupportedSDKError,
@@ -128,7 +129,7 @@ function initializeWebViewClient(): void {
128129
return super.shouldInterceptRequest(view, request as android.webkit.WebResourceRequest);
129130
}
130131

131-
let url: string;
132+
let url: string | void;
132133
if (typeof request === "string") {
133134
url = request;
134135
} else if (typeof request === "object") {
@@ -525,7 +526,7 @@ export class WebViewExt extends WebViewExtBase {
525526

526527
const baseUrl = `file:///${fs.knownFolders.currentApp().path}/`;
527528
this.writeTrace(`WebViewExt<android>._loadData("${src}") -> baseUrl: "${baseUrl}"`);
528-
nativeView.loadDataWithBaseURL(baseUrl, src, "text/html", "utf-8", null);
529+
nativeView.loadDataWithBaseURL(baseUrl, src, "text/html", "utf-8", null!);
529530
}
530531

531532
public get canGoBack(): boolean {
@@ -637,14 +638,15 @@ export class WebViewExt extends WebViewExtBase {
637638
}
638639

639640
const result = await new Promise<T>((resolve, reject) => {
640-
if (!this.nativeViewProtected) {
641+
const androidWebView = this.nativeViewProtected;
642+
if (!androidWebView) {
641643
this.writeTrace(`WebViewExt<android>.executeJavaScript() -> no nativeView?`, traceMessageType.error);
642644
reject(new Error("Native Android not initialized, cannot call executeJavaScript"));
643645

644646
return;
645647
}
646648

647-
this.nativeViewProtected.evaluateJavascript(
649+
androidWebView.evaluateJavascript(
648650
scriptCode,
649651
new android.webkit.ValueCallback({
650652
onReceiveValue(result: any) {
@@ -662,19 +664,21 @@ export class WebViewExt extends WebViewExtBase {
662664
}
663665

664666
public zoomIn() {
665-
if (!this.nativeViewProtected) {
667+
const androidWebView = this.nativeViewProtected;
668+
if (!androidWebView) {
666669
return false;
667670
}
668671

669-
return this.nativeViewProtected.zoomIn();
672+
return androidWebView.zoomIn();
670673
}
671674

672675
public zoomOut() {
673-
if (!this.nativeViewProtected) {
676+
const androidWebView = this.nativeViewProtected;
677+
if (!androidWebView) {
674678
return false;
675679
}
676680

677-
return this.nativeViewProtected.zoomOut();
681+
return androidWebView.zoomOut();
678682
}
679683

680684
public zoomBy(zoomFactor: number) {
@@ -704,46 +708,52 @@ export class WebViewExt extends WebViewExtBase {
704708
}
705709

706710
[builtInZoomControlsProperty.getDefault]() {
707-
if (!this.nativeViewProtected) {
711+
const androidWebView = this.nativeViewProtected;
712+
if (!androidWebView) {
708713
return false;
709714
}
710715

711-
const settings = this.nativeViewProtected.getSettings();
716+
const settings = androidWebView.getSettings();
712717

713718
return settings.getBuiltInZoomControls();
714719
}
715720

716721
[builtInZoomControlsProperty.setNative](enabled: boolean) {
717-
if (!this.nativeViewProtected) {
722+
const androidWebView = this.nativeViewProtected;
723+
if (!androidWebView) {
718724
return;
719725
}
720-
const settings = this.nativeViewProtected.getSettings();
726+
const settings = androidWebView.getSettings();
721727
settings.setBuiltInZoomControls(!!enabled);
722728
}
723729

724730
[displayZoomControlsProperty.getDefault]() {
725-
if (!this.nativeViewProtected) {
731+
const androidWebView = this.nativeViewProtected;
732+
if (!androidWebView) {
726733
return false;
727734
}
728-
const settings = this.nativeViewProtected.getSettings();
735+
736+
const settings = androidWebView.getSettings();
729737

730738
return settings.getDisplayZoomControls();
731739
}
732740

733741
[displayZoomControlsProperty.setNative](enabled: boolean) {
734-
if (!this.nativeViewProtected) {
742+
const androidWebView = this.nativeViewProtected;
743+
if (!androidWebView) {
735744
return;
736745
}
737-
const settings = this.nativeViewProtected.getSettings();
746+
const settings = androidWebView.getSettings();
738747
settings.setDisplayZoomControls(!!enabled);
739748
}
740749

741-
[cacheModeProperty.getDefault](): CacheMode {
742-
if (!this.nativeViewProtected) {
750+
[cacheModeProperty.getDefault](): CacheMode | null {
751+
const androidWebView = this.nativeViewProtected;
752+
if (!androidWebView) {
743753
return null;
744754
}
745755

746-
const settings = this.nativeViewProtected.getSettings();
756+
const settings = androidWebView.getSettings();
747757
const cacheModeInt = settings.getCacheMode();
748758
for (const [key, value] of cacheModeMap) {
749759
if (value === cacheModeInt) {
@@ -755,11 +765,12 @@ export class WebViewExt extends WebViewExtBase {
755765
}
756766

757767
[cacheModeProperty.setNative](cacheMode: CacheMode) {
758-
if (!this.nativeViewProtected) {
768+
const androidWebView = this.nativeViewProtected;
769+
if (!androidWebView) {
759770
return;
760771
}
761772

762-
const settings = this.nativeViewProtected.getSettings();
773+
const settings = androidWebView.getSettings();
763774
for (const [key, nativeValue] of cacheModeMap) {
764775
if (key === cacheMode) {
765776
settings.setCacheMode(nativeValue);
@@ -770,59 +781,84 @@ export class WebViewExt extends WebViewExtBase {
770781
}
771782

772783
[databaseStorageProperty.getDefault]() {
773-
if (!this.nativeViewProtected) {
784+
const androidWebView = this.nativeViewProtected;
785+
if (!androidWebView) {
774786
return false;
775787
}
776788

777-
const settings = this.nativeViewProtected.getSettings();
789+
const settings = androidWebView.getSettings();
778790

779791
return settings.getDatabaseEnabled();
780792
}
781793

782794
[databaseStorageProperty.setNative](enabled: boolean) {
783-
if (!this.nativeViewProtected) {
795+
const androidWebView = this.nativeViewProtected;
796+
if (!androidWebView) {
784797
return;
785798
}
786799

787-
const settings = this.nativeViewProtected.getSettings();
800+
const settings = androidWebView.getSettings();
788801
settings.setDatabaseEnabled(!!enabled);
789802
}
790803

791804
[domStorageProperty.getDefault]() {
792-
if (!this.nativeViewProtected) {
805+
const androidWebView = this.nativeViewProtected;
806+
if (!androidWebView) {
793807
return false;
794808
}
795809

796-
const settings = this.nativeViewProtected.getSettings();
810+
const settings = androidWebView.getSettings();
797811

798812
return settings.getDomStorageEnabled();
799813
}
800814

801815
[domStorageProperty.setNative](enabled: boolean) {
802-
if (!this.nativeViewProtected) {
816+
const androidWebView = this.nativeViewProtected;
817+
if (!androidWebView) {
803818
return;
804819
}
805820

806-
const settings = this.nativeViewProtected.getSettings();
821+
const settings = androidWebView.getSettings();
807822
settings.setDomStorageEnabled(!!enabled);
808823
}
809824

810825
[supportZoomProperty.getDefault]() {
811-
if (!this.nativeViewProtected) {
826+
const androidWebView = this.nativeViewProtected;
827+
if (!androidWebView) {
812828
return false;
813829
}
814830

815-
const settings = this.nativeViewProtected.getSettings();
831+
const settings = androidWebView.getSettings();
816832

817833
return settings.supportZoom();
818834
}
819835

820836
[supportZoomProperty.setNative](enabled: boolean) {
821-
if (!this.nativeViewProtected) {
837+
const androidWebView = this.nativeViewProtected;
838+
if (!androidWebView) {
822839
return;
823840
}
824841

825-
const settings = this.nativeViewProtected.getSettings();
842+
const settings = androidWebView.getSettings();
826843
settings.setSupportZoom(!!enabled);
827844
}
845+
846+
[isEnabledProperty.setNative](enabled: boolean) {
847+
const androidWebView = this.nativeViewProtected;
848+
if (!androidWebView) {
849+
return;
850+
}
851+
852+
if (enabled) {
853+
androidWebView.setOnTouchListener(null!);
854+
} else {
855+
androidWebView.setOnTouchListener(
856+
new android.view.View.OnTouchListener({
857+
onTouch() {
858+
return true;
859+
},
860+
}),
861+
);
862+
}
863+
}
828864
}

0 commit comments

Comments
 (0)