2013-12-04 05:18:34 +00:00
/ * *
2017-05-19 22:12:53 +00:00
* DO NOT EDIT THIS FILE .
* See the following change record for more information ,
2017-05-23 14:30:14 +00:00
* https : //www.drupal.org/node/2815083
2017-05-19 22:12:53 +00:00
* @ preserve
* * /
2013-12-04 05:18:34 +00:00
2013-06-29 21:42:22 +00:00
( function ( _ , $ , Backbone , Drupal ) {
2013-05-14 19:02:48 +00:00
2015-10-13 22:37:56 +00:00
'use strict' ;
2014-01-27 21:41:32 +00:00
2017-05-19 22:12:53 +00:00
Drupal . quickedit . EntityModel = Drupal . quickedit . BaseModel . extend ( {
defaults : {
2014-01-27 21:41:32 +00:00
el : null ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
entityID : null ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
entityInstanceID : null ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
id : null ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
label : null ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
fields : null ,
isActive : false ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
inTempStore : false ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
isDirty : false ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
isCommitting : false ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
state : 'closed' ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
fieldsInTempStore : [ ] ,
2015-06-06 15:55:42 +00:00
2014-01-27 21:41:32 +00:00
reload : false
} ,
2017-05-19 22:12:53 +00:00
initialize : function initialize ( ) {
2014-04-16 21:42:14 +00:00
this . set ( 'fields' , new Drupal . quickedit . FieldCollection ( ) ) ;
2014-01-27 21:41:32 +00:00
this . listenTo ( this , 'change:state' , this . stateChange ) ;
this . listenTo ( this . get ( 'fields' ) , 'change:state' , this . fieldStateChange ) ;
2014-04-16 21:42:14 +00:00
Drupal . quickedit . BaseModel . prototype . initialize . call ( this ) ;
2014-01-27 21:41:32 +00:00
} ,
2017-05-19 22:12:53 +00:00
stateChange : function stateChange ( entityModel , state , options ) {
2014-01-27 21:41:32 +00:00
var to = state ;
switch ( to ) {
case 'closed' :
this . set ( {
2015-08-07 14:08:23 +00:00
isActive : false ,
inTempStore : false ,
isDirty : false
2014-01-27 21:41:32 +00:00
} ) ;
break ;
2013-06-29 21:42:22 +00:00
2014-01-27 21:41:32 +00:00
case 'launching' :
break ;
2013-06-29 21:42:22 +00:00
2014-01-27 21:41:32 +00:00
case 'opening' :
entityModel . get ( 'fields' ) . each ( function ( fieldModel ) {
fieldModel . set ( 'state' , 'candidate' , options ) ;
2013-06-29 21:42:22 +00:00
} ) ;
2014-01-27 21:41:32 +00:00
break ;
case 'opened' :
this . set ( 'isActive' , true ) ;
break ;
case 'committing' :
var fields = this . get ( 'fields' ) ;
2017-05-19 22:12:53 +00:00
fields . chain ( ) . filter ( function ( fieldModel ) {
return _ . intersection ( [ fieldModel . get ( 'state' ) ] , [ 'active' ] ) . length ;
} ) . each ( function ( fieldModel ) {
fieldModel . set ( 'state' , 'candidate' ) ;
} ) ;
fields . chain ( ) . filter ( function ( fieldModel ) {
return _ . intersection ( [ fieldModel . get ( 'state' ) ] , Drupal . quickedit . app . changedFieldStates ) . length ;
} ) . each ( function ( fieldModel ) {
fieldModel . set ( 'state' , 'saving' ) ;
} ) ;
2014-01-27 21:41:32 +00:00
break ;
case 'deactivating' :
2017-05-19 22:12:53 +00:00
var changedFields = this . get ( 'fields' ) . filter ( function ( fieldModel ) {
return _ . intersection ( [ fieldModel . get ( 'state' ) ] , [ 'changed' , 'invalid' ] ) . length ;
} ) ;
if ( ( changedFields . length || this . get ( 'fieldsInTempStore' ) . length ) && ! options . saved && ! options . confirmed ) {
this . set ( 'state' , 'opened' , { confirming : true } ) ;
2014-01-27 21:41:32 +00:00
_ . defer ( function ( ) {
2014-04-16 21:42:14 +00:00
Drupal . quickedit . app . confirmEntityDeactivation ( entityModel ) ;
2014-01-27 21:41:32 +00:00
} ) ;
2017-05-19 22:12:53 +00:00
} else {
var invalidFields = this . get ( 'fields' ) . filter ( function ( fieldModel ) {
return _ . intersection ( [ fieldModel . get ( 'state' ) ] , [ 'invalid' ] ) . length ;
} ) ;
entityModel . set ( 'reload' , this . get ( 'fieldsInTempStore' ) . length || invalidFields . length ) ;
2014-01-27 21:41:32 +00:00
entityModel . get ( 'fields' ) . each ( function ( fieldModel ) {
if ( _ . intersection ( [ fieldModel . get ( 'state' ) ] , [ 'candidate' , 'highlighted' ] ) . length ) {
fieldModel . trigger ( 'change:state' , fieldModel , fieldModel . get ( 'state' ) , options ) ;
2017-05-19 22:12:53 +00:00
} else {
2014-01-27 21:41:32 +00:00
fieldModel . set ( 'state' , 'candidate' , options ) ;
}
} ) ;
}
break ;
case 'closing' :
options . reason = 'stop' ;
this . get ( 'fields' ) . each ( function ( fieldModel ) {
fieldModel . set ( {
2015-08-07 14:08:23 +00:00
inTempStore : false ,
state : 'inactive'
2014-01-27 21:41:32 +00:00
} , options ) ;
2013-06-29 21:42:22 +00:00
} ) ;
2014-01-27 21:41:32 +00:00
break ;
}
} ,
2017-05-19 22:12:53 +00:00
_updateInTempStoreAttributes : function _updateInTempStoreAttributes ( entityModel , fieldModel ) {
2014-01-27 21:41:32 +00:00
var current = fieldModel . get ( 'state' ) ;
var previous = fieldModel . previous ( 'state' ) ;
var fieldsInTempStore = entityModel . get ( 'fieldsInTempStore' ) ;
2017-05-19 22:12:53 +00:00
2014-01-27 21:41:32 +00:00
if ( current === 'saved' ) {
entityModel . set ( 'inTempStore' , true ) ;
2017-05-19 22:12:53 +00:00
2014-01-27 21:41:32 +00:00
fieldModel . set ( 'inTempStore' , true ) ;
2017-05-19 22:12:53 +00:00
2014-01-27 21:41:32 +00:00
fieldsInTempStore . push ( fieldModel . get ( 'fieldID' ) ) ;
fieldsInTempStore = _ . uniq ( fieldsInTempStore ) ;
entityModel . set ( 'fieldsInTempStore' , fieldsInTempStore ) ;
2017-05-19 22:12:53 +00:00
} else if ( current === 'candidate' && previous === 'inactive' ) {
fieldModel . set ( 'inTempStore' , _ . intersection ( [ fieldModel . get ( 'fieldID' ) ] , fieldsInTempStore ) . length > 0 ) ;
}
2014-01-27 21:41:32 +00:00
} ,
2017-05-19 22:12:53 +00:00
fieldStateChange : function fieldStateChange ( fieldModel , state ) {
2014-01-27 21:41:32 +00:00
var entityModel = this ;
var fieldState = state ;
2017-05-19 22:12:53 +00:00
2014-01-27 21:41:32 +00:00
switch ( this . get ( 'state' ) ) {
case 'closed' :
case 'launching' :
break ;
case 'opening' :
2013-06-29 21:42:22 +00:00
_ . defer ( function ( ) {
2014-01-27 21:41:32 +00:00
entityModel . set ( 'state' , 'opened' , {
2014-04-16 21:42:14 +00:00
'accept-field-states' : Drupal . quickedit . app . readyFieldStates
2014-01-27 21:41:32 +00:00
} ) ;
2013-06-29 21:42:22 +00:00
} ) ;
2014-01-27 21:41:32 +00:00
break ;
case 'opened' :
if ( fieldState === 'changed' ) {
entityModel . set ( 'isDirty' , true ) ;
2017-05-19 22:12:53 +00:00
} else {
2014-01-27 21:41:32 +00:00
this . _updateInTempStoreAttributes ( entityModel , fieldModel ) ;
}
break ;
case 'committing' :
if ( fieldState === 'invalid' ) {
_ . defer ( function ( ) {
2017-05-19 22:12:53 +00:00
entityModel . set ( 'state' , 'opened' , { reason : 'invalid' } ) ;
2014-01-27 21:41:32 +00:00
} ) ;
2017-05-19 22:12:53 +00:00
} else {
2014-01-27 21:41:32 +00:00
this . _updateInTempStoreAttributes ( entityModel , fieldModel ) ;
}
var options = {
2014-04-16 21:42:14 +00:00
'accept-field-states' : Drupal . quickedit . app . readyFieldStates
2014-01-27 21:41:32 +00:00
} ;
if ( entityModel . set ( 'isCommitting' , true , options ) ) {
entityModel . save ( {
2017-05-19 22:12:53 +00:00
success : function success ( ) {
2014-01-27 21:41:32 +00:00
entityModel . set ( {
2015-08-07 14:08:23 +00:00
state : 'deactivating' ,
isCommitting : false
2017-05-19 22:12:53 +00:00
} , { saved : true } ) ;
2014-01-27 21:41:32 +00:00
} ,
2017-05-19 22:12:53 +00:00
error : function error ( ) {
2014-01-27 21:41:32 +00:00
entityModel . set ( 'isCommitting' , false ) ;
2017-05-19 22:12:53 +00:00
entityModel . set ( 'state' , 'opened' , { reason : 'networkerror' } ) ;
var message = Drupal . t ( 'Your changes to <q>@entity-title</q> could not be saved, either due to a website problem or a network connection problem.<br>Please try again.' , { '@entity-title' : entityModel . get ( 'label' ) } ) ;
2015-10-02 21:43:24 +00:00
Drupal . quickedit . util . networkErrorModal ( Drupal . t ( 'Network problem!' ) , message ) ;
2014-01-27 21:41:32 +00:00
}
} ) ;
}
break ;
case 'deactivating' :
_ . defer ( function ( ) {
entityModel . set ( 'state' , 'closing' , {
2014-04-16 21:42:14 +00:00
'accept-field-states' : Drupal . quickedit . app . readyFieldStates
2014-01-27 21:41:32 +00:00
} ) ;
2013-06-29 21:42:22 +00:00
} ) ;
2014-01-27 21:41:32 +00:00
break ;
2013-06-29 21:42:22 +00:00
2014-01-27 21:41:32 +00:00
case 'closing' :
_ . defer ( function ( ) {
entityModel . set ( 'state' , 'closed' , {
'accept-field-states' : [ 'inactive' ]
} ) ;
2013-06-29 21:42:22 +00:00
} ) ;
2014-01-27 21:41:32 +00:00
break ;
}
} ,
2017-05-19 22:12:53 +00:00
save : function save ( options ) {
2014-01-27 21:41:32 +00:00
var entityModel = this ;
2015-05-20 11:02:57 +00:00
var entitySaverAjax = Drupal . ajax ( {
2014-04-16 21:42:14 +00:00
url : Drupal . url ( 'quickedit/entity/' + entityModel . get ( 'entityID' ) ) ,
2017-05-19 22:12:53 +00:00
error : function error ( ) {
2014-01-27 21:41:32 +00:00
options . error . call ( entityModel ) ;
}
} ) ;
2017-05-19 22:12:53 +00:00
2014-04-16 21:42:14 +00:00
entitySaverAjax . commands . quickeditEntitySaved = function ( ajax , response , status ) {
2014-01-27 21:41:32 +00:00
entityModel . get ( 'fields' ) . each ( function ( fieldModel ) {
fieldModel . set ( 'inTempStore' , false ) ;
} ) ;
entityModel . set ( 'inTempStore' , false ) ;
entityModel . set ( 'fieldsInTempStore' , [ ] ) ;
if ( options . success ) {
options . success . call ( entityModel ) ;
2013-06-29 21:42:22 +00:00
}
2014-01-27 21:41:32 +00:00
} ;
2017-05-19 22:12:53 +00:00
2015-05-20 11:02:57 +00:00
entitySaverAjax . execute ( ) ;
2014-01-27 21:41:32 +00:00
} ,
2017-05-19 22:12:53 +00:00
validate : function validate ( attrs , options ) {
2014-01-27 21:41:32 +00:00
var acceptedFieldStates = options [ 'accept-field-states' ] || [ ] ;
var currentState = this . get ( 'state' ) ;
var nextState = attrs . state ;
if ( currentState !== nextState ) {
if ( _ . indexOf ( this . constructor . states , nextState ) === - 1 ) {
return '"' + nextState + '" is an invalid state' ;
2013-06-29 21:42:22 +00:00
}
2014-01-27 21:41:32 +00:00
if ( ! this . _acceptStateChange ( currentState , nextState , options ) ) {
return 'state change not accepted' ;
2017-05-19 22:12:53 +00:00
} else if ( ! this . _fieldsHaveAcceptableStates ( acceptedFieldStates ) ) {
return 'state change not accepted because fields are not in acceptable state' ;
}
2013-12-14 07:19:04 +00:00
}
2013-06-29 21:42:22 +00:00
2014-01-27 21:41:32 +00:00
var currentIsCommitting = this . get ( 'isCommitting' ) ;
var nextIsCommitting = attrs . isCommitting ;
if ( currentIsCommitting === false && nextIsCommitting === true ) {
if ( ! this . _fieldsHaveAcceptableStates ( acceptedFieldStates ) ) {
return 'isCommitting change not accepted because fields are not in acceptable state' ;
}
2017-05-19 22:12:53 +00:00
} else if ( currentIsCommitting === true && nextIsCommitting === true ) {
2015-10-13 22:37:56 +00:00
return 'isCommitting is a mutex, hence only changes are allowed' ;
2013-06-29 21:42:22 +00:00
}
2014-01-27 21:41:32 +00:00
} ,
2013-06-29 21:42:22 +00:00
2017-05-19 22:12:53 +00:00
_acceptStateChange : function _acceptStateChange ( from , to , context ) {
2014-01-27 21:41:32 +00:00
var accept = true ;
2013-06-29 21:42:22 +00:00
2014-01-27 21:41:32 +00:00
if ( ! this . constructor . followsStateSequence ( from , to ) ) {
accept = false ;
if ( from === 'closing' && to === 'closed' ) {
accept = true ;
2017-05-19 22:12:53 +00:00
} else if ( from === 'committing' && to === 'opened' && context . reason && ( context . reason === 'invalid' || context . reason === 'networkerror' ) ) {
accept = true ;
} else if ( from === 'deactivating' && to === 'opened' && context . confirming ) {
accept = true ;
} else if ( from === 'opened' && to === 'deactivating' && context . confirmed ) {
accept = true ;
}
2013-06-29 21:42:22 +00:00
}
2014-01-27 21:41:32 +00:00
return accept ;
} ,
2017-05-19 22:12:53 +00:00
_fieldsHaveAcceptableStates : function _fieldsHaveAcceptableStates ( acceptedFieldStates ) {
2014-01-27 21:41:32 +00:00
var accept = true ;
if ( acceptedFieldStates . length > 0 ) {
var fieldStates = this . get ( 'fields' ) . pluck ( 'state' ) || [ ] ;
2017-05-19 22:12:53 +00:00
2014-01-27 21:41:32 +00:00
if ( _ . difference ( fieldStates , acceptedFieldStates ) . length ) {
accept = false ;
}
2013-06-29 21:42:22 +00:00
}
2014-01-27 21:41:32 +00:00
return accept ;
} ,
2017-05-19 22:12:53 +00:00
destroy : function destroy ( options ) {
2014-04-16 21:42:14 +00:00
Drupal . quickedit . BaseModel . prototype . destroy . call ( this , options ) ;
2014-01-27 21:41:32 +00:00
this . stopListening ( ) ;
2015-08-31 11:20:53 +00:00
this . get ( 'fields' ) . reset ( ) ;
2014-01-27 21:41:32 +00:00
} ,
2017-05-19 22:12:53 +00:00
sync : function sync ( ) {
2014-01-27 21:41:32 +00:00
return ;
2013-06-29 21:42:22 +00:00
}
2017-05-19 22:12:53 +00:00
} , {
states : [ 'closed' , 'launching' , 'opening' , 'opened' , 'committing' , 'deactivating' , 'closing' ] ,
followsStateSequence : function followsStateSequence ( from , to ) {
2014-01-27 21:41:32 +00:00
return _ . indexOf ( this . states , from ) < _ . indexOf ( this . states , to ) ;
2013-06-29 21:42:22 +00:00
}
2014-01-27 21:41:32 +00:00
} ) ;
2017-05-19 22:12:53 +00:00
Drupal . quickedit . EntityCollection = Backbone . Collection . extend ( {
2014-04-16 21:42:14 +00:00
model : Drupal . quickedit . EntityModel
2014-01-27 21:41:32 +00:00
} ) ;
2017-05-19 22:12:53 +00:00
} ) ( _ , jQuery , Backbone , Drupal ) ;