123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import 'package:fis_lib_report/converts/event_type.dart';
- import 'package:fis_lib_report/report/element_type.dart';
- import 'package:fis_lib_report/report/interfaces/block_element.dart';
- import 'package:fis_lib_report/report/interfaces/element.dart';
- import 'package:fis_lib_report/report_info/block_element_info_interface.dart';
- import 'package:fis_lib_report/report_info/element_info.dart';
- import 'package:fis_lib_report/report_info/input_image_info.dart';
- import 'package:fis_lib_report/report_info/input_image_list_info.dart';
- import 'package:fis_lib_report/report_info/input_text_info.dart';
- import 'package:fis_lib_report/report_info/multi_selected_info.dart';
- import 'package:fis_lib_report/report_info/paragraph_info.dart';
- import 'package:fis_lib_report/report_info/report_base_info.dart';
- import 'package:fis_lib_report/report_info/report_event_args.dart';
- import 'package:fis_lib_report/report_info/rt_table_info.dart';
- import 'package:fis_lib_report/report_info/single_selected_info.dart';
- class ReportInfo extends ReportBaseInfo {
- static ReportInfo? _reportInfo;
- ReportInfo._internal();
-
- static ReportInfo get instance {
- _reportInfo ??= ReportInfo._internal();
- return _reportInfo!;
- }
-
- void fromJson(Map<String, dynamic> json) {
- List<dynamic> jsonBlocks = json['BlockInfos'];
- if (jsonBlocks.isNotEmpty) {
- for (var block in jsonBlocks) {
- final type = block['ElementType'];
- final jsonType = ElementType.fromJson(type);
- final name = jsonType.name;
- if (name == ElementType.paragraph.name) {
- final paragraphInfo = block as Map<String, dynamic>;
- _paragraphInfoFromJson(paragraphInfo);
- } else if (name == ElementType.rtTable.name) {
- final tableInfo = block as Map<String, dynamic>;
- List<dynamic> jsonElements = tableInfo['Cells'];
- int index = 0;
- for (var map in jsonElements) {
-
- if (index.isOdd) {
- final jsonType = map['ElementType'];
- final type = ElementType.fromJson(jsonType);
- if (type.name == ElementType.rtCell.name) {
- final cellMap = map as Map<String, dynamic>;
- final jsonBlocks = cellMap['Blocks'] as Map<String, dynamic>;
- _paragraphInfoFromJson(jsonBlocks);
- }
- }
- index++;
- }
- }
- }
- }
- }
-
- Map<String, dynamic> toJson() {
- final map = <String, dynamic>{};
- try {
- final headInfos = [];
- for (var element in headers) {
- headInfos.add(_getElementInfo(element));
- }
- map['HeadInfos'] = headInfos;
- final blockInfos = [];
- for (var block in blocks) {
- blockInfos.add(_getElementInfo(block));
- }
- map['BlockInfos'] = blockInfos;
- final footerInfos = [];
- for (var block in footers) {
- footerInfos.add(_getElementInfo(block));
- }
- map['FooterInfos'] = footerInfos;
- } catch (e) {
- print(e);
- }
- return map;
- }
-
- void reload(String reporter, DateTime reportDate, String jsonStr,
- FEventHandler<String> onSelect) {
- _reportInfo = null;
- onReload.emit(
- this, ReportEventArgs(reportDate, reporter, jsonStr, onSelect));
- }
-
- IBlockElementInfo? getBlockElement(IBlockElement block) {
- final id = block.id;
- for (var element in headers) {
- if (element.id == id) {
- return element;
- }
- }
- for (var element in blocks) {
- if (element.id == id) {
- return element;
- }
- }
- for (var element in footers) {
- if (element.id == id) {
- return element;
- }
- }
- }
-
- void selectedInputImage(String imageUrl) {
- for (var element in headers) {
- if (_checkImageisSelected(element, imageUrl)) {
- return;
- }
- }
- for (var element in blocks) {
- if (_checkImageisSelected(element, imageUrl)) {
- return;
- }
- }
- for (var element in footers) {
- if (_checkImageisSelected(element, imageUrl)) {
- return;
- }
- }
- }
-
- ElementInfo? getElementInfo(IElement element) {
- try {
- for (var e in headers) {
- final elementInfo = _getBaseElementInfo(e, element);
- if (elementInfo != null) {
- return elementInfo;
- }
- }
- for (var e in blocks) {
- final elementInfo = _getBaseElementInfo(e, element);
- if (elementInfo != null) {
- return elementInfo;
- }
- }
- for (var e in footers) {
- final elementInfo = _getBaseElementInfo(e, element);
- if (elementInfo != null) {
- return elementInfo;
- }
- }
- } catch (e) {
- print(e);
- }
- return null;
- }
- void _paragraphInfoFromJson(Map<String, dynamic> paragraphInfo) {
- List<dynamic> jsonElements = paragraphInfo['ElementInfos'];
- if (jsonElements.isEmpty) {
- return;
- }
- for (var map in jsonElements) {
- final jsonType = ElementType.fromJson(map['ElementType']);
- final id = map['Id'];
- if (jsonType.name == ElementType.inputText.name) {
- final info = getElementInfoById(id);
- if (info != null) {
- final inputInfo = info as InputTextInfo;
- inputInfo.text = map['Text'];
- }
- } else if (jsonType.name == ElementType.singleSelected.name) {
- final info = getElementInfoById(id);
- if (info != null) {
- final inputInfo = info as SingleSelectedInfo;
- inputInfo.selectedItem = map['SelectedItem'];
- }
- } else if (jsonType.name == ElementType.multiSelected.name) {
- final info = getElementInfoById(id);
- if (info != null) {
- final inputInfo = info as MulitiSelectedInfo;
- List<String> list = [];
- final items = map['SelectedItems'];
- for (var i in items) {
- list.add(i.toString());
- }
- inputInfo.selectedItems = list;
- }
- }
- }
- }
- bool _checkImageisSelected(IBlockElementInfo block, String url) {
- if (block is InputImageListInfo) {
- final inputImage = block;
- if (inputImage.isSelected ?? false) {
- inputImage.addImage(url);
- return true;
- }
- } else if (block is ParagraphInfo) {
- for (var el in block.elementInfos!) {
- if (el is InputImageInfo) {
- final image = el;
- if (image.isSelected!) {
- image.addImage(url);
- return true;
- }
- }
- }
- }
- return false;
- }
-
- ElementInfo? _getBaseElementInfo(IBlockElementInfo block, IElement element) {
- ElementInfo? result;
- final type = block.elementType;
- if (type!.name == ElementType.paragraph.name) {
- final paragraph = block as ParagraphInfo;
- for (var el in paragraph.elementInfos!) {
- if (el.id == element.id) {
- result = el;
- break;
- }
- }
- }
- if (result == null) {
- if (type.name == ElementType.rtTable.name) {
- final table = block as RTTableInfo;
- final cells = table.cells!;
- for (var cell in cells.values) {
- for (var b in cell.blocks!) {
- final info = _getBaseElementInfo(b, element);
- if (info != null) {
- result = info;
- break;
- }
- }
- }
- }
- }
- return result;
- }
- Map<String, dynamic> _getElementInfo(IBlockElementInfo element) {
- final _type = element.elementType!;
- if (_type.name == ElementType.rtTable.name) {
- final table = element as RTTableInfo;
- return table.toJson();
- } else if (_type.name == ElementType.paragraph.name) {
- final paragraph = element as ParagraphInfo;
- return paragraph.toJson();
- } else {
- final inputImageList = element as InputImageListInfo;
- return inputImageList.toJson();
- }
- }
- }
|