diff --git a/ionic/components.ts b/ionic/components.ts index 1ed6bf3ba6..b1a022e051 100644 --- a/ionic/components.ts +++ b/ionic/components.ts @@ -28,3 +28,4 @@ export * from 'ionic/components/switch/switch' export * from 'ionic/components/view/view' export * from 'ionic/components/tabs/tabs' export * from 'ionic/components/tabs/tab' +export * from 'ionic/components/toolbar/toolbar' diff --git a/ionic/components/app/test/sink/main.html b/ionic/components/app/test/sink/main.html index 2fd325483c..f283a9c224 100644 --- a/ionic/components/app/test/sink/main.html +++ b/ionic/components/app/test/sink/main.html @@ -1,7 +1,5 @@ - - Ionic 2.0 - + Ionic 2.0 @@ -22,14 +20,3 @@ my-modal { height: 100%; } - diff --git a/ionic/components/app/test/weather/index.ts b/ionic/components/app/test/weather/index.ts index 07c37e2755..03c58de777 100755 --- a/ionic/components/app/test/weather/index.ts +++ b/ionic/components/app/test/weather/index.ts @@ -130,9 +130,7 @@ class WeatherApp { @IonicView({ template: ` - - Settings - + Settings
diff --git a/ionic/components/content/test/pull-to-refresh/main.html b/ionic/components/content/test/pull-to-refresh/main.html index dbd1119c7a..6754508a07 100644 --- a/ionic/components/content/test/pull-to-refresh/main.html +++ b/ionic/components/content/test/pull-to-refresh/main.html @@ -1,7 +1,5 @@ - - Pull to Refresh - + Pull to Refresh diff --git a/ionic/components/scroll/test/basic/main.html b/ionic/components/scroll/test/basic/main.html index 167abf62bd..849a3fa850 100644 --- a/ionic/components/scroll/test/basic/main.html +++ b/ionic/components/scroll/test/basic/main.html @@ -1,7 +1,5 @@ - - Scroll - + Scroll diff --git a/ionic/components/toolbar/toolbar.ts b/ionic/components/toolbar/toolbar.ts index aa06d4d480..5383b6c9dd 100644 --- a/ionic/components/toolbar/toolbar.ts +++ b/ionic/components/toolbar/toolbar.ts @@ -1,79 +1,125 @@ -import {Component, Directive, View, ElementRef} from 'angular2/angular2'; -import {ProtoViewRef} from 'angular2/src/core/compiler/view_ref'; +import {Directive, View, Parent, onInit, ElementRef, forwardRef} from 'angular2/angular2'; +import {Ion} from '../ion'; +import {IonicComponent} from '../../config/annotations'; import * as dom from '../../util/dom'; -@Component({ +@IonicComponent({ selector: 'ion-toolbar' }) @View({ - template: `
` + template: ` + + `, + directives: [ + forwardRef(() => Title), + forwardRef(() => NavbarItem) + ] }) -export class Toolbar { - constructor(elementRef:ElementRef) { - this.ele = elementRef.nativeElement; +export class Toolbar extends Ion { + constructor(elementRef: ElementRef) { + super(elementRef); + this.eleRef = elementRef; + this.itemEles = []; + } + + onInit() { + Toolbar.applyConfig(this); + } + + element() { + return this.eleRef; + } + + titleElement(eleRef) { + if (arguments.length) { + this._nbTlEle = eleRef; + } + return this._nbTlEle; + } + + itemElements(eleRef) { + if (arguments.length) { + this.itemEles.push(eleRef); + } + return this.itemEles; + } + + titleText(eleRef) { + if (arguments.length) { + this._ttTxt.push(eleRef); + } + return this._ttTxt; } alignTitle() { - const toolbarEle = this.ele; - const innerTitleEle = this._innerTitleEle || (this._innerTitleEle = toolbarEle.querySelector('.toolbar-inner-title')); - const titleEle = this._titleEle || (this._titleEle = innerTitleEle.querySelector('ion-title')); - const style = this._style || (this._style = window.getComputedStyle(titleEle)); + // called after the navbar/title has had a moment to + // finish rendering in their correct locations + const navbarEle = this.eleRef.nativeElement; + const titleEle = this._ttEle || (this._ttEle = navbarEle.querySelector('ion-title')); - const titleOffsetWidth = titleEle.offsetWidth; + // don't bother if there's no title element + if (!titleEle) return; + + // get the computed style of the title element + const titleStyle = this._ttStyle || (this._ttStyle = window.getComputedStyle(titleEle)); + + // don't bother if we're not trying to center align the title + if (titleStyle.textAlign !== 'center') return; + + // get all the dimensions const titleOffsetLeft = titleEle.offsetLeft; - const titleScrollWidth = titleEle.scrollWidth; - const toolbarOffsetWidth = toolbarEle.offsetWidth; + const titleOffsetRight = navbarEle.offsetWidth - (titleOffsetLeft + titleEle.offsetWidth); - // TODO!!! When an element is being reused by angular2, it'll sometimes keep the - // styles from the original element's use, causing these calculations to be wrong - if (window.getComputedStyle(innerTitleEle).margin !== '0px') { - this._showTitle(); - return; + let marginLeft = 0; + let marginRight = 0; + if (titleOffsetLeft < titleOffsetRight) { + marginLeft = (titleOffsetRight - titleOffsetLeft) + 5; + + } else if (titleOffsetLeft > titleOffsetRight) { + marginRight = (titleOffsetLeft - titleOffsetRight) - 5; } - // only align if the title is center and if it isn't already overflowing - if (style.textAlign !== 'center' || titleOffsetWidth < titleScrollWidth) { - this._showTitle(); + let margin = `0 ${marginRight}px 0 ${marginLeft}px`; - } else { - let rightMargin = toolbarOffsetWidth - (titleOffsetLeft + titleOffsetWidth); - let centerMargin = titleOffsetLeft - rightMargin; - - innerTitleEle.style.margin = `0 ${centerMargin}px 0 0`; - - dom.raf(() => { - if (titleEle.offsetWidth < titleEle.scrollWidth) { - // not enough room yet, just left align title - innerTitleEle.style.margin = ''; - innerTitleEle.style.textAlign = 'left'; - } - this._showTitle(); - }) - } - } - - _showTitle() { - if (!this._shown) { - this._shown = true; - this._innerTitleEle.classList.remove('toolbar-title-hide'); + if ((marginLeft || marginRight) && margin !== this._ttMargin) { + // only do an update if it has to + const innerTitleEle = this._innerTtEle || (this._innerTtEle = navbarEle.querySelector('.navbar-inner-title')); + innerTitleEle.style.margin = this._ttMargin = margin; } } } -/* - Used to find and register headers in a view, and this directive's - content will be moved up to the common toolbar location, and created - using the same context as the view's content area. -*/ @Directive({ - selector: 'template[toolbar]' + selector: '.navbar-title' }) -export class ToolbarTemplate { - constructor(item: ViewItem, protoViewRef: ProtoViewRef) { - item.addProtoViewRef('toolbar', protoViewRef); +class Title { + constructor(@Parent() toolbar: Toolbar, elementRef: ElementRef) { + toolbar.titleElement(elementRef); + } +} + + +@Directive({ + selector: '.navbar-item' +}) +class NavbarItem { + constructor(@Parent() toolbar: Toolbar, elementRef: ElementRef) { + toolbar.itemElements(elementRef); } } diff --git a/ionic/config/annotations.ts b/ionic/config/annotations.ts index 38ad3863d6..d7f2003bdb 100644 --- a/ionic/config/annotations.ts +++ b/ionic/config/annotations.ts @@ -9,6 +9,7 @@ import { Slides, Slide, SlidePager, Tabs, Tab, List, Item, + Toolbar, Icon, Checkbox, Switch, Label, Input, Segment, SegmentButton, SegmentControlValueAccessor, @@ -29,6 +30,7 @@ export function IonicView(config) { List, Item, Slides, Slide, SlidePager, Tabs, Tab, + Toolbar, // Media Icon,