-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathtemplate-item-input.js
234 lines (222 loc) · 8.45 KB
/
template-item-input.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* テンプレートのマスターデータを元に、入力項目(フォーム)を作るさいの共通処理
*/
$(document).ready(function() {
var timezones = [];
var createTextItem = function(item) {
// text
var tag = '<input type="text" class="form-control" name="item_' + item.itemNo;
var val = '';
if (item.itemValue) {
val = item.itemValue;
}
tag += '" value="' + val + '" />';
return tag;
};
var createTextAreaItem = function(item) {
// text area
var tag = '<textarea class="form-control" name="item_' + item.itemNo+ '" >';
var val = '';
if (item.itemValue) {
val = item.itemValue;
}
tag += val;
tag += '</textarea>';
return tag;
};
var createIntegerItem = function(item) {
// Integer
var tag = '<input type="number" class="form-control" name="item_' + item.itemNo;
var val = '';
if (item.itemValue) {
val = item.itemValue;
}
tag += '" value="' + val + '" min="0" step="1"/>';
return tag;
};
var createRadioItem = function(item) {
var tag = '';
if (item.choices) {
tag += '<br/>';
for (var j = 0; j < item.choices.length; j++) {
var choice = item.choices[j];
tag += '<label class="radio-inline"><input type="radio" class="" name="item_' + item.itemNo;
tag += '" value="' + choice.choiceValue + '" ';
if (choice.choiceValue == item.itemValue) {
tag += 'checked="checked" ';
}
tag += '/> ' + choice.choiceLabel + '</label><br/>';
}
}
return tag;
};
var createCheckboxItem = function(item) {
var tag = '';
if (item.choices) {
tag += '<br/>';
for (var j = 0; j < item.choices.length; j++) {
var choice = item.choices[j];
tag += '<label class="checkbox-inline"><input type="checkbox" class="" name="item_' + item.itemNo;
tag += '" value="' + choice.choiceValue + '" ';
if (item.itemValue) {
var vals = item.itemValue.split(',');
for (var k = 0; k < vals.length; k++) {
if (choice.choiceValue == vals[k].trim()) {
tag += 'checked="checked" ';
break;
}
}
}
tag += '/> ' + choice.choiceLabel + '</label><br/>';
}
}
return tag;
};
var createDateItem = function(item) {
var tag = '<div class="input-group">';
tag += '<input type="text" class="form-control datepicker" name="item_' + item.itemNo;
var val = '';
if (item.itemValue) {
val = item.itemValue;
}
tag += '" value="' + val + '" />';
tag += '<span class="input-group-addon">';
tag += '<span class="fa fa-calendar"></span>';
tag += '</span>';
tag += '</div>';
return tag;
};
var createTimeItem = function(item) {
var tag = '<div class="input-group clockpicker" data-placement="bottom" data-align="top" data-autoclose="true">';
tag += '<input type="text" class="form-control" name="item_' + item.itemNo;
var val = '';
if (item.itemValue) {
val = item.itemValue;
}
tag += '" value="' + val + '" />';
tag += '<span class="input-group-addon">';
tag += '<span class="fa fa-clock-o"></span>';
tag += '</span>';
tag += '</div>';
return tag;
};
var createTimeZoneItem = function(item) {
var tag = '<div class="input-group" data-placement="bottom" data-align="top" data-autoclose="true">';
tag += '<input type="text" class="form-control timezone" name="item_' + item.itemNo;
var val = '';
if (item.itemValue) {
val = item.itemValue;
} else {
var tz = jstz.determine();
val = tz.name();
logging(val);
}
tag += '" value="' + val + '" />';
tag += '<span class="input-group-addon">';
tag += '<span class="fa fa-globe"></span>';
tag += '</span>';
tag += '</div>';
return tag;
};
var initialValue = '';
var addTemplateItem = function(template) {
$('#template_msg').text(template.description);
$('#template_info').removeClass('hide');
$('#template_info').addClass('show');
var contentStr = $('#content').val();
if (!contentStr) {
$('#content').val(template.initialValue);
initialValue = template.initialValue;
} else {
if (initialValue.replace(/\r?\n/g,"") == contentStr.replace(/\r?\n/g,"")) {
// 初期値を指定してから変更していない
$('#content').val(template.initialValue ? template.initialValue : '');
initialValue = template.initialValue;
}
}
$('#template_items').html('');
var exists_date = false;
var exists_time = false;
var exists_timezone = false;
if (template.items && template.items.length > 0) {
for (var i = 0; i < template.items.length; i++) {
var item = template.items[i];
// console.log(item);
var tag = '<div class="form-group"><label for="item_' + item.itemNo + '">' + item.itemName + '</label>';
// テンプレートの項目の種類毎に生成する入力項目を変化
if (item.itemType === 1) {
// textarea
tag += createTextAreaItem(item);
} else if (item.itemType === 2) {
// Integer
tag += createIntegerItem(item);
} else if (item.itemType === 10) {
// Radio
tag += createRadioItem(item);
} else if (item.itemType === 11) {
// Checkbox
tag += createCheckboxItem(item);
} else if (item.itemType === 20) {
// Date
tag += createDateItem(item);
exists_date = true;
} else if (item.itemType === 21) {
// Time
tag += createTimeItem(item);
exists_time = true;
} else if (item.itemType === 22) {
// TimeZone
tag += createTimeZoneItem(item);
exists_timezone = true;
} else {
tag += createTextItem(item);
}
tag += '</div>';
$('#template_items').append(tag);
}
}
if (exists_date) {
var lang = 'en';
if (_LANG) {
lang = _LANG;
}
$('.datepicker').datepicker({
format: 'yyyy-mm-dd',
autoclose: true,
todayHighlight: true,
language: lang,
});
}
if (exists_time) {
$('.clockpicker').clockpicker()
.find('input').change(function(){
logging(this.value);
});
}
if (exists_timezone) {
// console.log('timezone');
Promise.try(function() {
if (timezones.length > 0) {
return Promise.resolve();
}
return new Promise(function(resolve, reject) {
$.ajax({
url: _CONTEXT + '/bower/moment-timezone/data/meta/latest.json',
type: 'GET',
timeout: 10000
}).done(function(result, textStatus, xhr) {
for (var zone in result.zones) {
timezones.push(zone);
}
return resolve();
}).fail(function(xhr, textStatus, error) {
return reject(error);
});
});
}).then(function() {
$('.timezone').typeahead({ source:timezones });
});
}
};
document.__add_Template_Edit_Item = addTemplateItem;
});