mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 05:18:39 +08:00
background-image css property added
This commit is contained in:
@ -9,6 +9,7 @@ import stylers = require("ui/styling/stylers");
|
|||||||
import styleProperty = require("ui/styling/style-property");
|
import styleProperty = require("ui/styling/style-property");
|
||||||
import converters = require("ui/styling/converters");
|
import converters = require("ui/styling/converters");
|
||||||
import enums = require("ui/enums");
|
import enums = require("ui/enums");
|
||||||
|
import imageSource = require("image-source");
|
||||||
|
|
||||||
// key is the property id and value is Dictionary<string, StylePropertyChangedHandler>;
|
// key is the property id and value is Dictionary<string, StylePropertyChangedHandler>;
|
||||||
var _registeredHandlers = {};
|
var _registeredHandlers = {};
|
||||||
@ -34,6 +35,13 @@ export class Style extends observable.DependencyObservable implements styling.St
|
|||||||
this._setValue(backgroundColorProperty, value, observable.ValueSource.Local);
|
this._setValue(backgroundColorProperty, value, observable.ValueSource.Local);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get backgroundImage(): string {
|
||||||
|
return this._getValue(backgroundImageProperty);
|
||||||
|
}
|
||||||
|
set backgroundImage(value: string) {
|
||||||
|
this._setValue(backgroundImageProperty, value, observable.ValueSource.Local);
|
||||||
|
}
|
||||||
|
|
||||||
get fontSize(): number {
|
get fontSize(): number {
|
||||||
return this._getValue(fontSizeProperty);
|
return this._getValue(fontSizeProperty);
|
||||||
}
|
}
|
||||||
@ -280,8 +288,8 @@ export class Style extends observable.DependencyObservable implements styling.St
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function registerHandler(property: dependencyObservable.Property,
|
export function registerHandler(property: dependencyObservable.Property,
|
||||||
handler: styling.stylers.StylePropertyChangedHandler,
|
handler: styling.stylers.StylePropertyChangedHandler,
|
||||||
className?: string) {
|
className?: string) {
|
||||||
var realClassName = className ? className : "default";
|
var realClassName = className ? className : "default";
|
||||||
if (_registeredHandlers.hasOwnProperty(property.id + "")) {
|
if (_registeredHandlers.hasOwnProperty(property.id + "")) {
|
||||||
_registeredHandlers[property.id][realClassName] = handler;
|
_registeredHandlers[property.id][realClassName] = handler;
|
||||||
@ -332,6 +340,25 @@ export var colorProperty = new styleProperty.Property("color", "color",
|
|||||||
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.Inheritable, undefined, undefined, color.Color.equals),
|
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.Inheritable, undefined, undefined, color.Color.equals),
|
||||||
converters.colorConverter);
|
converters.colorConverter);
|
||||||
|
|
||||||
|
export var backgroundImageProperty = new styleProperty.Property("backgroundImage", "background-image",
|
||||||
|
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.None, onBackgroundImagePropertyChanged));
|
||||||
|
|
||||||
|
function onBackgroundImagePropertyChanged(data: observable.PropertyChangeData) {
|
||||||
|
var style = <Style>data.object;
|
||||||
|
var newValue = <string>data.newValue;
|
||||||
|
|
||||||
|
if (imageSource.isFileOrResourcePath(newValue)) {
|
||||||
|
style._setValue(backgroundImageSourceProperty, imageSource.fromFileOrResource(newValue), observable.ValueSource.Local);
|
||||||
|
} else if (types.isString(newValue)) {
|
||||||
|
imageSource.fromUrl(newValue).then(r=> {
|
||||||
|
style._setValue(backgroundImageSourceProperty, r, observable.ValueSource.Local);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export var backgroundImageSourceProperty = new styleProperty.Property("backgroundImageSource", "background-image-source",
|
||||||
|
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.None, undefined, undefined, undefined));
|
||||||
|
|
||||||
export var backgroundColorProperty = new styleProperty.Property("backgroundColor", "background-color",
|
export var backgroundColorProperty = new styleProperty.Property("backgroundColor", "background-color",
|
||||||
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.None, undefined, undefined, color.Color.equals),
|
new observable.PropertyMetadata(undefined, observable.PropertyMetadataSettings.None, undefined, undefined, color.Color.equals),
|
||||||
converters.colorConverter);
|
converters.colorConverter);
|
||||||
@ -373,7 +400,7 @@ export var minHeightProperty = new styleProperty.Property("minHeight", "min-heig
|
|||||||
converters.numberConverter);
|
converters.numberConverter);
|
||||||
|
|
||||||
function parseThickness(value: any): { top: number; right: number; bottom: number; left: number } {
|
function parseThickness(value: any): { top: number; right: number; bottom: number; left: number } {
|
||||||
var result = { top: 0, right: 0, bottom: 0, left: 0};
|
var result = { top: 0, right: 0, bottom: 0, left: 0 };
|
||||||
if (types.isString(value)) {
|
if (types.isString(value)) {
|
||||||
var arr = value.split(/[ ,]+/);
|
var arr = value.split(/[ ,]+/);
|
||||||
var top = parseInt(arr[0]);
|
var top = parseInt(arr[0]);
|
||||||
@ -406,7 +433,7 @@ function onPaddingChanged(data: observable.PropertyChangeData) {
|
|||||||
|
|
||||||
style.paddingTop = thickness.top;
|
style.paddingTop = thickness.top;
|
||||||
style.paddingRight = thickness.right;
|
style.paddingRight = thickness.right;
|
||||||
style.paddingBottom= thickness.bottom;
|
style.paddingBottom = thickness.bottom;
|
||||||
style.paddingLeft = thickness.left;
|
style.paddingLeft = thickness.left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import definition = require("ui/styling");
|
|||||||
import stylersCommon = require("ui/styling/stylers-common");
|
import stylersCommon = require("ui/styling/stylers-common");
|
||||||
import enums = require("ui/enums");
|
import enums = require("ui/enums");
|
||||||
import utils = require("utils/utils");
|
import utils = require("utils/utils");
|
||||||
|
import imageSource = require("image-source");
|
||||||
|
|
||||||
// merge the exports of the common file with the exports of this file
|
// merge the exports of the common file with the exports of this file
|
||||||
declare var exports;
|
declare var exports;
|
||||||
@ -35,6 +36,27 @@ export class DefaultStyler implements definition.stylers.Styler {
|
|||||||
return drawable;
|
return drawable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Background image methods
|
||||||
|
private static setBackgroundImageSourceProperty(view: view.View, newValue: any) {
|
||||||
|
(<android.view.View>view.android).setBackgroundDrawable(new android.graphics.drawable.BitmapDrawable(newValue));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static resetBackgroundImageSourceProperty(view: view.View, nativeValue: any) {
|
||||||
|
if (types.isDefined(nativeValue)) {
|
||||||
|
(<android.view.View>view.android).setBackgroundDrawable(nativeValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getNativeBackgroundImageSourceValue(view: view.View): any {
|
||||||
|
var drawable = view.android.getBackground();
|
||||||
|
|
||||||
|
if (drawable instanceof android.graphics.drawable.BitmapDrawable) {
|
||||||
|
return drawable;
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
//Visibility methods
|
//Visibility methods
|
||||||
private static setVisibilityProperty(view: view.View, newValue: any) {
|
private static setVisibilityProperty(view: view.View, newValue: any) {
|
||||||
var androidValue = (newValue === enums.Visibility.visible) ? android.view.View.VISIBLE : android.view.View.GONE;
|
var androidValue = (newValue === enums.Visibility.visible) ? android.view.View.VISIBLE : android.view.View.GONE;
|
||||||
@ -78,6 +100,11 @@ export class DefaultStyler implements definition.stylers.Styler {
|
|||||||
DefaultStyler.resetBackgroundProperty,
|
DefaultStyler.resetBackgroundProperty,
|
||||||
DefaultStyler.getNativeBackgroundValue));
|
DefaultStyler.getNativeBackgroundValue));
|
||||||
|
|
||||||
|
style.registerHandler(style.backgroundImageSourceProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||||
|
DefaultStyler.setBackgroundImageSourceProperty,
|
||||||
|
DefaultStyler.resetBackgroundImageSourceProperty,
|
||||||
|
DefaultStyler.getNativeBackgroundImageSourceValue));
|
||||||
|
|
||||||
style.registerHandler(style.visibilityProperty, new stylersCommon.StylePropertyChangedHandler(
|
style.registerHandler(style.visibilityProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||||
DefaultStyler.setVisibilityProperty,
|
DefaultStyler.setVisibilityProperty,
|
||||||
DefaultStyler.resetVisibilityProperty));
|
DefaultStyler.resetVisibilityProperty));
|
||||||
|
@ -3,6 +3,7 @@ import style = require("ui/styling/style");
|
|||||||
import definition = require("ui/styling");
|
import definition = require("ui/styling");
|
||||||
import stylersCommon = require("ui/styling/stylers-common");
|
import stylersCommon = require("ui/styling/stylers-common");
|
||||||
import enums = require("ui/enums");
|
import enums = require("ui/enums");
|
||||||
|
import imageSource = require("image-source");
|
||||||
|
|
||||||
// merge the exports of the common file with the exports of this file
|
// merge the exports of the common file with the exports of this file
|
||||||
declare var exports;
|
declare var exports;
|
||||||
@ -32,6 +33,29 @@ export class DefaultStyler implements definition.stylers.Styler {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Background image methods
|
||||||
|
private static setBackgroundImageSourceProperty(view: view.View, newValue: any) {
|
||||||
|
var nativeView: UIView = <UIView>view._nativeView;
|
||||||
|
if (nativeView) {
|
||||||
|
nativeView.backgroundColor = UIColor.alloc().initWithPatternImage(newValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static resetBackgroundImageSourceProperty(view: view.View, nativeValue: any) {
|
||||||
|
var nativeView: UIView = <UIView>view._nativeView;
|
||||||
|
if (nativeView) {
|
||||||
|
nativeView.backgroundColor = nativeValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getNativeBackgroundImageSourceValue(view: view.View): any {
|
||||||
|
var nativeView: UIView = <UIView>view._nativeView;
|
||||||
|
if (nativeView) {
|
||||||
|
return nativeView.backgroundColor;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
//Visibility methods
|
//Visibility methods
|
||||||
private static setVisibilityProperty(view: view.View, newValue: any) {
|
private static setVisibilityProperty(view: view.View, newValue: any) {
|
||||||
var nativeView: UIView = <UIView>view._nativeView;
|
var nativeView: UIView = <UIView>view._nativeView;
|
||||||
@ -68,6 +92,11 @@ export class DefaultStyler implements definition.stylers.Styler {
|
|||||||
DefaultStyler.resetBackgroundProperty,
|
DefaultStyler.resetBackgroundProperty,
|
||||||
DefaultStyler.getNativeBackgroundValue));
|
DefaultStyler.getNativeBackgroundValue));
|
||||||
|
|
||||||
|
style.registerHandler(style.backgroundImageSourceProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||||
|
DefaultStyler.setBackgroundImageSourceProperty,
|
||||||
|
DefaultStyler.resetBackgroundImageSourceProperty,
|
||||||
|
DefaultStyler.getNativeBackgroundImageSourceValue));
|
||||||
|
|
||||||
style.registerHandler(style.visibilityProperty, new stylersCommon.StylePropertyChangedHandler(
|
style.registerHandler(style.visibilityProperty, new stylersCommon.StylePropertyChangedHandler(
|
||||||
DefaultStyler.setVisibilityProperty,
|
DefaultStyler.setVisibilityProperty,
|
||||||
DefaultStyler.resetVisibilityProperty));
|
DefaultStyler.resetVisibilityProperty));
|
||||||
|
Reference in New Issue
Block a user