Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Adds an AttributesObject to the context of each component and its sub-components, if any, rather than adding the attributes to the global context. #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
86 changes: 44 additions & 42 deletions src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,50 @@ class TwigAdapter extends Fractal.Adapter {
const render = Twig.Template.prototype.render;
Twig.Template.prototype.render = function(context, params) {

let attributes = new AttributesObject();

function AttributesObject() {
let self = this;
this.classes = '';
this.attr = [];

this.addClass = function(...str) {
// console.log(str);
self.classes = _.flatten(str).join(' ');

return self;
};

this.removeClass = function(...str) {
// todo implement
// self.classes = str.join(' ');

return self;
};

this.setAttribute = function(attribute, value) {
let str = `${attribute}="${value}"`;

self.attr.push(str);
self.attr = _.uniq(self.attr);

return self;
};
}

AttributesObject.prototype.toString = function toString() {
let attrList = [
this.classes ? `class="${this.classes}"` : '',
this.attr ? this.attr.join(' ') : '',
];

return attrList.join(' ');
};

if (!config.pristine) {
context['attributes'] = attributes;
}

if (!self._config.pristine && this.id) {

let handle = null;
Expand Down Expand Up @@ -177,59 +221,17 @@ class TwigAdapter extends Fractal.Adapter {
}

render(path, str, context, meta) {
let attributes = new AttributesObject();
let self = this;

meta = meta || {};

function AttributesObject() {
let self = this;
this.classes = '';
this.attr = [];

this.addClass = function(...str) {
// console.log(str);
self.classes = _.flatten(str).join(' ');

return self;
};

this.removeClass = function(...str) {
// todo implement
// self.classes = str.join(' ');

return self;
};

this.setAttribute = function(attribute, value) {
let str = `${attribute}="${value}"`;

self.attr.push(str);
self.attr = _.uniq(self.attr);

return self;
};
}

AttributesObject.prototype.toString = function toString() {
let attrList = [
this.classes ? `class="${this.classes}"` : '',
this.attr ? this.attr.join(' ') : '',
];

return attrList.join(' ');
};



if (!this._config.pristine) {
setEnv('_self', meta.self, context);
setEnv('_target', meta.target, context);
setEnv('_env', meta.env, context);
setEnv('_config', this._app.config(), context);
setEnv('title_prefix', '', context);
setEnv('title_suffix', '', context);
setEnv('attributes', attributes, context);
}

return new Promise(function(resolve, reject){
Expand Down