Skip to content

Commit

Permalink
remember last selected view
Browse files Browse the repository at this point in the history
  • Loading branch information
osvaldopina committed Aug 3, 2019
1 parent 76753c1 commit 71bcad0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 40 deletions.
8 changes: 6 additions & 2 deletions projects/chrome-extension/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Component } from '@angular/core';
import { Component, ChangeDetectorRef, ChangeDetectionStrategy, OnInit, Inject } from '@angular/core';

@Component({
selector: 'app-root',
template: '<hrc-hal-render-component [hal]="jsonValue"></hrc-hal-render-component>',
template: '<hrc-hal-render-component [hal]="jsonValue" [initialView]="initialView"></hrc-hal-render-component>',
})
export class AppComponent {

constructor(@Inject('initialView') public initialView: string) {
}

jsonValue = this.getPreJsonElement().textContent;

getPreJsonElement(): ChildNode {
Expand All @@ -19,4 +22,5 @@ export class AppComponent {

return preElement;
}

}
12 changes: 0 additions & 12 deletions projects/chrome-extension/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,4 @@ export class AppModule {
document.body.childNodes[1].remove();
}

checkFont(strFamily) {
const objDiv = document.createElement('div');

objDiv.style.fontFamily = strFamily;
objDiv.appendChild(document.createTextNode('FONT TEST'));

if (window.getComputedStyle) {
return window.getComputedStyle(objDiv, null).getPropertyValue('font-family') === strFamily;
}

return objDiv.style.fontFamily === strFamily;
}
}
3 changes: 2 additions & 1 deletion projects/chrome-extension/src/app/manifest/dev/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
],
"permissions": [
"*://*/*",
"<all_urls>"
"<all_urls>",
"storage"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
],
"permissions": [
"*://*/*",
"<all_urls>"
"<all_urls>",
"storage"
]
}
29 changes: 15 additions & 14 deletions projects/chrome-extension/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import '@webcomponents/custom-elements';
import 'core-js/es7/reflect';
import 'zone.js/dist/zone';

import { enableProdMode, NgModuleRef } from '@angular/core';
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';


document.addEventListener('DOMContentLoaded', () => {

if (halJsonPage()) {
const json = getJson();
if (json) {
Expand All @@ -20,19 +19,19 @@ document.addEventListener('DOMContentLoaded', () => {
changeBodyMargin();
}
}

}, false);


function bootstrapComponent() {

if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch((err) => {
console.log('error bootstraping plugin:' + err);
});
getInitialView().then((initialView) => {
platformBrowserDynamic([{ provide: 'initialView', useValue: initialView}]).bootstrapModule(AppModule)
.catch((err) => {
console.log('error bootstraping plugin:' + err);
});
});
}

function halJsonPage() {
Expand All @@ -46,18 +45,14 @@ function halJsonPage() {
}

function isJsonPage() {

return documentHasOnlyHtmlWithHeadAndBody() && bodyHasOnlyPre();

}

function documentHasOnlyHtmlWithHeadAndBody() {

return document.children.length === 1 && document.childNodes[0].nodeName.toUpperCase() === 'HTML' &&
document.children[0].children.length === 2 &&
document.children[0].childNodes[0].nodeName.toUpperCase() === 'HEAD' &&
document.children[0].childNodes[1].nodeName.toUpperCase() === 'BODY';

}

function bodyHasOnlyPre() {
Expand All @@ -66,13 +61,11 @@ function bodyHasOnlyPre() {
}

function getJson() {

try {
return JSON.parse(document.body.firstChild.textContent);
} catch (err) {
return null;
}

}

function addDocType() {
Expand All @@ -91,3 +84,11 @@ function addDocType() {
function changeBodyMargin() {
document.body.style.margin = '0px';
}

function getInitialView(): Promise<string> {
return new Promise<string>((resolve, reject) => {
chrome.storage.local.get(['hal-render-view'], (value) => {
resolve(value['hal-render-view']);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ export class HalFormTemplateComponent implements OnChanges {
hideLoader() {
this.loading = false;
}

}
45 changes: 35 additions & 10 deletions projects/hal-render-component/src/lib/hal-render.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ export class HalRenderComponent {

root: JsonElementNode;

json: String;
json: string;

currentView = CurrentView.TREE;
curView: CurrentView;

isAmudsenHalForm = false;
init: string;


@Input()
initialView: string;

currentView: CurrentView = null;

isAmundsenHalForm = false;

@Input()
set hal(value: string) {
Expand All @@ -31,11 +39,23 @@ export class HalRenderComponent {
this.json = value;
this.root = buildHalJsonTree(jsonValue, new Curies(), true);
this.expandAll();
this.isAmudsenHalForm = isAmundsenHalForm(jsonValue);
this.currentView = CurrentView.TREE;
this.isAmundsenHalForm = isAmundsenHalForm(jsonValue);
if (this.currentView === null) {
if (this.initialView === CurrentView.FORM && !this.isAmundsenHalForm) {
this.currentView = CurrentView.TREE;
} else if (this.initialView === null || this.initialView === undefined) {
this.currentView = CurrentView.TREE;
} else {
this.currentView = CurrentView[this.initialView];
}
}
}
}

get hal(): string {
return this.json;
}

expandAll() {
if (this.root) {
this.expand(this.root);
Expand All @@ -49,27 +69,32 @@ export class HalRenderComponent {
}

viewTree() {
this.currentView = CurrentView.TREE;
this.setView(CurrentView.TREE);
}

viewRaw() {
this.currentView = CurrentView.RAW;
this.setView(CurrentView.RAW);
}

viewForm() {
this.currentView = CurrentView.FORM;
this.setView(CurrentView.FORM);
}

setView(view: CurrentView) {
this.currentView = view;
chrome.storage.local.set({ 'hal-render-view': this.currentView });
}

showRawButton(): boolean {
return this.currentView !== CurrentView.RAW;
return this.currentView !== CurrentView.RAW;
}

showTreeButton(): boolean {
return this.currentView !== CurrentView.TREE;
}

showFormButton(): boolean {
return this.currentView !== CurrentView.FORM && this.isAmudsenHalForm;
return this.currentView !== CurrentView.FORM && this.isAmundsenHalForm;
}

isCurrentViewTree() {
Expand Down

0 comments on commit 71bcad0

Please sign in to comment.