-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
218 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Model } from '../..'; | ||
import Trait, { TraitProperties } from './Trait'; | ||
|
||
export interface TraitItemProperties extends TraitProperties { | ||
prefix: string; | ||
} | ||
|
||
export default class TraitItem<TModel extends Model = Model, TraitValueType = any> extends Trait< | ||
TModel, | ||
TraitValueType | ||
> { | ||
readonly prefix: string; | ||
constructor(name: string, model: TModel, opts: any) { | ||
super(name, model, opts); | ||
this.prefix = opts.prefix; | ||
} | ||
|
||
public get value(): TraitValueType { | ||
const { prefix, model, name } = this; | ||
const value = model.get(prefix)[name]; | ||
return value ?? this.opts.default; | ||
} | ||
public set value(value: TraitValueType) { | ||
const { name, model, prefix } = this; | ||
this.updatingValue = true; | ||
model.set(prefix, { ...model.get(prefix), [name]: value }); | ||
this.updatingValue = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { InputProperties, InputViewProperties } from '..'; | ||
import { Model } from '../..'; | ||
import Trait, { TraitProperties } from './Trait'; | ||
import TraitItem from './TraitItem'; | ||
|
||
export interface TraitListProperties extends TraitProperties { | ||
default?: any; | ||
value?: any; | ||
} | ||
|
||
export default class TraitList<TModel extends Model = Model> extends Trait<TModel, Trait[]> { | ||
// traits: (InputViewProperties) [] | ||
collection?: Trait[]; | ||
constructor(name: string, model: TModel, opts?: TraitListProperties) { | ||
super(name, model, { ...opts, type: 'list', changeProp: true } as any); | ||
// this.model.on("all", (e) => console.log(e)) | ||
// this.traits = opts?.traits ?? [{type: "list"}] | ||
model.get(name) ?? model.set(name, {}); | ||
} | ||
|
||
protected setValueFromModel() { | ||
this.collection = undefined; | ||
if (!this.updatingValue) { | ||
this.view?.onUpdateEvent(this.value); | ||
} | ||
} | ||
|
||
public get value(): Trait[] { | ||
const { model, name } = this; | ||
if (!this.collection) { | ||
const map = model.get(name); | ||
this.collection = Object.keys(map).map( | ||
key => new TraitItem(key, model, { type: 'text', name: key, prefix: name, value: map[key] }) | ||
); | ||
} | ||
return this.collection!; | ||
} | ||
|
||
public set value(values: Trait[]) { | ||
const { name, model } = this; | ||
this.updatingValue = true; | ||
|
||
model.set( | ||
name, | ||
values.reduce((map: any, tr) => { | ||
map[tr.name] = tr.value; | ||
return map; | ||
}, {}) | ||
); | ||
this.updatingValue = false; | ||
} | ||
|
||
public add(key: string) { | ||
const { model, name } = this; | ||
if (!this.collection?.find(tr => tr.name == key)) { | ||
this.value.push(new TraitItem(key, model, { type: 'text', name: key, prefix: name })); | ||
} | ||
} | ||
|
||
public remove(key: string) { | ||
const index = this.collection?.findIndex(tr => tr.name == key) ?? -1; | ||
if (index > -1) { | ||
this.value.splice(index, 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { isString } from 'underscore'; | ||
import TraitView from './TraitView'; | ||
import { Model } from '../..'; | ||
import EditorModel from '../../../editor/model/Editor'; | ||
import TraitList, { TraitListProperties } from '../model/TraitList'; | ||
import { TraitViewOpts } from './TraitView'; | ||
import InputFactory from '..'; | ||
|
||
export interface TraitListViewOpts extends TraitViewOpts { | ||
default?: any; | ||
name?: string; | ||
label?: string; | ||
paceholder?: string; | ||
noLabel?: boolean; | ||
} | ||
|
||
export default class TraitListView<TModel extends Model = Model> extends TraitView<TraitList<TModel>> { | ||
protected type = 'list'; | ||
|
||
get inputValue(): any { | ||
return this.target.value; | ||
} | ||
set inputValue(value: any) {} | ||
constructor(em: EditorModel, opts?: TraitListViewOpts) { | ||
super(em, opts); | ||
} | ||
|
||
setTarget(popertyName: string, model: TModel, opts?: TraitListProperties): this; | ||
setTarget(target: TraitList<TModel>): this; | ||
setTarget(target: unknown, model?: TModel, opts?: TraitListProperties) { | ||
if (isString(target) && model !== undefined) { | ||
target = new TraitList(target, model, opts); | ||
} | ||
this.target = target as TraitList<TModel>; | ||
this.model = this.target.model as any; | ||
this.name ?? (this.name = this.target.name); | ||
this.target.registerForUpdateEvent(this); | ||
return this; | ||
} | ||
|
||
render() { | ||
const { em } = this; | ||
var frag = document.createDocumentFragment(); | ||
this.$el.empty(); | ||
|
||
if (this.target.value.length) { | ||
this.target.value.forEach(view => { | ||
const rendered = InputFactory.buildView(view, em, view.opts).render().el; | ||
console.log(rendered); | ||
frag.appendChild(rendered); | ||
}); | ||
} | ||
console.log(frag); | ||
|
||
this.$el.append(frag); | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.