/*
This file contains proprietary software owned by Motorola Mobility, Inc.
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.
(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
*/
var Montage = require("montage/core/core").Montage,
Component = require("montage/ui/component").Component,
ShorthandProps = require("js/panels/CSSPanel/css-shorthand-map");
exports.StyleDeclaration = Montage.create(Component, {
cssText : { value: null },
focusDelegate : { value: null },
needsSort : { value: null },
includeEmptyStyle : {
value: true,
distinct: true
},
styles : {
value: [],
distinct: true
},
_styleSortFunction : {
value: function(styleA, styleB) {
///// If the style is an empty style (with Add button)
///// push to end of declaration
if(styleA.isEmpty) {
return 1;
} else if (styleB.isEmpty) {
return -1;
}
///// Alphabetic sort based on property name
if (styleA.name < styleB.name) {
return -1;
} else if (styleA.name > styleB.name) {
return 1;
} else {
return 0;
}
}
},
_styleFilterFunction: {
value: function(style, styleArray) {
var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[style.name];
///// No shorthands, return true to include style
if(!shorthands) { return true; }
var subProps = ShorthandProps.CSS_SHORTHAND_TO_SUBPROP_MAP[shorthands[0]],
stylesArray = styleArray,
hasAll;
debugger;
hasAll = subProps.every(function(subProp) {
debugger;
return this.declaration[subProp];
}, this);
if(hasAll) {
return false;
}
}
},
_declaration: {
value: null
},
declaration: {
get: function() {
return this._declaration;
},
set: function(dec) {
var stylesArray;
if(this._declaration) {
this.styles = null;
this.styles = [];
}
///// Take snapshot of declaration
this.cssText = dec.cssText;
stylesArray = Array.prototype.slice.call(dec);
stylesArray.forEach(function(prop, index) {
this.styles.push({
name: prop,
value: dec.getPropertyValue(prop)
});
}, this);
if(this.includeEmptyStyle) {
this.styles.push({
name : "property",
value : "value",
isEmpty : true
});
}
this._declaration = dec;
this.needsDraw = this.needsSort = true;
}
},
styleShorthander : {
value: function(styles) {
var shorthandsToAdd = [],
subProps, hasAll;
styles.forEach(function(property, index, styleArray) {
var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[property];
if(!shorthands) { return false; }
var subProps = ShorthandProps.CSS_SHORTHAND_TO_SUBPROP_MAP[shorthands[0]],
stylesArray = styleArray;
hasAll = subProps.every(function(subProp) {
return stylesArray.indexOf(subProp) !== -1;
});
if(hasAll) {
subProps.forEach(function(subProp) {
stylesArray.splice(stylesArray.indexOf(subProp), 1);
}, this);
shorthandsToAdd.push(shorthands[0]);
}
}, this);
return styles.concat(shorthandsToAdd);
}
},
_getStyleToIndexMap : {
value: function() {
var map = {};
for(var i = 0; i