Skip to content

Commit

Permalink
Cleanup of inline scripts (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
alansemenov committed Mar 21, 2022
1 parent af96582 commit 0321ae6
Show file tree
Hide file tree
Showing 20 changed files with 1,174 additions and 522 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {
repositories {
mavenLocal()
mavenCentral()
xp.enonicRepo()
xp.enonicRepo('dev')
}

node {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ group=com.enonic.app
projectName=siteimprove
appName=com.enonic.app.siteimprove
xpVersion=7.3.0
libAdminUiVersion=4.1.0
libAdminUiVersion=4.2.0-SNAPSHOT
version=1.6.2-SNAPSHOT
1,441 changes: 1,061 additions & 380 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"cssnano": "^5.1.4",
"enonic-admin-artifacts": "^1.8.1",
"error-logger-webpack-plugin": "^1.1.0",
"file-loader": "^6.2.0",
"less": "^4.1.2",
"less-loader": "^10.2.0",
"mini-css-extract-plugin": "^2.6.0",
Expand Down
21 changes: 4 additions & 17 deletions src/main/resources/admin/widgets/siteimprove/siteimprove.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
<widget data-th-id="${'widget-' + widgetId}">
<link rel="stylesheet" data-th-href="${portal.assetUrl({'_path=styles/siteimprove.css'})}" type="text/css" media="all"/>
<script data-th-src="${portal.assetUrl({'_path=js/siteimprove.js'})}"></script>
<div class="siteimprove"></div>
<script data-th-if="${!hasError}" data-th-src="${portal.assetUrl({'_path=js/siteimprove.js'})}" data-th-attr="data-config-service-url=${configServiceUrl}, data-content-id=${contentId}"></script>
<div class="siteimprove">
<div data-th-if="${hasError}" class="error" data-th-text="${errorMessage}"></div>
</div>

</widget>

<script data-th-inline="javascript">
/*<![CDATA[*/
var SITEIMPROVE = {
config: {
widgetId: [[${widgetId}]],
errorMessage: [[${errorMessage}]],
vhost: [[${vhost}]],
contentPath: [[${contentPath}]],
services: [[${services}]]
}
};

/*]]>*/
</script>
48 changes: 10 additions & 38 deletions src/main/resources/admin/widgets/siteimprove/siteimprove.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,23 @@
var contentLib = require('/lib/xp/content');
var portalLib = require('/lib/xp/portal');
var thymeleaf = require('/lib/thymeleaf');
var validator = require('./util/validator');
const portalLib = require('/lib/xp/portal');
const thymeleaf = require('/lib/thymeleaf');
const validator = require('./util/validator');

function handleGet(req) {

var contentId = req.params.contentId;
let contentId = req.params.contentId;

if (!contentId && portalLib.getContent()) {
contentId = portalLib.getContent()._id;
}

if (!contentId) {
return {
contentType: 'text/html',
body: '<widget class="error">No content selected</widget>'
};
}

var content = contentLib.get({key: contentId});
var site = contentLib.getSite({key: contentId});
var siteConfig = contentLib.getSiteConfig({key: contentId, applicationKey: app.name});
var pageId = (content.type.indexOf(':site') === -1 && site) ? content._path.replace(site._path, '') : '';
var errorMessage = validator.validate(contentId);
var contentPath = site ? content._path.slice(content._path.indexOf(site._name) - 1) : content._path;
const errorMessage = validator.validate(contentId);

var view = resolve('siteimprove.html');
var params = {
const view = resolve('siteimprove.html');
const params = {
widgetId: app.name,
configServiceUrl: portalLib.serviceUrl({ service: 'config'}),
contentId: contentId,
contentPath: contentPath,
errorMessage: errorMessage,
pageId: siteConfig ? pageId : -1,
vhost: siteConfig ? siteConfig.vhost : '',
services: {
sitesUrl: portalLib.serviceUrl({service: 'sites'}),
pagesUrl: portalLib.serviceUrl({service: 'pages'}),
dciOverviewUrl: portalLib.serviceUrl({service: 'dcioverview'}),
pageSummaryUrl: portalLib.serviceUrl({service: 'pagesummary'}),
crawlStatusUrl: portalLib.serviceUrl({service: 'crawlstatus'}),
crawlUrl: portalLib.serviceUrl({service: 'crawl'}),
checkStatusUrl: portalLib.serviceUrl({service: 'checkstatus'}),
checkUrl: portalLib.serviceUrl({service: 'check'}),
checkByUrlUrl: portalLib.serviceUrl({service: 'checkbyurl'}),
linksUrl: portalLib.serviceUrl({service: 'links'}),
checkUrlExistsUrl: portalLib.serviceUrl({service: 'checkurlexists'})
}
hasError: errorMessage.length > 0,
errorMessage: errorMessage
};

return {
Expand Down
24 changes: 14 additions & 10 deletions src/main/resources/admin/widgets/siteimprove/util/validator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var contentLib = require('/lib/xp/content');
var url = require('./url');
var siteImproveLib = require('/lib/siteimprove');
const contentLib = require('/lib/xp/content');
const url = require('./url');
const siteImproveLib = require('/lib/siteimprove');

function isAppConfigured() {
var hasUsername = !!app.config['siteimprove.username'];
var hasApiKey = !!app.config['siteimprove.apikey'];
const hasUsername = !!app.config['siteimprove.username'];
const hasApiKey = !!app.config['siteimprove.apikey'];
return hasUsername && hasApiKey;
}

Expand All @@ -22,21 +22,25 @@ function isValidVirtualHost(vhost) {

exports.validate = function validate(contentId) {
try {
var isSite = !!contentLib.getSite({key: contentId});
if (!contentId) {
return 'No content selected.'
}

const isSite = !!contentLib.getSite({key: contentId});

if (!isSite) {
return 'Content is not a site.'
}

var siteConfig = contentLib.getSiteConfig({
const siteConfig = contentLib.getSiteConfig({
key: contentId,
applicationKey: app.name
});
var content = contentLib.get({
const content = contentLib.get({
key: contentId,
branch: 'master'
});
var vhost = siteConfig ? siteConfig.vhost : null;
const vhost = siteConfig ? siteConfig.vhost : null;

if (!siteConfig) {
return 'Siteimprove app is not added to the site.';
Expand All @@ -54,7 +58,7 @@ exports.validate = function validate(contentId) {
}
return '';
} catch (error) {
var msg = (error && error.message) ? error.message : error;
const msg = (error && error.message) ? error.message : error;
return 'Server error: ' + (msg || 'Something went wrong.');
}
};
Binary file removed src/main/resources/assets/fonts/icomoon.eot
Binary file not shown.
14 changes: 0 additions & 14 deletions src/main/resources/assets/fonts/icomoon.svg

This file was deleted.

Binary file removed src/main/resources/assets/fonts/icomoon.ttf
Binary file not shown.
Binary file added src/main/resources/assets/fonts/icomoon.woff2
Binary file not shown.
35 changes: 13 additions & 22 deletions src/main/resources/assets/js/main.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import {Element} from 'lib-admin-ui/dom/Element';
import {Path} from 'lib-admin-ui/rest/Path';
import {SiteimproveWidget} from './widget/SiteimproveWidget';
import {WidgetError} from './widget/WidgetError';
import {SiteimproveWidgetConfig} from './widget/SiteimproveWidgetConfig';
import {CONFIG} from 'lib-admin-ui/util/Config';

type AppConfigType = {
config: SiteimproveWidgetConfig
};

declare const SITEIMPROVE: AppConfigType;
const widgetId = SITEIMPROVE.config.widgetId;

(() => {
(async () => {
const configServiceUrl = document.currentScript.getAttribute('data-config-service-url');
if (!configServiceUrl) {
throw 'Unable to fetch widget config';
}

const widgetContainer = document.getElementById(`widget-${widgetId}`);
const contentId = document.currentScript.getAttribute('data-content-id');
await CONFIG.init(`${configServiceUrl}?contentId=${contentId}`);
const widgetContainer = document.getElementById(`widget-${CONFIG.getString('widgetId')}`);

if (widgetContainer) {
const container = widgetContainer.getElementsByClassName('siteimprove').item(0);
Expand All @@ -22,15 +19,9 @@ const widgetId = SITEIMPROVE.config.widgetId;
}
const containerEl = Element.fromHtmlElement((container as HTMLElement), true);

if (SITEIMPROVE.config.errorMessage) {
const errorEl = new WidgetError(SITEIMPROVE.config.errorMessage);
containerEl.appendChild(errorEl);
} else {
const widget = new SiteimproveWidget();
containerEl.appendChild(widget);
widget.initialize(SITEIMPROVE.config);

widget.render();
}
const widget = new SiteimproveWidget();
containerEl.appendChild(widget);
widget.initialize(CONFIG.getConfig());
widget.render();
}
})();
6 changes: 3 additions & 3 deletions src/main/resources/assets/js/resource/SiteimproveRequest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Path} from 'lib-admin-ui/rest/Path';
import {ResourceRequest} from 'lib-admin-ui/rest/ResourceRequest';
import {ServiceUrl, SiteimproveWidgetConfig} from '../widget/SiteimproveWidgetConfig';
import {JSONObject, JSONValue} from 'lib-admin-ui/types';

export abstract class SiteimproveRequest<PARSED_TYPE>
extends ResourceRequest<PARSED_TYPE> {

static serviceUrls: ServiceUrl;
static serviceUrls: JSONValue;

private readonly apiPath: string;

Expand All @@ -26,7 +26,7 @@ export abstract class SiteimproveRequest<PARSED_TYPE>
return Path.fromString(this.apiPath);
}

static setConfig(config: SiteimproveWidgetConfig) {
static setConfig(config: JSONObject) {
this.serviceUrls = config.services;
}
}
7 changes: 1 addition & 6 deletions src/main/resources/assets/js/util/SiteimproveValidator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as Q from 'q';
import {StringHelper} from 'lib-admin-ui/util/StringHelper';
import {Path} from 'lib-admin-ui/rest/Path';
import {SiteimproveFetcher} from '../resource/SiteimproveFetcher';
import {UrlHelper} from './UrlHelper';
Expand All @@ -25,11 +24,7 @@ type GenericAsyncVR<T> = Q.Promise<ValidationResult | T>;

export class SiteimproveValidator {

static validate(serverError: string, url: string, path: Path): GenericAsyncVR<GenericAsyncVR<AsyncVR>> {
if (!StringHelper.isEmpty(serverError)) {
return Q({error: serverError, type: ValidationType.ERROR});
}

static validate(url: string, path: Path): GenericAsyncVR<GenericAsyncVR<AsyncVR>> {
return SiteimproveFetcher.fetchSiteIdByUrl(url).then((siteId: number) => {
if (!siteId) {
const error = `"${url}" is not enabled for your Siteimprove account.`;
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/assets/js/widget/SiteimproveWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {PageSummaryRequest} from '../resource/PageSummaryRequest';
import {CrawlStatusRequest} from '../resource/CrawlStatusRequest';
import {CheckStatusRequest} from '../resource/CheckStatusRequest';
import {PageReportLinksRequest} from '../resource/PageReportLinksRequest';
import {SiteimproveWidgetConfig} from './SiteimproveWidgetConfig';
import {JSONObject} from 'lib-admin-ui/types';
import {CONFIG} from 'lib-admin-ui/util/Config';

export class SiteimproveWidget
extends DivEl {
Expand All @@ -37,15 +38,14 @@ export class SiteimproveWidget
this.loadMask = new LoadMask(this);
}

initialize(config: SiteimproveWidgetConfig) {
initialize(config: JSONObject) {
SiteimproveRequest.setConfig(config);

this.loadMask.show();

const {errorMessage, vhost} = config;
const contentPath = Path.fromString(config.contentPath);
const contentPath = Path.fromString(CONFIG.getString('contentPath'));

SiteimproveValidator.validate(errorMessage, vhost, contentPath).then((result: ValidationResult) => {
SiteimproveValidator.validate(CONFIG.getString('vhost'), contentPath).then((result: ValidationResult) => {
const {url, error, siteId, pageId, type} = result;

if (type === ValidationType.ERROR || !StringHelper.isBlank(error)) {
Expand Down
11 changes: 0 additions & 11 deletions src/main/resources/assets/js/widget/SiteimproveWidgetConfig.ts

This file was deleted.

7 changes: 2 additions & 5 deletions src/main/resources/assets/styles/icons.less
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
@font-face {
font-family: 'siteimprove';
src: url('../fonts/icomoon.eot?d2dpl4');
src: url('../fonts/icomoon.eot?#iefixd2dpl4') format('embedded-opentype'),
url('../fonts/icomoon.woff?d2dpl4') format('woff'),
url('../fonts/icomoon.ttf?d2dpl4') format('truetype'),
url('../fonts/icomoon.svg?d2dpl4#icomoon-release') format('svg');
src: url('../fonts/icomoon.woff2') format('woff2'),
url('../fonts/icomoon.woff') format('woff');
}

.icon {
Expand Down
54 changes: 54 additions & 0 deletions src/main/resources/services/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const contentLib = require('/lib/xp/content');
const portalLib = require('/lib/xp/portal');

function handleGet(req) {
let contentId = req.params.contentId;
let content;
let site;
let siteConfig;
let pageId = -1;
let contentPath;

if (!contentId) {
const content = portalLib.getContent();
if (content) {
contentId = content._id;
}
}

if (contentId) {
content = contentLib.get({key: contentId});
site = contentLib.getSite({key: contentId});
contentPath = site ? content._path.slice(content._path.indexOf(site._name) - 1) : content._path;
siteConfig = contentLib.getSiteConfig({key: contentId, applicationKey: app.name});
if (siteConfig) {
pageId = (content.type.indexOf(':site') === -1 && site) ? content._path.replace(site._path, '') : '';
}
}

return {
status: 200,
contentType: 'application/json',
body: {
widgetId: app.name,
contentPath,
pageId,
vhost: siteConfig ? siteConfig.vhost : '',
services: {
sitesUrl: portalLib.serviceUrl({service: 'sites'}),
pagesUrl: portalLib.serviceUrl({service: 'pages'}),
dciOverviewUrl: portalLib.serviceUrl({service: 'dcioverview'}),
pageSummaryUrl: portalLib.serviceUrl({service: 'pagesummary'}),
crawlStatusUrl: portalLib.serviceUrl({service: 'crawlstatus'}),
crawlUrl: portalLib.serviceUrl({service: 'crawl'}),
checkStatusUrl: portalLib.serviceUrl({service: 'checkstatus'}),
checkUrl: portalLib.serviceUrl({service: 'check'}),
checkByUrlUrl: portalLib.serviceUrl({service: 'checkbyurl'}),
linksUrl: portalLib.serviceUrl({service: 'links'}),
checkUrlExistsUrl: portalLib.serviceUrl({service: 'checkurlexists'})
}
}
};
}

exports.get = handleGet;
5 changes: 5 additions & 0 deletions src/main/resources/services/config/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<service>
<allow>
<principal>role:system.authenticated</principal>
</allow>
</service>
Loading

0 comments on commit 0321ae6

Please sign in to comment.