forked from huiyan-fe/mapv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionDataControl.js
More file actions
209 lines (191 loc) · 6.99 KB
/
Copy pathOptionDataControl.js
File metadata and controls
209 lines (191 loc) · 6.99 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @file this object is to deal with the optional datas
* @author Mofei Zhu <zhuwenlong@baidu.com>
*/
/* globals util */
function OptionalData(superObj) {
// set params
var options = superObj.options || {};
this.drawType = options.drawType;
this.super = superObj;
// init options
this.options = options.drawOptions || {};
// init css
this.initCSS();
// append dom to body
this.initDom();
// bind event
this.bindEvent();
}
OptionalData.prototype.initCSS = function () {
util.addCssByStyle([
'.controlBox { position:absolute; left:0px; top:0px; background:rgba(0,0,0,0.5); padding:10px; }',
'.controlBox input {border-radius:6px; border:none; padding:10px;}',
'.controlBox button ',
'{ padding:8px 10px; border:none; width:40%; margin-left: 10px; border-radius:6px; cursor:pointer; }',
'.controlBoxBlock { color:#fff; padding: 10px; }',
'.controlBoxTitle { display:inline-block; width:100px; text-align:right; padding-right:10px; }'
].join('\n'));
};
/**
* append the content to body
* @return {DOM} return the appneded dom
*/
OptionalData.prototype.initDom = function () {
var box = this.box = document.createElement('div');
box.className = 'controlBox';
var contentBox = this.contentBox = document.createElement('div');
var btnBox = document.createElement('div');
btnBox.style.textAlign = 'center';
var updateBtn = this.updateBtn = document.createElement('button');
var resetBtn = this.resetBtn = document.createElement('button');
updateBtn.textContent = 'update';
resetBtn.textContent = 'reset';
box.appendChild(contentBox);
btnBox.appendChild(updateBtn);
btnBox.appendChild(resetBtn);
box.appendChild(btnBox);
document.body.appendChild(box);
return box;
};
/**
* init the controller to box
*/
OptionalData.prototype.initController = function (layer, drawType) {
return false
this._layer = layer;
var self = this;
var options;
if (drawType) {
var drawer = layer._getDrawer(drawType);
options = self.options = drawer.getDrawOptions();
self.drawType = drawType;
} else {
options = self.options;
}
var editTag = options.editable;
if (!editTag) {
self.box.style.display = 'none';
return false;
} else {
self.box.style.display = 'block';
}
self.contentBox.innerHTML = '';
var newTag = [];
for (var i = 0; i < editTag.length; i++) {
var tag = editTag[i];
if (typeof (tag) === 'string') {
tag = {
name: tag,
type: 'text'
};
editTag[i] = tag;
}
if (options[tag.name]) {
makeLabelInput(tag);
editTag[i]._oldVal = options[tag.name];
newTag.push(editTag[i]);
}
}
options.editable = newTag;
function makeLabelInput(tag) {
var box = document.createElement('div');
box.className = 'controlBoxBlock';
var span = document.createElement('span');
span.textContent = tag.name;
span.className = 'controlBoxTitle';
// span.style.padding = '0 10px';
// if type equal value , show normal inoput
// if type equal option , show checkboxk
var optionBox;
if (tag.type === 'text' || tag.type === 'color') {
optionBox = document.createElement('label');
var input = document.createElement('input');
input.name = tag.name;
input.value = options[tag.name];
input.type = tag.type;
optionBox.appendChild(input);
} else if (tag.type === 'option') {
optionBox = document.createElement('span');
for (var i = 0; i < tag.value.length; i++) {
var label = document.createElement('label');
label.style.padding = '0 10px 0 0';
label.style.cursor = 'pointer';
var radio = document.createElement('input');
radio.type = 'radio';
radio.name = tag.name;
radio.value = tag.value[i];
if (options[tag.name] === tag.value[i]) {
radio.checked = true;
}
var labelSpan = document.createElement('span');
labelSpan.textContent = tag.value[i];
label.appendChild(radio);
label.appendChild(labelSpan);
optionBox.appendChild(label);
}
} else if (tag.type === 'check') {
optionBox = document.createElement('label');
var input = document.createElement('input');
input.type = 'checkbox';
input.name = tag.name;
input.checked = options[tag.name];
optionBox.appendChild(input);
} else if (tag.type === 'json') {
optionBox = document.createElement('label');
var input = document.createElement('input');
input.setAttribute("isJson", true);
input.name = tag.name;
input.value = JSON.stringify(options[tag.name]);
optionBox.appendChild(input);
} else {
return false;
}
box.appendChild(span);
box.appendChild(optionBox);
self.contentBox.appendChild(box);
}
};
/**
* bind update and reset button's event
*/
OptionalData.prototype.bindEvent = function () {
var self = this;
this.updateBtn.onclick = function () {
for (var i = 0; i < self.options.editable.length; i++) {
var name = self.options.editable[i].name;
var val = self.contentBox.querySelector('input[name="' + name + '"]');
if (val) {
if (val.type === 'radio') {
val = self.contentBox.querySelector('input[name="' + name + '"]:checked');
self.options[name] = val.value;
} else if (val.type === 'checkbox') {
val = self.contentBox.querySelector('input[name="' + name + '"]');
self.options[name] = val.checked;
} else {
if (val.getAttribute('isJson') === 'true') {
console.log(1);
self.options[name] = JSON.parse(val.value);
} else {
self.options[name] = val.value;
}
}
}
}
var drawer = self._layer._getDrawer(self.drawType);
drawer.setDrawOptions(self.options);
self._layer.draw();
};
this.resetBtn.onclick = function () {
for (var i = 0; i < self.options.editable.length; i++) {
var name = self.options.editable[i].name;
var oldVal = self.options.editable[i]._oldVal;
self.options[name] = oldVal;
}
var drawer = this._layer._getDrawer(self.drawType);
drawer.setDrawOptions(self.options);
this._layer.draw();
self.initController();
// console.log('reset', self.options);
};
};