2016-09-14 21:03:42 +00:00
|
|
|
((global) => {
|
|
|
|
|
2016-09-19 20:41:36 +00:00
|
|
|
const COOKIE_NAME = 'cycle_analytics_help_dismissed';
|
|
|
|
|
2016-09-14 21:03:42 +00:00
|
|
|
gl.CycleAnalytics = class CycleAnalytics {
|
|
|
|
constructor() {
|
2016-09-19 20:41:36 +00:00
|
|
|
const that = this;
|
|
|
|
|
|
|
|
this.isHelpDismissed = $.cookie(COOKIE_NAME);
|
2016-09-14 21:03:42 +00:00
|
|
|
this.vue = new Vue({
|
|
|
|
el: '#cycle-analytics',
|
|
|
|
name: 'CycleAnalytics',
|
|
|
|
created: this.fetchData(),
|
2016-09-19 20:41:36 +00:00
|
|
|
data: this.decorateData({ isLoading: true }),
|
|
|
|
methods: {
|
|
|
|
dismissLanding() {
|
|
|
|
that.dismissLanding();
|
|
|
|
}
|
|
|
|
}
|
2016-09-14 21:03:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-19 20:41:36 +00:00
|
|
|
fetchData(options) {
|
|
|
|
options = options || { startDate: 30 };
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: $('#cycle-analytics').data('request-path'),
|
|
|
|
method: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
contentType: 'application/json',
|
|
|
|
data: { start_date: options.startDate }
|
|
|
|
}).done((data) => {
|
|
|
|
this.vue.$data = this.decorateData(data);
|
|
|
|
this.initDropdown();
|
|
|
|
})
|
|
|
|
.error((data) => {
|
|
|
|
this.handleError(data);
|
|
|
|
})
|
|
|
|
.always(() => {
|
|
|
|
this.vue.isLoading = false;
|
|
|
|
})
|
2016-09-14 21:03:42 +00:00
|
|
|
}
|
|
|
|
|
2016-09-19 20:41:36 +00:00
|
|
|
decorateData(data) {
|
|
|
|
data.summary = data.summary || [];
|
|
|
|
data.stats = data.stats || [];
|
|
|
|
data.isHelpDismissed = this.isHelpDismissed;
|
|
|
|
data.isLoading = data.isLoading || false;
|
|
|
|
|
|
|
|
data.summary.forEach((item) => {
|
|
|
|
item.value = item.value || '-';
|
|
|
|
});
|
|
|
|
|
|
|
|
data.stats.forEach((item) => {
|
2016-09-20 20:25:50 +00:00
|
|
|
item.value = item.value || '- - -';
|
2016-09-19 20:41:36 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return data;
|
2016-09-14 21:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleError(data) {
|
2016-09-19 20:41:36 +00:00
|
|
|
this.vue.$data = {
|
|
|
|
hasError: true,
|
|
|
|
isHelpDismissed: this.isHelpDismissed
|
|
|
|
};
|
|
|
|
|
|
|
|
new Flash('There was an error while fetching cycle analytics data.', 'alert');
|
|
|
|
}
|
|
|
|
|
|
|
|
dismissLanding() {
|
|
|
|
this.vue.isHelpDismissed = true;
|
|
|
|
$.cookie(COOKIE_NAME, true);
|
2016-09-14 21:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initDropdown() {
|
|
|
|
const $dropdown = $('.js-ca-dropdown');
|
|
|
|
const $label = $dropdown.find('.dropdown-label');
|
|
|
|
|
2016-09-19 20:41:36 +00:00
|
|
|
$dropdown.find('li a').off('click').on('click', (e) => {
|
2016-09-14 21:03:42 +00:00
|
|
|
e.preventDefault();
|
|
|
|
const $target = $(e.currentTarget);
|
2016-09-14 21:03:42 +00:00
|
|
|
const value = $target.data('value');
|
2016-09-14 21:03:42 +00:00
|
|
|
|
2016-09-14 21:03:42 +00:00
|
|
|
$label.text($target.text().trim());
|
2016-09-14 21:03:42 +00:00
|
|
|
this.vue.isLoading = true;
|
2016-09-19 20:41:36 +00:00
|
|
|
this.fetchData({ startDate: value });
|
2016-09-14 21:03:42 +00:00
|
|
|
})
|
|
|
|
}
|
2016-09-19 20:41:36 +00:00
|
|
|
|
2016-09-14 21:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
})(window.gl || (window.gl = {}));
|