|
| 1 | +import { |
| 2 | + Directive, |
| 3 | + DoCheck, |
| 4 | + ElementRef, |
| 5 | + EventEmitter, |
| 6 | + Inject, |
| 7 | + OnChanges, |
| 8 | + SimpleChange, |
| 9 | + Type |
| 10 | +} from 'angular2/angular2'; |
| 11 | +import {NG1_COMPILE, NG1_SCOPE} from './constants'; |
| 12 | + |
| 13 | +const CAMEL_CASE = /([A-Z])/g; |
| 14 | +const INITIAL_VALUE = { |
| 15 | + __UNINITIALIZED__: true |
| 16 | +}; |
| 17 | + |
| 18 | + |
| 19 | +export class ExportedNg1Component { |
| 20 | + type: Type; |
| 21 | + inputs: string[] = []; |
| 22 | + inputsRename: string[] = []; |
| 23 | + outputs: string[] = []; |
| 24 | + outputsRename: string[] = []; |
| 25 | + propertyOutputs: string[] = []; |
| 26 | + checkProperties: string[] = []; |
| 27 | + propertyMap: {[name: string]: string} = {}; |
| 28 | + |
| 29 | + constructor(public name: string) { |
| 30 | + var selector = name.replace(CAMEL_CASE, (all, next: string) => '-' + next.toLowerCase()); |
| 31 | + var self = this; |
| 32 | + this.type = |
| 33 | + Directive({selector: selector, inputs: this.inputsRename, outputs: this.outputsRename}) |
| 34 | + .Class({ |
| 35 | + constructor: [ |
| 36 | + new Inject(NG1_COMPILE), |
| 37 | + new Inject(NG1_SCOPE), |
| 38 | + ElementRef, |
| 39 | + function(compile: angular.ICompileService, scope: angular.IScope, |
| 40 | + elementRef: ElementRef) { |
| 41 | + return new Ng1ComponentFacade(compile, scope, elementRef, self.inputs, |
| 42 | + self.outputs, self.propertyOutputs, |
| 43 | + self.checkProperties, self.propertyMap); |
| 44 | + } |
| 45 | + ], |
| 46 | + onChanges: function() { /* needs to be here for ng2 to properly detect it */ }, |
| 47 | + doCheck: function() { /* needs to be here for ng2 to properly detect it */ } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + extractBindings(injector: angular.auto.IInjectorService) { |
| 52 | + var directives: angular.IDirective[] = injector.get(this.name + 'Directive'); |
| 53 | + if (directives.length > 1) { |
| 54 | + throw new Error('Only support single directive definition for: ' + this.name); |
| 55 | + } |
| 56 | + var directive = directives[0]; |
| 57 | + var scope = directive.scope; |
| 58 | + if (typeof scope == 'object') { |
| 59 | + for (var name in scope) { |
| 60 | + if (scope.hasOwnProperty(name)) { |
| 61 | + var localName = scope[name]; |
| 62 | + var type = localName.charAt(0); |
| 63 | + localName = localName.substr(1) || name; |
| 64 | + var outputName = 'output_' + name; |
| 65 | + var outputNameRename = outputName + ': ' + name; |
| 66 | + var inputName = 'input_' + name; |
| 67 | + var inputNameRename = inputName + ': ' + name; |
| 68 | + switch (type) { |
| 69 | + case '=': |
| 70 | + this.propertyOutputs.push(outputName); |
| 71 | + this.checkProperties.push(localName); |
| 72 | + this.outputs.push(outputName); |
| 73 | + this.outputsRename.push(outputNameRename); |
| 74 | + this.propertyMap[outputName] = localName; |
| 75 | + // don't break; let it fall through to '@' |
| 76 | + case '@': |
| 77 | + this.inputs.push(inputName); |
| 78 | + this.inputsRename.push(inputNameRename); |
| 79 | + this.propertyMap[inputName] = localName; |
| 80 | + break; |
| 81 | + case '&': |
| 82 | + this.outputs.push(outputName); |
| 83 | + this.outputsRename.push(outputNameRename); |
| 84 | + this.propertyMap[outputName] = localName; |
| 85 | + break; |
| 86 | + default: |
| 87 | + var json = JSON.stringify(scope); |
| 88 | + throw new Error( |
| 89 | + `Unexpected mapping '${type}' in '${json}' in '${this.name}' directive.`); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + static resolve(exportedComponents: {[name: string]: ExportedNg1Component}, |
| 97 | + injector: angular.auto.IInjectorService) { |
| 98 | + for (var name in exportedComponents) { |
| 99 | + if (exportedComponents.hasOwnProperty(name)) { |
| 100 | + var exportedComponent = exportedComponents[name]; |
| 101 | + exportedComponent.extractBindings(injector); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class Ng1ComponentFacade implements OnChanges, DoCheck { |
| 108 | + componentScope: angular.IScope = null; |
| 109 | + checkLastValues: any[] = []; |
| 110 | + |
| 111 | + constructor(compile: angular.ICompileService, scope: angular.IScope, elementRef: ElementRef, |
| 112 | + private inputs: string[], private outputs: string[], private propOuts: string[], |
| 113 | + private checkProperties: string[], private propertyMap: {[key: string]: string}) { |
| 114 | + var chailTail = scope.$$childTail; // remember where the next scope is inserted |
| 115 | + compile(elementRef.nativeElement)(scope); |
| 116 | + |
| 117 | + // If we are first scope take it, otherwise take the next one in list. |
| 118 | + this.componentScope = chailTail ? chailTail.$$nextSibling : scope.$$childHead; |
| 119 | + |
| 120 | + for (var i = 0; i < inputs.length; i++) { |
| 121 | + this[inputs[i]] = null; |
| 122 | + } |
| 123 | + for (var j = 0; j < outputs.length; j++) { |
| 124 | + var emitter = this[outputs[j]] = new EventEmitter(); |
| 125 | + this.setComponentProperty(outputs[j], ((emitter) => (value) => emitter.next(value))(emitter)); |
| 126 | + } |
| 127 | + for (var k = 0; k < propOuts.length; k++) { |
| 128 | + this[propOuts[k]] = new EventEmitter(); |
| 129 | + this.checkLastValues.push(INITIAL_VALUE); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + onChanges(changes) { |
| 134 | + for (var name in changes) { |
| 135 | + if (changes.hasOwnProperty(name)) { |
| 136 | + var change: SimpleChange = changes[name]; |
| 137 | + this.setComponentProperty(name, change.currentValue); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + doCheck() { |
| 143 | + var count = 0; |
| 144 | + var scope = this.componentScope; |
| 145 | + var lastValues = this.checkLastValues; |
| 146 | + var checkProperties = this.checkProperties; |
| 147 | + for (var i = 0; i < checkProperties.length; i++) { |
| 148 | + var value = scope[checkProperties[i]]; |
| 149 | + var last = lastValues[i]; |
| 150 | + if (value !== last) { |
| 151 | + if (typeof value == 'number' && isNaN(value) && typeof last == 'number' && isNaN(last)) { |
| 152 | + // ignore because NaN != NaN |
| 153 | + } else { |
| 154 | + var eventEmitter: EventEmitter = this[this.propOuts[i]]; |
| 155 | + eventEmitter.next(lastValues[i] = value); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + return count; |
| 160 | + } |
| 161 | + |
| 162 | + setComponentProperty(name: string, value: any) { |
| 163 | + this.componentScope[this.propertyMap[name]] = value; |
| 164 | + } |
| 165 | +} |
0 commit comments