]> git.proxmox.com Git - sencha-touch.git/blob - src/src/draw/modifier/Highlight.js
import Sencha Touch 2.4.2 source
[sencha-touch.git] / src / src / draw / modifier / Highlight.js
1 /**
2 * @class Ext.draw.modifier.Highlight
3 * @extends Ext.draw.modifier.Modifier
4 *
5 * Highlight is a modifier that will override the attributes
6 * with its `highlightStyle` attributes when `highlighted` is true.
7 */
8 Ext.define("Ext.draw.modifier.Highlight", {
9 extend: 'Ext.draw.modifier.Modifier',
10 alias: 'modifier.highlight',
11
12 config: {
13
14 /**
15 * @cfg {Boolean} enabled 'true' if the highlight is applied.
16 */
17 enabled: false,
18
19 /**
20 * @cfg {Object} highlightStyle The style attributes of the highlight modifier.
21 */
22 highlightStyle: null
23 },
24
25 preFx: true,
26
27 applyHighlightStyle: function (style, oldStyle) {
28 oldStyle = oldStyle || {};
29 if (this.getSprite()) {
30 Ext.apply(oldStyle, this.getSprite().self.def.normalize(style));
31 } else {
32 Ext.apply(oldStyle, style);
33 }
34 return oldStyle;
35 },
36
37 /**
38 * @inheritdoc
39 */
40 prepareAttributes: function (attr) {
41 if (!attr.hasOwnProperty('highlightOriginal')) {
42 attr.highlighted = false;
43 attr.highlightOriginal = Ext.Object.chain(attr);
44 }
45 if (this._previous) {
46 this._previous.prepareAttributes(attr.highlightOriginal);
47 }
48 },
49
50 updateSprite: function (sprite, oldSprite) {
51 if (sprite) {
52 if (this.getHighlightStyle()) {
53 this._highlightStyle = sprite.self.def.normalize(this.getHighlightStyle());
54 }
55 this.setHighlightStyle(sprite.config.highlightCfg);
56 }
57
58 // Before attaching to a sprite, register the highlight related
59 // attributes to its definition.
60 //
61 // TODO(zhangbei): Unfortunately this will effect all the sprites of the same type.
62 // As the redundant attributes would not effect performance, it is not yet a big problem.
63 var def = sprite.self.def;
64 this.setSprite(sprite);
65 def.setConfig({
66 defaults: {
67 highlighted: false
68 },
69
70 processors: {
71 highlighted: 'bool'
72 },
73
74 aliases: {
75 "highlight": "highlighted",
76 "highlighting": "highlighted"
77 },
78
79 dirtyFlags: {
80 },
81
82 updaters: {
83
84 }
85 });
86 },
87
88 /**
89 * Filter modifier changes if overriding source attributes.
90 * @param {Object} attr The source attributes.
91 * @param {Object} changes The modifier changes.
92 * @return {*} The filtered changes.
93 */
94 filterChanges: function (attr, changes) {
95 var me = this,
96 name,
97 original = attr.highlightOriginal,
98 style = me.getHighlightStyle();
99 if (attr.highlighted) {
100 for (name in changes) {
101 if (style.hasOwnProperty(name)) {
102 // If it's highlighted, then save the changes to lower level
103 // on overridden attributes.
104 original[name] = changes[name];
105 delete changes[name];
106 }
107 }
108 }
109
110 for (name in changes) {
111 if (name !== 'highlighted' && original[name] === changes[name]) {
112 // If it's highlighted, then save the changes to lower level
113 // on overridden attributes.
114 delete changes[name];
115 }
116 }
117
118 return changes;
119 },
120
121 /**
122 * @inheritdoc
123 */
124 pushDown: function (attr, changes) {
125 var style = this.getHighlightStyle(),
126 original = attr.highlightOriginal,
127 oldHighlighted, name;
128
129 if (changes.hasOwnProperty('highlighted')) {
130 oldHighlighted = changes.highlighted;
131 // Hide `highlighted` and `highlightStyle` to underlying modifiers.
132 delete changes.highlighted;
133
134 if (this._previous) {
135 changes = this._previous.pushDown(original, changes);
136 }
137 changes = this.filterChanges(attr, changes);
138
139 if (oldHighlighted !== attr.highlighted) {
140 if (oldHighlighted) {
141 // switching on
142 // At this time, original should be empty.
143 for (name in style) {
144 // If changes[name] just changed the value in lower levels,
145 if (name in changes) {
146 original[name] = changes[name];
147 } else {
148 original[name] = attr[name];
149 }
150 if (original[name] !== style[name]) {
151 changes[name] = style[name];
152 }
153 }
154 } else {
155 // switching off
156 for (name in style) {
157 if (!(name in changes)) {
158 changes[name] = original[name];
159 }
160 delete original[name]; // TODO: Need deletion API?
161 }
162 }
163 changes.highlighted = oldHighlighted;
164 }
165 } else {
166 if (this._previous) {
167 changes = this._previous.pushDown(original, changes);
168 }
169 changes = this.filterChanges(attr, changes);
170 }
171
172 return changes;
173 },
174
175 /**
176 * @inheritdoc
177 */
178 popUp: function (attr, changes) {
179 changes = this.filterChanges(attr, changes);
180 Ext.draw.modifier.Modifier.prototype.popUp.call(this, attr, changes);
181 }
182 });