1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
(function (_super) {
hui.ui.StyleEditor = function(options) {
_super.call(this, options);
this.value = null;
this.components = options.components;
this.queryEditors = [];
this._attach();
};
hui.ui.StyleEditor.create = function(options) {
options = options || {};
var element = hui.build('div.hui_styleeditor',{html:'<div class="hui_styleeditor_list"></div>'});
options.element = element;
return new hui.ui.StyleEditor(options);
};
hui.ui.StyleEditor.prototype = {
nodes : {
list : '.hui_styleeditor_list'
},
_attach : function() {
var self = this;
hui.on(this.element, 'click', function(e) {
e = hui.event(e);
var query = e.findByClass('hui_styleeditor_query');
if (query) {
self.editQuery(parseInt(query.getAttribute('data-index'), 10));
}
});
var button = hui.ui.Button.create({text:'Add', small:true});
this.element.appendChild(button.element);
button.listen({$click:this.add.bind(this)});
},
editQuery : function(index) {
var query = this.value.queries[index];
win = this.queryEditors[index];
if (win) {
win.show();
return;
}
win = hui.ui.Window.create({
title : this._getQueryDescription(query),
width: 400,
padding: 10
});
this.queryEditors[index] = win;
var self = this;
var overflow = hui.ui.Overflow.create({height: 400});
win.add(overflow);
hui.each(this.components,function(component) {
var form = hui.ui.Form.create();
form.buildGroup({above:false},[{
type : 'DropDown', label: 'Display:', options : {key:'display', value:'', items:[
{value:'',text:'Unchanged'}, {value:'block',text:'Block'}, {value:'flex',text:'Flex'}
]}
},{
type : 'DropDown', label: 'Flex direction:', options : {key:'flex-direction', value:'', items:[
{value:'',text:'Unchanged'},
{value:'row',text:'Row'},{value:'row-reverse',text:'Row reverse'},{value:'Column',text:'column'},{value:'column-reverse',text:'Column reverse'},
{value:'inherit',text:'Inherit'}, {value:'initial',text:'Initial'}, {value:'unset',text:'Unset'}, {value:'revert',text:'Revert'}
]}
},{
type : 'StyleLength', label: 'Width:', options : {key:'width', value:''}
},{
type : 'ColorInput', label: 'Text color:', options : {key:'color', value:''}
},{
type : 'StyleLength', label: 'Max width:', options : {key:'max-width', value:''}
},{
type : 'StyleLength', label: 'Min width:', options : {key:'min-width', value:''}
}]);
overflow.add(hui.build('div',{text:component.description, style: 'text-transform: uppercase; font-size: 12px;'}));
form.setValues(self._getComponentValues(query, component));
overflow.add(form);
var values = {};
form.listen({
$valuesChanged : function(values) {
var rules = self._getRulesFor({query:index, component:component.name});
hui.log(component.name, values, rules);
hui.each(rules,function(rule) {
if (values[rule.name]!==undefined) {
rule.value = values[rule.name];
values[rule.name] = undefined;
}
});
hui.each(values,function(key,value) {
if (key.indexOf('unnamed')!==0 && !hui.isBlank(value)) {
rules.push({name:key, value:value});
}
});
self.fireValueChange();
}
});
});
win.show();
},
_getComponentValues : function(query,component) {
var values = {};
if (query.components) {
var found = query.components.find(function(other) {
return other.name == component.name;
});
if (found) {
found.rules.forEach(function(rule) {
values[rule.name] = rule.value;
});
}
}
return values;
},
add : function() {
this.value.queries.push({components:[]});
this.draw();
},
setValue : function(value) {
this.value = value;
this.draw();
},
_getRulesFor : function(params) {
var query = this.value.queries[params.query];
var comps = query.components;
for (var i = 0; i < comps.length; i++) {
if (comps[i].name == params.component) {
if (!comps[i].rules) {
comps[i].rules = [];
}
return comps[i].rules;
}
}
var rules = [];
query.components.push({name:params.component, rules:rules});
return rules;
},
draw : function() {
this.nodes.list.innerHTML = '';
if (this.value && this.value.queries) {
for (var i = 0; i < this.value.queries.length; i++) {
var query = this.value.queries[i];
hui.build('div.hui_styleeditor_query',{text:this._getQueryDescription(query), parent: this.nodes.list, 'data-index':i});
}
}
},
_getQueryDescription : function(query) {
var text = [];
var props = ['max-width','min-width','max-height','min-height'];
for (var i = 0; i < props.length; i++) {
var prop = props[i];
if (query[prop]) {
text.push(prop + ': ' + query[prop]);
}
}
if (!text.length) {
text.push('Anything');
}
return text.join(', ');
},
$$childSizeChanged : function() {
},
$$layout : function() {
}
};
hui.extend(hui.ui.StyleEditor, _super);
})(hui.ui.Component);