Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: paths in links #5

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="css">
<ng-container *ngFor="let css of cssLinks"> <link rel="stylesheet" [href]="addHttp(css) | url" /> </ng-container>
<ng-container *ngFor="let css of cssLinks"> <link rel="stylesheet" [href]="css | url" /> </ng-container>
</div>
<div>
<ng-container *ngFor="let node of content">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ export class EditorViewComponent implements OnInit, OnDestroy {
data: null
};

const currentNode = parseInt(property.split('_').pop());
const componentData = {};

const contentHtml = !result.editable
? Converters.json2html(data, true, true, false, false)
? Converters.json2html(data, true, true, false, false, currentNode)
: Converters.json2xedit(property, data, this._moduleService, componentData, true, true, false, false);

if (result.editable) {
Expand Down
9 changes: 7 additions & 2 deletions src/app/models/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ export class File extends History {
Object.keys(json.nodes).forEach(nodeKey => {
const node = json.nodes[nodeKey];
schemas[nodeKey] = node.schema;
this.css = union(this.css, hasIn('css', node) ? node.css : []);
this.js = union(this.js, hasIn('js', node) ? node.js : []);

// convert all css.js resources into a full url path
const cssPath = hasIn('css', node) ? Converters.getPathByResource(node.css, node.id) : [];
const jsPath = hasIn('js', node) ? Converters.getPathByResource(node.js, node.id) : [];

this.css = union(this.css, cssPath);
this.js = union(this.js, jsPath);
});
}

Expand Down
60 changes: 54 additions & 6 deletions src/utils/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@ export class Converters {
* @param json Json object with content
* @param showIds If true added attribute id in tags
*/
static json2html(json, showIds = true, processXedit = true, resetIds = false, enableHover = true) {
static json2html(
json,
showIds = true,
processXedit = true,
resetIds = false,
enableHover = true,
currentNode?: number
) {
// Empty Elements - HTML 4.01
const empty = [
'area',
Expand All @@ -210,7 +217,14 @@ export class Converters {
if (json.child) {
child = Object.keys(json.child)
.map(function(uuid: string) {
return Converters.json2html(json.child[uuid], showIds, processXedit, resetIds, enableHover);
return Converters.json2html(
json.child[uuid],
showIds,
processXedit,
resetIds,
enableHover,
currentNode
);
})
.join('');
}
Expand All @@ -227,7 +241,7 @@ export class Converters {
if (Array.isArray(value)) {
value = value.join(' ');
}
return Converters.parseAttributes(key, value, processXedit, tag);
return Converters.parseAttributes(key, value, processXedit, tag, currentNode);
})
.join(' ');
if (attr !== '') {
Expand Down Expand Up @@ -310,6 +324,7 @@ export class Converters {

if (json.node === 'element') {
const { attr, tag } = json;
const currentNode = parseInt(nodeName.split('_').pop());
let { uuid } = json;

let attrString = '';
Expand All @@ -328,7 +343,7 @@ export class Converters {
if (Array.isArray(value)) {
value = value.join(' ');
}
return Converters.parseAttributes(key, value, processXedit, tag);
return Converters.parseAttributes(key, value, processXedit, tag, currentNode);
})
.join(' ');
}
Expand Down Expand Up @@ -394,14 +409,47 @@ export class Converters {
}
}

private static parseAttributes(key, value, processXedit, tag = 'a') {
/**
* Get the full api path pointing to the resource
*
* @param params Object with id or path of the resource, and id of current node
*/
private static getPathFromApi(params) {
return Router.configUrl(Api.getResourceUrl(), params);
}

/**
* Get the full path of a single resource or resource array
*
* @param resource String or string array of resources
* @param currentNode Number node identifier from where to start looking for the resource
*/
public static getPathByResource(resource: any, currentNode: number) {
let params = {};
if (currentNode) {
params['currentNode'] = currentNode;
}
if (Array.isArray(resource)) {
for (let index in resource) {
params['id'] = resource[index];
// array content is overwritten with the value of its full url
resource[index] = Converters.getPathFromApi(params);
}
} else {
params['id'] = resource;
resource = Converters.getPathFromApi(params);
}
return resource;
}

private static parseAttributes(key, value, processXedit, tag = 'a', currentNode) {
let extraData = '';
const linkType = hasIn(tag, XeditMapper.LINK_TYPES) ? XeditMapper.LINK_TYPES[tag] : 'href';
if (processXedit && contains(key, XeditMapper.requiredXeditAttributes)) {
if (equals(key, XeditMapper.TAG_LINK)) {
extraData = value;
if (!/^(f|ht)tps?:\/\//i.test(extraData)) {
extraData = Router.configUrl(Api.getResourceUrl(), { id: value });
extraData = Converters.getPathByResource(value, currentNode);
}
extraData = `${linkType}="${extraData}"`;
}
Expand Down