export-data.js
var timeBtnClick = (function() { function _todayClick() { $('.select-time .today').on('click', function() { $('#export-date-start-time').val(countDate.getDate(0)); $('#export-date-end-time').val(countDate.getDate(-1)); }); } function _yesterdayClick() { $('.select-time .yesterday').on('click', function(){ $('#export-date-start-time').val(countDate.getDate(1)); $('#export-date-end-time').val(countDate.getDate(0)); }); } function _sevenClick() { $('.select-time .recent-seven-days').on('click', function(){ $('#export-date-start-time').val(countDate.getDate(7)); $('#export-date-end-time').val(countDate.getDate(0)); }); } function _thirtyClick() { $('.select-time .recent-thirty-days').on('click', function(){ $('#export-date-start-time').val(countDate.getDate(30)); $('#export-date-end-time').val(countDate.getDate(0)); }); } return { init: function() { _todayClick(); _yesterdayClick(); _sevenClick(); _thirtyClick(); } }; })(); var countDate = (function() { function getDate(days) { var now = new Date(), newDate = new Date(now.getTime() - 86400000 * days), yyyy = newDate.getFullYear(), mm = (newDate.getMonth() + 1) < 9 ? '0' + (newDate.getMonth() + 1) : newDate.getMonth() + 1, dd = newDate.getDate() < 9 ? '0' + newDate.getDate() : newDate.getDate(); return (yyyy + '-' + mm + '-' + dd + ' 00:00'); } return { getDate: getDate }; })(); var inputInit = (function() { function _initPlaceholder() { $('.export-date-time').placeholder(); } function _initTimepicker() { $('.export-date-time').datetimepicker({ dateFormat : "yy-mm-dd", timeFormat : "HH:mm" }).placeholder(); } return { init: function() { _initPlaceholder(); _initTimepicker(); } }; })(); var exportBtnClick = (function() { function _initExportBtnClick() { $('.export-data-container').on('click', ".export-data-btn:not('.disabled-btn')", function(){ $this = $(this); $('.error-msg').hide(); $this.find('.loading-icon').hide(); var startTime = $('#export-date-start-time').val(), endTime = $('#export-date-end-time').val(), dataType = $this.data('type'), timeErrorMsg = $('.time-error-msg'); if (startTime > endTime) { timeErrorMsg.html(reminder_message.please_choose_correct_time); timeErrorMsg.show(); return; } else { timeErrorMsg.hide(); } if (!startTime || !endTime) { timeErrorMsg.html(reminder_message.please_choose_time); timeErrorMsg.show(); return; } else { timeErrorMsg.hide(); } $('.export-data-btn').addClass('disabled-btn'); $this.find('.loading-icon').css('display', 'inline-block'); ifContinue = true; check_export($this, dataType, startTime, endTime, ifContinue); }); } function check_export($this, dataType, startTime, endTime, ifContinue) { var taskId = $this.data('task-id') ? $this.data('task-id') : '', xlsFileLink = $('#xls-file-link'), loadingIcon = $this.find('.loading-icon'), exportDataBtn = $('.export-data-btn'); if (ifContinue) { $.ajax({ type: 'GET', url: '/export-data?startTime=' + startTime + '&endTime=' + endTime + '&dataType=' + dataType + '&taskId=' + taskId, dataType: 'JSON', success: function(data){ if (data.error_msg) { if_continue = false; $this.data('task-id', ''); $('.time-error-msg').show(); loadingIcon.hide(); exportDataBtn.removeClass('disabled-btn'); } if (data.xls_name) { ifContinue = false; xlsFileLink.attr('href', '/media/' + data.xls_name); xlsFileLink[0].click(); xlsFileLink.removeAttr('href'); $this.data('task-id', ''); loadingIcon.hide(); exportDataBtn.removeClass('disabled-btn'); } else if (data.task_id) { $this.data('task-id', data.task_id); } }, error: function(e){ if_continue = false; $this.data('task-id', ''); $this.next('.error-msg').show(); loadingIcon.hide(); exportDataBtn.removeClass('disabled-btn'); } }); t = setTimeout(function(){ console.log("Looping to query."); check_export($this, dataType, startTime, endTime, ifContinue); }, 1000); } } return { init: _initExportBtnClick }; })(); $(document).ready(function(){ timeBtnClick.init(); inputInit.init(); exportBtnClick.init(); });