mirror of https://github.com/artf/grapesjs.git
21 changed files with 525 additions and 362 deletions
@ -0,0 +1,102 @@ |
|||||
|
import { includes } from 'underscore'; |
||||
|
import Backbone from 'backbone'; |
||||
|
import View from './View'; |
||||
|
import Model from './Model'; |
||||
|
/*interface DomainView<TView, TModel>{ |
||||
|
constructor(model: TModel): TView |
||||
|
}*/ |
||||
|
type TModel<TCollection> = TCollection extends Backbone.Collection<infer TModel>? TModel: Model; |
||||
|
|
||||
|
export default abstract class DomainViews<TCollection extends Backbone.Collection<Model>, TItemView extends View> extends View<TModel<TCollection>> { |
||||
|
// Defines the View per type
|
||||
|
itemsView = ''; |
||||
|
|
||||
|
protected itemType = 'type'; |
||||
|
|
||||
|
reuseView = false; |
||||
|
|
||||
|
viewCollection: TItemView[] = []; |
||||
|
constructor(opts: any = {}, autoAdd = false) { |
||||
|
super(opts); |
||||
|
autoAdd && this.listenTo(this.collection, 'add', this.addTo); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Add new model to the collection |
||||
|
* @param {Model} model |
||||
|
* @private |
||||
|
* */ |
||||
|
private addTo(model: TModel<TCollection>) { |
||||
|
this.add(model); |
||||
|
} |
||||
|
|
||||
|
private itemViewNotFound(type: string) { |
||||
|
/*const { em, ns } = this; |
||||
|
const warn = `${ns ? `[${ns}]: ` : ''}'${type}' type not found`; |
||||
|
em?.logWarning(warn);*/ |
||||
|
} |
||||
|
protected abstract renderView(model: TModel<TCollection>, itemType: string): TItemView; |
||||
|
|
||||
|
/** |
||||
|
* Render new model inside the view |
||||
|
* @param {Model} model |
||||
|
* @param {Object} fragment Fragment collection |
||||
|
* @private |
||||
|
* */ |
||||
|
private add(model: TModel<TCollection>, fragment?: DocumentFragment) { |
||||
|
const { reuseView, viewCollection, itemsView = {} } = this; |
||||
|
var frag = fragment || null; |
||||
|
var typeField = model.get(this.itemType); |
||||
|
let view; |
||||
|
|
||||
|
//@ts-ignore
|
||||
|
if (model.view && reuseView) { |
||||
|
//@ts-ignore
|
||||
|
view = model.view; |
||||
|
} else { |
||||
|
view = this.renderView(model, typeField); |
||||
|
} |
||||
|
|
||||
|
viewCollection.push(view); |
||||
|
const rendered = view.render().el; |
||||
|
|
||||
|
if (frag) frag.appendChild(rendered); |
||||
|
else this.$el.append(rendered); |
||||
|
} |
||||
|
|
||||
|
render() { |
||||
|
var frag = document.createDocumentFragment(); |
||||
|
this.clearItems(); |
||||
|
this.$el.empty(); |
||||
|
|
||||
|
if (this.collection.length) |
||||
|
this.collection.each((model) => { |
||||
|
this.add(model, frag); |
||||
|
}, this); |
||||
|
|
||||
|
this.$el.append(frag); |
||||
|
this.onRender(); |
||||
|
return this; |
||||
|
} |
||||
|
|
||||
|
onRender() {} |
||||
|
|
||||
|
onRemoveBefore(items: TItemView[], opts: any) {} |
||||
|
onRemove(items: TItemView[], opts: any) {} |
||||
|
|
||||
|
remove(opts: any = {}) { |
||||
|
const { viewCollection } = this; |
||||
|
this.onRemoveBefore(viewCollection, opts); |
||||
|
this.clearItems(); |
||||
|
Backbone.View.prototype.remove.apply(this, opts); |
||||
|
this.onRemove(viewCollection, opts); |
||||
|
return this; |
||||
|
} |
||||
|
|
||||
|
clearItems() { |
||||
|
const items = this.viewCollection || []; |
||||
|
// TODO Traits do not update the target anymore
|
||||
|
// items.forEach(item => item.remove());
|
||||
|
// this.items = [];
|
||||
|
} |
||||
|
} |
||||
@ -1,19 +1,29 @@ |
|||||
import Backbone from 'backbone'; |
import Backbone from "backbone"; |
||||
import Model from './Model'; |
import Model from "./Model"; |
||||
|
import Module, { IBaseModule } from "./Module"; |
||||
|
|
||||
export default class View< |
export default class View< |
||||
TModel extends Model = Model, |
TModel extends Model = Model, |
||||
TElement extends Element = HTMLElement |
TElement extends Element = HTMLElement |
||||
> extends Backbone.View<TModel, TElement> { |
> extends Backbone.View<TModel, TElement> { |
||||
protected get pfx() { |
protected get pfx() { |
||||
return (this.model.module.em.config as any).stylePrefix || ''; |
return this.ppfx + this.config.stylePrefix || ""; |
||||
} |
} |
||||
|
|
||||
protected get ppfx() { |
protected get ppfx() { |
||||
return this.pfx + this.model.module.config.stylePrefix || ''; |
return (this.em.config as any).stylePrefix || ""; |
||||
|
} |
||||
|
|
||||
|
protected get module(): TModel extends Model<infer M>? M: unknown { |
||||
|
//console.log((this.collection.first as any).module)
|
||||
|
return this.model?.module ?? (this.collection as any).module; |
||||
} |
} |
||||
|
|
||||
protected get em() { |
protected get em() { |
||||
return this.model.module.em; |
return this.module.em; |
||||
|
} |
||||
|
|
||||
|
protected get config(): TModel extends Model<infer M> ? (M extends IBaseModule<infer C> ? C : unknown) : unknown{ |
||||
|
return this.module.config as any |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,21 +0,0 @@ |
|||||
import DomainViews from '../../domain_abstract/view/DomainViews'; |
|
||||
import FrameWrapView from './FrameWrapView'; |
|
||||
|
|
||||
export default class FramesView extends DomainViews { |
|
||||
constructor(opts = {}, config) { |
|
||||
super(opts, config, true); |
|
||||
this.listenTo(this.collection, 'reset', this.render); |
|
||||
} |
|
||||
|
|
||||
onRemoveBefore(items, opts) { |
|
||||
items.forEach(item => item.remove(opts)); |
|
||||
} |
|
||||
|
|
||||
onRender() { |
|
||||
const { config, $el } = this; |
|
||||
const { em } = config; |
|
||||
em && $el.attr({ class: `${em.getConfig().stylePrefix}frames` }); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
FramesView.prototype.itemView = FrameWrapView; |
|
||||
@ -0,0 +1,28 @@ |
|||||
|
import CanvasModule from '..'; |
||||
|
import DomainViews from '../../abstract/DomainViews'; |
||||
|
import Frames from '../model/Frames'; |
||||
|
import CanvasView from './CanvasView'; |
||||
|
import FrameWrapView from './FrameWrapView'; |
||||
|
|
||||
|
export default class FramesView extends DomainViews<Frames, FrameWrapView> { |
||||
|
canvasView: CanvasView; |
||||
|
private _module: CanvasModule; |
||||
|
|
||||
|
constructor(opts = {}, config: any) { |
||||
|
super(opts, true); |
||||
|
//console.log(this.collection)
|
||||
|
this.listenTo(this.collection, 'reset', this.render); |
||||
|
this.canvasView = config.canvasView |
||||
|
this._module = config.module; |
||||
|
} |
||||
|
|
||||
|
onRemoveBefore(items: FrameWrapView[], opts = {}) { |
||||
|
items.forEach(item => item.remove(opts)); |
||||
|
} |
||||
|
|
||||
|
onRender() { |
||||
|
const { $el, ppfx } = this; |
||||
|
$el.attr({ class: `${ppfx}frames` }); |
||||
|
} |
||||
|
protected renderView(item: any, type: string){return new FrameWrapView(item, this.canvasView)} |
||||
|
} |
||||
Loading…
Reference in new issue