@osimatic/helpers-js 1.1.77 → 1.1.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/form_date.js CHANGED
@@ -1,610 +1,610 @@
1
- // input period de type : Du <input type="date" name="start_date" /> au <input type="date" name="end_date" />
2
- class InputPeriod {
3
-
4
- static addLinks(form) {
5
- let divParent = form.find('input[type="date"][data-add_period_select_links]').parent();
6
- if (divParent.hasClass('input-group')) {
7
- divParent = divParent.parent();
8
- }
9
- divParent.append(''
10
- +'<div class="select_period_links">'
11
- +'<a href="#" class="period_select_yesterday">Hier</a> - '
12
- +'<a href="#" class="period_select_current_week">Cette semaine</a> - '
13
- +'<a href="#" class="period_select_last_week">La semaine dernière</a> - '
14
- +'<a href="#" class="period_select_current_month">Ce mois-ci</a> - '
15
- +'<a href="#" class="period_select_last_month">Le mois dernier</a> - '
16
- +'<a href="#" class="period_select_current_year">Cette année</a>'
17
- +'</div>'
18
- );
19
- this.init(form);
20
- }
21
-
22
- static init(form) {
23
- let link;
24
- //console.log(form.find('a.period_select_current_week'));
25
-
26
- if ((link = form.find('a.period_select_today')).length) {
27
- link.click(function() { InputPeriod.selectToday($(this)); return false; });
28
- }
29
- if ((link = form.find('a.period_select_yesterday')).length) {
30
- link.click(function() { InputPeriod.selectPreviousDay($(this), 1); return false; });
31
- }
32
- if ((link = form.find('a.period_select_tomorrow')).length) {
33
- link.click(function() { InputPeriod.selectFollowingDay($(this), 1); return false; });
34
- }
35
- if ((link = form.find('a.period_select_current_week')).length) {
36
- link.click(function() { InputPeriod.selectCurrentWeek($(this)); return false; });
37
- }
38
- if ((link = form.find('a.period_select_last_week')).length) {
39
- link.click(function() { InputPeriod.selectPreviousWeek($(this), 1); return false; });
40
- }
41
- if ((link = form.find('a.period_select_current_month')).length) {
42
- link.click(function() { InputPeriod.selectCurrentMonth($(this)); return false; });
43
- }
44
- if ((link = form.find('a.period_select_last_month')).length) {
45
- link.click(function() { InputPeriod.selectPreviousMonth($(this), 1); return false; });
46
- }
47
- if ((link = form.find('a.period_select_current_year')).length) {
48
- link.click(function() { InputPeriod.selectCurrentYear($(this)); return false; });
49
- }
50
- if ((link = form.find('a.period_select_last_year')).length) {
51
- link.click(function() { InputPeriod.selectPreviousYear($(this), 1); return false; });
52
- }
53
- }
54
-
55
-
56
- static selectToday(link) {
57
- let date = new Date();
58
- this.selectPeriod(link, date, date);
59
- }
60
-
61
- static selectPreviousDay(lien, nbDays) {
62
- this.selectFollowingDay(lien, -nbDays);
63
- }
64
- static selectFollowingDay(lien, nbDays) {
65
- let date = new Date();
66
- date.setUTCDate(date.getUTCDate() + nbDays);
67
- this.selectPeriod(lien, date, date);
68
- }
69
-
70
- static selectCurrentWeek(lien) {
71
- let date = new Date();
72
- this.selectPeriod(lien, DateTime.getFirstDayOfWeek(date), DateTime.getLastDayOfWeek(date));
73
- }
74
- static selectPreviousWeek(lien, nbWeeks) {
75
- this.selectFollowingWeek(lien, -nbWeeks);
76
- }
77
- static selectFollowingWeek(lien, nbWeeks) {
78
- let date = new Date();
79
- date.setUTCDate(date.getUTCDate() + (7*nbWeeks));
80
- this.selectPeriod(lien, DateTime.getFirstDayOfWeek(date), DateTime.getLastDayOfWeek(date));
81
- }
82
-
83
- static selectCurrentMonth(lien) {
84
- let date = new Date();
85
- this.selectPeriod(lien, DateTime.getFirstDayOfMonth(date), DateTime.getLastDayOfMonth(date));
86
- }
87
- static selectPreviousMonth(lien, nbMonths) {
88
- this.selectFollowingMonth(lien, -nbMonths);
89
- }
90
- static selectFollowingMonth(lien, nbMonths) {
91
- let date = new Date();
92
- date.setUTCDate(1);
93
- date.setUTCMonth(date.getUTCMonth() + nbMonths);
94
- this.selectPeriod(lien, DateTime.getFirstDayOfMonth(date), DateTime.getLastDayOfMonth(date));
95
- }
96
-
97
- static selectCurrentYear(lien) {
98
- this.selectFollowingYear(lien, 0);
99
- }
100
- static selectPreviousYear(lien, nbAnneesMoins) {
101
- this.selectFollowingYear(lien, -nbAnneesMoins);
102
- }
103
- static selectFollowingYear(lien, nbAnneesMoins) {
104
- let date = new Date();
105
- date.setUTCFullYear(date.getUTCFullYear() + nbAnneesMoins);
106
- this.selectPeriod(lien, DateTime.getFirstDayOfYear(date), DateTime.getLastDayOfYear(date));
107
- }
108
-
109
-
110
- static selectPeriod(link, startDate, endDate) {
111
- let inputPeriodStart = link.parent().parent().find('input[type="date"]').filter('[name="date_start"], [name="start_date"], [name="start_period"], [name="period_start_date"]');
112
- let inputPeriodEnd = link.parent().parent().find('input[type="date"]').filter('[name="date_end"], [name="end_date"], [name="end_period"], [name="period_end_date"]');
113
- if (inputPeriodStart.length === 0 || inputPeriodEnd.length === 0) {
114
- console.log('no period input found');
115
- return;
116
- }
117
-
118
- inputPeriodStart.val(DateTime.getDateForInputDate(startDate));
119
- inputPeriodEnd.val(DateTime.getDateForInputDate(endDate));
120
- }
121
-
122
- }
123
-
124
- // input period de type : <select class="period">Aujourd'hui / Ce mois-ci / etc. / Personnalisé</select>
125
- class FormDate {
126
-
127
- static fillYearSelect(select, nbYearsBefore=5, nbYearsAfter=0) {
128
- const currentDate = new Date();
129
- for (let year=currentDate.getUTCFullYear()-nbYearsBefore; year<=(currentDate.getUTCFullYear()+nbYearsAfter); year++) {
130
- select.append('<option value="'+year+'">'+year+'</option>');
131
- }
132
- }
133
-
134
- static fillMonthSelect(select, locale) {
135
- for (let month=1; month<=12; month++) {
136
- select.append('<option value="'+month+'">'+DateTime.getMonthNameByMonth(month, locale).capitalize()+'</option>');
137
- }
138
- }
139
-
140
- static fillDayOfWeekSelect(select, locale) {
141
- for (let dayOfWeek=1; dayOfWeek<=7; dayOfWeek++) {
142
- select.append('<option value="'+dayOfWeek+'">'+DateTime.getDayNameByDayOfWeek(dayOfWeek, locale).capitalize()+'</option>');
143
- }
144
- }
145
-
146
- static initForm(form) {
147
-
148
- function fillPeriodSelect(select) {
149
- Object.entries(FormDate.getPeriodList()).forEach(([idx, tabListPeriode]) => {
150
- let html = '<optgroup label="'+tabListPeriode['label']+'">';
151
- Object.entries(tabListPeriode['list']).forEach(([key, label]) => {
152
- html += '<option value="'+key+'">'+label+'</option>';
153
- });
154
- html += '</optgroup>';
155
- select.append(html);
156
- });
157
- if (select.data('default_value')) {
158
- select.val(select.data('default_value'));
159
- }
160
- }
161
-
162
- function updatePeriodSelect(select) {
163
- if (select.val() === 'perso') {
164
- select.closest('.form-group').next().removeClass('hide');
165
- }
166
- else {
167
- select.closest('.form-group').next().addClass('hide');
168
- }
169
- }
170
-
171
- function updateForm(form) {
172
- let periodSelect = form.find('select.periode');
173
- if (periodSelect.length === 0) {
174
- return;
175
- }
176
-
177
- updatePeriodSelect(periodSelect);
178
-
179
- let comparedPeriodSelect = form.find('select.periodeCompare');
180
- if (comparedPeriodSelect.length === 0) {
181
- return;
182
- }
183
-
184
- let listValues = [];
185
- let valueDefault = null;
186
-
187
- comparedPeriodSelect.find('option').attr('disabled', false);
188
-
189
- Object.entries(FormDate.getPeriodList()).forEach(([idx, tabListPeriode]) => {
190
- if (idx != 0) {
191
- let listKeyPeriode = Object.entries(tabListPeriode['list']).map(([key, value]) => key);
192
- if (listKeyPeriode.indexOf(periodSelect.val()) !== -1) {
193
- listValues = listKeyPeriode;
194
- valueDefault = listKeyPeriode[1];
195
- }
196
- else {
197
- comparedPeriodSelect.find('option[value="' + listKeyPeriode[0] + '"]').parent().children().attr('disabled', true);
198
- }
199
- }
200
- });
201
-
202
- if (periodSelect.val() === 'perso') {
203
- valueDefault = 'perso';
204
- }
205
- else if (comparedPeriodSelect.val() !== 'perso' && listValues.indexOf(comparedPeriodSelect.val()) !== -1) {
206
- valueDefault = comparedPeriodSelect.val();
207
- }
208
- comparedPeriodSelect.val(valueDefault);
209
-
210
- updatePeriodSelect(comparedPeriodSelect);
211
- }
212
-
213
- // ---------- Choix période (new) ----------
214
-
215
- if (form.find('select.periode').length > 0) {
216
- fillPeriodSelect(form.find('select.periode'));
217
- form.find('select.periode').change(function() {
218
- updateForm($(this).closest('form'));
219
- });
220
- }
221
-
222
- if (form.find('select.periodeCompare').length > 0) {
223
- fillPeriodSelect(form.find('select.periodeCompare'));
224
- form.find('select.periodeCompare').change(function() {
225
- updateForm($(this).closest('form'));
226
- });
227
- }
228
-
229
- updateForm(form);
230
-
231
- // ---------- Choix période (old) ----------
232
-
233
- if (form.find('select.day').length > 0 && form.find('select.month').length > 0 && form.find('select.year').length > 0) {
234
- form.find('select.year').after(
235
- '<br/>'+
236
- '<p class="select_date_fastly">'+
237
- ' <a href="#" class="lien_form_today">Auj.</a> - '+
238
- ' <a href="#" class="lien_form_yesterday">Hier</a> - '+
239
- ' <a href="#" class="lien_form_current_month">Ce mois-ci</a> - '+
240
- ' <a href="#" class="lien_form_last_month">Le mois dernier</a> - '+
241
- ' <a href="#" class="lien_form_current_year">Cette année</a>'+
242
- ' - '+
243
- ' <a href="#" class="lien_form_date_prev_day">Jour précédent</a>'+
244
- ' - '+
245
- ' <a href="#" class="lien_form_date_next_day">Jour suivant</a>'+
246
- '</p>'
247
- );
248
- }
249
- else if (form.find('select.month').length > 0 && form.find('select.year').length > 0) {
250
- form.find('select.year').after(
251
- '<br/>'+
252
- '<p class="select_date_fastly">'+
253
- ' <a href="#" class="lien_form_current_month">Ce mois-ci</a> - '+
254
- ' <a href="#" class="lien_form_last_month">Le mois dernier</a> - '+
255
- ' <a href="#" class="lien_form_current_year">Cette année</a>'+
256
- '</p>'
257
- );
258
- }
259
-
260
- if (form.find('select.dayCompare').length > 0 && form.find('select.monthCompare').length > 0 && form.find('select.yearCompare').length > 0) {
261
- form.find('select.yearCompare').after(
262
- '<br/>'+
263
- '<p class="select_date_fastly">'+
264
- ' <a href="#" class="lien_form_yesterday">Hier</a> - '+
265
- ' <a href="#" class="lien_form_day_moins_7">J-7</a> - '+
266
- ' <a href="#" class="lien_form_day_moins_8">J-8</a> - '+
267
- ' <a href="#" class="lien_form_last_month">Le mois dernier</a> - '+
268
- ' <a href="#" class="lien_form_month_moins_2">Mois M-2</a> - '+
269
- ' <a href="#" class="lien_form_last_year">L’année dernière</a>'+
270
- '</p>'
271
- );
272
- }
273
-
274
- // Lien de sélection de date
275
-
276
- if (form.find('a.lien_form_today').length > 0) {
277
- form.find('a.lien_form_today').click(function() {
278
- FormDate.setTodaySelected($(this).closest('.form-group'));
279
- return false;
280
- });
281
- }
282
- if (form.find('a.lien_form_yesterday').length > 0) {
283
- form.find('a.lien_form_yesterday').click(function() {
284
- FormDate.addNbDaysToToday($(this).closest('.form-group'), -1);
285
- return false;
286
- });
287
- }
288
- if (form.find('a.lien_form_day_moins_7').length > 0) {
289
- form.find('a.lien_form_day_moins_7').click(function() {
290
- FormDate.addNbDaysToToday($(this).closest('.form-group'), -7);
291
- return false;
292
- });
293
- }
294
- if (form.find('a.lien_form_day_moins_8').length > 0) {
295
- form.find('a.lien_form_day_moins_8').click(function() {
296
- FormDate.addNbDaysToToday($(this).closest('.form-group'), -8);
297
- return false;
298
- });
299
- }
300
-
301
- if (form.find('a.lien_form_current_month').length > 0) {
302
- form.find('a.lien_form_current_month').click(function() {
303
- FormDate.setCurrentMonthSelected($(this).closest('.form-group'));
304
- return false;
305
- });
306
- }
307
- if (form.find('a.lien_form_last_month').length > 0) {
308
- form.find('a.lien_form_last_month').click(function() {
309
- FormDate.addNbMonthsToToday($(this).closest('.form-group'), -1);
310
- return false;
311
- });
312
- }
313
- if (form.find('a.lien_form_month_moins_2').length > 0) {
314
- form.find('a.lien_form_month_moins_2').click(function() {
315
- FormDate.addNbMonthsToToday($(this).closest('.form-group'), -2);
316
- return false;
317
- });
318
- }
319
-
320
- if (form.find('a.lien_form_current_year').length > 0) {
321
- form.find('a.lien_form_current_year').click(function() {
322
- FormDate.setCurrentYearSelected($(this).closest('.form-group'));
323
- return false;
324
- });
325
- }
326
- if (form.find('a.lien_form_last_year').length > 0) {
327
- form.find('a.lien_form_last_year').click(function() {
328
- FormDate.addNbYearsToToday($(this).closest('.form-group'), -1);
329
- return false;
330
- });
331
- }
332
-
333
- if (form.find('a.lien_form_date_prev_day').length > 0) {
334
- form.find('a.lien_form_date_prev_day').click(function() {
335
- FormDate.addNbDaysToSelectedDate($(this).closest('.form-group'), -1);
336
- return false;
337
- });
338
- }
339
- if (form.find('a.lien_form_date_next_day').length > 0) {
340
- form.find('a.lien_form_date_next_day').click(function() {
341
- FormDate.addNbDaysToSelectedDate($(this).closest('.form-group'), 1);
342
- return false;
343
- });
344
- }
345
-
346
- //if ($('form select[name=select_date_fastly]').length > 0) {
347
- // $('form select[name=select_date_fastly]').change(function() {
348
- // valueOptionSelected = $('form select[name=select_date_fastly] option:selected').attr('value');
349
- // if (valueOptionSelected == 'today') {
350
- // selectFormDateToday();
351
- // }
352
- // else if (valueOptionSelected == 'current_month') {
353
- // selectFormDateCurrentMonth();
354
- // }
355
- // });
356
- //}
357
- }
358
-
359
- static getPeriodList() {
360
- return {
361
- other: {label: 'Autre', list: {
362
- perso: 'Personnalisé',
363
- }},
364
- '1d': {label: 'Un jour', list: { // 1 jour
365
- 'ajd': 'Aujourd’hui',
366
- 'hier': 'Hier',
367
- 'jourMoins2': 'Avant-hier',
368
- 'jourMoins3': 'J-3',
369
- 'jourMoins7': 'J-7',
370
- 'jourMoins8': 'J-8',
371
- 'same_day_last_year': 'Même jour l’année dernière',
372
- 'same_day_least_2_years': 'Même jour l’année A-2',
373
- 'same_day_as_yesterday_last_year': 'Même jour qu’hier l’année dernière',
374
- 'same_day_as_yesterday_least_2_years': 'Même jour qu’hier l’année A-2'
375
- }},
376
- '1w': {label: 'Une semaine', list: { // 1 semaine
377
- 'curr_week': 'Cette semaine',
378
- 'last_week': 'La semaine dernière',
379
- 'weekMoins2': 'Semaine S-2',
380
- 'weekMoins3': 'Semaine S-3',
381
- 'weekMoins4': 'Semaine S-4',
382
- }},
383
- '7d': {label: '7 jours', list: { // 7 jours
384
- 'last_7_days': 'Les 7 derniers jours',
385
- 'last_7_days_before': 'Les 7 jours avant',
386
- 'last_7_days_least_1_year': 'Les mêmes 7 jours l’année dernière',
387
- 'last_7_days_least_2_years': 'Les mêmes 7 jours l’année A-2',
388
- }},
389
- '14d': {label: '14 jours', list: { // 14 jours
390
- 'last_14_days': 'Les 14 derniers jours',
391
- 'last_14_days_before': 'Les 14 jours avant',
392
- 'last_14_days_least_1_year': 'Les mêmes 14 jours l’année dernière',
393
- 'last_14_days_least_2_years': 'Les mêmes 14 jours l’année A-2',
394
- }},
395
- // 30 jours
396
- '30d': {label: '30 jours', list: {
397
- 'last_30_days': 'Les 30 derniers jours',
398
- 'last_30_days_before': 'Les 30 jours avant',
399
- 'last_30_days_least_1_year': 'Les mêmes 30 jours l’année dernière',
400
- 'last_30_days_least_2_years': 'Les mêmes 30 jours l’année A-2',
401
- }},
402
- '60d': {label: '60 jours', list: { // 60 jours
403
- 'last_60_days': 'Les 60 derniers jours',
404
- 'last_60_days_before': 'Les 60 jours avant',
405
- 'last_60_days_least_1_year': 'Les mêmes 60 jours l’année dernière',
406
- 'last_60_days_least_2_years': 'Les mêmes 60 jours l’année A-2',
407
- }},
408
- '1m': {label: 'Un mois', list: { // 1 mois
409
- 'curr_month': 'Ce mois-ci',
410
- 'last_month': 'Le mois dernier',
411
- 'monthMoins2': 'Mois M-2',
412
- 'monthMoins3': 'Mois M-3',
413
- 'monthMoins4': 'Mois M-4',
414
- 'monthMoins5': 'Mois M-5',
415
- 'monthMoins6': 'Mois M-6',
416
- 'same_month_last_year': 'Même mois l’année dernière',
417
- 'same_month_least_2_years': 'Même mois l’année A-2',
418
- }},
419
- '3m': {label: '3 mois', list: { // 3 mois
420
- 'last_3_month': 'Les 3 derniers mois',
421
- 'last_3_month_before': 'Les 3 mois avant',
422
- 'last_3_month_least_1_year': 'Les mêmes 3 mois l’année dernière',
423
- 'last_3_month_least_2_years': 'Les mêmes 3 mois l’année A-2',
424
- }},
425
- '6m': {label: '6 mois', list: { // 6 mois
426
- 'last_6_month': 'Les 6 derniers mois',
427
- 'last_6_month_before': 'Les 6 mois avant',
428
- 'last_6_month_least_1_year': 'Les mêmes 6 mois l’année dernière',
429
- 'last_6_month_least_2_years': 'Les mêmes 6 mois l’année A-2',
430
- }},
431
- '12m': {label: '12 mois', list: { // 12 mois
432
- 'last_12_month': 'Les 12 derniers mois',
433
- 'last_12_month_before': 'Les 12 mois avant',
434
- 'last_12_month_least_1_year': 'Les mêmes 12 mois l’année dernière',
435
- 'last_12_month_least_2_years': 'Les mêmes 12 mois l’année A-2',
436
- }},
437
- '24m': {label: '24 mois', list: { // 24 mois
438
- 'last_24_month': 'Les 24 derniers mois',
439
- 'last_24_month_before': 'Les 24 mois avant',
440
- 'last_24_month_least_1_year': 'Les mêmes 24 mois l’année dernière',
441
- 'last_24_month_least_2_years': 'Les mêmes 24 mois l’année A-2',
442
- }},
443
- '1y': {label: 'Une année', list: { // 1 année
444
- 'curr_year': 'Cette année',
445
- 'last_year': 'L’année dernière',
446
- 'yearMoins2': 'Année A-2',
447
- 'yearMoins3': 'Année A-3',
448
- 'yearMoins4': 'Année A-4',
449
- }},
450
- };
451
- }
452
- static setTodaySelected(periodFormGroup) {
453
- let date = new Date();
454
- FormDate.setSelectedDate(periodFormGroup, date.getDate(), (date.getMonth() + 1), date.getFullYear());
455
- }
456
- static setCurrentMonthSelected(periodFormGroup) {
457
- let date = new Date();
458
- FormDate.setSelectedDate(periodFormGroup, -1, (date.getMonth() + 1), date.getFullYear());
459
- }
460
- static setCurrentYearSelected(periodFormGroup) {
461
- let today = new Date();
462
- FormDate.setSelectedDate(periodFormGroup, -1, -1, today.getFullYear());
463
- }
464
-
465
- static addNbDaysToToday(periodFormGroup, nbDays) {
466
- FormDate.addNbDaysToSelectedDate(periodFormGroup, nbDays, false);
467
- }
468
- static addNbMonthsToToday(periodFormGroup, nbMonths) {
469
- FormDate.addNbMonthsToSelectedDate(periodFormGroup, nbMonths, false);
470
- }
471
- static addNbYearsToToday(periodFormGroup, nbYears) {
472
- FormDate.addNbYearsToSelectedDate(periodFormGroup, nbYears, false);
473
- }
474
-
475
- static addNbDaysToSelectedDate(periodFormGroup, nbDays, fromSelectedDate) {
476
- let date = typeof fromSelectedDate == 'undefined' || fromSelectedDate ? FormDate.getSelectedDate(periodFormGroup) : new Date();
477
- date.setDate(date.getDate() + nbDays);
478
- FormDate.setSelectedDate(periodFormGroup, date.getDate(), (date.getMonth() + 1), date.getFullYear());
479
- }
480
- static addNbMonthsToSelectedDate(periodFormGroup, nbMonths, fromSelectedDate) {
481
- let date = typeof fromSelectedDate == 'undefined' || fromSelectedDate ? FormDate.getSelectedDate(periodFormGroup) : new Date();
482
- date.setDate(1);
483
- date.setMonth(date.getMonth() - nbMonths);
484
- FormDate.setSelectedDate(periodFormGroup, -1, (date.getMonth() + 1), date.getFullYear());
485
- }
486
- static addNbYearsToSelectedDate(periodFormGroup, nbYears, fromSelectedDate) {
487
- let date = typeof fromSelectedDate == 'undefined' || fromSelectedDate ? FormDate.getSelectedDate(periodFormGroup) : new Date();
488
- FormDate.setSelectedDate(periodFormGroup, -1, -1, date.getFullYear() - nbYears);
489
- }
490
-
491
- static getSelectedDate(periodFormGroup) {
492
- let day = periodFormGroup.find('select.day').val();
493
- let month = periodFormGroup.find('select.month').val();
494
- let year = periodFormGroup.find('select.year').val();
495
- if (null != day && null != month && null != year) {
496
- return new Date(year, month - 1, day);
497
- }
498
- return new Date();
499
- }
500
-
501
- static setSelectedDate(periodFormGroup, day, month, year) {
502
- periodFormGroup.find('select.day').val(day);
503
- periodFormGroup.find('select.month').val(month);
504
- periodFormGroup.find('select.year').val(year);
505
- }
506
-
507
-
508
-
509
- /*
510
- // deprecated
511
-
512
- static majSelectPeriode(select) {
513
- if (select.find(':selected').attr('value') === 'perso') {
514
- select.parent().parent().next().removeClass('hide');
515
- }
516
- else {
517
- select.parent().parent().next().addClass('hide');
518
- }
519
- }
520
-
521
- static majSelectCompare() {
522
- if ($('form select#periodeCompare').length === 0) {
523
- return;
524
- }
525
-
526
- let listValues = [];
527
- let periodeSelected = $('form select.periode :selected').attr('value');
528
- let selectCompare = $('form select#periodeCompare');
529
- let periodeCompareSelected = selectCompare.find(':selected').attr('value');
530
- let valueDefault = null;
531
-
532
- selectCompare.find('option').removeAttr('disabled');
533
-
534
- $.each(listePeriodeCompare, function (idx, tabListPeriode) {
535
- if (idx != 0) {
536
- let listKeyPeriode = array_keys(tabListPeriode.list);
537
- if (in_array(periodeSelected, listKeyPeriode)) {
538
- listValues = listKeyPeriode;
539
- valueDefault = listKeyPeriode[1];
540
- }
541
- else {
542
- selectCompare.find('option[value="' + listKeyPeriode[0] + '"]').parent().children().attr('disabled', 'disabled');
543
- }
544
- }
545
- });
546
-
547
- if (periodeSelected === 'perso') {
548
- valueDefault = 'perso';
549
- }
550
- else if (periodeCompareSelected !== 'perso' && in_array(periodeCompareSelected, listValues)) {
551
- valueDefault = periodeCompareSelected;
552
- }
553
- selectCompare.find('option[value="' + valueDefault + '"]').attr('selected', 'selected');
554
-
555
- FormDate.majSelectPeriode(selectCompare);
556
- }
557
-
558
- static selectFormDateToday(lien) {
559
- let date = new Date();
560
- FormDate.selectFormDate(lien, date.getDate(), (date.getMonth() + 1), date.getFullYear());
561
- }
562
- static selectFormDateDayMoinsNb(lien, nbJoursMoins) {
563
- let date = new Date();
564
- date.setDate(date.getDate() - nbJoursMoins);
565
- FormDate.selectFormDate(lien, date.getDate(), (date.getMonth() + 1), date.getFullYear());
566
- }
567
- static selectFormDateCurrentMonth(lien) {
568
- let date = new Date();
569
- FormDate.selectFormDate(lien, -1, (date.getMonth() + 1), date.getFullYear());
570
- }
571
- static selectFormDateMonthMoinsNb(lien, nbMoisMoins) {
572
- let date = new Date();
573
- date.setDate(1);
574
- date.setMonth(date.getMonth() - nbMoisMoins);
575
- FormDate.selectFormDate(lien, -1, (date.getMonth() + 1), date.getFullYear());
576
- }
577
- static selectFormDateCurrentYear(lien) {
578
- let today = new Date();
579
- FormDate.selectFormDate(lien, -1, -1, today.getFullYear());
580
- }
581
- static selectFormDateYearMoinsNb(lien, nbAnneesMoins) {
582
- let today = new Date();
583
- FormDate.selectFormDate(lien, -1, -1, today.getFullYear() - nbAnneesMoins);
584
- }
585
- static selectFormDateAddDayFromSelectedDay(lien, nbDaysAdded) {
586
- let date = FormDate.getDateObjectSelected(lien);
587
- date.setDate(date.getDate() + nbDaysAdded);
588
- FormDate.selectFormDate(lien, date.getDate(), (date.getMonth() + 1), date.getFullYear());
589
- }
590
- static getDateObjectSelected(lien) {
591
- let selectorDay = '#' + (lien.parent().prev().prev().prev().prev().attr('id')) + ' option:selected';
592
- let selectorMonth = '#' + (lien.parent().prev().prev().prev().attr('id')) + ' option:selected';
593
- let selectorYear = '#' + (lien.parent().prev().prev().attr('id')) + ' option:selected';
594
- if ($(selectorDay).length > 0 && $(selectorMonth).length > 0 && $(selectorYear).length > 0) {
595
- return new Date($(selectorYear).attr('value'), $(selectorMonth).attr('value') - 1, $(selectorDay).attr('value'));
596
- }
597
- return new Date();
598
- }
599
- static selectFormDate(lien, day, month, year) {
600
- let selectorDay = '#' + (lien.parent().prev().prev().prev().prev().attr('id')) + ' option[value=' + day + ']';
601
- let selectorMonth = '#' + (lien.parent().prev().prev().prev().attr('id')) + ' option[value=' + month + ']';
602
- let selectorYear = '#' + (lien.parent().prev().prev().attr('id')) + ' option[value=' + year + ']';
603
- if ($(selectorDay).length > 0) $(selectorDay).prop('selected', 'selected');
604
- if ($(selectorMonth).length > 0) $(selectorMonth).prop('selected', 'selected');
605
- if ($(selectorYear).length > 0) $(selectorYear).prop('selected', 'selected');
606
- }
607
- */
608
- }
609
-
610
- module.exports = { FormDate, InputPeriod };
1
+ // input period de type : Du <input type="date" name="start_date" /> au <input type="date" name="end_date" />
2
+ class InputPeriod {
3
+
4
+ static addLinks(form) {
5
+ let divParent = form.find('input[type="date"][data-add_period_select_links]').parent();
6
+ if (divParent.hasClass('input-group')) {
7
+ divParent = divParent.parent();
8
+ }
9
+ divParent.append(''
10
+ +'<div class="select_period_links">'
11
+ +'<a href="#" class="period_select_yesterday">Hier</a> - '
12
+ +'<a href="#" class="period_select_current_week">Cette semaine</a> - '
13
+ +'<a href="#" class="period_select_last_week">La semaine dernière</a> - '
14
+ +'<a href="#" class="period_select_current_month">Ce mois-ci</a> - '
15
+ +'<a href="#" class="period_select_last_month">Le mois dernier</a> - '
16
+ +'<a href="#" class="period_select_current_year">Cette année</a>'
17
+ +'</div>'
18
+ );
19
+ this.init(form);
20
+ }
21
+
22
+ static init(form) {
23
+ let link;
24
+ //console.log(form.find('a.period_select_current_week'));
25
+
26
+ if ((link = form.find('a.period_select_today')).length) {
27
+ link.click(function() { InputPeriod.selectToday($(this)); return false; });
28
+ }
29
+ if ((link = form.find('a.period_select_yesterday')).length) {
30
+ link.click(function() { InputPeriod.selectPreviousDay($(this), 1); return false; });
31
+ }
32
+ if ((link = form.find('a.period_select_tomorrow')).length) {
33
+ link.click(function() { InputPeriod.selectFollowingDay($(this), 1); return false; });
34
+ }
35
+ if ((link = form.find('a.period_select_current_week')).length) {
36
+ link.click(function() { InputPeriod.selectCurrentWeek($(this)); return false; });
37
+ }
38
+ if ((link = form.find('a.period_select_last_week')).length) {
39
+ link.click(function() { InputPeriod.selectPreviousWeek($(this), 1); return false; });
40
+ }
41
+ if ((link = form.find('a.period_select_current_month')).length) {
42
+ link.click(function() { InputPeriod.selectCurrentMonth($(this)); return false; });
43
+ }
44
+ if ((link = form.find('a.period_select_last_month')).length) {
45
+ link.click(function() { InputPeriod.selectPreviousMonth($(this), 1); return false; });
46
+ }
47
+ if ((link = form.find('a.period_select_current_year')).length) {
48
+ link.click(function() { InputPeriod.selectCurrentYear($(this)); return false; });
49
+ }
50
+ if ((link = form.find('a.period_select_last_year')).length) {
51
+ link.click(function() { InputPeriod.selectPreviousYear($(this), 1); return false; });
52
+ }
53
+ }
54
+
55
+
56
+ static selectToday(link) {
57
+ let date = new Date();
58
+ this.selectPeriod(link, date, date);
59
+ }
60
+
61
+ static selectPreviousDay(lien, nbDays) {
62
+ this.selectFollowingDay(lien, -nbDays);
63
+ }
64
+ static selectFollowingDay(lien, nbDays) {
65
+ let date = new Date();
66
+ date.setUTCDate(date.getUTCDate() + nbDays);
67
+ this.selectPeriod(lien, date, date);
68
+ }
69
+
70
+ static selectCurrentWeek(lien) {
71
+ let date = new Date();
72
+ this.selectPeriod(lien, DateTime.getFirstDayOfWeek(date), DateTime.getLastDayOfWeek(date));
73
+ }
74
+ static selectPreviousWeek(lien, nbWeeks) {
75
+ this.selectFollowingWeek(lien, -nbWeeks);
76
+ }
77
+ static selectFollowingWeek(lien, nbWeeks) {
78
+ let date = new Date();
79
+ date.setUTCDate(date.getUTCDate() + (7*nbWeeks));
80
+ this.selectPeriod(lien, DateTime.getFirstDayOfWeek(date), DateTime.getLastDayOfWeek(date));
81
+ }
82
+
83
+ static selectCurrentMonth(lien) {
84
+ let date = new Date();
85
+ this.selectPeriod(lien, DateTime.getFirstDayOfMonth(date), DateTime.getLastDayOfMonth(date));
86
+ }
87
+ static selectPreviousMonth(lien, nbMonths) {
88
+ this.selectFollowingMonth(lien, -nbMonths);
89
+ }
90
+ static selectFollowingMonth(lien, nbMonths) {
91
+ let date = new Date();
92
+ date.setUTCDate(1);
93
+ date.setUTCMonth(date.getUTCMonth() + nbMonths);
94
+ this.selectPeriod(lien, DateTime.getFirstDayOfMonth(date), DateTime.getLastDayOfMonth(date));
95
+ }
96
+
97
+ static selectCurrentYear(lien) {
98
+ this.selectFollowingYear(lien, 0);
99
+ }
100
+ static selectPreviousYear(lien, nbAnneesMoins) {
101
+ this.selectFollowingYear(lien, -nbAnneesMoins);
102
+ }
103
+ static selectFollowingYear(lien, nbAnneesMoins) {
104
+ let date = new Date();
105
+ date.setUTCFullYear(date.getUTCFullYear() + nbAnneesMoins);
106
+ this.selectPeriod(lien, DateTime.getFirstDayOfYear(date), DateTime.getLastDayOfYear(date));
107
+ }
108
+
109
+
110
+ static selectPeriod(link, startDate, endDate) {
111
+ let inputPeriodStart = link.parent().parent().find('input[type="date"]').filter('[name="date_start"], [name="start_date"], [name="start_period"], [name="period_start_date"]');
112
+ let inputPeriodEnd = link.parent().parent().find('input[type="date"]').filter('[name="date_end"], [name="end_date"], [name="end_period"], [name="period_end_date"]');
113
+ if (inputPeriodStart.length === 0 || inputPeriodEnd.length === 0) {
114
+ console.log('no period input found');
115
+ return;
116
+ }
117
+
118
+ inputPeriodStart.val(DateTime.getDateForInputDate(startDate));
119
+ inputPeriodEnd.val(DateTime.getDateForInputDate(endDate));
120
+ }
121
+
122
+ }
123
+
124
+ // input period de type : <select class="period">Aujourd'hui / Ce mois-ci / etc. / Personnalisé</select>
125
+ class FormDate {
126
+
127
+ static fillYearSelect(select, nbYearsBefore=5, nbYearsAfter=0) {
128
+ const currentDate = new Date();
129
+ for (let year=currentDate.getUTCFullYear()-nbYearsBefore; year<=(currentDate.getUTCFullYear()+nbYearsAfter); year++) {
130
+ select.append('<option value="'+year+'">'+year+'</option>');
131
+ }
132
+ }
133
+
134
+ static fillMonthSelect(select, locale) {
135
+ for (let month=1; month<=12; month++) {
136
+ select.append('<option value="'+month+'">'+DateTime.getMonthNameByMonth(month, locale).capitalize()+'</option>');
137
+ }
138
+ }
139
+
140
+ static fillDayOfWeekSelect(select, locale) {
141
+ for (let dayOfWeek=1; dayOfWeek<=7; dayOfWeek++) {
142
+ select.append('<option value="'+dayOfWeek+'">'+DateTime.getDayNameByDayOfWeek(dayOfWeek, locale).capitalize()+'</option>');
143
+ }
144
+ }
145
+
146
+ static initForm(form) {
147
+
148
+ function fillPeriodSelect(select) {
149
+ Object.entries(FormDate.getPeriodList()).forEach(([idx, tabListPeriode]) => {
150
+ let html = '<optgroup label="'+tabListPeriode['label']+'">';
151
+ Object.entries(tabListPeriode['list']).forEach(([key, label]) => {
152
+ html += '<option value="'+key+'">'+label+'</option>';
153
+ });
154
+ html += '</optgroup>';
155
+ select.append(html);
156
+ });
157
+ if (select.data('default_value')) {
158
+ select.val(select.data('default_value'));
159
+ }
160
+ }
161
+
162
+ function updatePeriodSelect(select) {
163
+ if (select.val() === 'perso') {
164
+ select.closest('.form-group').next().removeClass('hide');
165
+ }
166
+ else {
167
+ select.closest('.form-group').next().addClass('hide');
168
+ }
169
+ }
170
+
171
+ function updateForm(form) {
172
+ let periodSelect = form.find('select.periode');
173
+ if (periodSelect.length === 0) {
174
+ return;
175
+ }
176
+
177
+ updatePeriodSelect(periodSelect);
178
+
179
+ let comparedPeriodSelect = form.find('select.periodeCompare');
180
+ if (comparedPeriodSelect.length === 0) {
181
+ return;
182
+ }
183
+
184
+ let listValues = [];
185
+ let valueDefault = null;
186
+
187
+ comparedPeriodSelect.find('option').attr('disabled', false);
188
+
189
+ Object.entries(FormDate.getPeriodList()).forEach(([idx, tabListPeriode]) => {
190
+ if (idx != 0) {
191
+ let listKeyPeriode = Object.entries(tabListPeriode['list']).map(([key, value]) => key);
192
+ if (listKeyPeriode.indexOf(periodSelect.val()) !== -1) {
193
+ listValues = listKeyPeriode;
194
+ valueDefault = listKeyPeriode[1];
195
+ }
196
+ else {
197
+ comparedPeriodSelect.find('option[value="' + listKeyPeriode[0] + '"]').parent().children().attr('disabled', true);
198
+ }
199
+ }
200
+ });
201
+
202
+ if (periodSelect.val() === 'perso') {
203
+ valueDefault = 'perso';
204
+ }
205
+ else if (comparedPeriodSelect.val() !== 'perso' && listValues.indexOf(comparedPeriodSelect.val()) !== -1) {
206
+ valueDefault = comparedPeriodSelect.val();
207
+ }
208
+ comparedPeriodSelect.val(valueDefault);
209
+
210
+ updatePeriodSelect(comparedPeriodSelect);
211
+ }
212
+
213
+ // ---------- Choix période (new) ----------
214
+
215
+ if (form.find('select.periode').length > 0) {
216
+ fillPeriodSelect(form.find('select.periode'));
217
+ form.find('select.periode').change(function() {
218
+ updateForm($(this).closest('form'));
219
+ });
220
+ }
221
+
222
+ if (form.find('select.periodeCompare').length > 0) {
223
+ fillPeriodSelect(form.find('select.periodeCompare'));
224
+ form.find('select.periodeCompare').change(function() {
225
+ updateForm($(this).closest('form'));
226
+ });
227
+ }
228
+
229
+ updateForm(form);
230
+
231
+ // ---------- Choix période (old) ----------
232
+
233
+ if (form.find('select.day').length > 0 && form.find('select.month').length > 0 && form.find('select.year').length > 0) {
234
+ form.find('select.year').after(
235
+ '<br/>'+
236
+ '<p class="select_date_fastly">'+
237
+ ' <a href="#" class="lien_form_today">Auj.</a> - '+
238
+ ' <a href="#" class="lien_form_yesterday">Hier</a> - '+
239
+ ' <a href="#" class="lien_form_current_month">Ce mois-ci</a> - '+
240
+ ' <a href="#" class="lien_form_last_month">Le mois dernier</a> - '+
241
+ ' <a href="#" class="lien_form_current_year">Cette année</a>'+
242
+ ' - '+
243
+ ' <a href="#" class="lien_form_date_prev_day">Jour précédent</a>'+
244
+ ' - '+
245
+ ' <a href="#" class="lien_form_date_next_day">Jour suivant</a>'+
246
+ '</p>'
247
+ );
248
+ }
249
+ else if (form.find('select.month').length > 0 && form.find('select.year').length > 0) {
250
+ form.find('select.year').after(
251
+ '<br/>'+
252
+ '<p class="select_date_fastly">'+
253
+ ' <a href="#" class="lien_form_current_month">Ce mois-ci</a> - '+
254
+ ' <a href="#" class="lien_form_last_month">Le mois dernier</a> - '+
255
+ ' <a href="#" class="lien_form_current_year">Cette année</a>'+
256
+ '</p>'
257
+ );
258
+ }
259
+
260
+ if (form.find('select.dayCompare').length > 0 && form.find('select.monthCompare').length > 0 && form.find('select.yearCompare').length > 0) {
261
+ form.find('select.yearCompare').after(
262
+ '<br/>'+
263
+ '<p class="select_date_fastly">'+
264
+ ' <a href="#" class="lien_form_yesterday">Hier</a> - '+
265
+ ' <a href="#" class="lien_form_day_moins_7">J-7</a> - '+
266
+ ' <a href="#" class="lien_form_day_moins_8">J-8</a> - '+
267
+ ' <a href="#" class="lien_form_last_month">Le mois dernier</a> - '+
268
+ ' <a href="#" class="lien_form_month_moins_2">Mois M-2</a> - '+
269
+ ' <a href="#" class="lien_form_last_year">L’année dernière</a>'+
270
+ '</p>'
271
+ );
272
+ }
273
+
274
+ // Lien de sélection de date
275
+
276
+ if (form.find('a.lien_form_today').length > 0) {
277
+ form.find('a.lien_form_today').click(function() {
278
+ FormDate.setTodaySelected($(this).closest('.form-group'));
279
+ return false;
280
+ });
281
+ }
282
+ if (form.find('a.lien_form_yesterday').length > 0) {
283
+ form.find('a.lien_form_yesterday').click(function() {
284
+ FormDate.addNbDaysToToday($(this).closest('.form-group'), -1);
285
+ return false;
286
+ });
287
+ }
288
+ if (form.find('a.lien_form_day_moins_7').length > 0) {
289
+ form.find('a.lien_form_day_moins_7').click(function() {
290
+ FormDate.addNbDaysToToday($(this).closest('.form-group'), -7);
291
+ return false;
292
+ });
293
+ }
294
+ if (form.find('a.lien_form_day_moins_8').length > 0) {
295
+ form.find('a.lien_form_day_moins_8').click(function() {
296
+ FormDate.addNbDaysToToday($(this).closest('.form-group'), -8);
297
+ return false;
298
+ });
299
+ }
300
+
301
+ if (form.find('a.lien_form_current_month').length > 0) {
302
+ form.find('a.lien_form_current_month').click(function() {
303
+ FormDate.setCurrentMonthSelected($(this).closest('.form-group'));
304
+ return false;
305
+ });
306
+ }
307
+ if (form.find('a.lien_form_last_month').length > 0) {
308
+ form.find('a.lien_form_last_month').click(function() {
309
+ FormDate.addNbMonthsToToday($(this).closest('.form-group'), -1);
310
+ return false;
311
+ });
312
+ }
313
+ if (form.find('a.lien_form_month_moins_2').length > 0) {
314
+ form.find('a.lien_form_month_moins_2').click(function() {
315
+ FormDate.addNbMonthsToToday($(this).closest('.form-group'), -2);
316
+ return false;
317
+ });
318
+ }
319
+
320
+ if (form.find('a.lien_form_current_year').length > 0) {
321
+ form.find('a.lien_form_current_year').click(function() {
322
+ FormDate.setCurrentYearSelected($(this).closest('.form-group'));
323
+ return false;
324
+ });
325
+ }
326
+ if (form.find('a.lien_form_last_year').length > 0) {
327
+ form.find('a.lien_form_last_year').click(function() {
328
+ FormDate.addNbYearsToToday($(this).closest('.form-group'), -1);
329
+ return false;
330
+ });
331
+ }
332
+
333
+ if (form.find('a.lien_form_date_prev_day').length > 0) {
334
+ form.find('a.lien_form_date_prev_day').click(function() {
335
+ FormDate.addNbDaysToSelectedDate($(this).closest('.form-group'), -1);
336
+ return false;
337
+ });
338
+ }
339
+ if (form.find('a.lien_form_date_next_day').length > 0) {
340
+ form.find('a.lien_form_date_next_day').click(function() {
341
+ FormDate.addNbDaysToSelectedDate($(this).closest('.form-group'), 1);
342
+ return false;
343
+ });
344
+ }
345
+
346
+ //if ($('form select[name=select_date_fastly]').length > 0) {
347
+ // $('form select[name=select_date_fastly]').change(function() {
348
+ // valueOptionSelected = $('form select[name=select_date_fastly] option:selected').attr('value');
349
+ // if (valueOptionSelected == 'today') {
350
+ // selectFormDateToday();
351
+ // }
352
+ // else if (valueOptionSelected == 'current_month') {
353
+ // selectFormDateCurrentMonth();
354
+ // }
355
+ // });
356
+ //}
357
+ }
358
+
359
+ static getPeriodList() {
360
+ return {
361
+ other: {label: 'Autre', list: {
362
+ perso: 'Personnalisé',
363
+ }},
364
+ '1d': {label: 'Un jour', list: { // 1 jour
365
+ 'ajd': 'Aujourd’hui',
366
+ 'hier': 'Hier',
367
+ 'jourMoins2': 'Avant-hier',
368
+ 'jourMoins3': 'J-3',
369
+ 'jourMoins7': 'J-7',
370
+ 'jourMoins8': 'J-8',
371
+ 'same_day_last_year': 'Même jour l’année dernière',
372
+ 'same_day_least_2_years': 'Même jour l’année A-2',
373
+ 'same_day_as_yesterday_last_year': 'Même jour qu’hier l’année dernière',
374
+ 'same_day_as_yesterday_least_2_years': 'Même jour qu’hier l’année A-2'
375
+ }},
376
+ '1w': {label: 'Une semaine', list: { // 1 semaine
377
+ 'curr_week': 'Cette semaine',
378
+ 'last_week': 'La semaine dernière',
379
+ 'weekMoins2': 'Semaine S-2',
380
+ 'weekMoins3': 'Semaine S-3',
381
+ 'weekMoins4': 'Semaine S-4',
382
+ }},
383
+ '7d': {label: '7 jours', list: { // 7 jours
384
+ 'last_7_days': 'Les 7 derniers jours',
385
+ 'last_7_days_before': 'Les 7 jours avant',
386
+ 'last_7_days_least_1_year': 'Les mêmes 7 jours l’année dernière',
387
+ 'last_7_days_least_2_years': 'Les mêmes 7 jours l’année A-2',
388
+ }},
389
+ '14d': {label: '14 jours', list: { // 14 jours
390
+ 'last_14_days': 'Les 14 derniers jours',
391
+ 'last_14_days_before': 'Les 14 jours avant',
392
+ 'last_14_days_least_1_year': 'Les mêmes 14 jours l’année dernière',
393
+ 'last_14_days_least_2_years': 'Les mêmes 14 jours l’année A-2',
394
+ }},
395
+ // 30 jours
396
+ '30d': {label: '30 jours', list: {
397
+ 'last_30_days': 'Les 30 derniers jours',
398
+ 'last_30_days_before': 'Les 30 jours avant',
399
+ 'last_30_days_least_1_year': 'Les mêmes 30 jours l’année dernière',
400
+ 'last_30_days_least_2_years': 'Les mêmes 30 jours l’année A-2',
401
+ }},
402
+ '60d': {label: '60 jours', list: { // 60 jours
403
+ 'last_60_days': 'Les 60 derniers jours',
404
+ 'last_60_days_before': 'Les 60 jours avant',
405
+ 'last_60_days_least_1_year': 'Les mêmes 60 jours l’année dernière',
406
+ 'last_60_days_least_2_years': 'Les mêmes 60 jours l’année A-2',
407
+ }},
408
+ '1m': {label: 'Un mois', list: { // 1 mois
409
+ 'curr_month': 'Ce mois-ci',
410
+ 'last_month': 'Le mois dernier',
411
+ 'monthMoins2': 'Mois M-2',
412
+ 'monthMoins3': 'Mois M-3',
413
+ 'monthMoins4': 'Mois M-4',
414
+ 'monthMoins5': 'Mois M-5',
415
+ 'monthMoins6': 'Mois M-6',
416
+ 'same_month_last_year': 'Même mois l’année dernière',
417
+ 'same_month_least_2_years': 'Même mois l’année A-2',
418
+ }},
419
+ '3m': {label: '3 mois', list: { // 3 mois
420
+ 'last_3_month': 'Les 3 derniers mois',
421
+ 'last_3_month_before': 'Les 3 mois avant',
422
+ 'last_3_month_least_1_year': 'Les mêmes 3 mois l’année dernière',
423
+ 'last_3_month_least_2_years': 'Les mêmes 3 mois l’année A-2',
424
+ }},
425
+ '6m': {label: '6 mois', list: { // 6 mois
426
+ 'last_6_month': 'Les 6 derniers mois',
427
+ 'last_6_month_before': 'Les 6 mois avant',
428
+ 'last_6_month_least_1_year': 'Les mêmes 6 mois l’année dernière',
429
+ 'last_6_month_least_2_years': 'Les mêmes 6 mois l’année A-2',
430
+ }},
431
+ '12m': {label: '12 mois', list: { // 12 mois
432
+ 'last_12_month': 'Les 12 derniers mois',
433
+ 'last_12_month_before': 'Les 12 mois avant',
434
+ 'last_12_month_least_1_year': 'Les mêmes 12 mois l’année dernière',
435
+ 'last_12_month_least_2_years': 'Les mêmes 12 mois l’année A-2',
436
+ }},
437
+ '24m': {label: '24 mois', list: { // 24 mois
438
+ 'last_24_month': 'Les 24 derniers mois',
439
+ 'last_24_month_before': 'Les 24 mois avant',
440
+ 'last_24_month_least_1_year': 'Les mêmes 24 mois l’année dernière',
441
+ 'last_24_month_least_2_years': 'Les mêmes 24 mois l’année A-2',
442
+ }},
443
+ '1y': {label: 'Une année', list: { // 1 année
444
+ 'curr_year': 'Cette année',
445
+ 'last_year': 'L’année dernière',
446
+ 'yearMoins2': 'Année A-2',
447
+ 'yearMoins3': 'Année A-3',
448
+ 'yearMoins4': 'Année A-4',
449
+ }},
450
+ };
451
+ }
452
+ static setTodaySelected(periodFormGroup) {
453
+ let date = new Date();
454
+ FormDate.setSelectedDate(periodFormGroup, date.getDate(), (date.getMonth() + 1), date.getFullYear());
455
+ }
456
+ static setCurrentMonthSelected(periodFormGroup) {
457
+ let date = new Date();
458
+ FormDate.setSelectedDate(periodFormGroup, -1, (date.getMonth() + 1), date.getFullYear());
459
+ }
460
+ static setCurrentYearSelected(periodFormGroup) {
461
+ let today = new Date();
462
+ FormDate.setSelectedDate(periodFormGroup, -1, -1, today.getFullYear());
463
+ }
464
+
465
+ static addNbDaysToToday(periodFormGroup, nbDays) {
466
+ FormDate.addNbDaysToSelectedDate(periodFormGroup, nbDays, false);
467
+ }
468
+ static addNbMonthsToToday(periodFormGroup, nbMonths) {
469
+ FormDate.addNbMonthsToSelectedDate(periodFormGroup, nbMonths, false);
470
+ }
471
+ static addNbYearsToToday(periodFormGroup, nbYears) {
472
+ FormDate.addNbYearsToSelectedDate(periodFormGroup, nbYears, false);
473
+ }
474
+
475
+ static addNbDaysToSelectedDate(periodFormGroup, nbDays, fromSelectedDate) {
476
+ let date = typeof fromSelectedDate == 'undefined' || fromSelectedDate ? FormDate.getSelectedDate(periodFormGroup) : new Date();
477
+ date.setDate(date.getDate() + nbDays);
478
+ FormDate.setSelectedDate(periodFormGroup, date.getDate(), (date.getMonth() + 1), date.getFullYear());
479
+ }
480
+ static addNbMonthsToSelectedDate(periodFormGroup, nbMonths, fromSelectedDate) {
481
+ let date = typeof fromSelectedDate == 'undefined' || fromSelectedDate ? FormDate.getSelectedDate(periodFormGroup) : new Date();
482
+ date.setDate(1);
483
+ date.setMonth(date.getMonth() - nbMonths);
484
+ FormDate.setSelectedDate(periodFormGroup, -1, (date.getMonth() + 1), date.getFullYear());
485
+ }
486
+ static addNbYearsToSelectedDate(periodFormGroup, nbYears, fromSelectedDate) {
487
+ let date = typeof fromSelectedDate == 'undefined' || fromSelectedDate ? FormDate.getSelectedDate(periodFormGroup) : new Date();
488
+ FormDate.setSelectedDate(periodFormGroup, -1, -1, date.getFullYear() - nbYears);
489
+ }
490
+
491
+ static getSelectedDate(periodFormGroup) {
492
+ let day = periodFormGroup.find('select.day').val();
493
+ let month = periodFormGroup.find('select.month').val();
494
+ let year = periodFormGroup.find('select.year').val();
495
+ if (null != day && null != month && null != year) {
496
+ return new Date(year, month - 1, day);
497
+ }
498
+ return new Date();
499
+ }
500
+
501
+ static setSelectedDate(periodFormGroup, day, month, year) {
502
+ periodFormGroup.find('select.day').val(day);
503
+ periodFormGroup.find('select.month').val(month);
504
+ periodFormGroup.find('select.year').val(year);
505
+ }
506
+
507
+
508
+
509
+ /*
510
+ // deprecated
511
+
512
+ static majSelectPeriode(select) {
513
+ if (select.find(':selected').attr('value') === 'perso') {
514
+ select.parent().parent().next().removeClass('hide');
515
+ }
516
+ else {
517
+ select.parent().parent().next().addClass('hide');
518
+ }
519
+ }
520
+
521
+ static majSelectCompare() {
522
+ if ($('form select#periodeCompare').length === 0) {
523
+ return;
524
+ }
525
+
526
+ let listValues = [];
527
+ let periodeSelected = $('form select.periode :selected').attr('value');
528
+ let selectCompare = $('form select#periodeCompare');
529
+ let periodeCompareSelected = selectCompare.find(':selected').attr('value');
530
+ let valueDefault = null;
531
+
532
+ selectCompare.find('option').removeAttr('disabled');
533
+
534
+ $.each(listePeriodeCompare, function (idx, tabListPeriode) {
535
+ if (idx != 0) {
536
+ let listKeyPeriode = array_keys(tabListPeriode.list);
537
+ if (in_array(periodeSelected, listKeyPeriode)) {
538
+ listValues = listKeyPeriode;
539
+ valueDefault = listKeyPeriode[1];
540
+ }
541
+ else {
542
+ selectCompare.find('option[value="' + listKeyPeriode[0] + '"]').parent().children().attr('disabled', 'disabled');
543
+ }
544
+ }
545
+ });
546
+
547
+ if (periodeSelected === 'perso') {
548
+ valueDefault = 'perso';
549
+ }
550
+ else if (periodeCompareSelected !== 'perso' && in_array(periodeCompareSelected, listValues)) {
551
+ valueDefault = periodeCompareSelected;
552
+ }
553
+ selectCompare.find('option[value="' + valueDefault + '"]').attr('selected', 'selected');
554
+
555
+ FormDate.majSelectPeriode(selectCompare);
556
+ }
557
+
558
+ static selectFormDateToday(lien) {
559
+ let date = new Date();
560
+ FormDate.selectFormDate(lien, date.getDate(), (date.getMonth() + 1), date.getFullYear());
561
+ }
562
+ static selectFormDateDayMoinsNb(lien, nbJoursMoins) {
563
+ let date = new Date();
564
+ date.setDate(date.getDate() - nbJoursMoins);
565
+ FormDate.selectFormDate(lien, date.getDate(), (date.getMonth() + 1), date.getFullYear());
566
+ }
567
+ static selectFormDateCurrentMonth(lien) {
568
+ let date = new Date();
569
+ FormDate.selectFormDate(lien, -1, (date.getMonth() + 1), date.getFullYear());
570
+ }
571
+ static selectFormDateMonthMoinsNb(lien, nbMoisMoins) {
572
+ let date = new Date();
573
+ date.setDate(1);
574
+ date.setMonth(date.getMonth() - nbMoisMoins);
575
+ FormDate.selectFormDate(lien, -1, (date.getMonth() + 1), date.getFullYear());
576
+ }
577
+ static selectFormDateCurrentYear(lien) {
578
+ let today = new Date();
579
+ FormDate.selectFormDate(lien, -1, -1, today.getFullYear());
580
+ }
581
+ static selectFormDateYearMoinsNb(lien, nbAnneesMoins) {
582
+ let today = new Date();
583
+ FormDate.selectFormDate(lien, -1, -1, today.getFullYear() - nbAnneesMoins);
584
+ }
585
+ static selectFormDateAddDayFromSelectedDay(lien, nbDaysAdded) {
586
+ let date = FormDate.getDateObjectSelected(lien);
587
+ date.setDate(date.getDate() + nbDaysAdded);
588
+ FormDate.selectFormDate(lien, date.getDate(), (date.getMonth() + 1), date.getFullYear());
589
+ }
590
+ static getDateObjectSelected(lien) {
591
+ let selectorDay = '#' + (lien.parent().prev().prev().prev().prev().attr('id')) + ' option:selected';
592
+ let selectorMonth = '#' + (lien.parent().prev().prev().prev().attr('id')) + ' option:selected';
593
+ let selectorYear = '#' + (lien.parent().prev().prev().attr('id')) + ' option:selected';
594
+ if ($(selectorDay).length > 0 && $(selectorMonth).length > 0 && $(selectorYear).length > 0) {
595
+ return new Date($(selectorYear).attr('value'), $(selectorMonth).attr('value') - 1, $(selectorDay).attr('value'));
596
+ }
597
+ return new Date();
598
+ }
599
+ static selectFormDate(lien, day, month, year) {
600
+ let selectorDay = '#' + (lien.parent().prev().prev().prev().prev().attr('id')) + ' option[value=' + day + ']';
601
+ let selectorMonth = '#' + (lien.parent().prev().prev().prev().attr('id')) + ' option[value=' + month + ']';
602
+ let selectorYear = '#' + (lien.parent().prev().prev().attr('id')) + ' option[value=' + year + ']';
603
+ if ($(selectorDay).length > 0) $(selectorDay).prop('selected', 'selected');
604
+ if ($(selectorMonth).length > 0) $(selectorMonth).prop('selected', 'selected');
605
+ if ($(selectorYear).length > 0) $(selectorYear).prop('selected', 'selected');
606
+ }
607
+ */
608
+ }
609
+
610
+ module.exports = { FormDate, InputPeriod };