@ -1,35 +1,16 @@
import Backbone from 'backbone' ;
import {
isString ,
isFunction ,
isArray ,
result ,
each ,
bindAll
} from 'underscore' ;
import {
on ,
off ,
matches ,
getElement ,
getPointerEvent ,
isTextNode ,
getModel
} from 'utils/mixins' ;
import { isString , isFunction , isArray , result , each , bindAll } from 'underscore' ;
import { on , off , matches , getElement , getPointerEvent , isTextNode , getModel } from 'utils/mixins' ;
import { getComponentIds } from '../dom_components/model/Components' ;
const $ = Backbone . $ ;
const noop = ( ) => { } ;
export default Backbone . View . extend ( {
initialize ( opt ) {
this . opt = opt || { } ;
bindAll (
this ,
'startSort' ,
'onMove' ,
'endMove' ,
'rollback' ,
'updateOffset' ,
'moveDragHelper'
) ;
bindAll ( this , 'startSort' , 'onMove' , 'endMove' , 'rollback' , 'updateOffset' , 'moveDragHelper' ) ;
var o = opt || { } ;
this . elT = 0 ;
this . elL = 0 ;
@ -46,7 +27,7 @@ export default Backbone.View.extend({
this . pfx = o . pfx || '' ;
this . ppfx = o . ppfx || '' ;
this . freezeClass = o . freezeClass || this . pfx + 'freezed' ;
this . onStart = o . onStart || '' ;
this . onStart = o . onStart || noop ;
this . onEndMove = o . onEndMove || '' ;
this . customTarget = o . customTarget ;
this . onEnd = o . onEnd ;
@ -63,7 +44,7 @@ export default Backbone.View.extend({
this . document = o . document || document ;
this . $document = $ ( this . document ) ;
this . dropContent = null ;
this . em = o . em || '' ;
this . em = o . em || null ;
this . dragHelper = null ;
this . canvasRelative = o . canvasRelative || 0 ;
this . selectOnEnd = ! o . avoidSelectOnEnd ;
@ -93,9 +74,7 @@ export default Backbone.View.extend({
getDocuments ( el ) {
const em = this . em ;
const elDoc = el
? el . ownerDocument
: em && em . get ( 'Canvas' ) . getBody ( ) . ownerDocument ;
const elDoc = el ? el . ownerDocument : em && em . get ( 'Canvas' ) . getBody ( ) . ownerDocument ;
const docs = [ document ] ;
elDoc && docs . push ( elDoc ) ;
return docs ;
@ -105,7 +84,7 @@ export default Backbone.View.extend({
* Triggered when the offset of the editro is changed
* /
updateOffset ( ) {
const offset = this . em . get ( 'canvasOffset' ) || { } ;
const offset = this . em ? . get ( 'canvasOffset' ) || { } ;
this . offTop = offset . top ;
this . offLeft = offset . left ;
} ,
@ -120,7 +99,9 @@ export default Backbone.View.extend({
} ,
updateTextViewCursorPosition ( e ) {
const Canvas = this . em . get ( 'Canvas' ) ;
const { em } = this ;
if ( ! em ) return ;
const Canvas = em . get ( 'Canvas' ) ;
const targetDoc = Canvas . getDocument ( ) ;
let range = null ;
@ -138,6 +119,7 @@ export default Backbone.View.extend({
Canvas . getFrameEl ( ) . focus ( ) ;
sel . removeAllRanges ( ) ;
range && sel . addRange ( range ) ;
this . setContentEditable ( this . activeTextModel , true ) ;
} ,
setContentEditable ( model , mode ) {
@ -191,9 +173,7 @@ export default Backbone.View.extend({
. off ( 'mousemove' , this . moveDragHelper )
. on ( 'mousemove' , this . moveDragHelper ) ;
}
$ ( document )
. off ( 'mousemove' , this . moveDragHelper )
. on ( 'mousemove' , this . moveDragHelper ) ;
$ ( document ) . off ( 'mousemove' , this . moveDragHelper ) . on ( 'mousemove' , this . moveDragHelper ) ;
} ,
/ * *
@ -267,7 +247,7 @@ export default Backbone.View.extend({
var rect = el . getBoundingClientRect ( ) ;
return {
top : rect . top + document . body . scrollTop ,
left : rect . left + document . body . scrollLeft
left : rect . left + document . body . scrollLeft ,
} ;
} ,
@ -292,32 +272,26 @@ export default Backbone.View.extend({
* @ param { HTMLElement } src
* * /
startSort ( src , opts = { } ) {
const em = this . em ;
const itemSel = this . itemSel ;
const contSel = this . containerSel ;
const { em , itemSel , containerSel , plh } = this ;
const container = this . getContainerEl ( opts . container ) ;
const docs = this . getDocuments ( src ) ;
const onStart = this . onStart ;
let srcModel ;
let plh = this . plh ;
this . dropModel = null ;
this . target = null ;
this . prevTarget = null ;
this . moved = 0 ;
// Check if the start element is a valid one, if not get the
// closest valid one
if ( src && ! this . matches ( src , ` ${ itemSel } , ${ contSel } ` ) ) {
// Check if the start element is a valid one, if not, try the closest valid one
if ( src && ! this . matches ( src , ` ${ itemSel } , ${ containerSel } ` ) ) {
src = this . closest ( src , itemSel ) ;
}
this . eV = src ;
this . sourceEl = src ;
// Create placeholder if not yet exists
// Create placeholder if doesn't exist yet
if ( ! plh ) {
plh = this . createPlaceholder ( ) ;
container . appendChild ( plh ) ;
this . plh = plh ;
this . plh = this . createPlaceholder ( ) ;
container . appendChild ( this . plh ) ;
}
if ( src ) {
@ -329,19 +303,17 @@ export default Backbone.View.extend({
on ( container , 'mousemove dragover' , this . onMove ) ;
on ( docs , 'mouseup dragend touchend' , this . endMove ) ;
on ( docs , 'keydown' , this . rollback ) ;
onStart &&
onStart ( {
sorter : this ,
target : srcModel ,
parent : srcModel && srcModel . parent ( ) ,
index : srcModel && srcModel . index ( )
} ) ;
this . onStart ( {
sorter : this ,
target : srcModel ,
parent : srcModel && srcModel . parent ? . ( ) ,
index : srcModel && srcModel . index ? . ( ) ,
} ) ;
// Avoid strange effects on dragging
em && em . clearSelection ( ) ;
em ? . clearSelection ( ) ;
this . toggleSortCursor ( 1 ) ;
em && em . trigger ( 'sorter:drag:start' , src , srcModel ) ;
em ? . trigger ( 'sorter:drag:start' , src , srcModel ) ;
} ,
/ * *
@ -358,15 +330,10 @@ export default Backbone.View.extend({
* @ return { Model }
* /
getSourceModel ( source , { target , avoidChildren = 1 } = { } ) {
const { em , eV } = this ;
const src = source || eV ;
const { em , sourceEl } = this ;
const src = source || sourceEl ;
let { dropModel , dropContent } = this ;
const isTextable = src =>
src &&
target &&
src . opt &&
src . opt . avoidChildren &&
this . isTextableActive ( src , target ) ;
const isTextable = src => src && target && src . opt && src . opt . avoidChildren && this . isTextableActive ( src , target ) ;
if ( dropContent && em ) {
if ( isTextable ( dropModel ) ) {
@ -378,7 +345,7 @@ export default Backbone.View.extend({
const opts = {
avoidChildren ,
avoidStore : 1 ,
avoidUpdateStyle : 1
avoidUpdateStyle : 1 ,
} ;
const tempModel = comps . add ( dropContent , { ... opts , temporary : 1 } ) ;
dropModel = comps . remove ( tempModel , opts ) ;
@ -444,9 +411,7 @@ export default Backbone.View.extend({
var rX = e . pageX - this . elL + this . el . scrollLeft ;
if ( this . canvasRelative && em ) {
const mousePos = em
. get ( 'Canvas' )
. getMouseRelativeCanvas ( e , { noScroll : 1 } ) ;
const mousePos = em . get ( 'Canvas' ) . getMouseRelativeCanvas ( e , { noScroll : 1 } ) ;
rX = mousePos . x ;
rY = mousePos . y ;
}
@ -457,9 +422,7 @@ export default Backbone.View.extend({
//var targetNew = this.getTargetFromEl(e.target);
const sourceModel = this . getSourceModel ( ) ;
const targetEl = customTarget
? customTarget ( { sorter : this , event : e } )
: e . target ;
const targetEl = customTarget ? customTarget ( { sorter : this , event : e } ) : e . target ;
const dims = this . dimsFromTarget ( targetEl , rX , rY ) ;
const target = this . target ;
const targetModel = target && this . getTargetModel ( target ) ;
@ -471,8 +434,6 @@ export default Backbone.View.extend({
if ( this . isTextableActive ( sourceModel , targetModel ) ) {
this . activeTextModel = targetModel ;
this . setContentEditable ( targetModel , true ) ;
plh . style . display = 'none' ;
this . lastPos = pos ;
this . updateTextViewCursorPosition ( ev ) ;
@ -481,11 +442,7 @@ export default Backbone.View.extend({
this . activeTextModel = null ;
// If there is a significant changes with the pointer
if (
! this . lastPos ||
this . lastPos . index != pos . index ||
this . lastPos . method != pos . method
) {
if ( ! this . lastPos || this . lastPos . index != pos . index || this . lastPos . method != pos . method ) {
this . movePlaceholder ( this . plh , dims , pos , this . prevTargetDim ) ;
if ( ! this . $plh ) this . $plh = $ ( this . plh ) ;
@ -505,7 +462,7 @@ export default Backbone.View.extend({
event : e ,
target : sourceModel ,
parent : targetModel ,
index : pos . index + ( pos . method == 'after' ? 1 : 0 )
index : pos . index + ( pos . method == 'after' ? 1 : 0 ) ,
} ) ;
em &&
@ -516,7 +473,7 @@ export default Backbone.View.extend({
dims ,
pos ,
x : rX ,
y : rY
y : rY ,
} ) ;
} ,
@ -527,6 +484,7 @@ export default Backbone.View.extend({
disableTextable ( ) {
const { activeTextModel } = this ;
activeTextModel && activeTextModel . getView ( ) . disableEditing ( ) ;
this . setContentEditable ( activeTextModel , false ) ;
} ,
/ * *
@ -565,12 +523,7 @@ export default Backbone.View.extend({
if ( style . overflow && style . overflow !== 'visible' ) return ;
const propFloat = $el . css ( 'float' ) ;
if ( propFloat && propFloat !== 'none' ) return ;
if (
$parent &&
$parent . css ( 'display' ) == 'flex' &&
$parent . css ( 'flex-direction' ) !== 'column'
)
return ;
if ( $parent && $parent . css ( 'display' ) == 'flex' && $parent . css ( 'flex-direction' ) !== 'column' ) return ;
switch ( style . position ) {
case 'static' :
case 'relative' :
@ -611,7 +564,7 @@ export default Backbone.View.extend({
src ,
srcModel ,
trg ,
trgModel
trgModel ,
} ;
if ( ! src || ! trg ) {
@ -629,9 +582,7 @@ export default Backbone.View.extend({
} else {
draggable = draggable instanceof Array ? draggable . join ( ', ' ) : draggable ;
result . dragInfo = draggable ;
draggable = isString ( draggable )
? this . matches ( trg , draggable )
: draggable ;
draggable = isString ( draggable ) ? this . matches ( trg , draggable ) : draggable ;
result . draggable = draggable ;
}
@ -646,11 +597,8 @@ export default Backbone.View.extend({
droppable = droppable instanceof Backbone . Collection ? 1 : droppable ;
droppable = droppable instanceof Array ? droppable . join ( ', ' ) : droppable ;
result . dropInfo = droppable ;
droppable = isString ( droppable )
? this . matches ( src , droppable )
: droppable ;
droppable =
draggable && this . isTextableActive ( srcModel , trgModel ) ? 1 : droppable ;
droppable = isString ( droppable ) ? this . matches ( src , droppable ) : droppable ;
droppable = draggable && this . isTextableActive ( srcModel , trgModel ) ? 1 : droppable ;
result . droppable = droppable ;
}
@ -721,10 +669,7 @@ export default Backbone.View.extend({
// Generally, on any new target the poiner enters inside its area and
// triggers nearBorders(), so have to take care of this
if (
this . nearBorders ( this . prevTargetDim , rX , rY ) ||
( ! this . nested && ! this . cacheDims . length )
) {
if ( this . nearBorders ( this . prevTargetDim , rX , rY ) || ( ! this . nested && ! this . cacheDims . length ) ) {
const targetParent = this . targetP ;
if ( targetParent && this . validTarget ( targetParent ) . valid ) {
@ -846,12 +791,8 @@ export default Backbone.View.extend({
width = pos . width ; // + offsets.marginLeft + offsets.marginRight;
} else {
var o = this . offset ( el ) ;
top = this . relative
? el . offsetTop
: o . top - ( this . wmargin ? - 1 : 1 ) * this . elT ;
left = this . relative
? el . offsetLeft
: o . left - ( this . wmargin ? - 1 : 1 ) * this . elL ;
top = this . relative ? el . offsetTop : o . top - ( this . wmargin ? - 1 : 1 ) * this . elT ;
left = this . relative ? el . offsetLeft : o . left - ( this . wmargin ? - 1 : 1 ) * this . elL ;
height = el . offsetHeight ;
width = el . offsetWidth ;
}
@ -871,9 +812,7 @@ export default Backbone.View.extend({
// Get children based on getChildrenContainer
const trgModel = this . getTargetModel ( trg ) ;
if ( trgModel && trgModel . view && ! this . ignoreViewChildren ) {
const view = trgModel . getCurrentView
? trgModel . getCurrentView ( )
: trgModel . view ;
const view = trgModel . getCurrentView ? trgModel . getCurrentView ( ) : trgModel . view ;
trg = view . getChildrenContainer ( ) ;
}
@ -917,8 +856,7 @@ export default Backbone.View.extend({
var l = dim . left ;
var h = dim . height ;
var w = dim . width ;
if ( t + off > y || y > t + h - off || l + off > x || x > l + w - off )
result = 1 ;
if ( t + off > y || y > t + h - off || l + off > x || x > l + w - off ) result = 1 ;
return ! ! result ;
} ,
@ -1051,7 +989,7 @@ export default Backbone.View.extend({
* @ return void
* * /
endMove ( e ) {
const src = this . eV ;
const src = this . sourceEl ;
const moved = [ ] ;
const docs = this . getDocuments ( ) ;
const container = this . getContainerEl ( ) ;
@ -1100,11 +1038,9 @@ export default Backbone.View.extend({
const data = {
target : srcModel ,
parent : srcModel && srcModel . parent ( ) ,
index : srcModel && srcModel . index ( )
index : srcModel && srcModel . index ( ) ,
} ;
moved . length
? moved . forEach ( m => onEndMove ( m , this , data ) )
: onEndMove ( null , this , { ... data , cancelled : 1 } ) ;
moved . length ? moved . forEach ( m => onEndMove ( m , this , data ) ) : onEndMove ( null , this , { ... data , cancelled : 1 } ) ;
}
isFunction ( onEnd ) && onEnd ( { sorter : this } ) ;
@ -1117,90 +1053,72 @@ export default Backbone.View.extend({
* @ param { Object } pos Object with position coordinates
* * /
move ( dst , src , pos ) {
const { em , activeTextModel , dropContent } = this ;
const { em , dropContent } = this ;
const srcEl = getElement ( src ) ;
em && em . trigger ( 'component:dragEnd:before' , dst , srcEl , pos ) ; // @depricated
let index = pos . indexEl ;
let modelToDrop , modelTemp , created ;
const warns = [ ] ;
const index = pos . method === 'after' ? pos . indexEl + 1 : pos . indexEl ;
const validResult = this . validTarget ( dst , srcEl ) ;
const targetCollection = $ ( dst ) . data ( 'collection' ) ;
const model = validResult . srcModel ;
let { droppable } = validResult ;
const { trgModel , draggable } = validResult ;
const dropInfo =
validResult . dropInfo || ( trgModel && trgModel . get ( 'droppable' ) ) ;
const dragInfo = validResult . dragInfo || ( model && model . get ( 'draggable' ) ) ;
droppable = trgModel instanceof Backbone . Collection ? 1 : droppable ;
const isTextableActive = this . isTextableActive ( model , trgModel ) ;
const { trgModel , srcModel , draggable } = validResult ;
const droppable = trgModel instanceof Backbone . Collection ? 1 : validResult . droppable ;
let modelToDrop , created ;
if ( targetCollection && droppable && draggable ) {
index = pos . method === 'after' ? index + 1 : index ;
var opts = { at : index , noIncrement : 1 } ;
const opts = { at : index , action : 'move-component' } ;
const isTextable = this . isTextableActive ( srcModel , trgModel ) ;
if ( ! dropContent ) {
// Putting `avoidStore` here will make the UndoManager behave wrong
opts . temporary = 1 ;
modelTemp = targetCollection . add ( { } , { ... opts } ) ;
if ( model . collection ) {
modelToDrop = model . collection . remove ( model , { temporary : 1 } ) ;
const srcIndex = srcModel . collection . indexOf ( srcModel ) ;
const sameCollection = targetCollection === srcModel . collection ;
const sameIndex = srcIndex === index || srcIndex === index - 1 ;
const canRemove = ! sameCollection || ! sameIndex || isTextable ;
if ( canRemove ) {
modelToDrop = srcModel . collection . remove ( srcModel , { temporary : true } ) ;
if ( sameCollection && index > srcIndex ) {
opts . at = index - 1 ;
}
}
} else {
modelToDrop = isFunction ( dropContent ) ? dropContent ( ) : dropContent ;
opts . silent = false ;
opts . avoidUpdateStyle = 1 ;
}
if ( isTextableActive ) {
const viewActive = activeTextModel . getView ( ) ;
activeTextModel . trigger ( 'active' ) ;
const { activeRte } = viewActive ;
const modelEl = model . getEl ( ) ;
delete model . opt . temporary ;
model . getView ( ) . render ( ) ;
modelEl . setAttribute ( 'data-gjs-textable' , 'true' ) ;
const { outerHTML } = modelEl ;
activeRte . insertHTML && activeRte . insertHTML ( outerHTML ) ;
} else {
created = targetCollection . add ( modelToDrop , opts ) ;
opts . avoidUpdateStyle = true ;
opts . action = 'add-component' ;
}
if ( ! dropContent ) {
targetCollection . remove ( modelTemp ) ;
} else {
this . dropContent = null ;
if ( modelToDrop ) {
if ( isTextable ) {
delete opts . at ;
created = trgModel . getView ( ) . insertComponent ( modelToDrop , opts ) ;
} else {
created = targetCollection . add ( modelToDrop , opts ) ;
}
}
// This will cause to recalculate children dimensions
this . prevTarget = null ;
this . dropContent = null ;
this . prevTarget = null ; // This will recalculate children dimensions
} else if ( em ) {
const dropInfo = validResult . dropInfo || trgModel ? . get ( 'droppable' ) ;
const dragInfo = validResult . dragInfo || srcModel ? . get ( 'draggable' ) ;
! targetCollection && warns . push ( 'Target collection not found' ) ;
! droppable &&
dropInfo &&
warns . push ( ` Target is not droppable, accepts [ ${ dropInfo } ] ` ) ;
! draggable &&
dragInfo &&
warns . push ( ` Component not draggable, acceptable by [ ${ dragInfo } ] ` ) ;
! droppable && dropInfo && warns . push ( ` Target is not droppable, accepts [ ${ dropInfo } ] ` ) ;
! draggable && dragInfo && warns . push ( ` Component not draggable, acceptable by [ ${ dragInfo } ] ` ) ;
em . logWarning ( 'Invalid target position' , {
errors : warns ,
model ,
model : srcModel ,
context : 'sorter' ,
target : trgModel
target : trgModel ,
} ) ;
}
if ( em ) {
em . trigger ( 'component:dragEnd' , targetCollection , modelToDrop , warns ) ; // @deprecated
em . trigger ( 'sorter:drag:end' , {
targetCollection ,
modelToDrop ,
warns ,
validResult ,
dst ,
srcEl
} ) ;
}
em ? . trigger ( 'sorter:drag:end' , {
targetCollection ,
modelToDrop ,
warns ,
validResult ,
dst ,
srcEl ,
} ) ;
return created ;
} ,
@ -1218,5 +1136,5 @@ export default Backbone.View.extend({
this . moved = 0 ;
this . endMove ( ) ;
}
}
} ,
} ) ;