Rework reports to use KnockoutJS

This commit is contained in:
Hillel Coren 2018-03-06 09:43:43 +02:00
parent 016b54791a
commit a00ff7cbed

View File

@ -172,13 +172,12 @@
<div class="col-md-6"> <div class="col-md-6">
{!! Former::select('group') {!! Former::select('group')
->addOption('', '') ->data_bind("options: groups, optionsText: 'transPeriod', optionsValue: 'period', value: group") !!}
->addOption(trans('texts.day'), 'day')
->addOption(trans('texts.month'), 'monthyear')
->addOption(trans('texts.year'), 'year') !!}
{!! Former::select('subgroup') <span data-bind="visible: showSubgroup">
->data_bind("options: subgroups, optionsText: 'transField', optionsValue: 'field', value: subgroup") !!} {!! Former::select('subgroup')
->data_bind("options: subgroups, optionsText: 'transField', optionsValue: 'field', value: subgroup") !!}
</span>
<div id="statusField" style="display:none" data-bind="visible: showStatus"> <div id="statusField" style="display:none" data-bind="visible: showStatus">
<div class="form-group"> <div class="form-group">
@ -470,9 +469,12 @@
}); });
$('#group').change(function() { $('#group').change(function() {
if (isStorageSupported()) { if (! isStorageSupported()) {
localStorage.setItem('last:report_group', $('#group').val()); return;
} }
setTimeout(function() {
localStorage.setItem('last:report_group', model.group());
}, 1);
}); });
$('#subgroup').change(function() { $('#subgroup').change(function() {
@ -508,6 +510,12 @@
self.transFormat = transFormat; self.transFormat = transFormat;
} }
function GroupModel(period, transPeriod) {
var self = this;
self.period = period;
self.transPeriod = transPeriod;
}
function SubgroupModel(field, transField) { function SubgroupModel(field, transField) {
var self = this; var self = this;
self.field = field; self.field = field;
@ -528,6 +536,13 @@
self.report_types.push(new ReportTypeModel("{{ $key }}", "{{ $val}}")); self.report_types.push(new ReportTypeModel("{{ $key }}", "{{ $val}}"));
@endforeach @endforeach
self.groups = ko.observableArray([
new GroupModel('', ''),
new GroupModel('day', '{{ trans('texts.day') }}'),
new GroupModel('monthyear', '{{ trans('texts.month') }}'),
new GroupModel('year', '{{ trans('texts.year') }}'),
]);
self.subgroups = ko.computed(function() { self.subgroups = ko.computed(function() {
var reportType = self.report_type(); var reportType = self.report_type();
@ -584,6 +599,10 @@
if (lastReportType) { if (lastReportType) {
self.report_type(lastReportType); self.report_type(lastReportType);
} }
var lastGroup = localStorage.getItem('last:report_group');
if (lastGroup) {
self.group(lastGroup);
}
var lastSubgroup = localStorage.getItem('last:report_subgroup'); var lastSubgroup = localStorage.getItem('last:report_subgroup');
if (lastSubgroup) { if (lastSubgroup) {
self.subgroup(lastSubgroup); self.subgroup(lastSubgroup);
@ -594,6 +613,10 @@
} }
} }
self.showSubgroup = ko.computed(function() {
return self.group();
})
self.showInvoiceOrPaymentDate = ko.computed(function() { self.showInvoiceOrPaymentDate = ko.computed(function() {
return self.report_type() == '{{ ENTITY_TAX_RATE }}'; return self.report_type() == '{{ ENTITY_TAX_RATE }}';
}); });
@ -700,10 +723,6 @@
}).show(); }).show();
if (isStorageSupported()) { if (isStorageSupported()) {
var lastGroup = localStorage.getItem('last:report_group');
if (lastGroup) {
$('#group').val(lastGroup);
}
var lastDocumentFilter = localStorage.getItem('last:document_filter'); var lastDocumentFilter = localStorage.getItem('last:document_filter');
if (lastDocumentFilter) { if (lastDocumentFilter) {
$('#document_filter').val(lastDocumentFilter); $('#document_filter').val(lastDocumentFilter);