-
Notifications
You must be signed in to change notification settings - Fork 6
/
events.js
177 lines (149 loc) · 5.73 KB
/
events.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
$(function () {
// Only run on event pages.
if (!$('body').hasClass('event')) {
return;
}
// Setup the add/remove wiki fields when creating or editing a new event.
if ($('body').hasClass('event-new') || $('body').hasClass('event-edit') || $('body').hasClass('event-copy')) {
setupAddRemove('event', 'wiki');
}
// Add/remove participants hooks for when viewing an event.
if ($('body').hasClass('event-show')) {
setupAddRemove('event', 'participant');
}
var startDate = moment($('#form_start').val()).utc(),
endDate = moment($('#form_end').val()).utc();
// Set defaults if invalid or blank -- next week.
startDate = startDate.isValid() ? startDate : moment().add(7, 'days').startOf('week');
endDate = endDate.isValid() ? endDate : moment().add(7, 'days').endOf('week');
$('#form_time').daterangepicker({
timePicker: true,
timePicker24Hour: is24HourFormat(),
startDate: startDate,
endDate: endDate,
locale: {
format: getLocaleDatePattern() + ' ' + getLocaleTimePattern(),
applyLabel: $.i18n('apply'),
cancelLabel: $.i18n('cancel'),
customRangeLabel: $.i18n('custom-range'),
daysOfWeek: getWeekdayNames(),
monthNames: getMonthNames()
}
});
// Attempt to default the timezone to the user's timezone.
if ($('body').hasClass('event-new')) {
var timezome = 'UTC';
try {
timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
if ($("#form_timezone option[value='" + timezone + "']").length) {
$('#form_timezone').val(timezone);
}
} catch (_error) {
}
}
/**
* Populate hidden start/end datetime fields on form submission.
*/
$('#event_form').on('submit', function () {
var rangeData = $('#form_time').data().daterangepicker;
$('#form_start').val(rangeData.startDate.format('YYYY-MM-DDTHH:mm:00-00:00'));
$('#form_end').val(rangeData.endDate.format('YYYY-MM-DDTHH:mm:00-00:00'));
});
populateValidWikis().then(function (validWikis) {
$('.event__wikis').on('focus', '.event-wiki-input', function () {
if ($(this).data().typeahead) {
return;
}
$(this).typeahead({
source: validWikis
});
});
});
/**
* Listener for special wiki options.
*/
$('.special-wiki').on('click', function (e) {
e.preventDefault();
var $lastWiki = $('.event__wikis').find('input').last();
if ($lastWiki.val().trim() !== '') {
$('.add-wiki').trigger('click');
$lastWiki = $('.event__wikis').find('input').last();
}
$lastWiki.val($(e.target).data('value'));
});
/**
* Listener for calculate statistics button, which hits the
* process event endpoint, firing off a job.
*/
$('.event-process-btn').on('click', function () {
document.activeElement.blur();
$(this).addClass('disabled').text($.i18n('updating'));
$('.event-export-btn').addClass('disabled');
$('.event-stats-status').text($.i18n('updating-desc'));
$.get(baseUrl + 'events/process/' + $(this).data('event-id')).done(function (data) {
$(this).removeClass('disabled').text($.i18n('update-data'));
$('.event-export-btn').removeClass('disabled');
$('.event-stats-status').text('');
// TODO: Make controller return HTML and update view with
// rendered Twig template, rather than having to refresh.
window.location.reload(true);
}.bind(this)).fail(function (data) {
$('.event-process-btn').text($.i18n('error-failed'))
.addClass('btn-danger');
var feedbackLink = "<a target='_blank' href='https://meta.wikimedia.org/wiki/Talk:Grant_Metrics'>" +
'meta:Talk:Grant Metrics</a>';
$('.event-stats-status').html(
"<strong class='text-danger'>" +
$.i18n('error-internal', feedbackLink) +
'</strong>'
);
});
});
/**
* Link to process event in message shown when stats have not yet been generated.
*/
$('.event-process-link').on('click', function (e) {
$('.event-process-btn').trigger('click');
$('.event-wiki-stats--empty').html(' ');
e.preventDefault();
});
setupAutocompletion();
setupColumnSorting();
$('[data-toggle="tooltip"]').tooltip();
});
/**
* Makes an API request to the sitematrix API, returning a promise
* that resolves with the shortened domain names of all the Wikipedias.
* @return {Deferred}
*/
function populateValidWikis()
{
var dfd = $.Deferred();
$.ajax({
url: 'https://meta.wikimedia.org/w/api.php?action=sitematrix&' +
'formatversion=2&smsiteprop=url&smlangprop=site&format=json',
dataType: 'jsonp'
}).then(function (ret) {
delete ret.sitematrix.count;
var validWikis = [];
for (var lang in ret.sitematrix) {
var family = ret.sitematrix[lang];
if (!family.site) {
continue;
}
family.site.forEach(function (site) {
if (!site.closed && site.url.indexOf('.wikipedia.org') !== -1) {
validWikis.push(
site.url.replace(/\.org$/, '').replace(/^https?:\/\//, '')
);
}
})
}
// 'All Wikipedias' option
validWikis.push('*.wikipedia');
// Commons.
validWikis.push('commons.wikimedia');
dfd.resolve(validWikis);
});
return dfd;
}