2015-03-23 14:10:55 -07:00
|
|
|
import {isBlank} from 'angular2/src/facade/lang';
|
|
|
|
import {List, ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {CompileElement} from './compile_element';
|
|
|
|
import {CompileStep} from './compile_step';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controls the processing order of elements.
|
|
|
|
* Right now it only allows to add a parent element.
|
|
|
|
*/
|
|
|
|
export class CompileControl {
|
2015-06-12 23:11:11 +02:00
|
|
|
_currentStepIndex: number = 0;
|
|
|
|
_parent: CompileElement = null;
|
|
|
|
_results = null;
|
|
|
|
_additionalChildren = null;
|
2015-04-10 16:12:37 +02:00
|
|
|
_ignoreCurrentElement: boolean;
|
|
|
|
|
2015-06-12 23:11:11 +02:00
|
|
|
constructor(public _steps: List<CompileStep>) {}
|
2015-03-23 14:10:55 -07:00
|
|
|
|
|
|
|
// only public so that it can be used by compile_pipeline
|
2015-05-18 11:57:20 -07:00
|
|
|
internalProcess(results, startStepIndex, parent: CompileElement, current: CompileElement) {
|
2015-03-23 14:10:55 -07:00
|
|
|
this._results = results;
|
|
|
|
var previousStepIndex = this._currentStepIndex;
|
|
|
|
var previousParent = this._parent;
|
|
|
|
|
2015-04-10 16:12:37 +02:00
|
|
|
this._ignoreCurrentElement = false;
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
for (var i = startStepIndex; i < this._steps.length && !this._ignoreCurrentElement; i++) {
|
2015-03-23 14:10:55 -07:00
|
|
|
var step = this._steps[i];
|
|
|
|
this._parent = parent;
|
|
|
|
this._currentStepIndex = i;
|
|
|
|
step.process(parent, current, this);
|
|
|
|
parent = this._parent;
|
|
|
|
}
|
2015-04-10 16:12:37 +02:00
|
|
|
|
|
|
|
if (!this._ignoreCurrentElement) {
|
|
|
|
ListWrapper.push(results, current);
|
|
|
|
}
|
2015-03-23 14:10:55 -07:00
|
|
|
|
|
|
|
this._currentStepIndex = previousStepIndex;
|
|
|
|
this._parent = previousParent;
|
|
|
|
|
|
|
|
var localAdditionalChildren = this._additionalChildren;
|
|
|
|
this._additionalChildren = null;
|
|
|
|
return localAdditionalChildren;
|
|
|
|
}
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
addParent(newElement: CompileElement) {
|
|
|
|
this.internalProcess(this._results, this._currentStepIndex + 1, this._parent, newElement);
|
2015-03-23 14:10:55 -07:00
|
|
|
this._parent = newElement;
|
|
|
|
}
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
addChild(element: CompileElement) {
|
2015-03-23 14:10:55 -07:00
|
|
|
if (isBlank(this._additionalChildren)) {
|
|
|
|
this._additionalChildren = ListWrapper.create();
|
|
|
|
}
|
|
|
|
ListWrapper.push(this._additionalChildren, element);
|
|
|
|
}
|
2015-04-10 16:12:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ignores the current element.
|
|
|
|
*
|
2015-04-17 13:01:07 -07:00
|
|
|
* When a step calls `ignoreCurrentElement`, no further steps are executed on the current
|
|
|
|
* element and no `CompileElement` is added to the result list.
|
2015-04-10 16:12:37 +02:00
|
|
|
*/
|
2015-05-18 11:57:20 -07:00
|
|
|
ignoreCurrentElement() { this._ignoreCurrentElement = true; }
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|