Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
DI,
IContainer
} from '@aurelia/kernel';
import {
PropertyBinding,
BindingMode,
FromViewBindingBehavior,
OneTimeBindingBehavior,
ToViewBindingBehavior,
TwoWayBindingBehavior
} from '@aurelia/runtime';
import { assert } from '@aurelia/testing';
const tests = [
{ Behavior: OneTimeBindingBehavior, mode: BindingMode.oneTime },
{ Behavior: ToViewBindingBehavior, mode: BindingMode.toView },
{ Behavior: FromViewBindingBehavior, mode: BindingMode.fromView },
{ Behavior: TwoWayBindingBehavior, mode: BindingMode.twoWay }
];
describe('BindingModeBehavior', function () {
const container: IContainer = DI.createContainer();
let sut: OneTimeBindingBehavior;
let binding: PropertyBinding;
for (const { Behavior, mode } of tests) {
const initModeArr = [BindingMode.oneTime, BindingMode.toView, BindingMode.fromView, BindingMode.twoWay, BindingMode.default];
for (const initMode of initModeArr) {
describe(Behavior.name, function () {
// eslint-disable-next-line mocha/no-hooks
beforeEach(function () {
render(flags, dom, context, renderable, target, instruction) {
const expr = ensureExpression(this.parser, instruction.from, 48 /* IsPropertyCommand */ | BindingMode.toView);
const binding = applyBindingBehavior(new PropertyBinding(expr, target.style, instruction.to, BindingMode.toView, this.observerLocator, context), expr, context);
addBinding(renderable, binding);
}
};
import { TranslationBinding } from './translation-binding';
export const TranslationParametersInstructionType = 'tpt';
// `.bind` part is needed here only for vCurrent compliance
const attribute = 't-params.bind';
@attributePattern({ pattern: attribute, symbols: '' })
export class TranslationParametersAttributePattern {
public [attribute](rawName: string, rawValue: string, parts: string[]): AttrSyntax {
return new AttrSyntax(rawName, rawValue, '', attribute);
}
}
export class TranslationParametersBindingInstruction {
public readonly type: string = TranslationParametersInstructionType;
public mode: BindingMode.toView = BindingMode.toView;
public constructor(
public from: IsBindingBehavior,
public to: string,
) {}
}
@bindingCommand(attribute)
export class TranslationParametersBindingCommand implements BindingCommandInstance {
public readonly bindingType: BindingType.BindCommand = BindingType.BindCommand;
public compile(binding: PlainAttributeSymbol | BindingSymbol): TranslationParametersBindingInstruction {
return new TranslationParametersBindingInstruction(binding.expression as IsBindingBehavior, getTarget(binding, false));
}
}
render(flags, dom, context, renderable, target, instruction) {
const expr = ensureExpression(this.parser, instruction.from, 48 /* IsPropertyCommand */ | BindingMode.toView);
const binding = applyBindingBehavior(new AttributeBinding(expr, target, instruction.attr /* targetAttribute */, instruction.to /* targetKey */, BindingMode.toView, this.observerLocator, context), expr, context);
addBinding(renderable, binding);
}
};
public static from(def: CustomAttributeDefinition): AttrInfo {
const info = new AttrInfo(def.name, def.isTemplateController);
const bindables = def.bindables;
const defaultBindingMode = def.defaultBindingMode !== void 0 && def.defaultBindingMode !== BindingMode.default
? def.defaultBindingMode
: BindingMode.toView;
let bindable: BindableDefinition;
let prop: string;
let mode: BindingMode;
let hasPrimary: boolean = false;
let isPrimary: boolean = false;
let bindableInfo: BindableInfo;
for (prop in bindables) {
bindable = bindables[prop];
// explicitly provided property name has priority over the implicit property name
if (bindable.property !== void 0) {
prop = bindable.property;
}
if (bindable.mode !== void 0 && bindable.mode !== BindingMode.default) {
mode = bindable.mode;
this.bindingType = BindingType.TwoWayCommand;
}
public compile(binding: PlainAttributeSymbol | BindingSymbol): AttributeInstruction {
return new TwoWayBindingInstruction(binding.expression as IsBindingBehavior, getTarget(binding, false));
}
}
BindingCommandResource.define('two-way', TwoWayBindingCommand);
// Not bothering to throw on non-existing modes, should never happen anyway.
// Keeping all array elements of the same type for better optimizeability.
const modeToProperty = ['', '$1', '$2', '', '$4', '', '$6'];
const commandToMode = {
'bind': BindingMode.toView,
'one-time': BindingMode.oneTime,
'to-view': BindingMode.toView,
'from-view': BindingMode.fromView,
'two-way': BindingMode.twoWay,
};
export interface DefaultBindingCommand extends IBindingCommand {}
export class DefaultBindingCommand implements IBindingCommand {
public static readonly register: IRegistry['register'];
public readonly bindingType: BindingType.BindCommand;
public readonly $1: typeof OneTimeBindingCommand.prototype.compile;
public readonly $2: typeof ToViewBindingCommand.prototype.compile;
public readonly $4: typeof FromViewBindingCommand.prototype.compile;
public readonly $6: typeof TwoWayBindingCommand.prototype.compile;
constructor() {
this.bindingType = BindingType.BindCommand;
this.$1 = OneTimeBindingCommand.prototype.compile;
<input class="au" type="checkbox">
<button class="au">Reset</button>
`,
instructions: [
[
{
type: TargetedInstructionType.twoWayBinding,
srcOrExpr: 'name',
dest: 'value'
}
],
[
{
type: TargetedInstructionType.propertyBinding, mode: BindingMode.toView,
srcOrExpr: 'name',
dest: 'textContent'
},
{
type: TargetedInstructionType.stylePropertyBinding,
srcOrExpr: 'nameTagColor',
dest: 'color'
}
],
[
{
type: TargetedInstructionType.twoWayBinding,
srcOrExpr: 'nameTagColor',
dest: 'value'
}
],
render(itemRenderable) {
const text = AuNode.createText();
const wrapper = AuNode.createTemplate().appendChild(text);
const nodes = new AuNodeSequence(dom, wrapper);
const itemBinding = new PropertyBinding(new AccessScopeExpression('item'), text, 'textContent', BindingMode.toView, observerLocator, container);
binding.persistentFlags |= strategy;
(itemRenderable as Writable).nodes = nodes;
addBinding(itemRenderable, itemBinding);
}
};
(ctx, $1, [attr, to, value]) => [`${attr}.to-view`, value, { type: TT.propertyBinding, from: new AccessScopeExpression(value), to, mode: BindingMode.toView, oneTime: false }],
(ctx, $1, [attr, to, value]) => [`${attr}.one-time`, value, { type: TT.propertyBinding, from: new AccessScopeExpression(value), to, mode: BindingMode.oneTime, oneTime: true }],
(ctx, pdName, pdProp, pdAttr) => ({ [pdName]: { property: pdProp, attribute: pdAttr, mode: BindingMode.toView } }),
(ctx, pdName, pdProp, pdAttr) => ({ [pdName]: { property: pdProp, attribute: pdAttr, mode: BindingMode.fromView } }),