|
1 |
{% extends "qommon/forms/widget.html" %}
|
|
2 |
{% load qommon i18n %}
|
|
3 |
{% block widget-control %}
|
|
4 |
<select style="display: none" id="form_{{widget.name}}" name="{{widget.name}}"
|
|
5 |
{% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}"{% endfor %}>
|
|
6 |
<option value="">---</option>
|
|
7 |
{% for option in widget.get_options %}
|
|
8 |
{% with datetime=option.options.datetime|parse_datetime %}
|
|
9 |
<option{% for attr in option.attrs.items %} {{attr.0}}="{{attr.1}}"{% endfor %}
|
|
10 |
data-weekday="{{ datetime|date:"l" }}"
|
|
11 |
data-date="{{ datetime|date:"Y-m-d" }}"
|
|
12 |
data-time="{{ datetime|date:"H:i" }}"
|
|
13 |
>{{ option.description }}</option>
|
|
14 |
{% endwith %}
|
|
15 |
{% endfor %}
|
|
16 |
</select>
|
|
17 |
<div id="form_{{widget.name}}_table" class="meetings_table">
|
|
18 |
</div>
|
|
19 |
<script>
|
|
20 |
$(function() {
|
|
21 |
var WEEKDAYS = ["{% trans "Sunday" %}", "{% trans "Monday" %}",
|
|
22 |
"{% trans "Tuesday" %}", "{% trans "Wednesday" %}",
|
|
23 |
"{% trans "Thursday" %}", "{% trans "Friday" %}",
|
|
24 |
"{% trans "Saturday" %}"];
|
|
25 |
var $select = $('#form_{{widget.name}}');
|
|
26 |
var $table = $('#form_{{widget.name}}_table');
|
|
27 |
var column_count = 5;
|
|
28 |
|
|
29 |
function fill_with_items(items) {
|
|
30 |
$select.empty();
|
|
31 |
$('<option value=""></option>').appendTo($select);
|
|
32 |
for (var i=0; i<items.length; i++) {
|
|
33 |
var $option = $('<option></option>', {value: items[i].id, text: items[i].text});
|
|
34 |
if (items[i].disabled) {
|
|
35 |
$option.prop('disabled', true);
|
|
36 |
}
|
|
37 |
var date = new Date(items[i].datetime);
|
|
38 |
$option.attr('data-weekday', WEEKDAYS[date.getDay()]);
|
|
39 |
$option.attr('data-date', items[i].datetime.slice(0, 10));
|
|
40 |
$option.attr('data-time', items[i].datetime.slice(11, 16));
|
|
41 |
$option.appendTo($select);
|
|
42 |
}
|
|
43 |
}
|
|
44 |
|
|
45 |
$select.on('wcs:options-change', function(ev, data) {
|
|
46 |
if (data !== undefined) {
|
|
47 |
fill_with_items(data.items);
|
|
48 |
}
|
|
49 |
$table.empty();
|
|
50 |
var options = $select.find('option');
|
|
51 |
var current_date = null;
|
|
52 |
var current_day_div = null;
|
|
53 |
var current_offset = 0;
|
|
54 |
var nb_days = 0;
|
|
55 |
|
|
56 |
for (var i=0; i<options.length; i++) {
|
|
57 |
var $option = $(options[i]);
|
|
58 |
if ($option.attr('value') == '') continue;
|
|
59 |
var option_date = $option.data('date');
|
|
60 |
if (option_date !== current_date) {
|
|
61 |
var day_label = $option.text().split(' ', 3).join(' ');
|
|
62 |
if (Intl && Intl.DateTimeFormat) {
|
|
63 |
// create day label from actual date, this works for event agendas.
|
|
64 |
var date = new Date(option_date);
|
|
65 |
var month = new Intl.DateTimeFormat('fr', {month: 'long'}).format(date);
|
|
66 |
day_label = date.getDate() + ' ' + month + ' ' + date.getFullYear();
|
|
67 |
}
|
|
68 |
var weekday = $option.data('weekday');
|
|
69 |
nb_days += 1;
|
|
70 |
current_day_div = $('<div><div class="head">' + weekday + '<br>' + day_label + '</div></div>');
|
|
71 |
current_day_div.appendTo($('#form_{{widget.name}}_table'));
|
|
72 |
current_date = option_date;
|
|
73 |
}
|
|
74 |
var day_time = $option.data('time');
|
|
75 |
var option_span = $('<span class="selectable" data-idx="' + i + '">' + day_time + '</span>').appendTo(current_day_div);
|
|
76 |
if ($option.attr('disabled')) {
|
|
77 |
$(option_span).addClass('disabled').removeClass('selectable');
|
|
78 |
}
|
|
79 |
if ($option.attr('selected')) {
|
|
80 |
current_offset = nb_days - 1;
|
|
81 |
$(option_span).addClass('on');
|
|
82 |
}
|
|
83 |
}
|
|
84 |
var go_prev = $('<button class="prev">←</button>');
|
|
85 |
var go_next = $('<button class="next">→</button>');
|
|
86 |
go_prev.prependTo($('#form_{{widget.name}}_table'));
|
|
87 |
go_next.appendTo($('#form_{{widget.name}}_table'));
|
|
88 |
go_prev.on('click', function() {
|
|
89 |
current_offset = Math.max(0, current_offset - 1);
|
|
90 |
display(current_offset);
|
|
91 |
return false;
|
|
92 |
});
|
|
93 |
go_next.on('click', function() {
|
|
94 |
current_offset = Math.min(current_offset + 1, Math.max(0, nb_days-column_count));
|
|
95 |
display(current_offset);
|
|
96 |
return false;
|
|
97 |
});
|
|
98 |
|
|
99 |
function display(offset) {
|
|
100 |
$('#form_{{widget.name}}_table > div').each(function(idx, elem) {
|
|
101 |
if (idx >= offset && idx < offset+column_count) {
|
|
102 |
$(elem).show();
|
|
103 |
} else {
|
|
104 |
$(elem).hide();
|
|
105 |
}
|
|
106 |
});
|
|
107 |
if (go_prev.next().is(':visible')) {
|
|
108 |
go_prev.prop('disabled', true);
|
|
109 |
} else {
|
|
110 |
go_prev.prop('disabled', null);
|
|
111 |
}
|
|
112 |
if (go_next.prev().is(':visible')) {
|
|
113 |
go_next.prop('disabled', true);
|
|
114 |
} else {
|
|
115 |
go_next.prop('disabled', null);
|
|
116 |
}
|
|
117 |
current_offset = offset;
|
|
118 |
}
|
|
119 |
|
|
120 |
function set_layout() {
|
|
121 |
if ($select.parents('form').width() > 600) {
|
|
122 |
// desktop layout
|
|
123 |
column_count = 5;
|
|
124 |
$table.removeClass('mobile');
|
|
125 |
} else {
|
|
126 |
// mobile layout
|
|
127 |
column_count = 1;
|
|
128 |
$table.addClass('mobile');
|
|
129 |
}
|
|
130 |
display(current_offset);
|
|
131 |
t1 = new Date();
|
|
132 |
}
|
|
133 |
set_layout();
|
|
134 |
var layout_change_timeout_id = null;
|
|
135 |
$(window).on('resize', function() {
|
|
136 |
clearTimeout(layout_change_timeout_id);
|
|
137 |
layout_change_timeout_id = setTimeout(set_layout, 200);
|
|
138 |
});
|
|
139 |
|
|
140 |
$('#form_{{widget.name}}_table span.selectable').on('click', function() {
|
|
141 |
$('#form_{{widget.name}}_table span').removeClass('on');
|
|
142 |
$(this).addClass('on');
|
|
143 |
$('#form_{{widget.name}}').val($(options[$(this).data('idx')]).attr('value'));
|
|
144 |
});
|
|
145 |
});
|
|
146 |
$select.trigger('wcs:options-change');
|
|
147 |
});
|
|
148 |
</script>
|
|
149 |
{% if request.quixote_request.is_in_backoffice %}
|
|
150 |
<style>
|
|
151 |
div.meetings_table {
|
|
152 |
margin-top: 1ex;
|
|
153 |
display: flex;
|
|
154 |
width: 100%;
|
|
155 |
}
|
|
156 |
|
|
157 |
div.meetings_table > div {
|
|
158 |
flex: 0 1 auto;
|
|
159 |
width: 20%;
|
|
160 |
text-align: center;
|
|
161 |
display: none;
|
|
162 |
}
|
|
163 |
|
|
164 |
div.meetings_table div span {
|
|
165 |
display: block;
|
|
166 |
cursor: pointer;
|
|
167 |
padding: 0.3ex 0;
|
|
168 |
}
|
|
169 |
|
|
170 |
div.meetings_table div span.disabled {
|
|
171 |
opacity: 0.3;
|
|
172 |
cursor: not-allowed;
|
|
173 |
}
|
|
174 |
|
|
175 |
div.meetings_table div span.selectable.on {
|
|
176 |
background: #215D9C;
|
|
177 |
color: white;
|
|
178 |
}
|
|
179 |
|
|
180 |
div.meetings_table div.head {
|
|
181 |
padding-bottom: 1ex;
|
|
182 |
}
|
|
183 |
</style>
|
|
184 |
{% endif %}
|
|
185 |
{% endblock %}
|
|
186 |
|
|
187 |
{% block widget-hint %}
|
|
188 |
{% if widget.hint %}<div class="hint">{{widget.hint}}</div>{% endif %}
|
|
189 |
{% endblock %}
|
0 |
|
-
|