forked from xibosignage/xibo-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayprofile-form-edit-javascript.twig
384 lines (337 loc) · 14.3 KB
/
displayprofile-form-edit-javascript.twig
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
{#
/**
* Copyright (C) 2020 Xibo Signage Ltd
*
* Xibo - Digital Signage - http://www.xibo.org.uk
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/
#}
<script type="text/javascript">
/**
* Setup Display Profile setting forms
*/
function displayProfileFormOpen() {
// TIMERS
setupFormFields(
$('#timers-container'),
Handlebars.compile($('#template-timers').html()),
$('#timers-container').data('values'),
$('#timers-container').data('options'),
timersFormInit,
timersFormInit
);
// PICTURE OPTIONS
setupFormFields(
$('#picture-options-container'),
Handlebars.compile($('#template-picture-options').html()),
$('#picture-options-container').data('values'),
$('#picture-options-container').data('options'),
pictureOptionsFormInit,
pictureOptionsFormUpdate
);
// Android location field
setAndroidDimensions($('.tab-pane#location'));
// Popovers
$('[data-toggle="popover"]').popover();
}
/**
* Android dimension field
* @param {jquery} container
*/
function setAndroidDimensions($container) {
const $inputField = $container.find('#screenDimensions');
const template = Handlebars.compile($('#android-dimension-fields').html());
// Prevent to run if screen dimensions does not exist
if($inputField.length <= 0){
return;
}
let fieldsTemplate;
// Hide input
$inputField.attr('type', 'hidden');
// Parse value if exists and assign it to the elements
if($inputField.val() != "") {
let values = $inputField.val().split(',');
fieldsTemplate = template({
left: values[0],
top: values[1],
width: values[2],
height: values[3]
});
} else {
fieldsTemplate = template();
}
// Add fields to parent container
$inputField.parent().prepend(fieldsTemplate);
// Initialise values
$container.on('change', '.androidDimensionInput', function() {
// Get all separated values
var valueLeft = $container.find('#dimensionLeft').val();
var valueTop = $container.find('#dimensionTop').val();
var valueWidth = $container.find('#dimensionWidth').val();
var valueHeight = $container.find('#dimensionHeight').val();
// Set default colour
$('.androidDimensionInput').css('background-color', '#fff');
// Set value to hidden input
if(valueLeft != '' && valueTop != '' && valueWidth != '' && valueWidth >= 0 && valueHeight != '' && valueHeight >= 0) {
$inputField.val([valueLeft, valueTop, valueWidth, valueHeight].toString());
} else {
// Invalid value
$inputField.val('');
// If some of the elements aren't empty and it's invalid, show a specific background
if(valueLeft != '' || valueTop != '' || valueWidth != '' || valueHeight != ''){
$('.androidDimensionInput').css('background-color', '#ffce9c');
}
}
});
}
/**
* Setup form dynamic fields
* @param {jquery} container
* @param {hbstemplate} template
* @param {Object} values - Defined values
* @param {Object} options - All the options
*/
function setupFormFields(container, template, values, options, callbackFuncInit, callbackFuncUpdate) {
// Prevent forms to setup inexisting fields
if(container.length === 0) {
return;
}
if(Object.keys(values).length == 0) {
// Add a template row
var context = {
options: options,
index: 1,
name: "",
values: {},
buttonGlyph: "fa-plus"
};
container.append(template(context));
} else {
// For each of the existing codes, create form components
var i = 0;
$.each(values, function(index, values) {
i++;
var context = {
options: options,
index: i,
name: index,
values: values,
buttonGlyph: ((i == 1) ? "fa-plus" : "fa-minus")
};
container.append(template(context));
});
}
// Button click handle
container.on("click", "button", function(e) {
e.preventDefault();
// find the gylph
if($(this).find("i").hasClass("fa-plus")) {
var context = {
options: options,
index: container.find('.multiSelect:last').data('index') + 1,
name: "",
values: {},
buttonGlyph: "fa-minus"
};
// Append new object to container
container.append(template(context));
// Initialize form fields
callbackFuncUpdate(container, options, values);
} else {
// Remove this row
$(this).closest(".form-group").remove();
}
});
// Initialize form fields
callbackFuncInit(container, options, values);
}
/**
* Init/Update timePicker fields
*/
function timersFormInit(container) {
var customTimeFormat = 'HH:mm';
container.find('.input-group:not(.timerInit) .timePickerDisplayEditForm').each(function() {
if(calendarType == 'Jalali') {
initDatePicker(
$(this),
customTimeFormat,
customTimeFormat,
{
onlyTimePicker: true,
format: customTimeFormat,
timePicker: {
second: {
enabled: false
}
},
altFieldFormatter: function(unixTime) {
var newDate = moment.unix(unixTime / 1000);
newDate.set('second', 0);
return newDate.format(customTimeFormat);
}
}
);
} else {
initDatePicker(
$(this),
customTimeFormat,
customTimeFormat,
{
enableTime: true,
noCalendar: true,
time_24hr: true,
altFormat: customTimeFormat
}
);
}
$(this).parent().addClass('timerInit');
});
}
/**
* Initialise all form fields
*/
function pictureOptionsFormInit(container, options, values) {
container.find('.multiSelect').each(function() {
pictureOptionsFormChangeInputs($(this).val(), $(this).closest('.row'), options[$(this).val()], values);
});
container.on('change', '.multiSelect', function() {
pictureOptionsFormChangeInputs($(this).val(), $(this).closest('.row'), options[$(this).val()], values);
});
}
/**
* Update the last added field
*/
function pictureOptionsFormUpdate(container, options, values) {
container.find('.multiSelect:last').each(function() {
pictureOptionsFormChangeInputs($(this).val(), $(this).closest('.row'), options[$(this).val()], values);
});
}
/**
* Populate and create the slider
*/
function pictureOptionsFormChangeInputs(selProperty, row, propertyOptions, values) {
if(selProperty == undefined || selProperty == '') {
row.find('.multiSelectInputs').html(Handlebars.compile($('#template-picture-options-none').html())());
return;
}
// Add empty template to the container
var template = Handlebars.compile($('#template-picture-options-slider').html());
row.find('.multiSelectInputs').html(template({
index: row.find('.multiSelect').data('index')
}));
var propertyValue = values[selProperty];
var sliderOptions = propertyOptions.sliderOptions;
// Find respective index
if(typeof(propertyValue) == "string") {
for (var index = 0; index < sliderOptions.ticks_labels.length; index++) {
var label = sliderOptions.ticks_labels[index];
if(label == propertyValue) {
propertyValue = index;
}
}
}
// Set value to the options
sliderOptions.value = ((propertyValue != undefined) ? propertyValue : 0);
// Init slider
row.find('.pictureControlsSlider').bootstrapSlider(sliderOptions);
// Refresh on tab switch to fix broken labels
$('a[data-toggle="tab"]').on('shown.bs.tab', function() {
row.find('.pictureControlsSlider').bootstrapSlider('refresh', { useCurrentValue: true });
});
}
// Equals helper for the templates below
Handlebars.registerHelper('eq', function(v1, v2, opts) {
if (v1 === v2) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
});
</script>
<style>
/*
* Slider width fix
*/
.slider:not(.scaled-slider) {
width: 100% !important;
}
</style>
{% verbatim %}
<script type="text/x-handlebars-template" id="template-timers">
<div class="form-group col-sm-12 row form-group-timers">
<div class="col-sm-1">
<button type="button" class="btn btn-white"><i class="fa {{ buttonGlyph }}"></i></button>
</div>
<div class="col-sm-5">
<select class="form-control multiSelect" name="timers[{{ index }}][day]" data-index="{{ index }}">
<option value=""></option>
{{#each options}}
<option value="{{ id }}" {{#eq id ../name}} selected{{/eq}}>{{ name }}</option>
{{/each}}
</select>
</div>
<div class="col-sm-3 multiSelectInputs">
<div class="input-group">
<input class="form-control timePickerDisplayEditForm timersOn" type="text" name="timers[{{ index }}][on]" id="timersOn{{ index }}" value="{{ values.on }}" />
<span class="input-group-addon date-clear-button d-none" role="button"><i class="fa fa-times"></i></span>
</div>
</div>
<div class="col-sm-3 multiSelectInputs">
<div class="input-group">
<input class="form-control timePickerDisplayEditForm timersOff" type="text" name="timers[{{ index }}][off]" id="timersOff{{ index }}" value="{{ values.off }}" />
<span class="input-group-addon date-clear-button d-none" role="button"><i class="fa fa-times"></i></span>
</div>
</div>
</div>
</script>
<script type="text/x-handlebars-template" id="template-picture-options">
<div class="form-group col-sm-12 row form-group-picture-options">
<div class="col-sm-1">
<button type="button" class="btn btn-white"><i class="fa {{ buttonGlyph }}"></i></button>
</div>
<div class="col-sm-5">
<select class="form-control multiSelect" name="pictureControls[{{ index }}][property]" data-index="{{ index }}">
<option value=""></option>
{{#each options}}
<option value="{{@key}}" {{#eq @key ../name}} selected{{/eq}}>{{ name }}</option>
{{/each}}
</select>
</div>
<div class="col-sm-6 multiSelectInputs">
{% endverbatim %}
<p class="btn btn-block btn-primary" disabled>{% trans "Select a property to display inputs" %}</p>
{% verbatim %}
</div>
</div>
</script>
<script type="text/x-handlebars-template" id="template-picture-options-slider">
<input type="text" class="pictureControlsSlider" name="pictureControls[{{ index }}][value]"/>
</script>
<script type="text/x-handlebars-template" id="template-picture-options-none">
{% endverbatim %}
<p class="btn btn-block btn-primary" disabled>{% trans "Select a property to display inputs" %}</p>
{% verbatim %}
</script>
<script type="text/x-handlebars-template" id="android-dimension-fields">
<div class="row">
<input type="number" value="{{ left }}" class="col-3 form-control androidDimensionInput" name="dimensionLeft" id="dimensionLeft" placeholder="{% endverbatim %}{{ "Left"|trans }}{% verbatim %}"></input>
<input type="number" value="{{ top }}" class="col-3 form-control androidDimensionInput" name="dimensionTop" id="dimensionTop" placeholder="{% endverbatim %}{{ "Top"|trans }}{% verbatim %}"></input>
<input type="number" value="{{ width }}" class="col-3 form-control androidDimensionInput" name="dimensionWidth" id="dimensionWidth" placeholder="{% endverbatim %}{{ "Width"|trans }}{% verbatim %}"></input>
<input type="number" value="{{ height }}" class="col-3 form-control androidDimensionInput" name="dimensionHeight" id="dimensionHeight" placeholder="{% endverbatim %}{{ "Height"|trans }}{% verbatim %}"></input>
</div>
</script>
{% endverbatim %}