scripts/token-attacher.js
/**
		 * Detach previously saved selection of walls to the currently selected token
		 */
Detach previously saved selection of walls to the currently selected token
Detach previously saved selection of walls to the currently selected token
static async _DetachFromToken(token, elements, suppressNotification=false, options={}){
			if(!token) return ui.notifications.error(game.i18n.format(localizedStrings.error.NoTokensSelected));
			if(typeof token === 'string' || token instanceof String) token = canvas.tokens.get(token);
			if(!token) return;
			if(!elements || !elements.hasOwnProperty("type")){
				let attached=token.document.getFlag(moduleName, `attached`);
				if(Object.keys(attached).length > 0){
					for (const key in attached) {
						if (attached.hasOwnProperty(key)) {
							const arr = attached[key];
							let deletes = [];
							for (let i = 0; i < arr.length; i++) {
								deletes.push({_id: arr[i], [`flags.${moduleName}.-=parent`]: null, [`flags.${moduleName}.-=offset`]: null, [`flags.${moduleName}.-=unlocked`]: null});
							}	
							if(deletes.length > 0)	await canvas.scene.updateEmbeddedDocuments(key, deletes, {[moduleName]:{update:true}});						
						}
					}
				}
				await token.document.unsetFlag(moduleName, "attached");
				if(!suppressNotification) ui.notifications.info(game.i18n.format(localizedStrings.info.ObjectsDetached));
				return;
			}
			else{
				 passed elements
				let attached=token.document.getFlag(moduleName, `attached.${elements.type}`) || [];
				if(attached.length === 0) return;
				attached= attached.filter((item) => !elements.data.includes(item));
				let deletes = [];
				for (let i = 0; i < elements.data.length; i++) {
					deletes.push({_id: elements.data[i], [`flags.${moduleName}.-=parent`]: null, [`flags.${moduleName}.-=offset`]: null, [`flags.${moduleName}.-=unlocked`]: null});
				}
				if(deletes.length > 0 && !options.skip_update) await canvas.scene.updateEmbeddedDocuments(elements.type, deletes, {[moduleName]:{update:true}});	
				await token.document.setFlag(moduleName, `attached.${elements.type}`, attached);
				if(!suppressNotification) ui.notifications.info(game.i18n.format(localizedStrings.info.ObjectsDetached));
			}
		}
static async _DetachFromToken ( token , elements , suppressNotification = false , options = { } ) { if ( ! token ) return ui . notifications . error ( game . i18n . format ( localizedStrings . error . NoTokensSelected ) ) ; if ( typeof token === 'string' || token instanceof String ) token = canvas . tokens . get ( token ) ; if ( ! token ) return ; if ( ! elements || ! elements . hasOwnProperty ( "type" ) ) { let attached = token . document . getFlag ( moduleName , ` ` ) ; if ( Object . keys ( attached ) . length > 0 ) { for ( const key in attached ) { if ( attached . hasOwnProperty ( key ) ) { const arr = attached [ key ] ; let deletes = [ ] ; for ( let i = 0 ; i < arr . length ; i ++ ) { deletes . push ( { _id : arr [ i ] , [ ` ${ moduleName } ` ] : null , [ ` ${ moduleName } ` ] : null , [ ` ${ moduleName } ` ] : null } ) ; } if ( deletes . length > 0 ) await canvas . scene . updateEmbeddedDocuments ( key , deletes , { [ moduleName ] : { update : true } } ) ; } } } await token . document . unsetFlag ( moduleName , "attached" ) ; if ( ! suppressNotification ) ui . notifications . info ( game . i18n . format ( localizedStrings . info . ObjectsDetached ) ) ; return ; } else { let attached = token . document . getFlag ( moduleName , ` ${ elements . type } ` ) || [ ] ; if ( attached . length === 0 ) return ; attached = attached . filter ( ( item ) => ! elements . data . includes ( item ) ) ; let deletes = [ ] ; for ( let i = 0 ; i < elements . data . length ; i ++ ) { deletes . push ( { _id : elements . data [ i ] , [ ` ${ moduleName } ` ] : null , [ ` ${ moduleName } ` ] : null , [ ` ${ moduleName } ` ] : null } ) ; } if ( deletes . length > 0 && ! options . skip_update ) await canvas . scene . updateEmbeddedDocuments ( elements . type , deletes , { [ moduleName ] : { update : true } } ) ; await token . document . setFlag ( moduleName , ` ${ elements . type } ` , attached ) ; if ( ! suppressNotification ) ui . notifications . info ( game . i18n . format ( localizedStrings . info . ObjectsDetached ) ) ; } }
Detach previously saved selection of walls to the currently selected token
Detach previously saved selection of walls to the currently selected token
//Detach all //Detach all passed elements