feat(Ruler): introduce Ruler service

Closes #1089

Closes #1253
This commit is contained in:
Pawel Kozlowski
2015-04-06 21:45:54 +02:00
parent c349eb4fa4
commit 41262f4265
8 changed files with 112 additions and 2 deletions

View File

@ -176,6 +176,7 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
document.implementation.createHtmlDocument('fakeTitle');
HtmlDocument defaultDoc() => document;
Rectangle getBoundingClientRect(el) => el.getBoundingClientRect();
String getTitle() => document.title;
void setTitle(String newTitle) {
document.title = newTitle;

View File

@ -58,9 +58,9 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
}
content(node:HTMLElement):Node {
if (this.hasProperty(node, "content")) {
return node.content;
return node.content;
} else {
return node;
return node;
}
}
firstChild(el):Node {
@ -228,6 +228,9 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
defaultDoc() {
return document;
}
getBoundingClientRect(el) {
return el.getBoundingClientRect();
}
getTitle() {
return document.title;
}

View File

@ -210,6 +210,9 @@ export class DomAdapter {
defaultDoc() {
throw _abstract();
}
getBoundingClientRect(el) {
throw _abstract();
}
getTitle() {
throw _abstract();
}

View File

@ -377,6 +377,9 @@ export class Parse5DomAdapter extends DomAdapter {
}
return defDoc;
}
getBoundingClientRect(el) {
return {left: 0, top: 0, width: 0, height: 0};
}
getTitle() {
return this.defaultDoc().title || "";
}

34
modules/angular2/src/services/ruler.js vendored Normal file
View File

@ -0,0 +1,34 @@
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {DomAdapter} from 'angular2/src/dom/dom_adapter';
import {NgElement} from 'angular2/src/core/dom/element';
export class Rectangle {
left;
right;
top;
bottom;
height;
width;
constructor(left, top, width, height) {
this.left = left;
this.right = left + width;
this.top = top;
this.bottom = top + height;
this.height = height;
this.width = width;
}
}
export class Ruler {
domAdapter: DomAdapter;
constructor(domAdapter: DomAdapter) {
this.domAdapter = domAdapter;
}
measure(el:NgElement): Promise<Rectangle> {
var clntRect = this.domAdapter.getBoundingClientRect(el.domElement);
//even if getBoundingClientRect is synchronous we use async API in preparation for further changes
return PromiseWrapper.resolve(new Rectangle(clntRect.left, clntRect.top, clntRect.width, clntRect.height));
}
}