2019-04-02 17:14:36 +08:00
|
|
|
/* global animateSpinner I18n gaUrlQueryParams PerfectSb */
|
2019-04-09 15:08:27 +08:00
|
|
|
/* eslint-disable no-extend-native, no-underscore-dangle */
|
2019-03-06 17:34:04 +08:00
|
|
|
// Common code
|
|
|
|
|
|
|
|
|
|
|
|
Date.prototype.date_to_string = function() {
|
|
|
|
return this.getFullYear() + '-' + (this.getMonth() + 1) + '-' + this.getDate();
|
|
|
|
};
|
|
|
|
|
|
|
|
// GA code
|
|
|
|
|
|
|
|
function GlobalActivitiesFiltersGetDates() {
|
2019-03-26 00:35:54 +08:00
|
|
|
var fromDate = $('#calendar-from-date').data('DateTimePicker').date();
|
|
|
|
var toDate = $('#calendar-to-date').data('DateTimePicker').date();
|
|
|
|
if (fromDate) {
|
|
|
|
fromDate = fromDate._d.date_to_string();
|
|
|
|
}
|
|
|
|
if (toDate) {
|
|
|
|
toDate = toDate._d.date_to_string();
|
|
|
|
}
|
2019-03-06 17:34:04 +08:00
|
|
|
return { from: fromDate, to: toDate };
|
|
|
|
}
|
|
|
|
|
|
|
|
function GlobalActivitiesFilterPrepareArray() {
|
|
|
|
var teamFilter = ($('.ga-side .team-selector select').val() || [])
|
|
|
|
.map(e => { return parseInt(e, 10); });
|
|
|
|
var userFilter = ($('.ga-side .user-selector select').val() || [])
|
|
|
|
.map(e => { return parseInt(e, 10); });
|
|
|
|
var activityFilter = ($('.ga-side .activity-selector select').val() || [])
|
|
|
|
.map(e => { return parseInt(e, 10); });
|
|
|
|
var subjectFilter = {};
|
|
|
|
$.each(($('.ga-side .subject-selector select').val() || []), function(index, object) {
|
|
|
|
var splitObject = object.split('_');
|
|
|
|
if (subjectFilter[splitObject[0]] === undefined) subjectFilter[splitObject[0]] = [];
|
|
|
|
subjectFilter[splitObject[0]].push(parseInt(splitObject[1], 10));
|
|
|
|
});
|
2019-03-26 21:54:34 +08:00
|
|
|
|
|
|
|
// Clear request parameters if all options are selected
|
|
|
|
if (activityFilter.length === $('.ga-side .activity-selector option').length) {
|
|
|
|
activityFilter = [];
|
|
|
|
}
|
|
|
|
|
2019-03-06 17:34:04 +08:00
|
|
|
return {
|
2019-03-26 21:54:34 +08:00
|
|
|
teams: teamFilter,
|
|
|
|
users: userFilter,
|
|
|
|
types: activityFilter,
|
|
|
|
subjects: subjectFilter,
|
2019-03-06 17:34:04 +08:00
|
|
|
from_date: GlobalActivitiesFiltersGetDates().from,
|
|
|
|
to_date: GlobalActivitiesFiltersGetDates().to
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
$(function() {
|
2019-03-15 22:52:30 +08:00
|
|
|
var updateRunning = false;
|
2019-04-09 15:08:27 +08:00
|
|
|
var selectors = ['activity', 'user', 'team'];
|
2019-03-06 17:34:04 +08:00
|
|
|
// Ajax request for object search
|
|
|
|
var subjectAjaxQuery = {
|
|
|
|
url: '/global_activities/search_subjects',
|
|
|
|
dataType: 'json',
|
|
|
|
data: function(params) {
|
2019-04-09 15:08:27 +08:00
|
|
|
var filter = GlobalActivitiesFilterPrepareArray();
|
|
|
|
filter.query = params.term;
|
|
|
|
return filter;
|
2019-03-06 17:34:04 +08:00
|
|
|
},
|
|
|
|
// preparing results
|
|
|
|
processResults: function(data) {
|
|
|
|
var result = [];
|
|
|
|
$.each(data, (key, items) => {
|
|
|
|
var updateItems = items.map(item => {
|
|
|
|
return {
|
|
|
|
id: key + '_' + item.id,
|
|
|
|
text: item.name,
|
2019-04-02 17:14:36 +08:00
|
|
|
label: I18n.t('global_activities.subject_name.' + key.toLowerCase())
|
2019-03-06 17:34:04 +08:00
|
|
|
};
|
|
|
|
});
|
2019-03-29 16:26:27 +08:00
|
|
|
result.push({
|
|
|
|
text: I18n.t('global_activities.subject_name.' + key.toLowerCase()),
|
|
|
|
children: updateItems
|
|
|
|
});
|
2019-03-06 17:34:04 +08:00
|
|
|
});
|
|
|
|
return {
|
|
|
|
results: result
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2019-04-09 15:08:27 +08:00
|
|
|
// custom display function
|
2019-03-06 17:34:04 +08:00
|
|
|
var subjectCustomDisplay = (state) => {
|
2019-04-02 17:14:36 +08:00
|
|
|
return (state.label ? state.label + ': ' : '') + state.text;
|
2019-03-06 17:34:04 +08:00
|
|
|
};
|
2019-03-15 22:52:30 +08:00
|
|
|
|
2019-04-09 15:08:27 +08:00
|
|
|
var ajaxQuery = {};
|
|
|
|
$.each(selectors, (index, e) => {
|
|
|
|
var selector = $('.ga-side .' + e + '-selector select')[0];
|
|
|
|
if (e !== 'activity' && selector) {
|
|
|
|
ajaxQuery[e] = {
|
|
|
|
url: selector.dataset.ajaxUrl,
|
|
|
|
dataType: 'json',
|
|
|
|
data: function(params) {
|
|
|
|
var filter = GlobalActivitiesFilterPrepareArray();
|
|
|
|
filter.query = params.term;
|
|
|
|
return filter;
|
|
|
|
},
|
|
|
|
processResults: function(data) {
|
|
|
|
var result = [];
|
2019-04-26 21:38:33 +08:00
|
|
|
if (typeof (data.data) === 'object') return { results: result };
|
2019-04-09 15:08:27 +08:00
|
|
|
$.each(data, (key, obj) => {
|
|
|
|
result.push({
|
|
|
|
id: String(obj.id),
|
|
|
|
text: obj.name
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return { results: result };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
2019-04-02 22:05:48 +08:00
|
|
|
function GlobalActivitiesUpdateTopPaneTags() {
|
2019-03-26 00:35:54 +08:00
|
|
|
var dateContainer = $('.ga-side .date-selector.filter-block');
|
|
|
|
if (updateRunning) return false;
|
|
|
|
$('.ga-top .ga-tags').children().remove();
|
2019-03-28 01:47:34 +08:00
|
|
|
if (dateContainer[0].dataset.periodSelect) {
|
2019-03-26 00:35:54 +08:00
|
|
|
$('<li class="select2-selection__choice">'
|
|
|
|
+ dateContainer[0].dataset.periodLabel
|
|
|
|
+ $('.ga-side .date-selector.filter-block')[0].dataset.periodSelect
|
|
|
|
+ '</li>').appendTo('.ga-top .ga-tags');
|
|
|
|
}
|
|
|
|
$.each($('.ga-side .select2-selection__choice'), function(index, tag) {
|
|
|
|
var newTag = $(tag.outerHTML).appendTo('.ga-top .ga-tags');
|
|
|
|
var selectedValues = [];
|
|
|
|
var parentSelector = $(tag).parents('.select2-container').prev();
|
|
|
|
var elementToDelete = null;
|
|
|
|
newTag.find('.select2-selection__choice__remove')
|
|
|
|
.click(() => {
|
2019-04-02 17:14:36 +08:00
|
|
|
var parentTag = $(tag).find('span.select2-block-body')[0];
|
2019-04-02 22:05:48 +08:00
|
|
|
if (parentTag) {
|
2019-03-26 00:35:54 +08:00
|
|
|
// Adding remove action for native blocks
|
|
|
|
selectedValues = parentSelector.val();
|
2019-04-02 17:14:36 +08:00
|
|
|
elementToDelete = parentTag.dataset.selectId;
|
2019-03-26 00:35:54 +08:00
|
|
|
selectedValues = $.grep(selectedValues, v => { return v !== elementToDelete; });
|
|
|
|
parentSelector.val(selectedValues).change();
|
|
|
|
} else {
|
|
|
|
$(tag).find('.select2-selection__choice__remove').click();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2019-04-02 15:56:50 +08:00
|
|
|
PerfectSb().update_all();
|
2019-03-26 00:35:54 +08:00
|
|
|
}
|
|
|
|
|
2019-04-02 17:14:36 +08:00
|
|
|
function preloadFilters(filters) {
|
|
|
|
updateRunning = true;
|
|
|
|
if (filters.types) {
|
|
|
|
$('.ga-side .activity-selector select').val(filters.types).trigger('change');
|
|
|
|
}
|
|
|
|
if (filters.teams) {
|
|
|
|
$('.ga-side .team-selector select').val(filters.teams).trigger('change');
|
|
|
|
}
|
|
|
|
if (filters.users) {
|
|
|
|
$('.ga-side .user-selector select').val(filters.users).trigger('change');
|
|
|
|
}
|
|
|
|
if (filters.from_date) {
|
|
|
|
$('#calendar-from-date').data('DateTimePicker').date(new Date(filters.from_date));
|
|
|
|
}
|
|
|
|
if (filters.to_date) {
|
|
|
|
$('#calendar-to-date').data('DateTimePicker').date(new Date(filters.to_date));
|
|
|
|
}
|
|
|
|
if (filters.subject_labels) {
|
|
|
|
$.each(filters.subject_labels, function(index, subject) {
|
|
|
|
var subjectHash = JSON.parse(subject);
|
|
|
|
$('<option data-label="test" value="' + subjectHash.id + '" selected >' + subjectHash.label + '</option>').appendTo('.ga-side .subject-selector select');
|
|
|
|
});
|
|
|
|
$('.ga-side .subject-selector select').trigger('change');
|
|
|
|
}
|
|
|
|
updateRunning = false;
|
2019-04-02 22:05:48 +08:00
|
|
|
GlobalActivitiesUpdateTopPaneTags();
|
2019-04-02 17:14:36 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 22:52:30 +08:00
|
|
|
// update_filter
|
2019-03-26 00:35:54 +08:00
|
|
|
function reloadActivities() {
|
2019-03-15 22:52:30 +08:00
|
|
|
var moreButton = $('.btn-more-activities');
|
2019-03-27 16:43:04 +08:00
|
|
|
var noActivitiesMessage = $('.no-activities-message');
|
2019-03-15 22:52:30 +08:00
|
|
|
if (updateRunning) return false;
|
|
|
|
updateRunning = true;
|
|
|
|
$('.ga-activities-list .activities-day').remove();
|
|
|
|
animateSpinner(null, true);
|
|
|
|
$.ajax({
|
|
|
|
url: $('.ga-activities-list').data('activities-url'),
|
|
|
|
data: GlobalActivitiesFilterPrepareArray(),
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST',
|
|
|
|
success: function(json) {
|
|
|
|
$(json.activities_html).appendTo('.ga-activities-list');
|
2019-04-12 21:37:45 +08:00
|
|
|
if (json.next_page) {
|
2019-03-15 22:52:30 +08:00
|
|
|
moreButton.removeClass('hidden');
|
2019-04-12 21:37:45 +08:00
|
|
|
moreButton.data('next-page', json.next_page);
|
2019-03-15 22:52:30 +08:00
|
|
|
} else {
|
|
|
|
moreButton.addClass('hidden');
|
|
|
|
}
|
2019-03-29 16:26:27 +08:00
|
|
|
if (json.activities_html === '') {
|
2019-03-27 16:43:04 +08:00
|
|
|
noActivitiesMessage.removeClass('hidden');
|
|
|
|
} else {
|
|
|
|
noActivitiesMessage.addClass('hidden');
|
|
|
|
}
|
2019-04-12 21:37:45 +08:00
|
|
|
$('.ga-activities-list').data('starting-timestamp', json.starting_timestamp);
|
2019-03-15 22:52:30 +08:00
|
|
|
updateRunning = false;
|
|
|
|
animateSpinner(null, false);
|
2019-04-11 00:45:12 +08:00
|
|
|
|
|
|
|
$('.ga-main').scrollTop(0);
|
2019-04-02 15:56:50 +08:00
|
|
|
PerfectSb().update_all();
|
2019-03-15 22:52:30 +08:00
|
|
|
},
|
|
|
|
error: function() {
|
|
|
|
updateRunning = false;
|
|
|
|
animateSpinner(null, false);
|
|
|
|
}
|
|
|
|
});
|
2019-03-26 00:35:54 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-03-15 22:52:30 +08:00
|
|
|
|
2019-03-27 20:15:44 +08:00
|
|
|
function resetHotButtonsBackgroundColor() {
|
|
|
|
$('.date-selector .hot-button').each(function() {
|
|
|
|
$(this).removeClass('selected');
|
2019-03-28 01:47:34 +08:00
|
|
|
});
|
2019-03-27 20:15:44 +08:00
|
|
|
}
|
|
|
|
|
2019-03-06 17:34:04 +08:00
|
|
|
// Common selection intialize
|
|
|
|
$.each(selectors, (index, e) => {
|
2019-04-09 15:08:27 +08:00
|
|
|
var selectorParams;
|
|
|
|
if (e === 'activity') {
|
|
|
|
selectorParams = { singleDisplay: true };
|
|
|
|
} else {
|
|
|
|
selectorParams = { ajax: ajaxQuery[e], unlimitedSize: true };
|
|
|
|
}
|
|
|
|
$('.ga-side .' + e + '-selector select').select2Multiple(selectorParams)
|
2019-03-15 22:52:30 +08:00
|
|
|
.on('change', function() {
|
|
|
|
GlobalActivitiesUpdateTopPaneTags();
|
|
|
|
reloadActivities();
|
|
|
|
});
|
2019-03-06 17:34:04 +08:00
|
|
|
$('.ga-side .' + e + '-selector .clear').click(function() {
|
|
|
|
$('.ga-side .' + e + '-selector select').select2MultipleClearAll();
|
|
|
|
});
|
|
|
|
});
|
2019-04-09 15:08:27 +08:00
|
|
|
|
2019-03-06 17:34:04 +08:00
|
|
|
// Object selection intialize
|
|
|
|
$('.ga-side .subject-selector select').select2Multiple({
|
|
|
|
ajax: subjectAjaxQuery,
|
|
|
|
customSelection: subjectCustomDisplay,
|
|
|
|
unlimitedSize: true
|
2019-04-09 15:08:27 +08:00
|
|
|
}).on('change select2:select', function() {
|
2019-04-02 22:05:48 +08:00
|
|
|
GlobalActivitiesUpdateTopPaneTags();
|
2019-03-26 00:35:54 +08:00
|
|
|
reloadActivities();
|
|
|
|
});
|
2019-03-06 17:34:04 +08:00
|
|
|
$('.ga-side .subject-selector .clear').click(function() {
|
|
|
|
$('.ga-side .subject-selector select').select2MultipleClearAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.ga-tags-container .clear-container span').click(function() {
|
2019-03-15 22:52:30 +08:00
|
|
|
updateRunning = true;
|
2019-03-06 17:34:04 +08:00
|
|
|
$.each(selectors, (index, e) => { $('.ga-side .' + e + '-selector select').select2MultipleClearAll(); });
|
|
|
|
$('.ga-side .subject-selector select').select2MultipleClearAll();
|
2019-03-26 00:35:54 +08:00
|
|
|
$('#calendar-from-date').data('DateTimePicker').clear();
|
|
|
|
$('#calendar-to-date').data('DateTimePicker').clear();
|
2019-03-28 01:47:34 +08:00
|
|
|
$('.ga-side .date-selector.filter-block')[0].dataset.periodSelect = '';
|
|
|
|
|
2019-03-15 22:52:30 +08:00
|
|
|
updateRunning = false;
|
2019-03-26 21:54:34 +08:00
|
|
|
GlobalActivitiesUpdateTopPaneTags();
|
2019-03-15 22:52:30 +08:00
|
|
|
reloadActivities();
|
2019-03-29 15:18:15 +08:00
|
|
|
resetHotButtonsBackgroundColor();
|
2019-03-06 17:34:04 +08:00
|
|
|
});
|
|
|
|
|
2019-03-26 00:35:54 +08:00
|
|
|
$('#calendar-to-date').on('dp.change', function(e) {
|
2019-03-06 17:34:04 +08:00
|
|
|
var dateContainer = $('.ga-side .date-selector.filter-block');
|
2019-03-26 00:35:54 +08:00
|
|
|
if (!updateRunning) {
|
|
|
|
$('#calendar-from-date').data('DateTimePicker').minDate(e.date);
|
2019-03-29 15:18:15 +08:00
|
|
|
dateContainer[0].dataset.periodSelect = $('#calendar-to-date').val() + ' - ' + $('#calendar-from-date').val();
|
2019-03-26 00:35:54 +08:00
|
|
|
GlobalActivitiesUpdateTopPaneTags();
|
|
|
|
reloadActivities();
|
2019-03-28 01:47:34 +08:00
|
|
|
resetHotButtonsBackgroundColor();
|
2019-03-26 00:35:54 +08:00
|
|
|
}
|
2019-03-06 17:34:04 +08:00
|
|
|
});
|
|
|
|
|
2019-03-26 00:35:54 +08:00
|
|
|
$('#calendar-from-date').on('dp.change', function(e) {
|
2019-03-06 17:34:04 +08:00
|
|
|
var dateContainer = $('.ga-side .date-selector.filter-block');
|
2019-03-26 00:35:54 +08:00
|
|
|
if (!updateRunning) {
|
|
|
|
$('#calendar-to-date').data('DateTimePicker').maxDate(e.date);
|
2019-03-29 15:18:15 +08:00
|
|
|
dateContainer[0].dataset.periodSelect = $('#calendar-to-date').val() + ' - ' + $('#calendar-from-date').val();
|
2019-03-26 00:35:54 +08:00
|
|
|
GlobalActivitiesUpdateTopPaneTags();
|
|
|
|
reloadActivities();
|
2019-03-28 01:47:34 +08:00
|
|
|
resetHotButtonsBackgroundColor();
|
2019-03-26 00:35:54 +08:00
|
|
|
}
|
2019-03-06 17:34:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
GlobalActivitiesUpdateTopPaneTags();
|
|
|
|
|
2019-04-02 17:14:36 +08:00
|
|
|
if (typeof gaUrlQueryParams !== 'undefined' && gaUrlQueryParams) {
|
|
|
|
preloadFilters(gaUrlQueryParams);
|
|
|
|
}
|
|
|
|
|
2019-03-15 22:52:30 +08:00
|
|
|
$('.date-selector .hot-button').click(function() {
|
|
|
|
var selectPeriod = this.dataset.period;
|
|
|
|
var dateContainer = $('.ga-side .date-selector.filter-block');
|
|
|
|
var fromDate = $('#calendar-from-date').data('DateTimePicker');
|
|
|
|
var toDate = $('#calendar-to-date').data('DateTimePicker');
|
|
|
|
var today = new Date();
|
|
|
|
var yesterday = new Date(new Date().setDate(today.getDate() - 1));
|
|
|
|
var weekDay = today.getDay();
|
|
|
|
var monday = new Date(new Date().setDate(today.getDate() - weekDay + (weekDay === 0 ? -6 : 1)));
|
2019-03-26 00:35:54 +08:00
|
|
|
var lastWeekEnd = new Date(new Date().setDate(monday.getDate() - 1));
|
|
|
|
var lastWeekStart = new Date(new Date().setDate(monday.getDate() - 7));
|
2019-03-15 22:52:30 +08:00
|
|
|
var firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
|
2019-03-26 00:35:54 +08:00
|
|
|
var lastMonthEnd = new Date(new Date().setDate(firstDay.getDate() - 1));
|
|
|
|
var lastMonthStart = new Date(lastMonthEnd.getFullYear(), lastMonthEnd.getMonth(), 1);
|
2019-03-15 22:52:30 +08:00
|
|
|
updateRunning = true;
|
2019-03-29 16:07:46 +08:00
|
|
|
fromDate.minDate(new Date(1900, 1, 1));
|
|
|
|
toDate.maxDate(new Date(3000, 1, 1));
|
2019-03-15 22:52:30 +08:00
|
|
|
if (selectPeriod === 'today') {
|
|
|
|
fromDate.date(today);
|
2019-03-26 00:35:54 +08:00
|
|
|
toDate.date(today);
|
2019-03-15 22:52:30 +08:00
|
|
|
} else if (selectPeriod === 'yesterday') {
|
|
|
|
fromDate.date(yesterday);
|
2019-03-26 00:35:54 +08:00
|
|
|
toDate.date(yesterday);
|
2019-03-15 22:52:30 +08:00
|
|
|
} else if (selectPeriod === 'this_week') {
|
2019-03-26 00:35:54 +08:00
|
|
|
fromDate.date(today);
|
|
|
|
toDate.date(monday);
|
2019-03-15 22:52:30 +08:00
|
|
|
} else if (selectPeriod === 'last_week') {
|
2019-03-26 00:35:54 +08:00
|
|
|
fromDate.date(lastWeekEnd);
|
|
|
|
toDate.date(lastWeekStart);
|
2019-03-15 22:52:30 +08:00
|
|
|
} else if (selectPeriod === 'this_month') {
|
2019-03-26 00:35:54 +08:00
|
|
|
fromDate.date(today);
|
|
|
|
toDate.date(firstDay);
|
2019-03-15 22:52:30 +08:00
|
|
|
} else if (selectPeriod === 'last_month') {
|
2019-03-26 00:35:54 +08:00
|
|
|
fromDate.date(lastMonthEnd);
|
|
|
|
toDate.date(lastMonthStart);
|
2019-03-15 22:52:30 +08:00
|
|
|
}
|
|
|
|
updateRunning = false;
|
|
|
|
dateContainer[0].dataset.periodSelect = this.innerHTML;
|
|
|
|
GlobalActivitiesUpdateTopPaneTags();
|
|
|
|
reloadActivities();
|
2019-03-27 20:15:44 +08:00
|
|
|
|
|
|
|
resetHotButtonsBackgroundColor();
|
|
|
|
$(this).addClass('selected');
|
2019-03-15 22:52:30 +08:00
|
|
|
});
|
|
|
|
});
|