-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInputfieldRecurringDates.js
213 lines (189 loc) · 7.45 KB
/
InputfieldRecurringDates.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
function initInputfieldRecurringDates(root) {
if (root === undefined) {
root = document;
}
let components = root.querySelectorAll("[defer-x-data]");
components.forEach(function (component) {
// https://github.com/alpinejs/alpine/issues/359#issuecomment-973688464
let alpineComponent = component.getAttribute('defer-x-data');
Alpine.mutateDom(function () {
component.setAttribute('x-data', alpineComponent);
})
Alpine.initTree(component);
component.removeAttribute('defer-x-data');
});
}
jQuery(document).ready(function () {
initInputfieldRecurringDates();
});
jQuery(document).on('reloaded', '.InputfieldRepeaterItem', function (event) {
var inputfield = event.currentTarget;
initInputfieldRecurringDates(inputfield);
});
document.addEventListener('alpine:init', (e) => {
Alpine.data('recurringDatesInput', function () {
return {
inputfield: '',
_rrule: null,
rrule: {
DTSTART: "",
FREQ: "DAILY",
INTERVAL: 1,
COUNT: 0,
UNTIL: "",
BYDAY: [],
BYMONTH: [],
BYMONTHDAY: []
},
_settings: null,
settings: null,
hard_limit: null,
show_table: false,
catalogues: {
filters: [
{label: "Months", value: 'BYMONTH'},
{label: "Days of the week", value: 'BYDAY'},
{label: "Days of the month", value: 'BYMONTHDAY'},
],
daysOfWeek: [
{name: 'Sunday', value: 'SU'},
{name: 'Monday', value: 'MO'},
{name: 'Tuesday', value: 'TU'},
{name: 'Wednesday', value: 'WE'},
{name: 'Thursday', value: 'TH'},
{name: 'Friday', value: 'FR'},
{name: 'Saturday', value: 'SA'},
],
},
data: {
dates: [],
pagination: {
start: 0,
limit: null,
total: null,
sort: 'ascending',
pagination_string: '',
markup_pager: null,
}
},
init: function () {
this.inputfield = this.$el.dataset.inputfieldName;
this.pageId = parseInt(this.$el.dataset.pageId);
this.fieldId = parseInt(this.$el.dataset.fieldId)
this.hard_limit = parseInt(this.$el.dataset.hardLimit)
this.data.pagination.limit = this.$el.dataset.inputfieldLimit;
this.updateEventList();
this.$watch('rrule', (prop) => {
this.saveString();
});
this.$watch('settings', (prop, oldValue) => {
this._settings = JSON.stringify(this.settings);
var self = this;
self.catalogues.filters.forEach(function (filter) {
var found = self.settings.filters.find(filter_setting => filter_setting === filter.value);
if (found === undefined) {
if (self.rrule[filter.value] !== undefined) {
self.rrule[filter.value] = [];
}
}
});
this.saveString();
});
var json_rrule = this.$refs['main-input'].dataset.rrule;
var widget_settings = this.$refs['main-input'].dataset.settings;
if (widget_settings) {
this.settings = JSON.parse(widget_settings);
}
if (json_rrule) {
this.rrule = JSON.parse(json_rrule);
this._rrule = JSON.stringify(this.rrule);
} else {
//this.settings.limit_mode = "count";
}
},
updateEventList: function () {
var url = new URL('fieldtype-recurring-dates/get-dates/', window.origin);
var params = {
id: this.pageId,
field_id: this.fieldId,
limit: this.data.pagination.limit,
start: this.data.pagination.start
}
url.search = new URLSearchParams(params).toString();
fetch(url, {
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
})
.then(response => {
if (!response.ok) alert(`Something went wrong: ${response.status} - ${response.statusText}`)
return response.json()
})
.then(response => {
this.data = response;
});
},
previousPage(){
this.data.pagination.start -= this.data.pagination.limit
this.updateEventList()
},
nextPage(){
this.data.pagination.start += this.data.pagination.limit
this.updateEventList()
},
is_filtering: function (filter) {
if (this.rrule[filter] !== null || this.rrule[filter] !== undefined) {
if (this.rrule[filter].length) {
return true;
}
}
},
getToggleText(){
if(!this.show_table) {
return "Show dates <span uk-icon='chevron-down'></span>";
}else{
return "Hide dates <span uk-icon='chevron-up'></span>";
}
},
cloneObject: function (obj) {
// basic type deep copy
if (obj === null || obj === undefined || typeof obj !== 'object') {
return obj
}
// array deep copy
if (obj instanceof Array) {
var cloneA = [];
for (var i = 0; i < obj.length; ++i) {
cloneA[i] = this.cloneObject(obj[i]);
}
return cloneA;
}
// object deep copy
var cloneO = {};
for (var i in obj) {
cloneO[i] = this.cloneObject(obj[i]);
}
return cloneO;
},
saveString: function () {
var rrule_copy = this.cloneObject(this.rrule);
if (this.settings.limit_mode === "count") {
delete rrule_copy.UNTIL
}
if (this.settings.limit_mode === "until") {
delete rrule_copy.COUNT
}
if (this.settings.limit_mode === "never") {
delete rrule_copy.UNTIL
rrule_copy.COUNT = this.hard_limit;
}
console.log(rrule_copy);
var json_string = JSON.stringify(rrule_copy);
if (this.$refs['pre-debug'] !== undefined) {
this.$refs['pre-debug'].innerText = JSON.stringify(rrule_copy, null, 2);
}
this._rrule = json_string;
}
}
})
});