| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  |  MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  |  Author Tobias Koppers @sokra | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | const DependenciesBlockVariable = require("./DependenciesBlockVariable"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function disconnect(i) { | 
					
						
							|  |  |  | 	i.disconnect(); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | function unseal(i) { | 
					
						
							|  |  |  | 	i.unseal(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DependenciesBlock { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							|  |  |  | 		this.dependencies = []; | 
					
						
							|  |  |  | 		this.blocks = []; | 
					
						
							|  |  |  | 		this.variables = []; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	addBlock(block) { | 
					
						
							|  |  |  | 		this.blocks.push(block); | 
					
						
							|  |  |  | 		block.parent = this; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	addVariable(name, expression, dependencies) { | 
					
						
							|  |  |  | 		for(let v of this.variables) { | 
					
						
							|  |  |  | 			if(v.name === name && v.expression === expression) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		this.variables.push(new DependenciesBlockVariable(name, expression, dependencies)); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | 	addDependency(dependency) { | 
					
						
							|  |  |  | 		this.dependencies.push(dependency); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | 	updateHash(hash) { | 
					
						
							|  |  |  | 		function updateHash(i) { | 
					
						
							|  |  |  | 			i.updateHash(hash); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | 		this.dependencies.forEach(updateHash); | 
					
						
							|  |  |  | 		this.blocks.forEach(updateHash); | 
					
						
							|  |  |  | 		this.variables.forEach(updateHash); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | 	disconnect() { | 
					
						
							|  |  |  | 		this.dependencies.forEach(disconnect); | 
					
						
							|  |  |  | 		this.blocks.forEach(disconnect); | 
					
						
							|  |  |  | 		this.variables.forEach(disconnect); | 
					
						
							| 
									
										
										
										
											2013-01-31 01:49:25 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-01 05:28:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | 	unseal() { | 
					
						
							|  |  |  | 		this.blocks.forEach(unseal); | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | 	hasDependencies(filter) { | 
					
						
							|  |  |  | 		if(filter) { | 
					
						
							|  |  |  | 			if(this.dependencies.some(filter)) { | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			if(this.dependencies.length > 0) { | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return this.blocks.concat(this.variables).some(item => item.hasDependencies(filter)); | 
					
						
							| 
									
										
										
										
											2017-01-10 00:11:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | 	sortItems() { | 
					
						
							|  |  |  | 		this.blocks.forEach(block => block.sortItems()); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-07-13 17:03:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-04 05:22:23 +08:00
										 |  |  | module.exports = DependenciesBlock; |