@ -2,7 +2,10 @@ import { forEach, isEmpty, isNumber, isString, keys, result } from 'underscore';
import CanvasModule from '..' ;
import CanvasModule from '..' ;
import { ModuleModel } from '../../abstract' ;
import { ModuleModel } from '../../abstract' ;
import { BoxRect , PrevToNewIdMap } from '../../common' ;
import { BoxRect , PrevToNewIdMap } from '../../common' ;
import type Component from '../../dom_components/model/Component' ;
import ComponentWrapper from '../../dom_components/model/ComponentWrapper' ;
import ComponentWrapper from '../../dom_components/model/ComponentWrapper' ;
import { ComponentDefinition } from '../../dom_components/model/types' ;
import { ComponentsEvents } from '../../dom_components/types' ;
import Page from '../../pages/model/Page' ;
import Page from '../../pages/model/Page' ;
import { createId , isComponent , isObject } from '../../utils/mixins' ;
import { createId , isComponent , isObject } from '../../utils/mixins' ;
import FrameView from '../view/FrameView' ;
import FrameView from '../view/FrameView' ;
@ -13,6 +16,23 @@ import CanvasEvents from '../types';
const keyAutoW = '__aw' ;
const keyAutoW = '__aw' ;
const keyAutoH = '__ah' ;
const keyAutoH = '__ah' ;
export interface FrameProperties {
id? : string ;
page? : Page ;
component? : string | ComponentDefinition | ComponentDefinition [ ] | Component ;
width? : string | number | null ;
height? : string | number | null ;
x? : number ;
y? : number ;
attributes? : Record < string , unknown > ;
head ? : { tag : string ; attributes : any } [ ] ;
styles? : string | CssRuleJSON [ ] ;
refFrame? : string | Frame | null ;
refComponent? : string | Component | null ;
skipFromStorage? : boolean ;
[ key : string ] : unknown ;
}
const getDimension = ( frame : Frame , type : 'width' | 'height' ) = > {
const getDimension = ( frame : Frame , type : 'width' | 'height' ) = > {
const dim = frame . get ( type ) ;
const dim = frame . get ( type ) ;
const viewDim = frame . view ? . el [ type === 'width' ? 'offsetWidth' : 'offsetHeight' ] ;
const viewDim = frame . view ? . el [ type === 'width' ? 'offsetWidth' : 'offsetHeight' ] ;
@ -37,6 +57,7 @@ const getDimension = (frame: Frame, type: 'width' | 'height') => {
*
*
* /
* /
export default class Frame extends ModuleModel < CanvasModule > {
export default class Frame extends ModuleModel < CanvasModule > {
page? : Page ;
defaults() {
defaults() {
return {
return {
x : 0 ,
x : 0 ,
@ -58,8 +79,11 @@ export default class Frame extends ModuleModel<CanvasModule> {
/ * *
/ * *
* @hideconstructor
* @hideconstructor
* /
* /
constructor ( module : CanvasModule , attr : any ) {
constructor ( module : CanvasModule , attr : FrameProperties ) {
const page = attr . page ;
delete attr . page ;
super ( module , attr ) ;
super ( module , attr ) ;
this . page = page ;
const { em } = this ;
const { em } = this ;
const { styles , component } = this . attributes ;
const { styles , component } = this . attributes ;
const domc = em . Components ;
const domc = em . Components ;
@ -71,8 +95,11 @@ export default class Frame extends ModuleModel<CanvasModule> {
if ( ! isComponent ( component ) ) {
if ( ! isComponent ( component ) ) {
const wrp = isObject ( component ) ? component : { components : component } ;
const wrp = isObject ( component ) ? component : { components : component } ;
! wrp . type && ( wrp . type = 'wrapper' ) ;
! wrp . type && ( wrp . type = 'wrapper' ) ;
const Wrapper = domc . getType ( 'wrapper' ) ! . model ;
const Wrapper = ( domc . getType ( wrp . type as string ) || domc . getType ( 'wrapper' ) ! ) . model ;
this . set ( 'component' , new Wrapper ( wrp , modOpts ) ) ;
this . set ( 'component' , new Wrapper ( wrp , modOpts ) ) ;
} else {
this . updateComponentFrame ( component ) ;
this . emitComponentAdd ( component ) ;
}
}
if ( ! styles ) {
if ( ! styles ) {
@ -112,17 +139,38 @@ export default class Frame extends ModuleModel<CanvasModule> {
return this . get ( 'refFrame' ) ;
return this . get ( 'refFrame' ) ;
}
}
get refComponent ( ) : Component | undefined {
return this . get ( 'refComponent' ) ;
}
updateComponentFrame ( component : Component ) {
if ( component . frame !== this ) {
component . opt . frame = this ;
}
component . components ( ) . forEach ( ( child ) = > this . updateComponentFrame ( child ) ) ;
}
emitComponentAdd ( component : Component , opts : Record < string , any > = { } ) {
this . em . trigger ( ComponentsEvents . add , component , opts ) ;
component . components ( ) . forEach ( ( child ) = > this . emitComponentAdd ( child , opts ) ) ;
}
get root() {
get root() {
const { refFrame } = this ;
const { refFrame } = this ;
return refFrame ? . getComponent ( ) || this . getComponent ( ) ;
return refFrame ? . getComponent ( ) || this . getComponent ( ) ;
}
}
initRefs() {
initRefs() {
const { refFrame } = this ;
const { refFrame , refComponent , em } = this ;
if ( isString ( refFrame ) ) {
if ( isString ( refFrame ) ) {
const frame = this . module .framesById [ refFrame ] ;
const frame = this . module .framesById [ refFrame ] ;
frame && this . set ( { refFrame : frame } , { silent : true } ) ;
frame && this . set ( { refFrame : frame } , { silent : true } ) ;
}
}
if ( isString ( refComponent ) ) {
const component = em . Components . getById ( refComponent ) ;
component && this . set ( { refComponent : component } , { silent : true } ) ;
}
}
}
getBoxRect ( ) : BoxRect {
getBoxRect ( ) : BoxRect {
@ -138,7 +186,7 @@ export default class Frame extends ModuleModel<CanvasModule> {
}
}
onRemove() {
onRemove() {
! this . refFrame && this . getComponent ( ) . remove ( { root : 1 } ) ;
! this . refFrame && ! this . refComponent && this . getComponent ( ) . remove ( { root : 1 } ) ;
}
}
changesUp ( opt : any = { } ) {
changesUp ( opt : any = { } ) {
@ -224,7 +272,7 @@ export default class Frame extends ModuleModel<CanvasModule> {
}
}
getPage ( ) : Page | undefined {
getPage ( ) : Page | undefined {
return ( this . collection as unknown as Frames ) ? . page ;
return this . page || ( this . collection as unknown as Frames ) ? . page ;
}
}
_emitUpdated ( data = { } ) {
_emitUpdated ( data = { } ) {
@ -254,6 +302,8 @@ export default class Frame extends ModuleModel<CanvasModule> {
const defaults = result ( this , 'defaults' ) ;
const defaults = result ( this , 'defaults' ) ;
if ( opts . fromUndo ) delete obj . component ;
if ( opts . fromUndo ) delete obj . component ;
delete obj . skipFromStorage ;
delete obj . refComponent ;
delete obj . styles ;
delete obj . styles ;
delete obj . changesCount ;
delete obj . changesCount ;
obj [ keyAutoW ] && delete obj . width ;
obj [ keyAutoW ] && delete obj . width ;