Skip to content

Commit

Permalink
feat(relative-time): relative time bindingbehavior
Browse files Browse the repository at this point in the history
adds a bindingbehavior for the relative-time feature
  • Loading branch information
zewa666 committed Apr 6, 2017
1 parent 9028b63 commit fdddded
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
28 changes: 21 additions & 7 deletions src/aurelia-i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ import {PLATFORM} from 'aurelia-pal';

import {I18N} from './i18n';
import {RelativeTime} from './relativeTime';
import {DfValueConverter} from './df';
import {NfValueConverter} from './nf';
import {RtValueConverter} from './rt';
import {TValueConverter} from './t';
import {TBindingBehavior} from './t';
import {TCustomAttribute} from './t';
import {TParamsCustomAttribute} from './t';
import {
DfValueConverter,
DfBindingBehavior
} from './df';
import {
NfValueConverter,
NfBindingBehavior
} from './nf';
import {
RtValueConverter,
RtBindingBehavior
} from './rt';
import {
TValueConverter,
TBindingBehavior,
TCustomAttribute,
TParamsCustomAttribute
} from './t';
import {BaseI18N} from './base-i18n';
import {Backend} from './aurelia-i18n-loader';

Expand Down Expand Up @@ -77,8 +88,11 @@ export {
I18N,
RelativeTime,
DfValueConverter,
DfBindingBehavior,
NfValueConverter,
NfBindingBehavior,
RtValueConverter,
RtBindingBehavior,
TValueConverter,
TBindingBehavior,
TCustomAttribute,
Expand Down
4 changes: 2 additions & 2 deletions src/df.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export class DfBindingBehavior {
// bind the signal behavior
this.signalBindingBehavior.bind(binding, source, 'aurelia-translation-signal');

// rewrite the expression to use the TValueConverter.
// pass through any args to the binding behavior to the TValueConverter
// rewrite the expression to use the DfValueConverter.
// pass through any args to the binding behavior to the DfValueConverter
let sourceExpression = binding.sourceExpression;

// do create the sourceExpression only once
Expand Down
4 changes: 2 additions & 2 deletions src/nf.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export class NfBindingBehavior {
// bind the signal behavior
this.signalBindingBehavior.bind(binding, source, 'aurelia-translation-signal');

// rewrite the expression to use the TValueConverter.
// pass through any args to the binding behavior to the TValueConverter
// rewrite the expression to use the NfValueConverter.
// pass through any args to the binding behavior to the NfValueConverter
let sourceExpression = binding.sourceExpression;

// do create the sourceExpression only once
Expand Down
39 changes: 38 additions & 1 deletion src/rt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {RelativeTime} from './relativeTime';
import { RelativeTime } from './relativeTime';
import { SignalBindingBehavior } from 'aurelia-templating-resources';
import { ValueConverter } from 'aurelia-binding';

export class RtValueConverter {
static inject() { return [RelativeTime]; }
Expand All @@ -21,3 +23,38 @@ export class RtValueConverter {
return this.service.getRelativeTime(value);
}
}

export class RtBindingBehavior {
static inject() {return [SignalBindingBehavior];}

constructor(signalBindingBehavior) {
this.signalBindingBehavior = signalBindingBehavior;
}

bind(binding, source) {
// bind the signal behavior
this.signalBindingBehavior.bind(binding, source, 'aurelia-translation-signal');

// rewrite the expression to use the RtValueConverter.
// pass through any args to the binding behavior to the RtValueConverter
let sourceExpression = binding.sourceExpression;

// do create the sourceExpression only once
if (sourceExpression.rewritten) {
return;
}
sourceExpression.rewritten = true;

let expression = sourceExpression.expression;
sourceExpression.expression = new ValueConverter(
expression,
'rt',
sourceExpression.args,
[expression, ...sourceExpression.args]);
}

unbind(binding, source) {
// unbind the signal behavior
this.signalBindingBehavior.unbind(binding, source);
}
}

0 comments on commit fdddded

Please sign in to comment.