@xuda.io/runtime-bundle 1.0.1148 → 1.0.1149

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11268,7 +11268,3949 @@ func.expression.secure_eval = async function (SESSION_ID, sourceP, val, job_id,
11268
11268
  }
11269
11269
  }
11270
11270
  };
11271
- func.UI.component = {};
11271
+ func.UI.screen = {};
11272
+ func.UI.screen.init = async function (SESSION_ID, prog_id, sourceScreenP, callingDataSource_objP, $callingContainerP, triggerIdP, rowIdP, jobNoP, is_panelP, parameters_obj_inP, source_functionP, call_screen_propertiesP, refreshed_ds, parameters_raw_obj) {
11273
+ if (!prog_id) return console.error('program is empty');
11274
+ let screen_ret = await func.utils.get_screen_obj(SESSION_ID, prog_id);
11275
+ if (!screen_ret) return console.error('program is not a screen object');
11276
+ await func.UI.utils.init_ui_framework(SESSION_ID, prog_id);
11277
+
11278
+ let _session = SESSION_OBJ[SESSION_ID];
11279
+
11280
+ const screenInfo = _.cloneDeep(screen_ret);
11281
+ // const $callingContainerP_data = $callingContainerP.clone(true)?.data();
11282
+
11283
+ var screen_type = source_functionP?.split('_')?.[1]; //|| (is_panelP && "panel");
11284
+
11285
+ var screenId = (glb.screen_num++).toString();
11286
+
11287
+ if (SCREEN_BLOCKER_OBJ[prog_id + (sourceScreenP ? '_' + sourceScreenP : '')]) {
11288
+ const wait_for_SCREEN_BLOCKER_release = () => {
11289
+ return new Promise((resolve) => {
11290
+ var interval = setInterval(function () {
11291
+ if (!SCREEN_BLOCKER_OBJ[prog_id + (sourceScreenP ? '_' + sourceScreenP : '')]) {
11292
+ window.clearInterval(interval);
11293
+ resolve();
11294
+ // run_screen();
11295
+ }
11296
+ }, 5);
11297
+ });
11298
+ };
11299
+ await wait_for_SCREEN_BLOCKER_release();
11300
+ }
11301
+
11302
+ func.UI.utils.screen_blocker(true, prog_id + (sourceScreenP ? '_' + sourceScreenP : ''));
11303
+
11304
+ if ($callingContainerP && !_.isEmpty($callingContainerP)) $callingContainerP.data().xuData.screenInfo = screenInfo;
11305
+
11306
+ var $dialogDiv;
11307
+ var $rootFrame;
11308
+ var containerId;
11309
+
11310
+ var params = {
11311
+ prog_id,
11312
+ sourceScreenP,
11313
+ $callingContainerP,
11314
+ triggerIdP,
11315
+ callingDataSource_objP,
11316
+ rowIdP,
11317
+ renderType: screenInfo.properties?.renderType,
11318
+ parameters_obj_inP,
11319
+ source_functionP,
11320
+ is_panelP,
11321
+ screen_type,
11322
+ screenInfo,
11323
+ call_screen_propertiesP,
11324
+ parentDataSourceNoP: _session.DS_GLB?.[callingDataSource_objP?.dsSession]?.dsSession || callingDataSource_objP?.parentDataSourceNo || 0,
11325
+ parameters_raw_obj,
11326
+ };
11327
+
11328
+ switch (screen_type) {
11329
+ case 'embed':
11330
+ $dialogDiv = $('<div>')
11331
+ .attr({
11332
+ id: screenId,
11333
+ ui_engine: UI_FRAMEWORK_INSTALLED,
11334
+ })
11335
+ .addClass('xu_embed_container')
11336
+ .css({ display: 'contents' })
11337
+ .data({
11338
+ xuData: {
11339
+ paramsP: params,
11340
+ screenInfo: params.screenInfo,
11341
+ },
11342
+ });
11343
+
11344
+ let rootTagName = 'div';
11345
+ if (typeof glb.SLIM_BUNDLE === 'undefined' && !glb.SLIM_BUNDLE) {
11346
+ const ui_plugin_core = new UI_FRAMEWORK_PLUGIN.core();
11347
+ rootTagName = ui_plugin_core?.rootTagName();
11348
+ }
11349
+
11350
+ $rootFrame = $(`<${rootTagName}>`).data('xuData', {}).data('xuAttributes', {}).appendTo($dialogDiv);
11351
+
11352
+ $dialogDiv.appendTo($callingContainerP);
11353
+ break;
11354
+
11355
+ case 'panel':
11356
+ $dialogDiv = $callingContainerP;
11357
+ $dialogDiv.data({
11358
+ xuData: {
11359
+ paramsP: params,
11360
+ screenInfo: params.screenInfo,
11361
+ },
11362
+ });
11363
+ $rootFrame = $dialogDiv;
11364
+ break;
11365
+
11366
+ case 'page':
11367
+ case 'modal':
11368
+ case 'popover':
11369
+ $dialogDiv = $('<div>');
11370
+ $rootFrame = $('<div>').appendTo($dialogDiv);
11371
+ $dialogDiv.appendTo('body');
11372
+ break;
11373
+
11374
+ default:
11375
+ }
11376
+
11377
+ params.containerIdP = $rootFrame.attr('id');
11378
+ params.$container = $rootFrame;
11379
+
11380
+ containerId = 'container_' + params.screenInfo.properties?.id + '_' + screenId;
11381
+
11382
+ var data = {
11383
+ note: ' ROOT container',
11384
+ root: true,
11385
+ screenId,
11386
+ is_panelP,
11387
+ prog_id,
11388
+ screen_type,
11389
+ container: '#' + containerId, // initiate container that hold the element
11390
+ };
11391
+ if (is_panelP) {
11392
+ $rootFrame.data().xuData.rootFrame = data;
11393
+ } else {
11394
+ if (!$rootFrame.data().xuData) {
11395
+ $rootFrame.data().xuData = {};
11396
+ }
11397
+ $rootFrame.attr('id', containerId).data().xuData.rootFrame = data;
11398
+ $rootFrame.css('display', 'contents');
11399
+ }
11400
+
11401
+ var node = JSON.parse(JSON.stringify(screen_ret.progUi));
11402
+
11403
+ func.UI.utils.indicator.screen.busy();
11404
+
11405
+ const ret = await func.datasource.create(SESSION_ID, prog_id, refreshed_ds, params.parentDataSourceNoP, $rootFrame.attr('id'), rowIdP, jobNoP, null, parameters_raw_obj, null, null, null, null, null, parameters_obj_inP);
11406
+
11407
+ var _ds = SESSION_OBJ[SESSION_ID].DS_GLB[ret.dsSessionP];
11408
+ _ds.screen_params = params;
11409
+
11410
+ params.dsSessionP = ret.dsSessionP;
11411
+
11412
+ func.UI.screen.update_SYS_OBJ_WIN_INFO(SESSION_ID, params.dsSessionP);
11413
+
11414
+ if (ret.dsSessionP >= 0) {
11415
+ // Call from contact info screen
11416
+ var viewDoc;
11417
+ let view_ret = await func.utils.VIEWS_OBJ.get(SESSION_ID, SESSION_OBJ[SESSION_ID].DS_GLB[ret.dsSessionP].prog_id);
11418
+ if (view_ret) {
11419
+ viewDoc = view_ret;
11420
+ }
11421
+ if (!viewDoc?.progUi) {
11422
+ return func.utils.alerts.invoke(SESSION_ID, 'system_msg', 'SYS_MSG_0780', params.renderType, ret.dsSessionP);
11423
+ }
11424
+ var node;
11425
+ node = _.cloneDeep(viewDoc.progUi);
11426
+ if (!node.length) return console.warn('ui node empty');
11427
+ const ret_render_$container = await func.UI.screen.render_ui_tree(SESSION_ID, $rootFrame, node[0], null, params, jobNoP, null, null, null, null, null, $rootFrame);
11428
+
11429
+ // $(".skeleton_wrapper").remove();
11430
+
11431
+ // fix for svg // deprecated Aug 12 25
11432
+
11433
+ // const fix_svg = function () {
11434
+ // let $svg = $('svg');
11435
+ // $.each($svg, function (key, elm) {
11436
+ // elm.outerHTML = elm.outerHTML;
11437
+ // });
11438
+ // };
11439
+ // setTimeout(function () {
11440
+ // fix_svg();
11441
+ // }, 200);
11442
+
11443
+ func.UI.utils.indicator.screen.normal();
11444
+
11445
+ let ret_screen_loading = await func.UI.screen.screen_loading_done(SESSION_ID, params, ret_render_$container, jobNoP);
11446
+
11447
+ // for (const [container_xu_ui_id, val] of Object.entries(UI_WORKER_OBJ.pending_for_viewport_render)) {
11448
+ // const height = val.base_$div.height();
11449
+ // if (height) {
11450
+ // const total_height = height * val.data.length || 1;
11451
+ // if (total_height > val.$container.height()) {
11452
+ // val.$container.css('height', height * val.data.length);
11453
+ // }
11454
+ // }
11455
+ // debugger;
11456
+ // }
11457
+
11458
+ return ret_screen_loading;
11459
+ }
11460
+ };
11461
+
11462
+ func.UI.screen.update_SYS_OBJ_WIN_INFO = function (SESSION_ID, dsNoP) {
11463
+ var _ds = SESSION_OBJ[SESSION_ID].DS_GLB[dsNoP];
11464
+ if (!_ds) return;
11465
+ if (!_ds.data_system) {
11466
+ _ds.data_system = {};
11467
+ }
11468
+
11469
+ _ds.data_system['SYS_STR_WIN_ID'] = _ds.tree_obj?.id;
11470
+ _ds.data_system['SYS_STR_WIN_NAME'] = _ds.tree_obj?.menuName;
11471
+ if (SESSION_OBJ[SESSION_ID].DS_GLB[dsNoP].mode) {
11472
+ _ds.data_system['SYS_STR_WIN_MODE'] = SESSION_OBJ[SESSION_ID].DS_GLB[dsNoP].mode;
11473
+ }
11474
+ // _ds.data_system["SYS_OBJ_WIN_INFO"] = func.UI.screen.get_info(
11475
+ // SESSION_ID,
11476
+ // dsNoP
11477
+ // );
11478
+ // get all top fields into json
11479
+ };
11480
+
11481
+ func.UI.screen.validate_exit_events = async function (SESSION_ID, div_data_paramsP, forceP) {
11482
+ return new Promise(async (resolve) => {
11483
+ await func.events.validate(SESSION_ID, 'on_exit', div_data_paramsP.dsSessionP, null, 'screen');
11484
+
11485
+ var interval = setInterval(function () {
11486
+ if (!SESSION_OBJ[SESSION_ID].WORKER_OBJ.jobs.length || forceP) {
11487
+ clearInterval(interval);
11488
+ resolve();
11489
+ }
11490
+ }, 5);
11491
+ });
11492
+ };
11493
+
11494
+ func.UI.screen.call_embed = function (SESSION_ID, prog) {
11495
+ $('#embed_' + SESSION_ID)
11496
+ .empty()
11497
+ .data().xuData.screenInfo = null;
11498
+
11499
+ _.forEach(SESSION_OBJ[SESSION_ID].DS_GLB, function (val, key) {
11500
+ if (key) func.datasource.del(SESSION_ID, key);
11501
+ });
11502
+ func.UI.main.embed_prog_execute(SESSION_ID, prog);
11503
+ };
11504
+ func.UI.screen.refresh_xu_attributes = async function (SESSION_ID, fields_arr, jobNoP, $elm_to_search, dsSession_changed, avoid_xu_for_refresh, trigger) {
11505
+ if (trigger !== 'click') {
11506
+ if (!_.isEmpty(SCREEN_BLOCKER_OBJ)) {
11507
+ // let dom to finish
11508
+ setTimeout(() => {
11509
+ func.UI.screen.refresh_xu_attributes(SESSION_ID, fields_arr, jobNoP, $elm_to_search, dsSession_changed);
11510
+ }, 100);
11511
+ return;
11512
+ }
11513
+ }
11514
+
11515
+ UI_WORKER_OBJ.cache = {};
11516
+ const _session = SESSION_OBJ[SESSION_ID];
11517
+ if (glb.DEBUG_MODE) {
11518
+ console.info('========= xu-attributes refresh info ==============');
11519
+ console.info('fields_arr:', fields_arr);
11520
+ }
11521
+
11522
+ var selectors = {};
11523
+ var search_from = 'body';
11524
+ var new_job = jobNoP;
11525
+
11526
+ if ($elm_to_search) {
11527
+ search_from = '#' + $elm_to_search.attr('id');
11528
+ }
11529
+ const get_selectors = function () {
11530
+ $.each(fields_arr, function (key_field, val_field) {
11531
+ $(search_from)
11532
+ .find('*')
11533
+ .filter(function () {
11534
+ // check if the changed field include in the calling in parameters
11535
+ const elm_data = $(this).data();
11536
+ if (!elm_data.xuData) return true;
11537
+
11538
+ if (typeof dsSession_changed !== 'undefined' && elm_data.xuData.paramsP && elm_data.xuData.paramsP.dsSessionP < dsSession_changed) return true;
11539
+
11540
+ let attr = [];
11541
+
11542
+ const validate_param_in = function (exp, without_var) {
11543
+ let exp_val_for_parameter_in_validation;
11544
+ if (typeof exp === 'string') exp_val_for_parameter_in_validation = exp;
11545
+ if (typeof exp === 'object') exp_val_for_parameter_in_validation = JSON.stringify(exp);
11546
+ if (!exp_val_for_parameter_in_validation) return;
11547
+ if (!exp.includes('@')) return;
11548
+ if (_.isEmpty(elm_data?.xuData?.paramsP?.parameters_raw_obj)) return;
11549
+
11550
+ for (const [param_key, param_val] of Object.entries(elm_data?.xuData?.paramsP?.parameters_raw_obj)) {
11551
+ if (!param_val.includes('@')) continue;
11552
+ exp_val_for_parameter_in_validation = exp_val_for_parameter_in_validation.replaceAll((without_var ? '' : '@') + param_key, param_val);
11553
+ }
11554
+ return exp_val_for_parameter_in_validation?.includes?.((without_var ? '' : '@') + val_field);
11555
+ };
11556
+
11557
+ $.each(elm_data?.xuAttributes, function (key, val) {
11558
+ if (typeof val !== 'string' && typeof val !== 'object') return true;
11559
+ if (typeof val === 'string' && !val?.includes('@') && key !== 'xu-bind' && key !== 'xu-for') return true;
11560
+ if (key.substr(0, 3) !== 'xu-') return true;
11561
+
11562
+ if (key === 'xu-bind' || key === 'xu-for') {
11563
+ if (val?.includes?.(val_field) || validate_param_in(val, true)) {
11564
+ attr.push(key);
11565
+ }
11566
+ return true;
11567
+ }
11568
+
11569
+ // if (key === 'xu-for') {
11570
+ // // match static value for xu-for
11571
+ // if (val?.includes?.(val_field) || validate_param_in(val)) {
11572
+ // attr.push(key);
11573
+ // }
11574
+ // return true;
11575
+ // }
11576
+
11577
+ // console.log(key, val);
11578
+
11579
+ if (key.substr(0, 6) === 'xu-exp' && key.substr(6, 1) === ':') {
11580
+ if (val?.includes?.('@' + val_field) || validate_param_in(val)) {
11581
+ attr.push(key);
11582
+ }
11583
+ return true;
11584
+ }
11585
+
11586
+ if (key.substr(0, 8) === 'xu-class') {
11587
+ try {
11588
+ const classes_string = val;
11589
+ const classes_obj = _.isString(classes_string) ? JSON.parse(classes_string) : _.defaults(classes_string, {});
11590
+
11591
+ $.each(classes_obj, function (cla, cond) {
11592
+ if (cond.includes('@' + val_field) || validate_param_in(cond)) {
11593
+ attr.push('xu-class');
11594
+ return false;
11595
+ }
11596
+ });
11597
+ if (attr.length) {
11598
+ return false;
11599
+ }
11600
+ } catch (e) {
11601
+ console.warn('parse error:' + val);
11602
+ }
11603
+ }
11604
+
11605
+ if (key === 'xu-ui-plugin') {
11606
+ const plugin_str = JSON.stringify(val);
11607
+ if (plugin_str.includes('@' + val_field) || validate_param_in(plugin_str)) {
11608
+ attr.push(key);
11609
+ return false;
11610
+ }
11611
+ }
11612
+ });
11613
+
11614
+ // const selector_id = glb.new_xu_render ? $(this).data()?.xuData?.ui_id : $(this).data()?.xuData?.xu_id;
11615
+
11616
+ const selector_id = $(this).attr('xu-ui-id');
11617
+
11618
+ if (attr.length) {
11619
+ selectors[selector_id] = selectors[selector_id];
11620
+
11621
+ if (!selectors[selector_id]) {
11622
+ selectors[selector_id] = { attributes: [], $elm: $(this) };
11623
+ }
11624
+ attr.forEach(function (value) {
11625
+ if (!selectors[selector_id].attributes.includes(value)) selectors[selector_id].attributes.push(value);
11626
+ });
11627
+ }
11628
+ });
11629
+ });
11630
+ };
11631
+
11632
+ get_selectors();
11633
+ // console.log('selectors>>>>', selectors);
11634
+
11635
+ for await (let [elem_key, elem_val] of Object.entries(selectors)) {
11636
+ if (elem_key === 'length') break;
11637
+
11638
+ if (!elem_val.$elm.data().xuData) continue;
11639
+ if (elem_val.$elm.data().xuData.pending_to_delete) continue;
11640
+
11641
+ const add_execute_queue = async function (type) {
11642
+ if (!elem_val.$elm?.data?.()?.xuData) return;
11643
+ try {
11644
+ const obj = {
11645
+ ui_type: elem_val.$elm.data().xuData.ui_type,
11646
+ SESSION_ID,
11647
+ fields_arr,
11648
+ elem_key,
11649
+ elem_val,
11650
+ };
11651
+
11652
+ new_job = await func.UI.worker.add_to_queue(SESSION_ID, 'gui event', type, obj, new_job, elem_val.$elm);
11653
+
11654
+ if (glb.DEBUG_MODE) {
11655
+ console.log(type + '>>>', new_job, obj);
11656
+ }
11657
+ } catch (error) {
11658
+ debugger;
11659
+ }
11660
+ };
11661
+ //////////// process render first ///////////////
11662
+
11663
+ var performed_render = undefined;
11664
+
11665
+ if (!elem_val?.$elm?.data()?.xuAttributes) continue;
11666
+
11667
+ if (elem_val.attributes.includes('xu-exp:xu-render')) {
11668
+ var res = await func.expression.get(SESSION_ID, elem_val.$elm.data().xuAttributes['xu-exp:xu-render'], elem_val.$elm.data().xuData.paramsP.dsSessionP, 'UI Property EXP', elem_val.$elm.data().xuData.recordid);
11669
+
11670
+ var attr_value = await func.common.get_cast_val(SESSION_ID, 'refresh xu-attributes', 'xu-render', 'bool', res.result);
11671
+
11672
+ if (!attr_value && elem_val.$elm[0].tagName === 'XURENDER') continue; // bypass job
11673
+
11674
+ if (attr_value && elem_val.$elm[0].tagName !== 'XURENDER') {
11675
+ await add_execute_queue('execute_xu_all_attributes');
11676
+
11677
+ continue; // bypass render job
11678
+ }
11679
+
11680
+ if (!elem_val?.$elm?.data()?.xuData?.ui_type) continue;
11681
+
11682
+ // RENDER = false handler
11683
+ const obj = {
11684
+ ui_type: elem_val.$elm.data().xuData.ui_type,
11685
+ SESSION_ID,
11686
+ elem_key,
11687
+ elem_val,
11688
+ fields_arr,
11689
+ attr_value,
11690
+ };
11691
+ new_job = jobNoP;
11692
+ new_job = await func.UI.worker.add_to_queue(SESSION_ID, 'gui event', 'execute_xu_render_attributes', obj, new_job, elem_val.$elm, elem_val.$elm.data().xuData.paramsP.dsSessionP);
11693
+ performed_render = true;
11694
+
11695
+ // mark children items ignore list
11696
+ $.each(elem_val.$elm.find('*'), function (key, val) {
11697
+ if ($(val).data().xuData) {
11698
+ $(val).data().xuData.pending_to_delete = true;
11699
+ }
11700
+ });
11701
+
11702
+ if (glb.DEBUG_MODE) {
11703
+ console.info('execute_xu_render_attributes', obj);
11704
+ }
11705
+ }
11706
+
11707
+ if (performed_render || elem_val.$elm[0].tagName === 'XURENDER') continue; // bypass job
11708
+
11709
+ if (elem_val.attributes.includes('xu-exp:xu-for') || elem_val.attributes.includes('xu-for')) continue;
11710
+
11711
+ add_execute_queue('execute_xu_all_attributes');
11712
+ }
11713
+
11714
+ // xu-for
11715
+ selectors = {};
11716
+ get_selectors();
11717
+ // console.log('selectors', selectors);
11718
+ let refreshed_ids = [];
11719
+ // handle xu-for
11720
+ let parent_element_ui_id;
11721
+ for await (let [elem_key, elem_val] of Object.entries(selectors)) {
11722
+ if (elem_key === 'length') break;
11723
+
11724
+ if (!elem_val.$elm.data().xuData) continue;
11725
+ if (elem_val.$elm.data().xuData.pending_to_delete) continue;
11726
+
11727
+ if (!elem_val.attributes.includes('xu-exp:xu-for') && !elem_val.attributes.includes('xu-for')) continue;
11728
+
11729
+ let _parent_element_ui_id;
11730
+ if (elem_val?.$elm?.data()?.xuPanelData) {
11731
+ // handle li panel
11732
+ _parent_element_ui_id = elem_val?.$elm?.data()?.xuPanelData?.parent_element_ui_id;
11733
+ } else {
11734
+ // handle regular li
11735
+ _parent_element_ui_id = elem_val?.$elm?.data()?.xuData?.parent_element_ui_id;
11736
+ }
11737
+
11738
+ if (!parent_element_ui_id || _parent_element_ui_id != parent_element_ui_id) {
11739
+ parent_element_ui_id = _parent_element_ui_id;
11740
+
11741
+ const _$elem = $(`[xu-ui-id=${parent_element_ui_id}]`);
11742
+ const _elem_key = parent_element_ui_id;
11743
+ const _elem_val = { attributes: [], $elm: _$elem };
11744
+
11745
+ $.each(_$elem.data()?.xuAttributes, function (key, val) {
11746
+ _elem_val.attributes.push(key);
11747
+ });
11748
+ const obj = {
11749
+ ui_type: _elem_val.$elm.data().xuData.ui_type,
11750
+ SESSION_ID,
11751
+ elem_key: _elem_key,
11752
+ elem_val: _elem_val,
11753
+ fields_arr,
11754
+ xu_for_item_id: elem_val?.$elm?.data()?.xuPanelData ? elem_val?.$elm?.data()?.xuPanelData.node.id : elem_val?.$elm?.data().xuData.nodeid,
11755
+ };
11756
+ if (avoid_xu_for_refresh) return;
11757
+ await func.UI.worker.add_to_queue(SESSION_ID, 'gui event', 'execute_xu_for', obj, new_job, _elem_val.$elm, _elem_val.$elm.data().xuData.paramsP.dsSessionP);
11758
+
11759
+ refreshed_ids.push(parent_element_ui_id);
11760
+
11761
+ if (glb.DEBUG_MODE) {
11762
+ console.info('execute_xu_for', obj);
11763
+ }
11764
+ }
11765
+ }
11766
+
11767
+ /////////// xu-for for non displayed elements ////////////
11768
+ const iterate_field_in_progUi = async function (progUi, field_id, panel_val) {
11769
+ let found;
11770
+ const iterate_progUi = async function (node, node_id) {
11771
+ for (let item of node) {
11772
+ if (!_.isEmpty(item.attributes)) {
11773
+ const parent_element_ui_id = node_id;
11774
+ // const _$elem = $(`[xu-node-id="${parent_element_ui_id}"]`);
11775
+ for await (const [attr, val] of Object.entries(item.attributes)) {
11776
+ if (attr === 'xu-exp:xu-for' || attr === 'xu-for') {
11777
+ if (val?.includes(field_id)) {
11778
+ // const parent_element_ui_id = node_id;
11779
+ // debugger;
11780
+ // let _$elem = $(`[xu-ui-id^=${parent_element_ui_id}]`);
11781
+ // let _$elem = $(`[xu-node-id=${parent_element_ui_id}]`);
11782
+ const _$elem = func.UI.utils.find_in_element_data('xuData', $(SESSION_OBJ[SESSION_ID].root_element), 'nodeid', parent_element_ui_id);
11783
+ // const _elem_key = parent_element_ui_id;
11784
+ const _elem_key = _$elem.attr('xu-ui-id');
11785
+ const _elem_val = { attributes: [], $elm: _$elem };
11786
+ if (!_$elem?.length) continue;
11787
+
11788
+ if (refreshed_ids.includes(_$elem.attr('xu-ui-id'))) break;
11789
+
11790
+ $.each(_$elem.data()?.xuAttributes, function (key, val) {
11791
+ _elem_val.attributes.push(key);
11792
+ });
11793
+ const obj = {
11794
+ ui_type: _elem_val.$elm.data().xuData.ui_type,
11795
+ SESSION_ID,
11796
+ elem_key: _elem_key,
11797
+ elem_val: _elem_val,
11798
+ fields_arr,
11799
+ xu_for_item_id: item.id,
11800
+ };
11801
+ if (avoid_xu_for_refresh) return;
11802
+ await func.UI.worker.add_to_queue(SESSION_ID, 'gui event', 'execute_xu_for', obj, new_job, _elem_val.$elm, _elem_val.$elm.data().xuData.paramsP.dsSessionP);
11803
+
11804
+ if (glb.DEBUG_MODE) {
11805
+ console.info('node execute_xu_for', obj, panel_val);
11806
+ }
11807
+
11808
+ break;
11809
+ }
11810
+ }
11811
+
11812
+ // if (glb.new_xu_render) {
11813
+ // if (attr === 'xu-exp:xu-render') {
11814
+ // //|| attr === 'xu-render'
11815
+ // for await (const [key, val] of Object.entries(_$elem)) {
11816
+ // if (key === 'length') break;
11817
+ // if ($(val).data().xuData.node) {
11818
+ // for await (const node of $(val).data().xuData.node.children) {
11819
+ // if (item.id !== node.id) continue;
11820
+
11821
+ // if (typeof node.xu_render_made !== 'undefined' && !node.xu_render_made) {
11822
+ // const node_data = UI_WORKER_OBJ.xu_render_cache[node.xu_render_xu_ui_id + node.xu_render_cache_id];
11823
+ // parent_element_ui_id;
11824
+ // const elem_key = node.xu_render_xu_ui_id;
11825
+ // const elem_val = { attributes: [attr], $elm: node_data.$div, data: node_data.data };
11826
+ // if (!elem_val.data?.xuData) continue;
11827
+ // elem_val.data.xuData.parent_element_ui_id = $(val).attr('xu-ui-id');
11828
+ // var res = await func.expression.get(SESSION_ID, elem_val.data.xuAttributes['xu-exp:xu-render'], elem_val.data.xuData.paramsP.dsSessionP, 'UI Property EXP', elem_val.data.xuData.recordid);
11829
+
11830
+ // var attr_value = await func.common.get_cast_val(SESSION_ID, 'refresh xu-attributes', 'xu-render', 'bool', res.result);
11831
+ // try {
11832
+ // const obj = {
11833
+ // ui_type: elem_val.data.xuData.ui_type,
11834
+ // SESSION_ID,
11835
+ // elem_key,
11836
+ // elem_val,
11837
+ // fields_arr,
11838
+ // attr_value,
11839
+ // };
11840
+ // func.UI.worker.add_to_queue(SESSION_ID, 'gui event', 'execute_xu_render_attributes', obj, new_job, elem_val.$elm, elem_val.data.xuData.paramsP.dsSessionP);
11841
+ // } catch (error) {
11842
+ // debugger;
11843
+ // }
11844
+ // }
11845
+ // }
11846
+ // }
11847
+ // }
11848
+ // }
11849
+ // }
11850
+ }
11851
+ }
11852
+ if (found) break;
11853
+
11854
+ if (item.children) {
11855
+ await iterate_progUi(item.children, item.id);
11856
+ }
11857
+ }
11858
+ };
11859
+ await iterate_progUi(progUi);
11860
+ return found;
11861
+ };
11862
+
11863
+ const $xu_embed_container = $('.xu_embed_container');
11864
+ const panels_obj = await func.UI.utils.get_panels_wrapper_from_dom(SESSION_ID, $xu_embed_container, true);
11865
+
11866
+ for await (const field_id of fields_arr) {
11867
+ // run root
11868
+ if ($xu_embed_container.length) {
11869
+ const progUi = $xu_embed_container?.data()?.xuData?.screenInfo?.progUi;
11870
+ if (progUi) {
11871
+ await iterate_field_in_progUi(progUi, field_id);
11872
+ }
11873
+ }
11874
+ // run panels
11875
+ for await (const [panel_wrapper_element_ui_id, panel_val] of Object.entries(panels_obj)) {
11876
+ await iterate_field_in_progUi(panel_val.progUi, field_id, panel_val);
11877
+ }
11878
+ }
11879
+
11880
+ func.events.delete_job(SESSION_ID, jobNoP);
11881
+
11882
+ if (glb.DEBUG_MODE) {
11883
+ console.info('===================================================');
11884
+ }
11885
+ };
11886
+
11887
+ func.UI.screen.refresh_screen = async function (SESSION_ID, fields_changed_arr, datasource_changed, fields_changed_datasource) {
11888
+ var found, refresh_reason, refresh_details;
11889
+ const validate_change = function (prog_doc, panelXuAttributes, skip_ui_check) {
11890
+ found = null;
11891
+ refresh_reason = null;
11892
+ refresh_details = null;
11893
+ const progDataSource_str = JSON.stringify(prog_doc.progDataSource);
11894
+ for (const field_id of fields_changed_arr) {
11895
+ // get panel attributes
11896
+ const _attributes = panelXuAttributes || {};
11897
+
11898
+ // detect if program changed
11899
+ found = _attributes['xu-exp:program']?.includes('@' + field_id);
11900
+
11901
+ if (found) {
11902
+ refresh_reason = `program ${_attributes['xu-exp:program']} ${field_id} changed `;
11903
+ refresh_details = _attributes;
11904
+ break;
11905
+ }
11906
+
11907
+ // _attributes holds also info of parameters in code_in: @code
11908
+ // search field changed in panel call send parameters exp
11909
+ for (const [attr, value] of Object.entries(_attributes)) {
11910
+ const pattern = /xu-exp:(\w+)/;
11911
+ const match = attr.match(pattern);
11912
+
11913
+ if (!match) {
11914
+ // continue if attribute is not expression
11915
+ continue;
11916
+ }
11917
+ // code_in
11918
+ const parameter_in_field_id = match?.[1];
11919
+ // @code
11920
+ if (value.includes(field_id)) {
11921
+ // search parameter in field in the target program's progDataSource
11922
+ found = progDataSource_str?.includes('@' + parameter_in_field_id);
11923
+
11924
+ if (found) {
11925
+ refresh_reason = `field ${field_id} in progDataSource parameter_in changed`;
11926
+ refresh_details = prog_doc?.progDataSource;
11927
+
11928
+ break;
11929
+ }
11930
+ if (!skip_ui_check) {
11931
+ found = func.UI.find_field_in_progUi_attributes(prog_doc.progUi, parameter_in_field_id, 'xu-for').length;
11932
+
11933
+ if (found) {
11934
+ refresh_reason = `field ${field_id} in progUi xu-for parameter_in changed`;
11935
+ refresh_details = found;
11936
+
11937
+ break;
11938
+ }
11939
+ }
11940
+ }
11941
+ }
11942
+
11943
+ if (found) break;
11944
+
11945
+ // search field changed in the target program's progDataSource // @code
11946
+ found = progDataSource_str?.includes('@' + field_id);
11947
+ if (found) {
11948
+ refresh_reason = `field ${field_id} in progDataSource changed`;
11949
+ refresh_details = prog_doc?.progDataSource;
11950
+
11951
+ break;
11952
+ }
11953
+ if (!skip_ui_check) {
11954
+ found = func.UI.find_field_in_progUi_attributes(prog_doc.progUi, field_id, 'xu-for').length;
11955
+ if (found) {
11956
+ refresh_reason = `field ${field_id} in progUi xu-for changed`;
11957
+ refresh_details = found;
11958
+
11959
+ break;
11960
+ }
11961
+ }
11962
+
11963
+ if (found) {
11964
+ break;
11965
+ }
11966
+ }
11967
+ };
11968
+
11969
+ // check the main program
11970
+ if (fields_changed_datasource) {
11971
+ const _session = SESSION_OBJ[SESSION_ID];
11972
+ const _ds = _session.DS_GLB[fields_changed_datasource];
11973
+ const prog_doc = await func.utils.DOCS_OBJ.get(SESSION_ID, _ds.prog_id);
11974
+ if (prog_doc.progUi) {
11975
+ validate_change(prog_doc, null, true);
11976
+ if (found) {
11977
+ const $elm = $(`#container_${_ds.prog_id}_0`);
11978
+ if ($elm?.length) {
11979
+ const elm_data = $elm.data();
11980
+ const $wrapper = $elm.parent();
11981
+
11982
+ const refreshed_ds = _ds.dsSession;
11983
+
11984
+ $elm.empty();
11985
+ if ($elm.data()) {
11986
+ const new_$div = await func.UI.screen.render_ui_tree(
11987
+ SESSION_ID,
11988
+ $elm, // the wrapper
11989
+ _.cloneDeep($elm.data().xuData.node), // the xu-panel node
11990
+ {},
11991
+ elm_data.xuData.paramsP, // the wrapper params
11992
+ null,
11993
+ null,
11994
+ elm_data.xuData.key, // the wrapper key
11995
+ refreshed_ds, // the refreshed_ds
11996
+ elm_data.xuData.parent_node, // the wrapper parent node
11997
+ null,
11998
+ elm_data.xuData.$root_container, // the wrapper root container
11999
+ );
12000
+
12001
+ if (glb.DEBUG_MODE) {
12002
+ console.info('========= refresh main info ==============');
12003
+ console.info('reason:', refresh_reason);
12004
+ console.info('element:', $elm);
12005
+ console.info('==========================================');
12006
+ }
12007
+
12008
+ return;
12009
+ }
12010
+ }
12011
+ }
12012
+ }
12013
+ }
12014
+
12015
+ const panels_obj = await func.UI.utils.get_panels_wrapper_from_dom(SESSION_ID, $(SESSION_OBJ[SESSION_ID].root_element), false);
12016
+
12017
+ for await (const [parent_element_ui_id, panel_val] of Object.entries(panels_obj)) {
12018
+ // const progDataSource_str = JSON.stringify(panel_val.prog_doc.progDataSource);
12019
+ if (!panel_val.$panel_div?.data()?.xuData) continue;
12020
+
12021
+ if (panel_val.$panel_div.data().xuData.pending_to_delete) continue;
12022
+
12023
+ if (fields_changed_arr) {
12024
+ if (fields_changed_datasource && panel_val._ds.dsSession <= Number(fields_changed_datasource)) {
12025
+ continue;
12026
+ }
12027
+ validate_change(panel_val.prog_doc, panel_val?.panelXuAttributes);
12028
+ }
12029
+
12030
+ if (datasource_changed) {
12031
+ if (panel_val._ds.dsSession == datasource_changed) {
12032
+ refresh_reason = `panel datasource ${datasource_changed} changed`;
12033
+ refresh_details = '';
12034
+
12035
+ found = true;
12036
+ }
12037
+ }
12038
+ if (found) {
12039
+ UI_WORKER_OBJ.cache = {};
12040
+ const _session = SESSION_OBJ[SESSION_ID];
12041
+ if (glb.DEBUG_MODE) {
12042
+ console.info('========= refresh info ==============');
12043
+ console.info('reason:', refresh_reason);
12044
+ console.info('details:', refresh_details);
12045
+ console.info('panel:', panel_val);
12046
+ console.info('=====================================');
12047
+ }
12048
+ const $div_elm = panel_val.$panel_div;
12049
+ const wrapper_data = $div_elm.data();
12050
+
12051
+ if (_.isEmpty(wrapper_data)) continue;
12052
+
12053
+ try {
12054
+ const ts = Date.now();
12055
+ // remove old panel content
12056
+ $.each(panel_val.ids, async function (key, val) {
12057
+ $("[xu-ui-id='" + val + "']")
12058
+ .attr('xu-ui-id', val + ts)
12059
+ .removeData();
12060
+ });
12061
+
12062
+ let refreshed_ds;
12063
+ // check if ds exist and deleted by garbage collector
12064
+
12065
+ if (_session.DS_GLB[panel_val._ds.dsSession]) {
12066
+ refreshed_ds = panel_val._ds.dsSession;
12067
+ }
12068
+ for await (const item of wrapper_data.xuData.node_org.children) {
12069
+ if (item.tagName !== 'xu-panel') continue;
12070
+
12071
+ const new_$div = await func.UI.screen.render_ui_tree(
12072
+ SESSION_ID,
12073
+ $div_elm, // the wrapper
12074
+ _.cloneDeep(item), // _.cloneDeep(wrapper_data.xuData.node_org.children[0]), // the xu-panel node
12075
+ {},
12076
+ wrapper_data.xuData.paramsP, // the wrapper params
12077
+ null,
12078
+ null,
12079
+ wrapper_data.xuData.key, // the wrapper key
12080
+ refreshed_ds, // the refreshed_ds
12081
+ wrapper_data.xuData.parent_node, // the wrapper parent node
12082
+ null,
12083
+ wrapper_data.xuData.$root_container, // the wrapper root container
12084
+ );
12085
+ }
12086
+
12087
+ // remove old panel content
12088
+ $.each(panel_val.ids, async function (key, val) {
12089
+ $("[xu-ui-id='" + val + ts + "']").remove();
12090
+ });
12091
+ } catch (error) {
12092
+ debugger;
12093
+ }
12094
+ // continue;
12095
+ }
12096
+ }
12097
+ };
12098
+
12099
+ const get_params_obj_new = async function (SESSION_ID, prog_id, nodeP, dsSession) {
12100
+ const _prog = await func.utils.VIEWS_OBJ.get(SESSION_ID, prog_id);
12101
+ if (!_prog) return;
12102
+
12103
+ // get in parameters
12104
+ let params_res = {},
12105
+ params_raw = {};
12106
+ if (_prog?.properties?.progParams) {
12107
+ for await (const [key, val] of Object.entries(_prog.properties.progParams)) {
12108
+ if (!['in', 'out'].includes(val.data.dir)) continue;
12109
+
12110
+ if (nodeP.attributes) {
12111
+ if (nodeP.attributes[val.data.parameter]) {
12112
+ params_res[val.data.parameter] = nodeP.attributes[val.data.parameter];
12113
+ } else if (nodeP.attributes[`xu-exp:${val.data.parameter}`]) {
12114
+ if (val.data.dir == 'out') {
12115
+ // only reference
12116
+ params_res[val.data.parameter] = nodeP.attributes[`xu-exp:${val.data.parameter}`].replaceAll('@', '');
12117
+ } else {
12118
+ // in parameter
12119
+ let ret = await func.expression.get(SESSION_ID, nodeP.attributes[`xu-exp:${val.data.parameter}`], dsSession, 'parameters');
12120
+ params_res[val.data.parameter] = ret.result;
12121
+ params_raw[val.data.parameter] = nodeP.attributes[`xu-exp:${val.data.parameter}`];
12122
+ }
12123
+ }
12124
+ continue;
12125
+ }
12126
+ console.warn(`Warning: Program ${_prog.properties.menuName} expected In parameter: ${val.data.parameter} but received null instead`);
12127
+ }
12128
+ }
12129
+ return { params_res, params_raw };
12130
+ };
12131
+
12132
+ func.UI.screen.execute_xu_functions = async function (SESSION_ID, is_skeleton, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, xu_func, $elm, val, is_init, refreshed_ds) {
12133
+ if (is_skeleton) return;
12134
+
12135
+ // console.log(nodeP.id, xu_func, val);
12136
+ var _ds = SESSION_OBJ[SESSION_ID].DS_GLB[paramsP.dsSessionP];
12137
+ const tag_fx = {
12138
+ [`xu-panel`]: {
12139
+ program: async function ($elm, val) {
12140
+ var ret = {};
12141
+ var _session = SESSION_OBJ[SESSION_ID];
12142
+ var _ds = _session.DS_GLB[paramsP.dsSessionP];
12143
+ const init_program = async function () {
12144
+ async function render_panel() {
12145
+ const prog_id = val.value?.prog || val.value;
12146
+ const params_obj = await get_params_obj_new(SESSION_ID, prog_id, nodeP, paramsP.dsSessionP);
12147
+ let ret_panel = await func.UI.screen.init(SESSION_ID, prog_id, paramsP.screenId, _ds, $elm, null, _ds.currentRecordId, null, true, params_obj.params_res, 'initXu_panel', undefined, prog_id !== _ds.prog_id ? null : refreshed_ds, params_obj.params_raw);
12148
+ ret = { $new_div: ret_panel };
12149
+ if ($container.data().xuData) {
12150
+ $container.data().xuData.xuPanelProps = $elm.data().xuAttributes;
12151
+ $container.data().xuData.xuPanelData = ret_panel.data();
12152
+ }
12153
+ return ret;
12154
+ }
12155
+
12156
+ if (!val.value) {
12157
+ val.value = '_empty_panel_program';
12158
+ }
12159
+
12160
+ ret = await render_panel();
12161
+
12162
+ return ret;
12163
+ };
12164
+ const alter_program = async function () {
12165
+ var _session = SESSION_OBJ[SESSION_ID];
12166
+ var _ds = _session.DS_GLB[paramsP.dsSessionP];
12167
+
12168
+ /////////////////
12169
+ async function render_panel() {
12170
+ // // if (!cache_str) {
12171
+ // // await update_container(screen_ready_function);
12172
+ // // }
12173
+ // // if (cache_str && !CACHE_PROG_UI[cache_str]) {
12174
+ // // await update_container(screen_ready_function);
12175
+ // // save_cache();
12176
+ // // }
12177
+ // // if (callback) callback($div);
12178
+ // // $container.data().xuData.node.children =
12179
+ // // $div.data().xuData.node.children;
12180
+ // // //swiper-wrapper
12181
+ // // var restore_slides_elements = async function () {
12182
+ // // if ($tmp_div.children().length) {
12183
+ // // $tmp_div
12184
+ // // .find(".swiper-wrapper")
12185
+ // // .empty()
12186
+ // // .append($new_div.children());
12187
+ // // $new_div.append($tmp_div.children());
12188
+ // // }
12189
+ // // };
12190
+ // // // console.log($tmp_div);
12191
+ // // await restore_slides_elements();
12192
+ // // // CHANGE_PANEL_BUSY = false;
12193
+ // // func.events.delete_job(SESSION_ID, jobNo);
12194
+ // };
12195
+ const program = val.value?.prog || val.value;
12196
+ var $wrapper = $('<div>');
12197
+ var $div = await func.UI.screen.create_container(SESSION_ID, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, nodeP.attributes, null, null, null, $wrapper, '');
12198
+ const params_obj = await get_params_obj_new(SESSION_ID, program, nodeP, paramsP.dsSessionP);
12199
+ let ret_init = await func.UI.screen.init(SESSION_ID, program, paramsP.screenId, _ds, $div, null, _ds.currentRecordId, jobNoP, true, params_obj.params_res, 'alterXu_panel', undefined, undefined, params_obj.params_raw);
12200
+ ret = {
12201
+ $new_div: ret_init,
12202
+ abort: true,
12203
+ };
12204
+ await func.UI.screen.panel_post_render_handler(SESSION_ID, $elm, $new_div, nodeP, $div.clone(true), jobNoP);
12205
+
12206
+ return ret;
12207
+ }
12208
+ if (!val.value) {
12209
+ return { abort: true };
12210
+ }
12211
+ await render_panel();
12212
+ return ret;
12213
+ };
12214
+
12215
+ if (is_init) {
12216
+ let ret = await init_program();
12217
+ return ret;
12218
+ }
12219
+ return alter_program();
12220
+ },
12221
+
12222
+ 'xu-render': async function ($elm, val) {
12223
+ let ret = await common_fx['xu-render']($elm, val, true);
12224
+ return ret;
12225
+ },
12226
+ 'xu-ref': async function ($elm, val) {
12227
+ let ret = await common_fx['xu-ref']($container, val, $container.data().xuData.xuPanelData.xuData.paramsP.dsSessionP);
12228
+ return ret;
12229
+ },
12230
+ },
12231
+ [`xu-teleport`]: {
12232
+ to: async function ($elm, val) {
12233
+ if (!glb.new_xu_render) {
12234
+ if (val.value) {
12235
+ // parent_infoP.is_xu_teleport;
12236
+ if ($elm?.parent()?.data()?.xuData?.length) {
12237
+ $elm.parent().data('xuTeleportData', []);
12238
+ for (const [key, node] of Object.entries(nodeP.children)) {
12239
+ const $teleport_elm = await func.UI.screen.render_ui_tree(SESSION_ID, $(val.value), node, parent_infoP, paramsP, jobNoP, is_skeleton, Number(key), null, node, null, $root_container);
12240
+
12241
+ $elm.parent().data().xuTeleportData.push($teleport_elm.attr('xu-ui-id'));
12242
+ $teleport_elm.attr('xu-teleport-parent-id', $elm.parent().attr('xu-ui-id'));
12243
+ }
12244
+ $elm.remove();
12245
+ } else {
12246
+ $elm.data('xuTeleportData', []).attr('hidden', true);
12247
+ for (const [key, node] of Object.entries(nodeP.children)) {
12248
+ const $to_container = $(val.value);
12249
+ if (!$to_container?.length) {
12250
+ return console.error(`container ${val.value} for xuTeleportData not found`);
12251
+ }
12252
+ const $teleport_elm = await func.UI.screen.render_ui_tree(SESSION_ID, $to_container, node, parent_infoP, paramsP, jobNoP, is_skeleton, Number(key), null, node, null, $root_container);
12253
+
12254
+ $elm.data().xuTeleportData.push($teleport_elm.attr('xu-ui-id'));
12255
+ $teleport_elm.attr('xu-teleport-parent-id', $elm.attr('xu-ui-id'));
12256
+ }
12257
+ }
12258
+ }
12259
+ return { abort: true };
12260
+ }
12261
+
12262
+ if (val.value) {
12263
+ // // parent_infoP.is_xu_teleport;
12264
+ // if ($elm?.parent()?.data()?.xuData?.length) {
12265
+ // $elm.parent().data('xuTeleportData', []);
12266
+ // for (const [key, node] of Object.entries(nodeP.children)) {
12267
+ // const $teleport_elm = await func.UI.screen.render_ui_tree(SESSION_ID, $(val.value), node, parent_infoP, paramsP, jobNoP, is_skeleton, Number(key), null, node, null, $root_container);
12268
+ // $elm.parent().data().xuTeleportData.push($teleport_elm.attr('xu-ui-id'));
12269
+ // $teleport_elm.attr('xu-teleport-parent-id', $elm.parent().attr('xu-ui-id'));
12270
+ // }
12271
+ // $elm.remove();
12272
+ // } else {
12273
+ // $elm.data('xuTeleportData', []).attr('hidden', true);
12274
+ // for (const [key, node] of Object.entries(nodeP.children)) {
12275
+ // const $to_container = $(val.value);
12276
+ // if (!$to_container?.length) {
12277
+ // return console.error(`container ${val.value} for xuTeleportData not found`);
12278
+ // }
12279
+ // const $teleport_elm = await func.UI.screen.render_ui_tree(SESSION_ID, $to_container, node, parent_infoP, paramsP, jobNoP, is_skeleton, Number(key), null, node, null, $root_container);
12280
+ // $elm.data().xuTeleportData.push($teleport_elm.attr('xu-ui-id'));
12281
+ // $teleport_elm.attr('xu-teleport-parent-id', $elm.attr('xu-ui-id'));
12282
+ // }
12283
+ // }
12284
+ }
12285
+ return {};
12286
+ },
12287
+ 'xu-render': async function ($elm, val) {
12288
+ let ret = await common_fx['xu-render']($elm, val, true);
12289
+ return ret;
12290
+ },
12291
+ 'xu-show': async function ($elm, val) {
12292
+ let ret = await common_fx['xu-show']($elm, val, true);
12293
+ return ret;
12294
+ },
12295
+ },
12296
+ };
12297
+
12298
+ const load_cdn = async function (resource) {
12299
+ // console.log("loading cdn", resource);
12300
+ if (!_.isObject(resource) && _.isString(resource)) {
12301
+ resource = { src: resource, type: 'js' };
12302
+ }
12303
+ if (!_.isObject(resource)) {
12304
+ throw new Error('cdn resource in wrong format');
12305
+ }
12306
+ return new Promise(async (resolve) => {
12307
+ try {
12308
+ switch (resource.type) {
12309
+ case 'js':
12310
+ await func.utils.load_js_on_demand(resource.src);
12311
+ break;
12312
+ case 'css':
12313
+ await func.utils.load_js_on_demand(resource.src);
12314
+ break;
12315
+ case 'module':
12316
+ func.utils.load_js_on_demand(resource.src, 'module');
12317
+ break;
12318
+
12319
+ default:
12320
+ await func.utils.load_js_on_demand(resource.src);
12321
+ break;
12322
+ }
12323
+ resolve();
12324
+ } catch (error) {
12325
+ func.utils.debug_report(SESSION_ID, 'xu-cdn', 'Fail to load: ' + resource, 'W');
12326
+ resolve();
12327
+ }
12328
+
12329
+ // if (resource.type === "js" || !resource.type) {
12330
+ // await func.utils.load_js_on_demand(resource.src);
12331
+ // return resolve();
12332
+ // }
12333
+ // if (resource.type === "css") {
12334
+ // func.utils.load_css_on_demand(resource.src);
12335
+ // return resolve();
12336
+ // }
12337
+ // if (resource.type === "module") {
12338
+ // func.utils.load_js_on_demand(resource.src, "module");
12339
+ // return resolve();
12340
+ // }
12341
+ // func.utils.debug_report(
12342
+ // SESSION_ID,
12343
+ // "xu-cdn",
12344
+ // "Fail to load: " + resource,
12345
+ // "W"
12346
+ // );
12347
+ // return resolve();
12348
+ });
12349
+ };
12350
+
12351
+ const common_fx = {
12352
+ 'xu-attrs': async function ($elm, val) {
12353
+ if (!val.value) return {};
12354
+ if (!_.isObject(val.value)) throw 'xu-attrs value us not an object';
12355
+ for (const [attr_key, attr_val] of Object.entries(val.value)) {
12356
+ nodeP.attributes[attr_key] = attr_val;
12357
+ }
12358
+
12359
+ return {};
12360
+ },
12361
+ 'xu-ref': async function ($elm, val, dsSession) {
12362
+ if (!val.value) return {};
12363
+
12364
+ func.UI.update_xu_ref(SESSION_ID, dsSession || paramsP.dsSessionP, val.value, $elm);
12365
+
12366
+ // Select the node that will be observed for mutations
12367
+ const targetNode = $elm[0];
12368
+
12369
+ if (!targetNode) return;
12370
+
12371
+ // Options for the observer (which mutations to observe)
12372
+ const config = { attributes: true, childList: true, subtree: true };
12373
+
12374
+ // Callback function to execute when mutations are observed
12375
+ const callback = (mutationList, observer) => {
12376
+ func.UI.screen.refresh_xu_attributes(SESSION_ID, [val.value]);
12377
+ };
12378
+
12379
+ // Create an observer instance linked to the callback function
12380
+ const observer = new MutationObserver(callback);
12381
+
12382
+ // Start observing the target node for configured mutations
12383
+ observer.observe(targetNode, config);
12384
+
12385
+ // Later, you can stop observing
12386
+ // observer.disconnect();
12387
+
12388
+ return {};
12389
+ },
12390
+ 'xu-bind': async function ($elm, val) {
12391
+ if (is_skeleton) return;
12392
+
12393
+ let val_is_reference_field = false;
12394
+
12395
+ let _prog_id = $elm.data().xuData.paramsP.prog_id;
12396
+ let _dsP = $elm.data().xuData.paramsP.dsSessionP;
12397
+ const view_ret = await func.utils.VIEWS_OBJ.get(SESSION_ID, _prog_id);
12398
+ if (!view_ret) return {};
12399
+
12400
+ let is_dynamic_field = false;
12401
+ let field_prop;
12402
+ let bind_field_id;
12403
+
12404
+ const input_field_type = $elm.attr('type');
12405
+
12406
+ const get_bind_field = async function (field_id) {
12407
+ if (['_FOR_VAL', '_FOR_KEY'].includes(field_id)) {
12408
+ is_dynamic_field = true;
12409
+ field_prop = _ds.dynamic_fields[field_id];
12410
+ } else {
12411
+ field_prop = func.common.find_item_by_key(view_ret.progFields, 'field_id', field_id);
12412
+ if (!field_prop) {
12413
+ /// find the field everywhere in the chain Aug 30 2024
12414
+ const ret_get_value = await func.datasource.get_value(SESSION_ID, field_id, _dsP);
12415
+
12416
+ if (ret_get_value.found) {
12417
+ _dsP = ret_get_value.dsSessionP;
12418
+ let _ds = SESSION_OBJ[SESSION_ID].DS_GLB[_dsP];
12419
+ _prog_id = _ds.prog_id;
12420
+ const view_ret = await func.utils.VIEWS_OBJ.get(SESSION_ID, _prog_id);
12421
+ if (!view_ret) return {};
12422
+ field_prop = func.common.find_item_by_key(view_ret.progFields, 'field_id', field_id);
12423
+ }
12424
+ if (!field_prop) {
12425
+ throw `field ${field_id} not found in the program scope`;
12426
+ }
12427
+ }
12428
+ }
12429
+ return field_id;
12430
+ };
12431
+
12432
+ try {
12433
+ bind_field_id = await get_bind_field(val.value.split('.')[0]);
12434
+ val_is_reference_field = true;
12435
+ } catch (err) {
12436
+ console.error(err?.message || err);
12437
+ return {};
12438
+ }
12439
+
12440
+ const field_changed = async function (e) {
12441
+ var _ds = SESSION_OBJ[SESSION_ID].DS_GLB[_dsP];
12442
+
12443
+ // update array for checkbox that not in xu-for
12444
+ if (field_prop.props.fieldType === 'array' && input_field_type === 'checkbox' && val_is_reference_field) {
12445
+ let arr_value_before_cast = _.clone((await func.datasource.get_value(SESSION_ID, bind_field_id, _dsP, _ds.currentRecordId)).ret.value);
12446
+ let value_from_getter = bind.getter($elm[0]);
12447
+ let value;
12448
+ if (arr_value_before_cast.includes(value_from_getter)) {
12449
+ value = arr_value_before_cast.filter((item) => !_.isEqual(item, value_from_getter));
12450
+ } else {
12451
+ arr_value_before_cast.push(value_from_getter);
12452
+ value = arr_value_before_cast;
12453
+ }
12454
+
12455
+ let datasource_changes = {
12456
+ [_dsP]: {
12457
+ [_ds.currentRecordId]: {
12458
+ [bind_field_id]: value,
12459
+ },
12460
+ },
12461
+ };
12462
+
12463
+ return await func.datasource.update(SESSION_ID, datasource_changes);
12464
+ }
12465
+
12466
+ // update array for radio that not in xu-for
12467
+ if (field_prop.props.fieldType === 'array' && input_field_type === 'radio' && val_is_reference_field) {
12468
+ let value_from_getter = bind.getter($elm[0]);
12469
+
12470
+ let datasource_changes = {
12471
+ [_dsP]: {
12472
+ [_ds.currentRecordId]: {
12473
+ [bind_field_id]: [value_from_getter],
12474
+ },
12475
+ },
12476
+ };
12477
+
12478
+ return await func.datasource.update(SESSION_ID, datasource_changes);
12479
+ }
12480
+
12481
+ var value = await func.common.get_cast_val(SESSION_ID, 'xu-bind', 'value', field_prop.props.fieldType, bind.getter($elm[0]));
12482
+
12483
+ if (field_prop.props.fieldType === 'object') {
12484
+ value = await func.common.get_cast_val(SESSION_ID, 'xu-bind', 'value', input_field_type, bind.getter($elm[0]));
12485
+ }
12486
+
12487
+ if (!_ds.currentRecordId) return;
12488
+
12489
+ let datasource_changes = {
12490
+ [_dsP]: {
12491
+ [_ds.currentRecordId]: {
12492
+ [bind_field_id]: value,
12493
+ },
12494
+ },
12495
+ };
12496
+
12497
+ await func.datasource.update(SESSION_ID, datasource_changes);
12498
+ const iterate_info = $elm?.data()?.xuData?.iterate_info;
12499
+ const reference_source_obj = iterate_info?.reference_source_obj;
12500
+ if (reference_source_obj) {
12501
+ if (reference_source_obj.ret.type === 'array') {
12502
+ if (iterate_info.iterator_val === bind_field_id) {
12503
+ const arr_idx = Number($elm?.data()?.xuData?.iterate_info._key);
12504
+
12505
+ const dataset_arr = await func.datasource.get_value(SESSION_ID, reference_source_obj.fieldIdP, _dsP, reference_source_obj.currentRecordId);
12506
+ let new_arr = _.cloneDeep(dataset_arr.ret.value);
12507
+
12508
+ if (field_prop.props.fieldType === 'object' && val_is_reference_field) {
12509
+ let obj_item = new_arr[arr_idx];
12510
+
12511
+ let e_exp = val.value.replace(bind_field_id, 'obj_item');
12512
+
12513
+ let new_val = eval(e_exp + (input_field_type === 'string' ? `="${value}"` : `=${value}`));
12514
+
12515
+ new_arr[arr_idx] = obj_item;
12516
+ } else {
12517
+ new_arr[arr_idx] = value;
12518
+ }
12519
+ // datasource_changes[_dsP][_ds.currentRecordId][reference_source_obj.fieldIdP] = new_arr;
12520
+
12521
+ let datasource_changes = {
12522
+ [_dsP]: {
12523
+ [_ds.currentRecordId]: {
12524
+ [reference_source_obj.fieldIdP]: new_arr,
12525
+ },
12526
+ },
12527
+ };
12528
+
12529
+ await func.datasource.update(SESSION_ID, datasource_changes, null, true);
12530
+ }
12531
+ }
12532
+ }
12533
+
12534
+ await func.datasource.update_changes_for_out_parameter(SESSION_ID, _dsP, _ds.parentDataSourceNo);
12535
+ };
12536
+
12537
+ const bind = new UI_FRAMEWORK_PLUGIN.bind();
12538
+
12539
+ bind.listener($elm[0], field_changed);
12540
+
12541
+ const set_value = function () {
12542
+ let _ds = SESSION_OBJ[SESSION_ID].DS_GLB[paramsP.dsSessionP];
12543
+ if (!_ds.currentRecordId) return;
12544
+ let value;
12545
+ try {
12546
+ if (val_is_reference_field) {
12547
+ if (is_dynamic_field) {
12548
+ value = _ds.dynamic_fields[bind_field_id].value;
12549
+ } else {
12550
+ const row_idx = func.common.find_ROWID_idx(_ds, _ds.currentRecordId);
12551
+ value = _ds.data_feed.rows?.[row_idx]?.[bind_field_id];
12552
+ }
12553
+ if (field_prop.props.fieldType === 'array' && $elm.attr('type') === 'checkbox' && $elm.attr('value')) {
12554
+ if (value.includes($elm.attr('value'))) {
12555
+ value = true;
12556
+ } else {
12557
+ value = false;
12558
+ }
12559
+ } else if (field_prop.props.fieldType === 'array' && $elm.attr('type') === 'radio' && $elm.attr('value')) {
12560
+ if (value.includes($elm.attr('value'))) {
12561
+ value = $elm.attr('value');
12562
+ } else {
12563
+ value = false;
12564
+ }
12565
+ } else if (field_prop.props.fieldType === 'object' && val.value.split('.').length > 1) {
12566
+ let str = val.value.replace(bind_field_id, '(' + JSON.stringify(value) + ')');
12567
+ value = eval(str);
12568
+ }
12569
+ } else {
12570
+ value = val.value;
12571
+ }
12572
+ if (typeof value === 'undefined') return;
12573
+ bind.setter($elm[0], value);
12574
+ } catch (err) {
12575
+ console.error(err);
12576
+ }
12577
+ };
12578
+ /// init value from ds
12579
+ $('body').on('xu-bind-refresh.' + _ds.dsSession.toString(), () => {
12580
+ set_value();
12581
+ });
12582
+
12583
+ set_value();
12584
+ return {};
12585
+ },
12586
+ 'xu-render': async function ($elm, val, from_panel) {
12587
+ const old_render = async function () {
12588
+ var value = await func.common.get_cast_val(SESSION_ID, 'common fx', 'xu-render', 'bool', val.value);
12589
+ const init_render = function () {
12590
+ if (!value) {
12591
+ var cloned_$div = $elm.clone(true);
12592
+
12593
+ let $xurender = $('<xurender>').attr('xu-ui-id', $elm.attr('xu-ui-id')).attr('hidden', true).appendTo($container); //.hide();
12594
+ let original_data_obj = {
12595
+ $container: cloned_$div,
12596
+ nodeP: _.cloneDeep(nodeP),
12597
+ parent_infoP,
12598
+ paramsP,
12599
+ keyP,
12600
+ parent_nodeP,
12601
+ $root_container,
12602
+ };
12603
+ $xurender.data('xuData', cloned_$div.data().xuData);
12604
+ $xurender.data().xuData.original_data_obj = original_data_obj;
12605
+ $xurender.data().xuData.xurender_node = cloned_$div;
12606
+ $xurender.data().xuAttributes = nodeP.attributes || {};
12607
+ // $xurender.hide();
12608
+
12609
+ $elm.remove();
12610
+ return { abort: true };
12611
+ }
12612
+ return {};
12613
+ };
12614
+
12615
+ const post_render = async function () {
12616
+ if (value) {
12617
+ try {
12618
+ // abort if already rended
12619
+ if ($elm[0].tagName !== 'XURENDER' && $elm?.length) {
12620
+ return func.events.delete_job(SESSION_ID, jobNoP);
12621
+ }
12622
+
12623
+ let original_data_obj = $elm.data().xuData.original_data_obj;
12624
+
12625
+ if (!original_data_obj) {
12626
+ func.events.delete_job(SESSION_ID, jobNoP);
12627
+ return { delete_job: jobNoP };
12628
+ }
12629
+
12630
+ const new_$div = await func.UI.screen.render_ui_tree(
12631
+ SESSION_ID,
12632
+ $elm, //original_data_obj.$container,
12633
+ _.cloneDeep(original_data_obj.nodeP),
12634
+ original_data_obj.parent_infoP,
12635
+ original_data_obj.paramsP,
12636
+ jobNoP,
12637
+ null,
12638
+ original_data_obj.keyP,
12639
+ null,
12640
+ original_data_obj.parent_nodeP,
12641
+ null,
12642
+ original_data_obj.$root_container,
12643
+ );
12644
+
12645
+ new_$div.data().xuData.original_data_obj = original_data_obj;
12646
+ new_$div.data().xuData.xurender_node = $elm.clone(true);
12647
+ new_$div.data().xuAttributes = $elm.data().xuAttributes || {};
12648
+
12649
+ const replace = async function () {
12650
+ $elm.replaceWith(new_$div);
12651
+ if (from_panel) {
12652
+ const xuPanelWrapper = _.clone(new_$div.data().xuPanelWrapper);
12653
+ $elm.parent().data().xuPanelWrapper = xuPanelWrapper;
12654
+ $elm.replaceWith(new_$div.children());
12655
+ }
12656
+
12657
+ if (val.fields_arr) {
12658
+ return await func.UI.screen.refresh_xu_attributes(SESSION_ID, val.fields_arr, val.jobNoP, new_$div);
12659
+ }
12660
+ func.events.delete_job(SESSION_ID, jobNoP);
12661
+ };
12662
+ // if ($elm && func.UI.utils.find_in_element_data('xuData', $(SESSION_OBJ[SESSION_ID].root_element), 'xu_id', $elm.data().xuData.xu_id).length) {
12663
+ if ($elm && $(`[xu-ui-id="${$elm.attr('xu-ui-id')}"]`).length) {
12664
+ if (new_$div.data().xuData.paramsP) {
12665
+ return await replace();
12666
+ }
12667
+
12668
+ func.events.delete_job(SESSION_ID, jobNoP);
12669
+ }
12670
+ } catch (error) {
12671
+ func.events.delete_job(SESSION_ID, jobNoP);
12672
+ }
12673
+ return;
12674
+ }
12675
+ // if (!value) {
12676
+ if ($elm.prop('tagName') === 'XURENDER') {
12677
+ func.events.delete_job(SESSION_ID, jobNoP);
12678
+ return;
12679
+ }
12680
+
12681
+ let tmp_$div = $('<div>');
12682
+
12683
+ let $xurender = $('<xurender>').attr('xu-ui-id', $elm.attr('xu-ui-id')).appendTo(tmp_$div); //.hide();
12684
+ // was true before
12685
+ if ($elm.data().xuData.xurender_node) {
12686
+ $xurender.data({
12687
+ xuAttributes: $elm.data().xuData.xurender_node.data().xuAttributes || {},
12688
+ xuData: $elm.data().xuData.xurender_node.data().xuData || {},
12689
+ });
12690
+ } else {
12691
+ // default new state
12692
+
12693
+ $xurender.data({
12694
+ xuAttributes: $elm.data().xuAttributes || {},
12695
+ xuData: $elm.data().xuData || {},
12696
+ });
12697
+ const original_data_obj = {
12698
+ nodeP: _.cloneDeep($elm.data().xuData.node_org),
12699
+ paramsP: $elm.data().xuData.paramsP,
12700
+ $container: $elm.clone(true),
12701
+ parent_infoP: parent_infoP,
12702
+ };
12703
+
12704
+ $xurender.data().xuData.original_data_obj = original_data_obj;
12705
+ }
12706
+
12707
+ //remove xu-teleport trace
12708
+ $.each($elm.find('xu-teleport'), (key, val) => {
12709
+ const xuTeleportData = $(val).data().xuTeleportData || [];
12710
+ for (const teleported_elm_id of xuTeleportData) {
12711
+ $(`[xu-ui-id="${teleported_elm_id}"]`).remove();
12712
+ }
12713
+ });
12714
+
12715
+ $elm.replaceWith(tmp_$div.children());
12716
+ func.events.delete_job(SESSION_ID, jobNoP);
12717
+ // }
12718
+ };
12719
+ if (is_init) {
12720
+ return init_render();
12721
+ }
12722
+ return await post_render();
12723
+ };
12724
+
12725
+ const new_render = async function () {
12726
+ var value = await func.common.get_cast_val(SESSION_ID, 'common fx', 'xu-render', 'bool', val.value);
12727
+ const has_xu_render_attribute = true;
12728
+ const has_xu_exp_render_attribute = $elm.data()?.xuData?.attr_exp_info?.['xu-render'] ? true : false;
12729
+ const init_render = async function () {
12730
+ nodeP.xu_render_made = value;
12731
+ if (!value) {
12732
+ if (has_xu_exp_render_attribute) {
12733
+ return { has_xu_exp_render_attribute, has_xu_render_attribute, xu_render_background_processing: true };
12734
+ }
12735
+ return { has_xu_render_attribute, abort: true };
12736
+ }
12737
+ return { has_xu_exp_render_attribute, has_xu_render_attribute };
12738
+ };
12739
+
12740
+ const post_render = async function () {
12741
+ // always come from refresh
12742
+ let nodeP = $container.data().xuData.node.children[keyP];
12743
+ nodeP.xu_render_made = value;
12744
+ if (value) {
12745
+ try {
12746
+ const xu_render_cache_id = await get_xu_render_cache_str(SESSION_ID, paramsP.dsSessionP, Object.keys($elm.data()?.xuData?.attr_exp_info?.['xu-render']?.fields || {}));
12747
+ const xu_ui_id = $elm.attr('xu-ui-id');
12748
+ let new_$div = UI_WORKER_OBJ?.xu_render_cache?.[xu_ui_id + xu_render_cache_id]?.$div.clone(true);
12749
+ let found_parent_vars = false;
12750
+ if (new_$div) {
12751
+ // validate if $div contains fields from parent ds
12752
+ const parent_data = get_parent_ds_fields(SESSION_ID, paramsP.dsSessionP);
12753
+ const parent_fields = Object.keys(parent_data);
12754
+
12755
+ $.each(new_$div.find('*'), (key, val) => {
12756
+ const _xuAttributes = $(val)?.data()?.xuAttributes;
12757
+ if (found_parent_vars || !_xuAttributes) return;
12758
+ for (const [attr_key, attr_val] of Object.entries(_xuAttributes)) {
12759
+ if (found_parent_vars) break;
12760
+ for (const [key, val] of Object.entries(parent_fields)) {
12761
+ if (attr_val.includes('@' + key)) {
12762
+ found_parent_vars = true;
12763
+ break;
12764
+ }
12765
+ }
12766
+ }
12767
+ });
12768
+ }
12769
+
12770
+ if (!new_$div || found_parent_vars) {
12771
+ UI_WORKER_OBJ.xu_render_cache[xu_ui_id + xu_render_cache_id] = { paramsP };
12772
+ nodeP.xu_render_xu_ui_id = xu_ui_id;
12773
+ nodeP.xu_render_cache_id = xu_render_cache_id;
12774
+ new_$div = await func.UI.screen.render_ui_tree(SESSION_ID, $container, nodeP, parent_infoP, paramsP, jobNoP, null, keyP, null, parent_nodeP, null, $root_container);
12775
+ const _$div = new_$div.clone(true);
12776
+ UI_WORKER_OBJ.xu_render_cache[xu_ui_id + xu_render_cache_id].$div = _$div;
12777
+ UI_WORKER_OBJ.xu_render_cache[xu_ui_id + xu_render_cache_id].data = _$div.data();
12778
+ }
12779
+ // append order handling
12780
+
12781
+ if (!$container.children().length) {
12782
+ new_$div.appendTo($container);
12783
+ } else {
12784
+ // iterate the container node
12785
+ let $last_elm_found = [];
12786
+ $.each($container.data().xuData.node.children, (item_key, item_val) => {
12787
+ // const $elm = $(`[xu-node-id="${item_val.id}"]`);
12788
+ const $elm = func.UI.utils.find_in_element_data('xuData', $(SESSION_OBJ[SESSION_ID].root_element), 'nodeid', item_val.id);
12789
+ if ($elm.length) {
12790
+ $last_elm_found = $elm;
12791
+ }
12792
+ if (keyP == item_key) {
12793
+ if ($last_elm_found.length) {
12794
+ new_$div.after($last_elm_found);
12795
+ } else {
12796
+ $container.prepend(new_$div);
12797
+ }
12798
+ }
12799
+ });
12800
+ }
12801
+ } catch (error) {
12802
+ func.events.delete_job(SESSION_ID, jobNoP);
12803
+ }
12804
+ return;
12805
+ }
12806
+
12807
+ /////////// !value ///////////
12808
+
12809
+ const xu_ui_id = $elm.attr('xu-ui-id');
12810
+
12811
+ const cache_str = await get_xu_render_cache_str(SESSION_ID, paramsP.dsSessionP, Object.keys($elm.data()?.xuData?.attr_exp_info?.['xu-render']?.fields || {}));
12812
+ const _$div = $elm.clone(true);
12813
+ UI_WORKER_OBJ.xu_render_cache[xu_ui_id + cache_str] = { $div: _$div, data: _$div.data(), paramsP };
12814
+ $elm.remove();
12815
+ func.events.delete_job(SESSION_ID, jobNoP);
12816
+ };
12817
+ if (is_init) {
12818
+ return await init_render();
12819
+ }
12820
+ return await post_render();
12821
+ };
12822
+
12823
+ if (glb.new_xu_render) {
12824
+ return new_render();
12825
+ }
12826
+ return old_render();
12827
+ },
12828
+ 'xu-show': async function ($elm, val) {
12829
+ var value = await func.common.get_cast_val(SESSION_ID, 'common fx', 'xu-show', 'bool', val.value);
12830
+ if (value) {
12831
+ $elm.show();
12832
+ }
12833
+ if (!value) {
12834
+ $elm.hide();
12835
+ }
12836
+ return {};
12837
+ },
12838
+ 'xu-content': async function ($elm, val) {
12839
+ try {
12840
+ $elm.html(val.value);
12841
+ } catch (error) {
12842
+ console.warn(e);
12843
+ }
12844
+ return;
12845
+ },
12846
+ 'xu-text': async function ($elm, val) {
12847
+ try {
12848
+ $elm.text(val.value);
12849
+ } catch (error) {
12850
+ console.warn(e);
12851
+ }
12852
+ return;
12853
+ },
12854
+ 'xu-html': async function ($elm, val) {
12855
+ try {
12856
+ $elm.html(val.value);
12857
+ } catch (error) {
12858
+ console.warn(e);
12859
+ }
12860
+ return;
12861
+ },
12862
+ 'xu-for': async function ($elm, data) {
12863
+ // exit if call from rendered xu-for item to prevent infante loop (parent_infoP?.iterate_info indicate call from rendered item)
12864
+ if (parent_infoP?.iterate_info) return {};
12865
+ if (!data.value) return {};
12866
+ try {
12867
+ // data.value (xu-for) can store actual values such as an array, a CSV, or a field_id that references a specific field within the dataset, initialized with values for the iteration.
12868
+ let arr = data.value;
12869
+
12870
+ // find reference source field
12871
+ let reference_source_obj;
12872
+
12873
+ const _progFields = await func.datasource.get_progFields(SESSION_ID, paramsP.dsSessionP);
12874
+
12875
+ let view_field_obj = func.common.find_item_by_key(_progFields, 'field_id', data.value);
12876
+ // detect if data.value (xu-for) is reference field_id by checking if exist in the dataset
12877
+ if (view_field_obj) {
12878
+ // xu-for is reference field_id
12879
+ reference_source_obj = await func.datasource.get_value(SESSION_ID, data.value, paramsP.dsSessionP);
12880
+ arr = reference_source_obj?.ret?.value;
12881
+ } else {
12882
+ // xu-for is actual data
12883
+ if (typeof data.value === 'string') {
12884
+ arr = eval(data.value.replaceAll('\\', ''));
12885
+ }
12886
+ if (typeof arr === 'number') {
12887
+ arr = Array.from(Array(arr).keys());
12888
+ }
12889
+ }
12890
+
12891
+ const custom_iterator_key = $elm.data().xuData.iterator_key;
12892
+ const custom_iterator_val = $elm.data().xuData.iterator_val;
12893
+
12894
+ let iterator_key = custom_iterator_key;
12895
+ let iterator_val = custom_iterator_val;
12896
+ let is_key_dynamic_field, is_val_dynamic_field;
12897
+
12898
+ // custom FOR_VAL name or namespaced default name
12899
+ if (!custom_iterator_key) {
12900
+ is_key_dynamic_field = true;
12901
+
12902
+ iterator_key = '_FOR_KEY';
12903
+ }
12904
+
12905
+ if (!custom_iterator_val) {
12906
+ is_val_dynamic_field = true;
12907
+
12908
+ iterator_val = '_FOR_VAL';
12909
+ }
12910
+
12911
+ var i = 0;
12912
+ for await (let [_key, _val] of Object.entries(arr)) {
12913
+ if (_.isArray(arr)) {
12914
+ _key = Number(_key);
12915
+ }
12916
+
12917
+ const set_value = async function (is_dynamic_field, currentRecordId, field_id, value) {
12918
+ if (is_dynamic_field) {
12919
+ func.datasource.add_dynamic_field_to_ds(SESSION_ID, paramsP.dsSessionP, field_id, value);
12920
+ } else {
12921
+ const _progFields = await func.datasource.get_progFields(SESSION_ID, paramsP.dsSessionP);
12922
+
12923
+ let view_field_obj = func.common.find_item_by_key(_progFields, 'field_id', field_id);
12924
+ if (view_field_obj) {
12925
+ let _ds = SESSION_OBJ[SESSION_ID].DS_GLB[paramsP.dsSessionP];
12926
+ try {
12927
+ const row_idx = func.common.find_ROWID_idx(_ds, currentRecordId);
12928
+ _ds.data_feed.rows[row_idx][field_id] = value;
12929
+ } catch (err) {
12930
+ console.error(err);
12931
+ }
12932
+ } else {
12933
+ console.error('field not exist in dataset for xu-for method');
12934
+ }
12935
+ }
12936
+ };
12937
+
12938
+ var currentRecordId = SESSION_OBJ[SESSION_ID].DS_GLB[paramsP.dsSessionP].currentRecordId.toString();
12939
+
12940
+ await set_value(is_key_dynamic_field, currentRecordId, iterator_key, _key);
12941
+ await set_value(is_val_dynamic_field, currentRecordId, iterator_val, _val);
12942
+
12943
+ var iterate_info = {
12944
+ _val,
12945
+ _key,
12946
+ iterator_key,
12947
+ iterator_val,
12948
+ is_key_dynamic_field,
12949
+ is_val_dynamic_field,
12950
+ reference_source_obj,
12951
+ };
12952
+ let _parent_info = _.cloneDeep(parent_infoP) || {};
12953
+ _parent_info.iterate_info = iterate_info;
12954
+
12955
+ const $divP = await func.UI.screen.render_ui_tree(
12956
+ SESSION_ID,
12957
+ $container,
12958
+ nodeP,
12959
+ _parent_info, //parent_infoP ? _.cloneDeep(_parent_info) : null,
12960
+ paramsP,
12961
+ jobNoP,
12962
+ null,
12963
+ i,
12964
+ null,
12965
+ nodeP,
12966
+ null,
12967
+ $root_container,
12968
+ );
12969
+
12970
+ $.each($divP.children(), function (key, val) {
12971
+ if ($(val)?.data()?.xuData) {
12972
+ $(val).data().xuData.iterate_info = iterate_info;
12973
+ }
12974
+ });
12975
+
12976
+ i++;
12977
+ }
12978
+ $elm.remove();
12979
+ return { abort: true };
12980
+ } catch (e) {
12981
+ console.error(' Iterator Arr parse error');
12982
+ return { abort: true };
12983
+ }
12984
+ },
12985
+ 'xu-for-key': async function ($elm, val) {
12986
+ $elm.data().xuData.iterator_key = val.value;
12987
+ return {};
12988
+ },
12989
+ 'xu-for-val': async function ($elm, val) {
12990
+ $elm.data().xuData.iterator_val = val.value;
12991
+ return {};
12992
+ },
12993
+ 'xu-class': async function ($elm, val) {
12994
+ try {
12995
+ const classes_string = val.value;
12996
+ // let obj = _.isString(classes_string) ? JSON.parse(classes_string) : _.defaults(classes_string, {});
12997
+
12998
+ const classes_obj = _.isString(classes_string) ? JSON.parse(classes_string) : _.defaults(classes_string, {});
12999
+ for await (const [cla, cond] of Object.entries(classes_obj)) {
13000
+ let res = await func.expression.get(
13001
+ SESSION_ID,
13002
+ cond,
13003
+ paramsP.dsSessionP,
13004
+ 'UI Attr EXP',
13005
+ $elm.data().xuData.currentRecordId, // SESSION_OBJ[SESSION_ID].DS_GLB[paramsP.dsSessionP].currentRecordId
13006
+ null,
13007
+ null,
13008
+ null,
13009
+ null,
13010
+ null,
13011
+ $elm.data().xuData.iterate_info,
13012
+ );
13013
+
13014
+ if (res.result) {
13015
+ $elm.addClass(cla);
13016
+ } else {
13017
+ $elm.removeClass(cla);
13018
+ }
13019
+
13020
+ $elm.data().xuData.debug_info.attribute_stat['xu-class'] = $elm.attr('class');
13021
+ }
13022
+ return {};
13023
+ } catch (e) {
13024
+ console.warn('parse error:' + val.value);
13025
+ return { abort: true };
13026
+ }
13027
+ },
13028
+ 'xu-exp': async function ($elm, val) {
13029
+ let exp = val.value === null ? true : val.value;
13030
+
13031
+ let exp_ret = await func.expression.get(SESSION_ID, exp, paramsP.dsSessionP, 'UI Attr EXP', SESSION_OBJ[SESSION_ID].DS_GLB[paramsP.dsSessionP].currentRecordId, null, null, null, null, null, $elm.data().xuData.iterate_info);
13032
+
13033
+ let value = func.UI.screen.fix_val_defaults(val.key, exp_ret.result);
13034
+
13035
+ var new_val = {
13036
+ key: val.key,
13037
+ value,
13038
+ };
13039
+
13040
+ if (nodeP.tagName.substr(0, 3) === 'xu-') {
13041
+ if (tag_fx[nodeP.tagName][new_val.key]) {
13042
+ return await tag_fx[nodeP.tagName][new_val.key]($elm, new_val);
13043
+ }
13044
+ console.warn(`attribute ${new_val.key} not found for ${nodeP.tagName}`);
13045
+ return {};
13046
+ }
13047
+ if (!$elm.data().xuData) return;
13048
+
13049
+ $elm.data().xuData.debug_info.attribute_stat[new_val.key] = new_val.value;
13050
+
13051
+ // IGNORE UNDEFINED or NULL ATTRIBUTES
13052
+ if (typeof new_val.value === 'undefined' || new_val.value === null) {
13053
+ return {};
13054
+ }
13055
+
13056
+ // IGNORE ATTRIBUTES WITH EMPTY VALUES
13057
+ if (glb.solid_attributes.includes(new_val.key) && !new_val.value) {
13058
+ return {};
13059
+ }
13060
+
13061
+ if (new_val.key.substr(0, 2) === 'xu') {
13062
+ return await common_fx[new_val.key]($elm, new_val);
13063
+ }
13064
+
13065
+ $elm.attr(new_val.key, ($elm.attr(new_val.key) || '') + new_val.value);
13066
+ return {};
13067
+ },
13068
+ 'xu-on': async function ($elm, val) {
13069
+ CLIENT_ACTIVITY_TS = Date.now();
13070
+ const trigger = val.key.split('xu-on:')[1].toLowerCase();
13071
+ $elm.on(trigger, async function (evt) {
13072
+ const _$elm = $(evt.currentTarget);
13073
+ if (_.isEmpty(_$elm.data().xuAttributes)) return;
13074
+
13075
+ for await (const [key, val] of Object.entries(_$elm.data().xuAttributes['xu-on:' + evt.type])) {
13076
+ if (!_.isEmpty(val.props.condition)) {
13077
+ const expCond = await func.expression.get(SESSION_ID, val.props.condition, paramsP.dsSessionP, 'condition', paramsP.recordid); // execute expression
13078
+ if (!expCond.result) continue;
13079
+ }
13080
+
13081
+ if (val.event_modifiers && evt[val.event_modifiers]) {
13082
+ evt[val.event_modifiers]();
13083
+ }
13084
+
13085
+ // if (val.handler === 'custom') {
13086
+ if (val.workflow) {
13087
+ // do BL
13088
+ for await (const [key2, val2] of Object.entries(val.workflow)) {
13089
+ // var cond = val2.data.enabled;
13090
+ // if (val2.data.enabled && val2.props.condition) {
13091
+ // expCond = await func.expression.get(SESSION_ID, val2.props.condition, paramsP.dsSessionP, 'condition', paramsP.recordid); // execute expression
13092
+ // cond = expCond.result;
13093
+ // }
13094
+ // if (!cond) continue;
13095
+
13096
+ if (!val2.data.enabled) continue; // added Jul 3, 25 - condition validate on execution
13097
+
13098
+ func.events.add_to_queue(SESSION_ID, 'element event', val2.id, evt.type, val2.data.action, val2.data.name, null, _$elm.attr('xu-ui-id'), null, evt, null, null, null, paramsP.dsSessionP, null, null, null, evt.type, val2.data.name, null, null, val2, null, null, null, null, null, null);
13099
+ }
13100
+ }
13101
+ }
13102
+ });
13103
+ return {};
13104
+ },
13105
+ 'xu-script': async function ($elm, val) {
13106
+ var checkExist = setInterval(function () {
13107
+ if ($elm.is(':visible')) {
13108
+ try {
13109
+ // var res = eval('(' + val.value + ')');
13110
+ const fn = `(function(el, evt) {
13111
+ ${val.value}
13112
+ })(document.querySelector(\`[xu-ui-id="${$elm.attr('xu-ui-id')}"]\`),evt);`;
13113
+
13114
+ var res = eval(fn);
13115
+ // if (typeof res === 'function') {
13116
+ // res($elm[0]);
13117
+ // }
13118
+ } catch (e) {
13119
+ eval(val.value);
13120
+ }
13121
+
13122
+ clearInterval(checkExist);
13123
+ }
13124
+ }, 100); // check every 100ms
13125
+
13126
+ return {};
13127
+ },
13128
+ 'xu-style-global': async function ($elm, val) {
13129
+ $('head').append(`<style>${val.value}</style>`);
13130
+ return {};
13131
+ },
13132
+ 'xu-style': async function ($elm, val) {
13133
+ var cssString = val.value;
13134
+
13135
+ var parser = new cssjs();
13136
+
13137
+ var parsed = parser.parseCSS(cssString);
13138
+ var xuUiId = `[xu-ui-id="${$elm.attr('xu-ui-id')}"]`;
13139
+
13140
+ $.each(parsed, function (key, val) {
13141
+ var selectors_arr = val.selector.split(',');
13142
+
13143
+ $.each(selectors_arr, function (key2, val2) {
13144
+ selectors_arr[key2] = `${xuUiId} ${val2}, ${xuUiId}${val2}`;
13145
+ // console.log(new_selector);
13146
+ });
13147
+
13148
+ val.selector = selectors_arr.join(',');
13149
+ // console.log(parsed);
13150
+ });
13151
+
13152
+ var newCSSString = parser.getCSSForEditor(parsed);
13153
+
13154
+ $('head').append(`<style>${newCSSString}</style>`);
13155
+ return {};
13156
+ },
13157
+ 'xu-cdn': async function ($elm, val) {
13158
+ for await (const [key, resource] of Object.entries(val.value)) {
13159
+ await load_cdn(resource);
13160
+ }
13161
+
13162
+ return {};
13163
+ },
13164
+ 'xu-ui-plugin': async function ($elm, val) {
13165
+ var _session = SESSION_OBJ[SESSION_ID];
13166
+
13167
+ for await (const [plugin_name, value] of Object.entries(val.value)) {
13168
+ const _plugin = APP_OBJ[_session.app_id]?.app_plugins_purchased?.[plugin_name];
13169
+ if (_plugin?.installed && _plugin?.manifest?.['runtime.mjs']?.exist && _plugin?.manifest?.['index.mjs']?.exist && value.enabled) {
13170
+ if (_plugin?.manifest?.['runtime.mjs'].dist && _plugin?.manifest?.['runtime.mjs']?.css) {
13171
+ const plugin_runtime_css_url = await func.utils.get_plugin_npm_cdn(SESSION_ID, plugin_name, 'dist/runtime.css');
13172
+ func.utils.load_css_on_demand(plugin_runtime_css_url);
13173
+ }
13174
+
13175
+ const plugin_index_src = await func.utils.get_plugin_npm_cdn(SESSION_ID, plugin_name, `${_plugin.manifest['index.mjs'].dist ? 'dist/' : ''}index.mjs`);
13176
+
13177
+ const plugin_index_resources = await import(plugin_index_src);
13178
+
13179
+ let properties = _.cloneDeep(plugin_index_resources.properties);
13180
+ for await (let [prop_name, prop_val] of Object.entries(properties)) {
13181
+ prop_val.value = value?.attributes?.[prop_name];
13182
+ if (value?.attributes?.[`xu-exp:${prop_name}`]) {
13183
+ const res = await func.expression.get(SESSION_ID, value?.attributes?.[`xu-exp:${prop_name}`], paramsP.dsSessionP, 'UI Attr EXP');
13184
+ prop_val.value = res.result;
13185
+ }
13186
+ }
13187
+ // $elm.data().xu_ui_plugin = { properties };
13188
+ const plugin_runtime_src = await func.utils.get_plugin_npm_cdn(SESSION_ID, plugin_name, `${_plugin.manifest['runtime.mjs'].dist ? 'dist/' : ''}runtime.mjs`);
13189
+
13190
+ const plugin_runtime_resources = await import(plugin_runtime_src);
13191
+
13192
+ if (plugin_runtime_resources.cdn && typeof _.isArray(plugin_runtime_resources.cdn)) {
13193
+ for await (const resource of plugin_runtime_resources.cdn) {
13194
+ await load_cdn(resource);
13195
+ }
13196
+ }
13197
+
13198
+ if (plugin_runtime_resources.fn) {
13199
+ await plugin_runtime_resources.fn(plugin_name, $elm?.[0], properties);
13200
+ // await plugin_runtime_resources.fn(plugin_name, $elm?.[0], $elm.data().xu_ui_plugin.properties);
13201
+ }
13202
+ }
13203
+ }
13204
+
13205
+ return {};
13206
+ },
13207
+ 'xu-store': async function ($elm, val) {
13208
+ try {
13209
+ const fields_obj = JSON5.parse(val.value);
13210
+ for (const [field_id, value] of Object.entries(fields_obj)) {
13211
+ func.datasource.add_dynamic_field_to_ds(SESSION_ID, paramsP.dsSessionP, field_id, value);
13212
+ }
13213
+ } catch (err) {
13214
+ console.error(err);
13215
+ }
13216
+ return {};
13217
+ },
13218
+ 'xu-viewport': async function ($elm, val) {
13219
+ // functionality in draw_html_element
13220
+ return {};
13221
+ },
13222
+ };
13223
+
13224
+ if (nodeP.tagName.substr(0, 3) === 'xu-') {
13225
+ if (xu_func === 'xu-exp') {
13226
+ return common_fx[xu_func]($elm, val);
13227
+ }
13228
+
13229
+ if (tag_fx?.[nodeP.tagName]?.[xu_func]) {
13230
+ let ret = await tag_fx[nodeP.tagName][xu_func]($elm, val);
13231
+ return ret;
13232
+ }
13233
+ // if (xu_func !== "tree_id")
13234
+ console.warn(`attribute ${xu_func} not found for ${nodeP.tagName}`);
13235
+ return {};
13236
+ }
13237
+ if (_.isEmpty($elm.data())) {
13238
+ return {};
13239
+ }
13240
+ if (!$elm.data().xuData.debug_info.attribute_stat) {
13241
+ $elm.data().xuData.debug_info.attribute_stat = {};
13242
+ }
13243
+ if (xu_func !== 'xu-exp') {
13244
+ $elm.data().xuData.debug_info.attribute_stat[xu_func] = val.value;
13245
+ }
13246
+ try {
13247
+ if (!common_fx[xu_func]) {
13248
+ console.warn('invalid xu-tag', xu_func, error);
13249
+ return {};
13250
+ }
13251
+
13252
+ return await common_fx[xu_func]($elm, val);
13253
+ } catch (error) {
13254
+ debugger;
13255
+ }
13256
+ };
13257
+
13258
+ func.UI.screen.fix_val_defaults = function (key, val) {
13259
+ var ret = val;
13260
+ if (
13261
+ key === 'xu-render' &&
13262
+ (typeof val === 'undefined' || val === null || val === '') //|| val === ""
13263
+ ) {
13264
+ // ret = 'Y'; // was ret = "N";
13265
+ ret = true;
13266
+ }
13267
+ if (
13268
+ key === 'xu-show' &&
13269
+ (typeof val === 'undefined' || val === null || val === '') //|| val === ""
13270
+ ) {
13271
+ // ret = 'Y';
13272
+ ret = true;
13273
+ }
13274
+ return ret;
13275
+ };
13276
+
13277
+ func.UI.screen.set_attributes_new = async function (SESSION_ID, is_skeleton, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, $elm, is_init, execute_attributes = [], refreshed_ds) {
13278
+ var done_exp = [];
13279
+
13280
+ const _ds = SESSION_OBJ[SESSION_ID].DS_GLB[paramsP.dsSessionP];
13281
+ if (!_ds) return { abort: true };
13282
+
13283
+ const get_attr_value = async function (key) {
13284
+ let ret = func.UI.screen.fix_val_defaults(key, nodeP.attributes[key]);
13285
+ if (nodeP?.attributes?.hasOwnProperty(`xu-exp:${key}`)) {
13286
+ ret = await get_xuExp(key);
13287
+ }
13288
+ return ret;
13289
+ };
13290
+
13291
+ const get_xuExp = async function (attrib) {
13292
+ if (is_skeleton) return;
13293
+ if (glb.new_xu_render) {
13294
+ let _xuData = $elm.data().xuData;
13295
+ if (!_xuData.attr_exp_info) {
13296
+ _xuData.attr_exp_info = {};
13297
+ }
13298
+ }
13299
+ const attr = `xu-exp:${attrib}`;
13300
+
13301
+ if (!nodeP?.attributes?.hasOwnProperty(attr)) return;
13302
+ // const attr = `xu-exp-${attrib}`;
13303
+ var exp = nodeP.attributes[attr];
13304
+ // if (!value) return func.UI.screen.fix_val_defaults(attrib, exp || nodeP.attributes[attrib]);
13305
+
13306
+ var res = await func.expression.get(SESSION_ID, exp, paramsP.dsSessionP, 'UI Attr EXP', _ds.currentRecordId);
13307
+ if (glb.new_xu_render) {
13308
+ _xuData.attr_exp_info[attrib] = res;
13309
+ }
13310
+ // nodeP.attributes[attr] = value; //{ value: value, res: res };
13311
+ done_exp.push(attr);
13312
+ return res.result; //func.UI.screen.fix_val_defaults(attrib, res.result);
13313
+ };
13314
+
13315
+ var _ret = {};
13316
+ if (nodeP.type !== 'element' || !nodeP.attributes) return _ret;
13317
+
13318
+ for (let [key, val] of Object.entries(nodeP.attributes)) {
13319
+ // REMOVE STATIC ATTRIBUTES IF EXP EXISTS to avoid dup
13320
+ if (key.substring(0, 6) === 'xu-exp') {
13321
+ if (_.isEmpty(val)) {
13322
+ delete nodeP.attributes[key];
13323
+ continue;
13324
+ }
13325
+ const clean_key = key.split(':')[1];
13326
+ if (typeof nodeP.attributes[clean_key] !== 'undefined') {
13327
+ delete nodeP.attributes[clean_key];
13328
+ }
13329
+ }
13330
+ // FIX abbreviations
13331
+ if (glb.attr_abbreviations_arr.includes(key)) {
13332
+ nodeP.attributes[`xu-on:${key.substring(3)}`] = [
13333
+ {
13334
+ handler: 'custom',
13335
+ props: {},
13336
+ event: [
13337
+ {
13338
+ id: Date.now(),
13339
+ data: {
13340
+ action: 'update',
13341
+ name: { value: val },
13342
+ enabled: true,
13343
+ },
13344
+ props: {},
13345
+ },
13346
+ ],
13347
+ },
13348
+ ];
13349
+ delete nodeP.attributes[key];
13350
+ }
13351
+ }
13352
+
13353
+ for (let [key, val] of Object.entries(nodeP.attributes)) {
13354
+ // FIX STATIC DEFAULTS
13355
+ val = func.UI.screen.fix_val_defaults(key, val);
13356
+
13357
+ // REMOVE UNDEFINED or NULL ATTRIBUTES
13358
+ if (typeof val === 'undefined' || val === null) {
13359
+ delete nodeP.attributes[key];
13360
+ }
13361
+
13362
+ // REMOVE ATTRIBUTES WITH EMPTY VALUES
13363
+ if (glb.solid_attributes.includes(key) && !val) {
13364
+ delete nodeP.attributes[key];
13365
+ }
13366
+ }
13367
+
13368
+ // XU-ATTRS
13369
+ if (nodeP?.attributes?.['xu-attrs'] || nodeP?.attributes?.['xu-exp:xu-attrs']) {
13370
+ const attr = 'xu-attrs';
13371
+ let ret = await func.UI.screen.execute_xu_functions(
13372
+ SESSION_ID,
13373
+ is_skeleton,
13374
+ $root_container,
13375
+ nodeP,
13376
+ $container,
13377
+ paramsP,
13378
+ parent_infoP,
13379
+ jobNoP,
13380
+ keyP,
13381
+ parent_nodeP,
13382
+ attr,
13383
+ $elm,
13384
+ {
13385
+ key: attr,
13386
+ value: await get_attr_value(attr),
13387
+ },
13388
+ is_init,
13389
+ );
13390
+ }
13391
+
13392
+ // BEFORE
13393
+ if (!_.isEmpty(nodeP.attributes)) {
13394
+ for await (const [key, attr] of Object.entries(glb.run_xu_before)) {
13395
+ if (_ret.abort || $container?.data()?.xuData?.pending_to_delete) break;
13396
+ if (glb.html5_events_handler.includes(attr) || execute_attributes.includes(attr)) {
13397
+ continue;
13398
+ }
13399
+
13400
+ if (!nodeP?.attributes?.hasOwnProperty(attr) && !nodeP?.attributes?.hasOwnProperty(`xu-exp:${attr}`)) {
13401
+ continue;
13402
+ }
13403
+
13404
+ if (!nodeP.attributes[`xu-exp:${attr}`] && nodeP?.attributes?.hasOwnProperty(attr) && typeof func.UI.screen.fix_val_defaults(attr, nodeP.attributes[attr]) === 'undefined') {
13405
+ continue;
13406
+ }
13407
+
13408
+ let ret = await func.UI.screen.execute_xu_functions(
13409
+ SESSION_ID,
13410
+ is_skeleton,
13411
+ $root_container,
13412
+ nodeP,
13413
+ $container,
13414
+ paramsP,
13415
+ parent_infoP,
13416
+ jobNoP,
13417
+ keyP,
13418
+ parent_nodeP,
13419
+ attr,
13420
+ $elm,
13421
+ {
13422
+ key: attr,
13423
+
13424
+ value: await get_attr_value(attr),
13425
+ },
13426
+ is_init,
13427
+ );
13428
+ _ret = _.assignIn(_ret, ret);
13429
+ }
13430
+ }
13431
+
13432
+ // ALL
13433
+
13434
+ for await (const [key, val] of Object.entries(nodeP.attributes)) {
13435
+ if (_ret.abort || $container?.data()?.xuData?.pending_to_delete) break;
13436
+ if (glb.html5_events_handler.includes(key) || execute_attributes.includes(key)) {
13437
+ continue;
13438
+ }
13439
+
13440
+ const new_key = key.split(':')[0]; // break expression
13441
+ if (
13442
+ nodeP.tagName !== 'xu-panel' &&
13443
+ nodeP.tagName !== 'xu-teleport' && // nodeP.tagName.substr(0, 3) !== "xu-" &&
13444
+ (new_key.substr(0, 2) !== 'xu' || new_key.substr(2, 1) !== '-')
13445
+ ) {
13446
+ // handle common html attributes
13447
+ try {
13448
+ $elm.get(0).setAttribute(key, val);
13449
+ } catch (err) {
13450
+ console.error(err.message);
13451
+ }
13452
+
13453
+ continue;
13454
+ }
13455
+ // handle xu attributes
13456
+ try {
13457
+ if ($elm?.data()?.xuAttributes) {
13458
+ // in some cases xu data delete in purpose when refreshing the screen
13459
+ $elm.data().xuAttributes[key] = val;
13460
+ }
13461
+ } catch (error) {
13462
+ debugger;
13463
+ console.error(error);
13464
+ }
13465
+
13466
+ if (new_key === 'xu-exp' || nodeP.attributes['xu-exp:' + new_key] || glb.run_xu_before.includes(new_key) || glb.run_xu_after.includes(new_key)) {
13467
+ continue;
13468
+ }
13469
+
13470
+ if (new_key === 'xu-on') {
13471
+ let ret = await func.UI.screen.execute_xu_functions(
13472
+ SESSION_ID,
13473
+ is_skeleton,
13474
+ $root_container,
13475
+ nodeP,
13476
+ $container,
13477
+ paramsP,
13478
+ parent_infoP,
13479
+ jobNoP,
13480
+ keyP,
13481
+ parent_nodeP,
13482
+ 'xu-on',
13483
+ $elm,
13484
+ {
13485
+ key: key,
13486
+ // value: (await get_xuExp(new_key)) || func.UI.screen.fix_val_defaults(key, val),
13487
+ value: await get_attr_value(key),
13488
+ },
13489
+ is_init,
13490
+ refreshed_ds,
13491
+ );
13492
+ _ret = _.assignIn(_ret, ret);
13493
+ continue;
13494
+ }
13495
+
13496
+ let ret = await func.UI.screen.execute_xu_functions(
13497
+ SESSION_ID,
13498
+ is_skeleton,
13499
+ $root_container,
13500
+ nodeP,
13501
+ $container,
13502
+ paramsP,
13503
+ parent_infoP,
13504
+ jobNoP,
13505
+ keyP,
13506
+ parent_nodeP,
13507
+ new_key,
13508
+ $elm,
13509
+ {
13510
+ key: key,
13511
+ // value: (await get_xuExp(new_key)) || func.UI.screen.fix_val_defaults(key, val),
13512
+ value: await get_attr_value(key),
13513
+ },
13514
+ is_init,
13515
+ refreshed_ds,
13516
+ );
13517
+
13518
+ _ret = _.assignIn(_ret, ret);
13519
+ }
13520
+
13521
+ // EXP for
13522
+
13523
+ for await (const [key, val] of Object.entries(nodeP.attributes)) {
13524
+ if (_ret.abort || $container?.data()?.xuData?.pending_to_delete) break;
13525
+
13526
+ const attr = key.split('xu-exp:')[1];
13527
+
13528
+ if (!attr) {
13529
+ continue;
13530
+ }
13531
+
13532
+ if (glb.html5_events_handler.includes(attr) || execute_attributes.includes(attr)) continue;
13533
+
13534
+ if (done_exp.includes(key)) {
13535
+ continue;
13536
+ }
13537
+ let ret = await func.UI.screen.execute_xu_functions(
13538
+ SESSION_ID,
13539
+ is_skeleton,
13540
+ $root_container,
13541
+ nodeP,
13542
+ $container,
13543
+ paramsP,
13544
+ parent_infoP,
13545
+ jobNoP,
13546
+ keyP,
13547
+ parent_nodeP,
13548
+ 'xu-exp',
13549
+ $elm,
13550
+ {
13551
+ key: attr,
13552
+ value: val,
13553
+ },
13554
+ true,
13555
+ refreshed_ds,
13556
+ );
13557
+ _ret = _.assignIn(_ret, ret);
13558
+ }
13559
+
13560
+ // AFTER
13561
+
13562
+ for await (const [key, attr] of Object.entries(glb.run_xu_after)) {
13563
+ if ($container?.data()?.xuData?.pending_to_delete) break;
13564
+
13565
+ if (glb.html5_events_handler.includes(attr) || execute_attributes.includes(attr)) continue;
13566
+
13567
+ if (!nodeP.attributes || !nodeP.attributes[attr] & !nodeP.attributes[`xu-exp:${attr}`]) continue;
13568
+
13569
+ let ret = await func.UI.screen.execute_xu_functions(
13570
+ SESSION_ID,
13571
+ is_skeleton,
13572
+ $root_container,
13573
+ nodeP,
13574
+ $container,
13575
+ paramsP,
13576
+ parent_infoP,
13577
+ jobNoP,
13578
+ keyP,
13579
+ parent_nodeP,
13580
+ attr,
13581
+ $elm,
13582
+ {
13583
+ key: attr,
13584
+ // value: (await get_xuExp(attr)) || func.UI.screen.fix_val_defaults(attr, nodeP.attributes[attr]),
13585
+ value: await get_attr_value(attr),
13586
+ },
13587
+ is_init,
13588
+ refreshed_ds,
13589
+ );
13590
+ _ret = _.assignIn(_ret, ret);
13591
+ }
13592
+
13593
+ // REGISTER EVENTS ATTRIBUTES
13594
+
13595
+ for await (const [key, val] of Object.entries(nodeP.attributes)) {
13596
+ if ($container?.data()?.xuData?.pending_to_delete) break;
13597
+ if (!glb.html5_events_handler.includes(key)) break;
13598
+ // $elm.attr(key, await get_xuExp(key)) || val;
13599
+ $elm.attr(key, await get_xuExp(key)) || val;
13600
+ }
13601
+
13602
+ return _ret;
13603
+ };
13604
+
13605
+ func.UI.screen.panel_post_render_handler = async function (
13606
+ SESSION_ID,
13607
+ $container, // the parent program container
13608
+ $wrapper, // the wrapper element contains the rendered panel elements e.g: xu-single-view
13609
+ nodeP, // the xu-panel node
13610
+ $panel_div, // the panel div
13611
+ jobNoP,
13612
+ ) {
13613
+ const _container_ds = SESSION_OBJ[SESSION_ID].DS_GLB[$container.data().xuData.paramsP.dsSessionP];
13614
+ const _ds = SESSION_OBJ[SESSION_ID].DS_GLB[$wrapper.data().xuData.paramsP.dsSessionP];
13615
+ const find_old_panels_elements = function () {
13616
+ return func.UI.utils.find_in_element_data('xuPanelData', $container.parent(), 'xu-ui-id', $container.data()?.xuPanelData?.xu_panel_xu_ui_id);
13617
+ };
13618
+ var $old_panel_div = find_old_panels_elements();
13619
+ const set_xuPanelData_to_the_new_rendered_items = () => {
13620
+ $container.data().xuPanelWrapper = { isWrapper: true, panelXuAttributes: _.clone($wrapper.data().xuAttributes), panelDivData: _.clone($wrapper.data()) };
13621
+ $container.attr('xu-panel-wrapper-id', $wrapper.attr('xu-ui-id'));
13622
+ $.each($wrapper.children(), function (key, val) {
13623
+ if (!$(val).data().xuPanelData) {
13624
+ $(val).data().xuPanelData = {};
13625
+ }
13626
+ $(val).data().xuPanelData.parent_element_ui_id = $old_panel_div?.length ? $container.parent().data().xuData.ui_id : $container.data().xuData.ui_id;
13627
+
13628
+ $(val).data().xuPanelData.xu_panel_xu_ui_id = (nodeP.xu_tree_id || nodeP.id) + '-' + _container_ds?.currentRecordId;
13629
+ $(val).data().xuPanelData.node = nodeP;
13630
+ $(val).data().xuPanelData.$panel_div = $panel_div.clone(true);
13631
+ });
13632
+ };
13633
+ set_xuPanelData_to_the_new_rendered_items();
13634
+
13635
+ if ($old_panel_div?.length) {
13636
+ $($old_panel_div[0]).after($wrapper.children());
13637
+ } else {
13638
+ $.each($wrapper.children(), function (key, child) {
13639
+ $.each($container.children(), function (key, elm) {
13640
+ if ($(elm)?.data()?.xuData && $(elm)?.data()?.xuData?.elem_key === $(child)?.data()?.xuData?.elem_key) {
13641
+ $(elm).remove();
13642
+ }
13643
+ });
13644
+ $container.append($(child));
13645
+ });
13646
+ }
13647
+
13648
+ if (!$wrapper.data()?.xuData?.dsSession) return;
13649
+
13650
+ if ($old_panel_div?.length) {
13651
+ $container.parent().data().xuData.paramsP.dsSessionP = _ds.parentDataSourceNo; // set the new ds when panel replaced
13652
+ } else {
13653
+ $container.data().xuData.paramsP.dsSessionP = _ds.parentDataSourceNo; // set the new ds when panel replaced
13654
+ }
13655
+
13656
+ if ($old_panel_div?.length) {
13657
+ $old_panel_div.remove();
13658
+ }
13659
+ return jobNoP;
13660
+ };
13661
+
13662
+ const generate_xu_ui_id = async function (SESSION_ID, nodeP, $container, paramsP, keyP) {
13663
+ const _paramsP = _.cloneDeep(paramsP);
13664
+ var _ds = SESSION_OBJ[SESSION_ID].DS_GLB[_paramsP.dsSessionP];
13665
+
13666
+ const currentRecordId = $container?.data?.()?.xuData?.recordid || (_ds ? _ds.currentRecordId : '');
13667
+ const key_path = `${$container?.data()?.xuData?.key_path || '0'}-${keyP || '0'}`;
13668
+ const elem_key = `${nodeP.xu_tree_id || nodeP.id}-${key_path}-${currentRecordId}`;
13669
+ let ui_id = `${nodeP.id}-${elem_key}-${_paramsP?.dsSessionP?.toString() || ''}`; //nodeP.xu_tree_id ||
13670
+
13671
+ const new_ui_id = await func.common.sha256(ui_id);
13672
+ return new_ui_id;
13673
+ };
13674
+
13675
+ func.UI.screen.create_container = async function (SESSION_ID, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, prop, classP, elem_propP, div_typeP, $appendToP, attr_str, is_placeholder) {
13676
+ const _paramsP = _.cloneDeep(paramsP);
13677
+ var _ds = SESSION_OBJ[SESSION_ID].DS_GLB[_paramsP.dsSessionP];
13678
+ var $appendTo = $container;
13679
+ if ($appendToP) $appendTo = $appendToP;
13680
+ if (!$appendTo || !$appendTo.length) return null; // screen closed or not exist abort build
13681
+ var div = 'div';
13682
+ if (div_typeP) div = div_typeP;
13683
+ var items = [];
13684
+ if (nodeP.children)
13685
+ items = nodeP.children.map(function (val) {
13686
+ return val.xu_tree_id || val.id;
13687
+ });
13688
+ var currentRecordId = $container?.data?.()?.xuData?.recordid || (_ds ? _ds.currentRecordId : '');
13689
+ // var xu_id = (glb.screen_num++).toString();
13690
+ // xu_id = xu_id += '_' + currentRecordId;
13691
+
13692
+ try {
13693
+ const key_path = `${$container?.data()?.xuData?.key_path || '0'}-${keyP || '0'}`;
13694
+ const elem_key = `${nodeP.xu_tree_id || nodeP.id}-${key_path}-${currentRecordId}`;
13695
+ // let ui_id = `${nodeP.id}-${elem_key}-${_paramsP?.dsSessionP?.toString() || ''}`; //nodeP.xu_tree_id ||
13696
+
13697
+ /////////////////////////////////
13698
+
13699
+ var $div;
13700
+
13701
+ if (div === 'svg') {
13702
+ const draw_svg = function (element) {
13703
+ const get_tag_str = function (element, prop, val) {
13704
+ let class_str = '';
13705
+ let attr_str = '';
13706
+ for (const [key, val] of Object.entries(prop)) {
13707
+ if (key.substr(0, 2) !== 'xu') {
13708
+ attr_str += ` ${key}="${val}" `;
13709
+ }
13710
+ }
13711
+ if (element === 'svg') {
13712
+ return `<${element} ${attr_str} > `;
13713
+ }
13714
+ let ret = '';
13715
+ if (val?.children?.length) {
13716
+ ret = iterate_svg(val);
13717
+ }
13718
+
13719
+ return `<${element} ${class_str} ${attr_str} > ${ret} </${element}>`;
13720
+ };
13721
+ let svg_str = get_tag_str(element, prop);
13722
+ let inner_str = '';
13723
+ const iterate_svg = function (node) {
13724
+ let ret = '';
13725
+ if (node.children) {
13726
+ for (let val of node.children) {
13727
+ let prop = val.attributes;
13728
+ ret += get_tag_str(val.tagName, prop, val);
13729
+ }
13730
+ }
13731
+ return ret;
13732
+ };
13733
+ inner_str = iterate_svg(nodeP);
13734
+
13735
+ $div = $(svg_str + inner_str + '</svg>').appendTo($appendTo);
13736
+ };
13737
+
13738
+ draw_svg(div_typeP);
13739
+ } else {
13740
+ $div = $(`<${div} ${attr_str ? attr_str : ''}>`);
13741
+ }
13742
+
13743
+ // // Returns a 32-bit unsigned integer hash of a string (FNV-1a)
13744
+ // function hash32(str) {
13745
+ // let h = 0x811c9dc5; // FNV offset basis
13746
+ // for (let i = 0; i < str.length; i++) {
13747
+ // h ^= str.charCodeAt(i);
13748
+ // // multiply by FNV prime (2^24 + 2^8 + 0x93) with 32-bit overflow
13749
+ // h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
13750
+ // }
13751
+ // // Convert to unsigned 32-bit
13752
+ // return h >>> 0;
13753
+ // }
13754
+
13755
+ // function hash32hex(str) {
13756
+ // return (hash32(str) >>> 0).toString(16).padStart(8, '0');
13757
+ // }
13758
+
13759
+ // const new_ui_id = hash32hex(ui_id);
13760
+ // const new_ui_id = await func.common.sha256(ui_id);
13761
+ const new_ui_id = await generate_xu_ui_id(SESSION_ID, nodeP, $container, paramsP, keyP);
13762
+
13763
+ $div
13764
+ .attr('xu-ui-id', new_ui_id)
13765
+ // .attr('xu-node-id', nodeP.id)
13766
+ .data({
13767
+ xuData: {
13768
+ prog_id: _paramsP.prog_id,
13769
+ nodeid: nodeP.id,
13770
+ ui_type: nodeP.tagName,
13771
+ // xu_id,
13772
+ recordid: currentRecordId,
13773
+ paramsP: _paramsP,
13774
+ key: keyP,
13775
+ key_path, //:($container?.data()?.xuData?.key || "0") + "-" + (keyP || "0"),
13776
+ screenId: _paramsP.screenId,
13777
+ parent_container: $container?.attr('id'),
13778
+ elem_key,
13779
+ properties: prop,
13780
+ node: nodeP,
13781
+ node_org: _.cloneDeep(nodeP),
13782
+ is_panelP: _paramsP.is_panelP,
13783
+ ui_id: new_ui_id,
13784
+ elem_prop: elem_propP,
13785
+ debug_info: {
13786
+ id: nodeP.id,
13787
+ parent_id: $container?.data()?.xuData?.ui_id,
13788
+ items: items,
13789
+ },
13790
+ parent_node: parent_nodeP,
13791
+ currentRecordId: currentRecordId,
13792
+ $root_container: $root_container,
13793
+ parent_element_ui_id: $container?.data()?.xuData?.ui_id,
13794
+ },
13795
+ xuAttributes: {},
13796
+ });
13797
+ if (is_placeholder) {
13798
+ $div.addClass('display_none');
13799
+ }
13800
+
13801
+ if (div_typeP !== 'svg') {
13802
+ $div.appendTo($appendTo);
13803
+ }
13804
+ } catch (e) {
13805
+ console.error(e);
13806
+ }
13807
+
13808
+ if (parent_infoP?.iterate_info) {
13809
+ $div.data().xuData.iterate_info = parent_infoP.iterate_info;
13810
+ }
13811
+
13812
+ if (classP) $div.addClass(classP);
13813
+
13814
+ return $div;
13815
+ };
13816
+ func.UI.screen.execute_screen_ready_events = async function (SESSION_ID, paramsP, sourceP, $div, jobNoP, $div_objP) {
13817
+ var _ds = SESSION_OBJ[SESSION_ID].DS_GLB[paramsP.dsSessionP];
13818
+ if (!_ds) return;
13819
+ var viewEventExec_arr = [];
13820
+ const execute_view_events = async function (sourceP) {
13821
+ for await (const val of viewEventExec_arr) {
13822
+ cond = val.eventInfo.data.enabled;
13823
+ if (val.expression) {
13824
+ expCond = await func.expression.get(SESSION_ID, val.expression, paramsP.dsSessionP, 'condition', paramsP.rowIdP); // execute expression
13825
+ cond = expCond.result;
13826
+ }
13827
+ if (cond) {
13828
+ await func.events.execute(
13829
+ SESSION_ID,
13830
+ null,
13831
+ val.eventId,
13832
+ val.triggerId,
13833
+ val.eventInfo.data.action,
13834
+ val.eventInfo.data.name,
13835
+ null,
13836
+ null,
13837
+ null,
13838
+ null,
13839
+ val.eventInfo.data.action, //val.eventInfo[4]
13840
+ null,
13841
+ paramsP.dsSessionP,
13842
+ val.eventId,
13843
+ sourceP + ' event',
13844
+ true,
13845
+ null,
13846
+ null,
13847
+ paramsP.dsSessionP,
13848
+ null,
13849
+ null,
13850
+ val.eventInfo, // val.eventInfo[8],
13851
+ null,
13852
+ null,
13853
+ _ds.prog_id,
13854
+ _ds.nodeId,
13855
+ _ds.parentDataSourceNo,
13856
+ $div,
13857
+ );
13858
+
13859
+ // update changed values to screen added 18/09/13
13860
+ var fields_to_refresh = [];
13861
+ try {
13862
+ var current_record_id = _ds.currentRecordId;
13863
+ } catch (e) {
13864
+ console.error(e);
13865
+ }
13866
+
13867
+ if (_ds?.data_feed?.form_fields_changed?.[current_record_id]) {
13868
+ $.each(_ds.data_feed.form_fields_changed[current_record_id], function (key, val) {
13869
+ fields_to_refresh.push(key);
13870
+ });
13871
+
13872
+ var containerId = _ds.containerId;
13873
+ var $container = $('#' + containerId);
13874
+ if ($container?.data()?.xuData?.is_mobile_modal || $container?.data()?.xuData?.params?.is_mobile_page) {
13875
+ await func.UI.screen.refresh_container(SESSION_ID, 'init', fields_to_refresh, $container, paramsP.dsSessionP, null);
13876
+ } else {
13877
+ $container.trigger(containerId + '.refresh', ['init', fields_to_refresh]);
13878
+ }
13879
+
13880
+ if ($div_objP) {
13881
+ await func.UI.screen.refresh_container(SESSION_ID, 'init', fields_to_refresh, $div_objP, paramsP.dsSessionP, null);
13882
+ }
13883
+ }
13884
+ }
13885
+ continue;
13886
+ }
13887
+ };
13888
+ const get_view_events_count = async function (typeP) {
13889
+ const _prog = await func.utils.VIEWS_OBJ.get(SESSION_ID, _ds.prog_id);
13890
+ viewEventExec_arr = [];
13891
+
13892
+ if (!_prog.progEvents || _.isEmpty(_prog.progEvents)) {
13893
+ return 0;
13894
+ }
13895
+ for await (const event_obj of _prog.progEvents) {
13896
+ if (event_obj.data.type !== typeP) continue;
13897
+
13898
+ if (_.isEmpty(event_obj.workflow)) continue;
13899
+
13900
+ if (event_obj.data.condition) {
13901
+ let res = await func.expression.get(SESSION_ID, event_obj.data.condition, paramsP.dsSessionP, 'condition', paramsP.rowIdP, null, null, null, null, event_obj);
13902
+ if (!res.result) {
13903
+ continue;
13904
+ }
13905
+ }
13906
+
13907
+ for await (const trigger_obj of event_obj.workflow) {
13908
+ var expression = undefined;
13909
+ var cond = undefined;
13910
+ if (!trigger_obj.data.enabled) {
13911
+ continue;
13912
+ }
13913
+ cond = true;
13914
+ if (trigger_obj.props.condition) expression = trigger_obj.props.condition;
13915
+ var expCond = {};
13916
+ if (expression) {
13917
+ expCond = await func.expression.get(SESSION_ID, expression, paramsP.dsSessionP, 'condition', paramsP.rowIdP, trigger_obj.data.type); // execute expression
13918
+ cond = expCond.result;
13919
+ expCond.conditional = true;
13920
+ }
13921
+
13922
+ if (!trigger_obj.data.action) {
13923
+ func.utils.debug_report(SESSION_ID, 'get_view_events_count', `Error initiating ${typeP} prog: ${_ds.viewSourceDesc} reason: missing action`, 'E');
13924
+ break;
13925
+ }
13926
+ if (!glb.REFERENCE_LESS_FUNCTIONS.includes(trigger_obj.data.action) && !trigger_obj.data.name?.prog) {
13927
+ func.utils.debug_report(SESSION_ID, 'get_view_events_count', `Error initiating ${typeP} prog: ${_ds.viewSourceDesc} reason: missing reference`, 'E');
13928
+ break;
13929
+ }
13930
+
13931
+ viewEventExec_arr.push({
13932
+ eventInfo: trigger_obj,
13933
+ eventId: event_obj.id,
13934
+ triggerId: trigger_obj.id,
13935
+ expression: expression,
13936
+ });
13937
+ }
13938
+ }
13939
+ return viewEventExec_arr.length;
13940
+ };
13941
+ try {
13942
+ let count = await get_view_events_count('screen_ready');
13943
+
13944
+ if (!count) return;
13945
+ return await execute_view_events(sourceP);
13946
+ } catch (error) {
13947
+ debugger;
13948
+ }
13949
+ };
13950
+ func.UI.screen.screen_loading_done = async function (SESSION_ID, paramsP, $div, jobNoP) {
13951
+ // console.log($div.attr('xu-ui-id'));
13952
+
13953
+ // await
13954
+ let retries = 0;
13955
+ let interval = setInterval(() => {
13956
+ if (!$(SESSION_OBJ[SESSION_ID].root_element).find(`[xu-ui-id=${$div.attr('xu-ui-id')}]`).length && !$(SESSION_OBJ[SESSION_ID].root_element).find(`[xu-panel-wrapper-id=${$div.attr('xu-ui-id')}]`).length && !$div.attr('xu-ui-id') && $div.attr('xu-ui-id')) {
13957
+ retries++;
13958
+ if (retries > 100) {
13959
+ console.warn('deadlock detected for screen ready');
13960
+ } else {
13961
+ return $div;
13962
+ }
13963
+ }
13964
+
13965
+ clearInterval(interval);
13966
+
13967
+ func.UI.screen.execute_screen_ready_events(SESSION_ID, paramsP, paramsP.screenInfo.properties?.renderType, $div, jobNoP);
13968
+ var _session = SESSION_OBJ[SESSION_ID];
13969
+ func.events.delete_job(SESSION_ID, jobNoP);
13970
+ func.UI.utils.screen_blocker(false, paramsP.prog_id + (paramsP.sourceScreenP ? '_' + paramsP.sourceScreenP : ''));
13971
+ if (_session.prog_id === paramsP.prog_id) {
13972
+ _session.system_ready = true;
13973
+ if (_session.engine_mode === 'live_preview' && STUDIO_PEER_CONN_SEND_METHOD) {
13974
+ STUDIO_PEER_CONN_SEND_METHOD({
13975
+ service: 'system_ready',
13976
+ data: {},
13977
+ id: STUDIO_PEER.id,
13978
+ source: 'runtime',
13979
+ session_id: SESSION_ID,
13980
+ app_id: _session.app_id,
13981
+ gtp_token: _session.gtp_token,
13982
+ app_token: _session.app_token,
13983
+ });
13984
+ // }
13985
+ }
13986
+ }
13987
+ }, 100);
13988
+
13989
+ return $div;
13990
+ };
13991
+
13992
+ func.UI.screen.render_ui_tree = async function (SESSION_ID, $container, nodeP, parent_infoP, paramsP, jobNoP, is_skeleton, keyP, refreshed_ds, parent_nodeP, check_existP, $root_container) {
13993
+ if (!is_skeleton) {
13994
+ var _session = SESSION_OBJ[SESSION_ID];
13995
+ var _ds = _session.DS_GLB[paramsP.dsSessionP];
13996
+ }
13997
+ var prop;
13998
+ try {
13999
+ prop = nodeP.attributes;
14000
+ } catch (error) {
14001
+ // debugger;
14002
+ }
14003
+
14004
+ ///////////
14005
+ var is_mobile = glb.MOBILE_ARR.includes(paramsP.screenInfo.properties?.menuType) ? true : false;
14006
+
14007
+ const get_element_info = function () {
14008
+ var ret = {};
14009
+ let currentRecordId = _ds?.currentRecordId || '';
14010
+
14011
+ let $div = func.UI.utils.find_in_element_data('xuData', $container.parent(), 'nodeid', nodeP.id);
14012
+
14013
+ $.each($div, function (key, val) {
14014
+ if ($(val)?.data().xuData?.recordid === currentRecordId && $(val)?.data().xuData?.key === keyP && $(val)?.prop('tagName') !== 'XURENDER') {
14015
+ ret = {
14016
+ div: $div,
14017
+ };
14018
+ }
14019
+ });
14020
+
14021
+ return ret;
14022
+ };
14023
+
14024
+ const init = async function () {
14025
+ var ret = true;
14026
+ if (!nodeP) ret = false;
14027
+ return ret;
14028
+ };
14029
+ const debug = function (is_errorP, error_descP) {
14030
+ func.utils.debug.log(SESSION_ID, paramsP.prog_id + '_' + nodeP.id_org + '_ui_prop', {
14031
+ module: 'gui',
14032
+ action: 'init',
14033
+ prop: nodeP.id,
14034
+ details: error_descP,
14035
+ result: null,
14036
+ error: is_errorP,
14037
+ source: _ds?.tree_obj?.menuName || '',
14038
+ fields: null,
14039
+ type: null,
14040
+ prog_id: paramsP.prog_id,
14041
+ dsSession: null,
14042
+ });
14043
+ };
14044
+
14045
+ const open_modal = async function ($div) {
14046
+ const modal_id = 'app_modal-' + paramsP.dsSessionP.toString();
14047
+ var xu_modal_controller = document.querySelector('xu-modal-controller');
14048
+ if (!xu_modal_controller) {
14049
+ func.UI.component.create_app_modal_component(SESSION_ID, modal_id);
14050
+ xu_modal_controller = document.querySelector('xu-modal-controller');
14051
+ }
14052
+
14053
+ var controller_params = $(xu_modal_controller).data('xuControllerParams');
14054
+
14055
+ if (!controller_params) {
14056
+ controller_params = {};
14057
+ }
14058
+
14059
+ var params = {
14060
+ menuTitle: paramsP.screenInfo.properties?.menuTitle,
14061
+ screenId: paramsP.screenId,
14062
+ $dialogDiv: $div.children(),
14063
+ $container: $container,
14064
+ dsSession: paramsP.dsSessionP,
14065
+ };
14066
+
14067
+ controller_params[modal_id] = params;
14068
+
14069
+ $(xu_modal_controller).data('xuControllerParams', controller_params);
14070
+ const modalController = await new UI_FRAMEWORK_PLUGIN.modal();
14071
+
14072
+ if (!APP_MODAL_OBJ[modal_id]) {
14073
+ const modal = await modalController.create(SESSION_ID, modal_id, paramsP.screenInfo, close_modal);
14074
+ APP_MODAL_OBJ[modal_id] = modal;
14075
+ } else {
14076
+ $(modal_id).empty();
14077
+ }
14078
+ await modalController.init(SESSION_ID, modal_id);
14079
+
14080
+ return $div;
14081
+ };
14082
+
14083
+ const close_modal = async function (modal_id) {
14084
+ delete APP_MODAL_OBJ[modal_id];
14085
+ const xu_modal_controller = document.querySelector('xu-modal-controller');
14086
+ var params = $(xu_modal_controller).data().xuControllerParams[modal_id];
14087
+ if (params && params.$container) {
14088
+ await func.UI.screen.validate_exit_events(SESSION_ID, params.$container.data().xuData.paramsP, null);
14089
+ func.datasource.clean_all(SESSION_ID, params.dsSession);
14090
+ }
14091
+ };
14092
+
14093
+ const close_all_modals = function () {
14094
+ $.each(APP_MODAL_OBJ, function (key, val) {
14095
+ if (val) {
14096
+ // close_modal(key);
14097
+ UI_FRAMEWORK_PLUGIN.modal.close(key);
14098
+ }
14099
+ });
14100
+ };
14101
+
14102
+ const open_popover = async function ($div) {
14103
+ const xu_popover_controller = func.UI.component.create_app_popover_component(SESSION_ID);
14104
+
14105
+ $(xu_popover_controller).data('xuControllerParams', {
14106
+ menuTitle: paramsP.screenInfo.properties?.menuTitle,
14107
+ screenId: paramsP.screenId,
14108
+ $dialogDiv: $div.children(),
14109
+ $container: $container,
14110
+ });
14111
+ const popover = new UI_FRAMEWORK_PLUGIN.popover(
14112
+ SESSION_ID,
14113
+ // ELEMENT_CLICK_EVENT,
14114
+ // props
14115
+ );
14116
+ await popover.open(SESSION_ID);
14117
+ CURRENT_APP_POPOVER = popover;
14118
+
14119
+ return;
14120
+ popoverController
14121
+ .create({
14122
+ component: 'xu-popover-content-' + SESSION_ID,
14123
+ event: ELEMENT_CLICK_EVENT,
14124
+ translucent: true,
14125
+ })
14126
+ .then((modal) => {
14127
+ modal.present().then(() => {
14128
+ CURRENT_APP_POPOVER = modal;
14129
+
14130
+ if (callbackP) callbackP($div);
14131
+ });
14132
+ });
14133
+ };
14134
+ const iterate_child = async function ($divP, nodeP, parent_infoP, $root_container, before_record_function) {
14135
+ if (!is_mobile && nodeP.busy) return;
14136
+ nodeP.busy = true;
14137
+ const done = async function ($divP) {
14138
+ setTimeout(function () {
14139
+ nodeP.busy = false;
14140
+ }, 1000);
14141
+
14142
+ return $divP;
14143
+ };
14144
+ if (!nodeP || !nodeP.children) {
14145
+ return await done($divP);
14146
+ }
14147
+
14148
+ if (before_record_function) {
14149
+ await before_record_function();
14150
+ }
14151
+ if (nodeP?.children?.length) {
14152
+ let node_promises = [];
14153
+ for (const [key, val] of Object.entries(nodeP.children)) {
14154
+ node_promises.push(
14155
+ new Promise(async (resolve, reject) => {
14156
+ const ret = await func.UI.screen.render_ui_tree(SESSION_ID, $divP, nodeP.children[key], parent_infoP, paramsP, jobNoP, is_skeleton, Number(key), null, nodeP, null, $root_container);
14157
+
14158
+ resolve();
14159
+ }),
14160
+ );
14161
+ }
14162
+ await Promise.all(node_promises);
14163
+ }
14164
+ return await done($divP);
14165
+ };
14166
+
14167
+ // const iterate_child = async function ($divP, nodeP, parent_infoP, $root_container, before_record_function) {
14168
+ // if (!is_mobile && nodeP.busy) return;
14169
+ // nodeP.busy = true;
14170
+ // const done = async function ($divP) {
14171
+ // setTimeout(function () {
14172
+ // nodeP.busy = false;
14173
+ // }, 1000);
14174
+
14175
+ // return $divP;
14176
+ // };
14177
+ // if (!nodeP || !nodeP.children) {
14178
+ // return await done($divP);
14179
+ // }
14180
+
14181
+ // if (before_record_function) {
14182
+ // await before_record_function();
14183
+ // }
14184
+ // if (nodeP?.children?.length) {
14185
+ // for await (const [key, val] of Object.entries(nodeP.children)) {
14186
+ // const ret = await func.UI.screen.render_ui_tree(SESSION_ID, $divP, nodeP.children[key], parent_infoP, paramsP, jobNoP, is_skeleton, Number(key), null, nodeP, null, $root_container);
14187
+ // }
14188
+ // }
14189
+ // return await done($divP);
14190
+ // };
14191
+
14192
+ const _$ = function ($elm) {
14193
+ try {
14194
+ const id = $elm.attr('xu-ui-id');
14195
+ if (!id || !glb.DEBUG_MODE) return $elm;
14196
+ const $el = $(`[xu-ui-id="${id}"]`);
14197
+
14198
+ if ($el.length > 1) {
14199
+ console.warn('Multiple elements for xu-ui-id: ' + id, $el);
14200
+ }
14201
+
14202
+ return $($el[0]);
14203
+ } catch (e) {
14204
+ console.error(e);
14205
+ }
14206
+ };
14207
+
14208
+ const hover_in = function ($div, e) {
14209
+ if (is_skeleton || (e && (EXP_BUSY || UI_WORKER_OBJ.jobs.length))) return;
14210
+ CLIENT_ACTIVITY_TS = Date.now();
14211
+ if (_$($container)?.data()?.xuData?.debug_info) _$($container).data().xuData.debug_info.hover_item = $div.attr('xu-ui-id');
14212
+ if (!_ds) return;
14213
+ ///////// SET Attributes///////////
14214
+ let attributes = {};
14215
+ $.each($div[0].attributes, function (index, attr) {
14216
+ attributes[attr.name] = attr.value;
14217
+ });
14218
+
14219
+ _session.DS_GLB[0].data_system.SYS_OBJ_WIN_ELEMENT_HOVERED_ATTRIBUTES = attributes;
14220
+ //////////////////////////////////
14221
+ if (!$div.data()?.xuData) return;
14222
+ const _iterate_info = $div.data().xuData.iterate_info;
14223
+ if (_iterate_info) {
14224
+ if (_iterate_info.is_key_dynamic_field) {
14225
+ _ds.dynamic_fields[_iterate_info.iterator_key].value = _iterate_info._key;
14226
+ } else {
14227
+ try {
14228
+ const row_idx = func.common.find_ROWID_idx(_ds, _ds.currentRecordId);
14229
+ _ds.data_feed.rows[row_idx][_iterate_info.iterator_key] = _iterate_info._key;
14230
+ } catch (err) {
14231
+ console.error(err);
14232
+ }
14233
+ }
14234
+
14235
+ if (_iterate_info.is_val_dynamic_field) {
14236
+ _ds.dynamic_fields[_iterate_info.iterator_val].value = _iterate_info._val;
14237
+ } else {
14238
+ try {
14239
+ const row_idx = func.common.find_ROWID_idx(_ds, _ds.currentRecordId);
14240
+ _ds.data_feed.rows[row_idx][_iterate_info.iterator_val] = _iterate_info._val;
14241
+ } catch (err) {
14242
+ console.error(err);
14243
+ }
14244
+ }
14245
+ }
14246
+
14247
+ if ($div && _$($div) && _ds && paramsP.renderType === 'grid') {
14248
+ func.UI.worker.add_to_queue(SESSION_ID, 'gui event', 'update_datasource', { currentRecordId: _$($div).data().xuData.currentRecordId }, null, null, paramsP.dsSessionP);
14249
+ }
14250
+
14251
+ const set_value = function (field_id, value) {
14252
+ var currentRecordId = _$($div).data().xuData.currentRecordId;
14253
+
14254
+ func.UI.worker.add_to_queue(
14255
+ SESSION_ID,
14256
+ 'gui event',
14257
+ 'update_datasource',
14258
+ {
14259
+ currentRecordId,
14260
+ field_id,
14261
+ field_value: value,
14262
+ },
14263
+ null,
14264
+ null,
14265
+ paramsP.dsSessionP,
14266
+ );
14267
+ };
14268
+
14269
+ if ($div?.data()?.iterate_info) {
14270
+ var data = $div.data().xuData.iterate_info;
14271
+ if (data.iterator_key) {
14272
+ set_value(data.iterator_key, data._key);
14273
+ }
14274
+ if (data.iterator_val) {
14275
+ set_value(data.iterator_val, data._val);
14276
+ }
14277
+ }
14278
+ };
14279
+ const hover_out = function () {
14280
+ if (is_skeleton) return;
14281
+
14282
+ CLIENT_ACTIVITY_TS = Date.now();
14283
+ if (_$($container)?.data()?.xuData?.debug_info) {
14284
+ _$($container).data().xuData.debug_info.hover_item = null;
14285
+ }
14286
+ if (_ds?.data_system) {
14287
+ SESSION_OBJ[SESSION_ID].DS_GLB[0].data_system.SYS_OBJ_WIN_ELEMENT_HOVERED_ATTRIBUTES = {};
14288
+ }
14289
+ };
14290
+ const render_screen_type = async function ($div) {
14291
+ const set_call_screen_properties_values = async function (ui_framework) {
14292
+ params.properties = {};
14293
+ const get_values = async function (property) {
14294
+ var property_value = paramsP?.screenInfo?.properties?.[property] || paramsP?.screenInfo?.properties?.frameworkProperties?.[property];
14295
+ if (paramsP?.call_screen_propertiesP) {
14296
+ if (paramsP.call_screen_propertiesP?.[property]) {
14297
+ property_value = paramsP.call_screen_propertiesP[property];
14298
+ }
14299
+ if (paramsP.call_screen_propertiesP[`xu-exp:${property}`]) {
14300
+ property_value = (await func.expression.get(SESSION_ID, paramsP.call_screen_propertiesP[`xu-exp:${property}`], paramsP.dsSessionP, property)).result;
14301
+ }
14302
+ }
14303
+ return property_value;
14304
+ };
14305
+ params.properties['name'] = await get_values('menuTitle');
14306
+ if (await ui_framework?.properties()) {
14307
+ for await (const [key, val] of Object.entries(await ui_framework.properties())) {
14308
+ params.properties[key] = await get_values(key);
14309
+ }
14310
+ }
14311
+ };
14312
+
14313
+ var $div_content = $div.children();
14314
+
14315
+ $.each($div_content, function (key, val) {
14316
+ if (!$(val)?.data()?.xuData?.parent_container) {
14317
+ return true;
14318
+ }
14319
+ $(val).data().xuData.parent_container = $div.data().xuData.parent_container;
14320
+ });
14321
+
14322
+ let $ret = $div;
14323
+ var $nav = $(SESSION_OBJ[SESSION_ID].root_element).find('xu-nav');
14324
+ var params;
14325
+ switch (paramsP.screen_type) {
14326
+ case 'modal':
14327
+ const modal_id = 'app_modal-' + paramsP.dsSessionP.toString();
14328
+ var xu_modal_controller = document.querySelector('xu-modal-controller');
14329
+ if (!xu_modal_controller) {
14330
+ func.UI.component.create_app_modal_component(SESSION_ID, modal_id);
14331
+ xu_modal_controller = document.querySelector('xu-modal-controller');
14332
+ }
14333
+
14334
+ var controller_params = $(xu_modal_controller).data('xuControllerParams');
14335
+
14336
+ if (!controller_params) {
14337
+ controller_params = {};
14338
+ }
14339
+
14340
+ params = {
14341
+ screenId: paramsP.screenId,
14342
+ $dialogDiv: $div.children(),
14343
+ $container: $container,
14344
+ dsSession: paramsP.dsSessionP,
14345
+ modal_id,
14346
+ screenInfo: paramsP.screenInfo,
14347
+ close_callback: close_modal,
14348
+ paramsP,
14349
+ };
14350
+
14351
+ controller_params[modal_id] = params;
14352
+
14353
+ $(xu_modal_controller).data('xuControllerParams', controller_params);
14354
+ const modalController = await new UI_FRAMEWORK_PLUGIN.modal();
14355
+ await set_call_screen_properties_values(modalController);
14356
+ if (!APP_MODAL_OBJ[modal_id]) {
14357
+ const modal = await modalController.create(params);
14358
+
14359
+ APP_MODAL_OBJ[modal_id] = modal;
14360
+ } else {
14361
+ $(modal_id).empty();
14362
+ }
14363
+
14364
+ await modalController.init(params);
14365
+
14366
+ break;
14367
+
14368
+ case 'popover':
14369
+ // open_popover($div);
14370
+
14371
+ const xu_popover_controller = func.UI.component.create_app_popover_component(SESSION_ID);
14372
+ params = {
14373
+ menuTitle: paramsP.screenInfo.properties?.menuTitle,
14374
+ screenId: paramsP.screenId,
14375
+ $dialogDiv: $div.children(),
14376
+ $container: $container,
14377
+ };
14378
+
14379
+ $(xu_popover_controller).data('xuControllerParams', params);
14380
+ const popover = new UI_FRAMEWORK_PLUGIN.popover(SESSION_ID);
14381
+ await set_call_screen_properties_values(popover);
14382
+ await popover.open(params);
14383
+ CURRENT_APP_POPOVER = popover;
14384
+
14385
+ func.UI.utils.screen_blocker(false, paramsP.prog_id + '_' + paramsP.sourceScreenP);
14386
+ break;
14387
+
14388
+ case 'page':
14389
+ const nav = $nav[0];
14390
+
14391
+ params = {
14392
+ div: $div_content,
14393
+ name: paramsP.screenInfo.properties?.menuTitle,
14394
+ screenId: paramsP.screenId,
14395
+ $container: $container,
14396
+ dsSession: paramsP.dsSessionP,
14397
+ SESSION_ID,
14398
+ nav,
14399
+ paramsP,
14400
+ };
14401
+
14402
+ var component_name = 'xu-page-component-' + paramsP.dsSessionP;
14403
+ if (!$(nav).data().xuData.nav_params) {
14404
+ $(nav).data().xuData.nav_params = {};
14405
+ }
14406
+
14407
+ //restore validate
14408
+ if ($(nav)?.data()?.xuData?.params?.[paramsP.dsSessionP]) {
14409
+ params.$container.data().xuData.validate_screen_ready = $(nav).data().xuData.params[paramsP.dsSessionP].$container.data().xuData.validate_screen_ready;
14410
+ }
14411
+
14412
+ if (!$(nav)?.data()?.xuData) return;
14413
+ $(nav).data().xuData.nav_params[paramsP.dsSessionP] = params;
14414
+ if (!$(component_name).length) {
14415
+ await func.UI.component.create_app_page_component(SESSION_ID, paramsP.dsSessionP);
14416
+ const page = new UI_FRAMEWORK_PLUGIN.page();
14417
+ await set_call_screen_properties_values(page);
14418
+ await page.create(params);
14419
+ await page.init(params);
14420
+ nav.push(component_name, { params });
14421
+ } else {
14422
+ debugger;
14423
+ $(component_name).empty();
14424
+
14425
+ await UI_FRAMEWORK_PLUGIN.page(SESSION_ID, paramsP.dsSessionP);
14426
+ }
14427
+ $div.data().xuData.paramsP = $container.data().xuData.paramsP;
14428
+ break;
14429
+
14430
+ case 'panel':
14431
+ $container.append($div_content);
14432
+ $ret = $container;
14433
+ break;
14434
+
14435
+ default: // set data to nav to use in the component
14436
+ if ($nav && $nav.length) {
14437
+ // refresh made
14438
+ } else {
14439
+ $nav = $('<xu-nav>'); //.attr('xu-ui-id', SESSION_ID);
14440
+ $container.append($nav);
14441
+ func.UI.component.init_xu_nav($container, $nav);
14442
+ }
14443
+
14444
+ $nav.data().xuData.$div = $div_content;
14445
+
14446
+ await $nav[0].setRoot('xu-root-component-' + SESSION_ID);
14447
+ $ret = $container;
14448
+ break;
14449
+ }
14450
+ return $ret;
14451
+ };
14452
+
14453
+ if (!(await init())) return;
14454
+ debug();
14455
+ const fx = {
14456
+ widget: async function () {
14457
+ var _session = SESSION_OBJ[SESSION_ID];
14458
+
14459
+ var exist_elm_obj = get_element_info();
14460
+ var $div = exist_elm_obj.div;
14461
+ if (!exist_elm_obj.div) {
14462
+ $div = await func.UI.screen.create_container(SESSION_ID, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, prop, 'widget_wrapper', null, null, null, null);
14463
+
14464
+ //////////////////////////
14465
+
14466
+ let plugin_name = prop['xu-widget'],
14467
+ method = prop['xu-method'],
14468
+ dsP = paramsP.dsSessionP,
14469
+ propsP = prop,
14470
+ sourceP = 'widgets';
14471
+
14472
+ // const set_SYS_GLOBAL_OBJ_WIDGET_INFO = async function (docP) {
14473
+ // var obj = _.clone(docP);
14474
+ // obj.date = await func.utils.get_dateTime(
14475
+ // SESSION_ID,
14476
+ // "SYS_DATE",
14477
+ // docP.date
14478
+ // );
14479
+ // obj.time = await func.utils.get_dateTime(
14480
+ // SESSION_ID,
14481
+ // "SYS_TIME",
14482
+ // docP.date
14483
+ // );
14484
+
14485
+ // var datasource_changes = {
14486
+ // [0]: {
14487
+ // ["data_system"]: {
14488
+ // ["SYS_GLOBAL_OBJ_WIDGET_INFO"]: obj,
14489
+ // },
14490
+ // },
14491
+ // };
14492
+ // await func.datasource.update(SESSION_ID, datasource_changes);
14493
+ // };
14494
+ const call_plugin_api = async function (plugin_nameP, dataP) {
14495
+ return await func.utils.call_plugin_api(SESSION_ID, plugin_nameP, dataP);
14496
+ };
14497
+ const report_error = function (descP, warn) {
14498
+ func.utils.debug.log(SESSION_ID, _session.DS_GLB[dsP].prog_id + '_' + _session.DS_GLB[dsP].callingMenuId, {
14499
+ module: 'widgets',
14500
+ action: 'Init',
14501
+ source: sourceP,
14502
+ prop: descP,
14503
+ details: descP,
14504
+ result: null,
14505
+ error: warn ? false : true,
14506
+ fields: null,
14507
+ type: 'widgets',
14508
+ prog_id: _session.DS_GLB[dsP].prog_id,
14509
+ });
14510
+ };
14511
+ const get_fields_data = async function (fields, props) {
14512
+ const report_error = function (descP, warn) {
14513
+ func.utils.debug.log(SESSION_ID, _session.DS_GLB[dsP].prog_id + '_' + _session.DS_GLB[dsP].callingMenuId, {
14514
+ module: 'widgets',
14515
+ action: 'Init',
14516
+ source: sourceP,
14517
+ prop: descP,
14518
+ details: descP,
14519
+ result: null,
14520
+ error: warn ? false : true,
14521
+ fields: null,
14522
+ type: 'widgets',
14523
+ prog_id: _session.DS_GLB[dsP].prog_id,
14524
+ });
14525
+ };
14526
+ const get_property_value = async function (fieldIdP, val) {
14527
+ if (!val) return;
14528
+ var value = fieldIdP in props ? props[fieldIdP] : typeof val.defaultValue === 'function' ? val?.defaultValue?.() : val?.defaultValue;
14529
+ if (val.render === 'eventId') {
14530
+ value = props?.[fieldIdP]?.event;
14531
+ }
14532
+
14533
+ if (props[`xu-exp:${fieldIdP}`]) {
14534
+ value = (await func.expression.get(SESSION_ID, props[`xu-exp:${fieldIdP}`], dsP, 'widget property')).result;
14535
+ }
14536
+
14537
+ return func.common.get_cast_val(
14538
+ SESSION_ID,
14539
+ 'widgets',
14540
+ fieldIdP,
14541
+ val.type, //val.type !== "string" || val.type !== "number" ? "string" : val.type,
14542
+ value,
14543
+ null,
14544
+ );
14545
+ };
14546
+ var data_obj = {};
14547
+ var return_code = 1;
14548
+ // $.each(fields, function (key, val) {
14549
+ for await (const [key, val] of Object.entries(fields)) {
14550
+ data_obj[key] = await get_property_value(key, val);
14551
+ if (!data_obj[key] && val.mandatory) {
14552
+ return_code = -1;
14553
+ report_error(`${key} is a mandatory field.`);
14554
+ break;
14555
+ }
14556
+ // console.log(val);
14557
+ }
14558
+ for await (const key of ['xu-bind']) {
14559
+ data_obj[key] = await get_property_value(key, props[key]);
14560
+ }
14561
+
14562
+ return { code: return_code, data: data_obj };
14563
+ };
14564
+
14565
+ const load_css_style = function () {
14566
+ const get_css_path = function (resource) {
14567
+ if (_session.worker_type === 'Dev') {
14568
+ return `../../plugins/${plugin_name}/${resource}`;
14569
+ }
14570
+ return `https://${_session.domain}/plugins/${plugin_name}/${APP_OBJ[_session.app_id].app_plugins_purchased[plugin_name].manifest[resource].dist ? 'dist/' : ''}${resource}?gtp_token=${_session.gtp_token}&app_id=${_session.app_id}`;
14571
+ };
14572
+ let path = get_css_path('style.css');
14573
+ func.utils.load_css_on_demand(path);
14574
+ };
14575
+
14576
+ const _plugin = APP_OBJ[_session.app_id]?.app_plugins_purchased?.[plugin_name];
14577
+ const index = await func.utils.get_plugin_resource(SESSION_ID, plugin_name, `${_plugin.manifest['index.mjs'].dist ? 'dist/' : ''}index.mjs`);
14578
+ const methods = index.methods;
14579
+ if (methods && !methods[method]) {
14580
+ return report_error('method not found');
14581
+ }
14582
+
14583
+ const fields_ret = await get_fields_data(methods[method].fields, propsP);
14584
+ if (fields_ret.code < 0) {
14585
+ return report_error(fields_ret.data);
14586
+ }
14587
+ const fields = fields_ret.data;
14588
+
14589
+ let exclude_attributes = [];
14590
+ for await (const [key, val] of Object.entries(propsP)) {
14591
+ if (typeof fields[key] !== 'undefined' || typeof fields[`xu-exp:${key}`] !== 'undefined') {
14592
+ exclude_attributes.push(key);
14593
+ }
14594
+ }
14595
+
14596
+ let ret = await func.UI.screen.set_attributes_new(SESSION_ID, is_skeleton, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, $div, true, exclude_attributes);
14597
+
14598
+ $div.addClass('widget_wrapper'); // class get override in set_attributes_new
14599
+
14600
+ if (!APP_OBJ[_session.app_id].app_plugins_purchased[plugin_name]) {
14601
+ return report_error(`plugin ${plugin_name} not found`);
14602
+ }
14603
+
14604
+ if (APP_OBJ[_session.app_id].app_plugins_purchased[plugin_name].manifest['style.css'].exist) {
14605
+ load_css_style();
14606
+ }
14607
+
14608
+ const plugin_setup_ret = await func.utils.get_plugin_setup(SESSION_ID, plugin_name);
14609
+ if (plugin_setup_ret.code < 0) {
14610
+ return report_error(plugin_setup_ret);
14611
+ }
14612
+
14613
+ const api_utils = await func.common.get_module(SESSION_ID, 'xuda-api-library.mjs', {
14614
+ func,
14615
+ glb,
14616
+ SESSION_OBJ,
14617
+ SESSION_ID,
14618
+ APP_OBJ,
14619
+ dsSession: paramsP.dsSessionP,
14620
+ job_id: jobNoP,
14621
+ });
14622
+
14623
+ const params = {
14624
+ SESSION_ID,
14625
+ method,
14626
+ _session,
14627
+ dsP,
14628
+ sourceP,
14629
+ propsP,
14630
+ plugin_name,
14631
+ $containerP: $div,
14632
+ plugin_setup: plugin_setup_ret.data,
14633
+
14634
+ report_error,
14635
+ call_plugin_api,
14636
+ // set_SYS_GLOBAL_OBJ_WIDGET_INFO,
14637
+ api_utils,
14638
+ };
14639
+
14640
+ const fx = await func.utils.get_plugin_resource(SESSION_ID, plugin_name, `${_plugin.manifest['runtime.mjs'].dist ? 'dist/' : ''}runtime.mjs`);
14641
+
14642
+ if (_plugin?.manifest?.['runtime.mjs'].dist && _plugin?.manifest?.['runtime.mjs']?.css) {
14643
+ const plugin_runtime_css_url = await func.utils.get_plugin_npm_cdn(SESSION_ID, plugin_name, 'dist/runtime.css');
14644
+ func.utils.load_css_on_demand(plugin_runtime_css_url);
14645
+ }
14646
+
14647
+ if (!fx[method]) {
14648
+ throw `Method: ${method} does not exist`;
14649
+ }
14650
+ try {
14651
+ await fx[method](fields, params);
14652
+ } catch (err) {
14653
+ func.utils.debug_report(SESSION_ID, `${plugin_name} widget`, err.message, 'E');
14654
+ }
14655
+ }
14656
+ return $div;
14657
+ },
14658
+ [`xu-single-view`]: async function () {
14659
+ var exist_elm_obj = get_element_info();
14660
+ var $div = exist_elm_obj.div;
14661
+
14662
+ if (!exist_elm_obj.div) {
14663
+ var $wrapper = $('<div>');
14664
+ $div = await func.UI.screen.create_container(SESSION_ID, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, prop, null, null, 'div', $wrapper, '');
14665
+
14666
+ if (!$div) return;
14667
+
14668
+ if (!REFRESHER_IN_PROGRESS && (paramsP.is_mobile_popover || paramsP.is_mobile_page)) {
14669
+ close_all_modals();
14670
+ }
14671
+
14672
+ $div.hover(
14673
+ function (e) {
14674
+ hover_in();
14675
+ // func.UI.screen.hover_in(SESSION_ID, null, $container, paramsP, is_skeleton);
14676
+ },
14677
+ function (e) {
14678
+ // func.UI.screen.hover_out(SESSION_ID, $container, is_skeleton, paramsP);
14679
+ hover_out();
14680
+ },
14681
+ );
14682
+ }
14683
+
14684
+ const ret = await iterate_child($div, nodeP, null, $div);
14685
+ if (_.isEmpty($container.data().xuAttributes)) {
14686
+ await func.UI.screen.set_attributes_new(SESSION_ID, is_skeleton, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, $container, true);
14687
+ }
14688
+
14689
+ $.each($div.data().xuData, function (key, val) {
14690
+ $container.data().xuData[key] = _.cloneDeep(val);
14691
+ });
14692
+ $.each($div.data().xuAttributes, function (key, val) {
14693
+ $container.data().xuAttributes[key] = _.cloneDeep(val);
14694
+ });
14695
+
14696
+ return await render_screen_type($div);
14697
+ },
14698
+ [`xu-multi-view`]: async function () {
14699
+ var $div = $container;
14700
+
14701
+ if (!$div.data().xuData.node || !$div.data().xuData.node.children) {
14702
+ $div.data().xuData.node = nodeP;
14703
+ }
14704
+
14705
+ if (!$div.data().xuData.debug_info) {
14706
+ $div.data().xuData.debug_info = {
14707
+ id: nodeP.id,
14708
+ parent_id: $container.data().xuData.ui_id,
14709
+ };
14710
+ }
14711
+
14712
+ const done = async function (continuous_idx) {
14713
+ // const do_callback = async function ($div) {
14714
+ // // if ($root_container.data().xuData.progress_bar_circle) {
14715
+ // // setTimeout(function () {
14716
+ // // $.each(
14717
+ // // $root_container.data().xuData.progress_bar_circle,
14718
+ // // function (key, val) {
14719
+ // // val.bar.set(parseFloat(val.value)); // Number from 0.0 to 1.0
14720
+ // // }
14721
+ // // );
14722
+ // // }, 2000);
14723
+ // // }
14724
+
14725
+ // if (paramsP.screenInfo.properties?.rtl) {
14726
+ // $div_content.attr('dir', 'rtl');
14727
+ // }
14728
+
14729
+ // return $div;
14730
+ // };
14731
+ await func.UI.screen.set_attributes_new(SESSION_ID, is_skeleton, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, $container, true);
14732
+
14733
+ return await render_screen_type($div);
14734
+ };
14735
+
14736
+ if (!REFRESHER_IN_PROGRESS && (paramsP.is_mobile_popover || paramsP.is_mobile_page)) {
14737
+ close_all_modals();
14738
+ }
14739
+
14740
+ const empty_result = async function () {
14741
+ // var content = prop.empty_result_content || '';
14742
+
14743
+ // var res = await func.expression.get(
14744
+ // SESSION_ID,
14745
+ // content, // prop["xu-exp:empty_result_content"],
14746
+ // paramsP.dsSessionP,
14747
+ // 'empty_result_content_EXP',
14748
+ // _ds.currentRecordId,
14749
+ // );
14750
+ // content = res.result;
14751
+
14752
+ // let empty_result_node = {
14753
+ // type: 'element',
14754
+ // id: crypto.randomUUID(),
14755
+ // content,
14756
+ // // : content || (typeof content === "undefined" && "Empty results"),
14757
+ // tagName: 'div',
14758
+ // attributes: {},
14759
+ // children: [],
14760
+ // };
14761
+
14762
+ // const ret = await func.UI.screen.render_ui_tree(SESSION_ID, $container, empty_result_node, parent_infoP, paramsP, jobNoP, null, 0, null, nodeP, null, $root_container);
14763
+ await func.events.validate(SESSION_ID, 'record_not_found', paramsP.dsSessionP);
14764
+ return await done(null);
14765
+ };
14766
+ var _ds = SESSION_OBJ[SESSION_ID].DS_GLB[paramsP.dsSessionP];
14767
+
14768
+ var continuous_idx = null;
14769
+
14770
+ if (!_ds.data_feed || _.isEmpty(_ds.data_feed.rows)) {
14771
+ return await empty_result();
14772
+ }
14773
+
14774
+ var i = 0;
14775
+ for await (const [key, val] of Object.entries(_ds.data_feed.rows)) {
14776
+ var node = JSON.parse(JSON.stringify(nodeP));
14777
+
14778
+ _ds.currentRecordId = val._ROWID;
14779
+ const ret = await iterate_child($div, node, { continuous_idx }, $root_container);
14780
+
14781
+ if (_.isEmpty($container.data().xuAttributes)) {
14782
+ await func.UI.screen.set_attributes_new(SESSION_ID, is_skeleton, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, $container, true);
14783
+ }
14784
+ }
14785
+
14786
+ return await done(continuous_idx);
14787
+ },
14788
+ [`xu-panel`]: async function () {
14789
+ const done = async function ($new_div) {
14790
+ if (!$container.data()?.xuData?.paramsP) {
14791
+ return $container;
14792
+ }
14793
+ var $div_items = $div.data().xuData.node.children;
14794
+
14795
+ await func.UI.screen.panel_post_render_handler(SESSION_ID, $container, $new_div, nodeP, $div, jobNoP);
14796
+
14797
+ // TO FIX should be timeout
14798
+ $container.data().xuData.node.children = $div_items;
14799
+
14800
+ return $container;
14801
+ };
14802
+
14803
+ var $wrapper = $('<div>');
14804
+ $div = await func.UI.screen.create_container(SESSION_ID, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, prop, null, null, null, $wrapper, '');
14805
+
14806
+ let ret = await func.UI.screen.set_attributes_new(SESSION_ID, is_skeleton, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, $div.clone(true), true, undefined, refreshed_ds);
14807
+ if (ret.abort) {
14808
+ // render N
14809
+ return (ret.$new_div = $('<template>').append($div));
14810
+ }
14811
+
14812
+ let $ret_panel_div = ret.$new_div;
14813
+
14814
+ if (!$ret_panel_div?.children()?.length) {
14815
+ ////// render default children tree
14816
+ if (nodeP.children.length) {
14817
+ $ret_panel_div = await func.UI.screen.render_ui_tree(SESSION_ID, $container, nodeP.children[0], parent_infoP, paramsP, jobNoP, null, 0, null, nodeP, null, $root_container);
14818
+ }
14819
+ }
14820
+
14821
+ let ret_done = await done($ret_panel_div);
14822
+
14823
+ return ret_done;
14824
+ },
14825
+ };
14826
+
14827
+ const draw_html_element_org = async function (element) {
14828
+ const done = async function (ret = {}) {
14829
+ return $div;
14830
+ };
14831
+ if (!element || element === 'script') return await done();
14832
+ let str = '';
14833
+
14834
+ var $div = await func.UI.screen.create_container(SESSION_ID, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, prop, null, null, element, null, str);
14835
+
14836
+ $div.hover(
14837
+ function (e) {
14838
+ hover_in($div, e);
14839
+ },
14840
+ function (e) {
14841
+ hover_out();
14842
+ },
14843
+ );
14844
+ if (paramsP.paramsP === 'grid' || parent_infoP?.iterate_info) {
14845
+ $div.on('click contextmenu', function (e) {
14846
+ hover_in($div, e);
14847
+ });
14848
+ }
14849
+
14850
+ // let ret = await func.UI.screen.set_attributes_new(SESSION_ID, is_skeleton, $container, nodeP, $div, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, $div, true);
14851
+ let ret = await func.UI.screen.set_attributes_new(SESSION_ID, is_skeleton, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, $div, true);
14852
+ if (ret.abort || nodeP.tagName === 'svg' || !_.isEmpty(nodeP?.attributes?.['xu-text']) || !_.isEmpty(nodeP?.attributes?.['xu-html']) || !_.isEmpty(nodeP?.attributes?.['xu-exp:xu-text']) || !_.isEmpty(nodeP?.attributes?.['xu-exp:xu-html'])) {
14853
+ return await done(ret);
14854
+ }
14855
+ // check if iterator made to prevent children render
14856
+
14857
+ if (nodeP?.attributes?.['xu-viewport'] == 'true') {
14858
+ // const xu_viewport = async function () {
14859
+ // const data = { $div: $div.clone(true), nodeP, parent_infoP, $root_container, paramsP, jobNoP, is_skeleton, keyP, refreshed_ds, parent_nodeP, check_existP, $container };
14860
+ // const container_id = $container.attr('xu-ui-id');
14861
+ // if (!UI_WORKER_OBJ.pending_for_viewport_render[container_id]) {
14862
+ // UI_WORKER_OBJ.pending_for_viewport_render[container_id] = { base_$div: $div, data: [], $container };
14863
+ // await iterate_child($div, nodeP, parent_infoP, $root_container);
14864
+ // } else {
14865
+ // $div.remove();
14866
+ // }
14867
+ // UI_WORKER_OBJ.pending_for_viewport_render[container_id].data.push(data);
14868
+
14869
+ // // if (!$div.children().length) {
14870
+ // // // render the first element to determine height
14871
+ // // await iterate_child($div, nodeP, parent_infoP, $root_container);
14872
+ // // // hover_in($div);
14873
+ // // // func.UI.worker.add_to_queue(SESSION_ID, 'gui event', 'render_viewport', data, null, null, paramsP.dsSessionP);
14874
+ // // } else {
14875
+ // // }
14876
+ // };
14877
+ const xu_viewport = function () {
14878
+ const observer_inViewport = new IntersectionObserver(
14879
+ function (entries) {
14880
+ entries.forEach((entry) => {
14881
+ if (entry.isIntersecting) {
14882
+ $(entry.target).trigger('inViewport');
14883
+
14884
+ // Optional: stop observing once triggered
14885
+ observer_inViewport.unobserve(entry.target);
14886
+ }
14887
+ });
14888
+ },
14889
+ {
14890
+ threshold: 0.1, // Trigger when 10% of element is visible
14891
+ },
14892
+ );
14893
+
14894
+ const observer_outViewport = new IntersectionObserver(
14895
+ function (entries) {
14896
+ entries.forEach((entry) => {
14897
+ if (!entry.isIntersecting) {
14898
+ // Element is OUT of viewport - trigger custom event
14899
+ $(entry.target).trigger('outViewport');
14900
+
14901
+ // Optional: stop observing once triggered
14902
+ // observer_outViewport.unobserve(entry.target);
14903
+ }
14904
+ });
14905
+ },
14906
+ {
14907
+ threshold: 0, // Trigger when element is completely out of view
14908
+ },
14909
+ );
14910
+
14911
+ let ui_job_id;
14912
+ $div.on('inViewport', function () {
14913
+ if ($div.children().length) {
14914
+ $div.removeClass('skeleton');
14915
+ return;
14916
+ }
14917
+
14918
+ // if (UI_WORKER_OBJ.cache[$div.attr('xu-ui-id')]) {
14919
+ // $div[0].style.removeProperty('height');
14920
+ // $div.removeClass('skeleton');
14921
+ // $div.html(UI_WORKER_OBJ.cache[$div.attr('xu-ui-id')]);
14922
+ // } else {
14923
+ hover_in($div);
14924
+ ui_job_id = func.UI.worker.add_to_queue(SESSION_ID, 'gui event', 'render_viewport', { $div, nodeP, parent_infoP, $root_container, paramsP, jobNoP, is_skeleton, keyP, refreshed_ds, parent_nodeP, check_existP, $container }, null, null, paramsP.dsSessionP);
14925
+ // }
14926
+ observer_outViewport.observe($div[0]);
14927
+ });
14928
+
14929
+ $div.on('outViewport', function () {
14930
+ func.UI.worker.delete_job(SESSION_ID, ui_job_id);
14931
+
14932
+ if ($div.children().length) {
14933
+ // UI_WORKER_OBJ.cache[$div.attr('xu-ui-id')] = $div.children().clone(true);
14934
+ $div.empty();
14935
+ const height = $div?.data()?.xuData?.viewport_height || 10;
14936
+ if (typeof height !== 'undefined') {
14937
+ $div.css('height', height);
14938
+ }
14939
+ }
14940
+ // $div.addClass('skeleton');
14941
+ observer_inViewport.observe($div[0]);
14942
+ });
14943
+ $div.addClass('skeleton');
14944
+ observer_inViewport.observe($div[0]);
14945
+ };
14946
+ xu_viewport();
14947
+ } else {
14948
+ await iterate_child($div, nodeP, parent_infoP, $root_container);
14949
+ }
14950
+
14951
+ // const ret_iterate_child = await iterate_child($div, nodeP, parent_infoP, null, $root_container);
14952
+ return await done(ret);
14953
+ };
14954
+
14955
+ const draw_html_element = async function (element) {
14956
+ const done = async function (ret = {}) {
14957
+ const xu_ui_id = $div.attr('xu-ui-id');
14958
+ $div.removeClass('display_none');
14959
+ if (ret.has_xu_exp_render_attribute) {
14960
+ // $div.css('display', 'unset');
14961
+
14962
+ const xu_render_cache_id = await get_xu_render_cache_str(SESSION_ID, paramsP.dsSessionP, Object.keys($div.data()?.xuData?.attr_exp_info?.['xu-render']?.fields || {}));
14963
+ const _$div = $div.clone(true);
14964
+ UI_WORKER_OBJ.xu_render_cache[xu_ui_id + xu_render_cache_id] = { $div: _$div, paramsP, data: _$div.data() };
14965
+ nodeP.xu_render_xu_ui_id = xu_ui_id;
14966
+ nodeP.xu_render_cache_id = xu_render_cache_id;
14967
+
14968
+ if (ret.xu_render_background_processing) {
14969
+ temp_$div.remove();
14970
+ // $container.find(`[xu-ui-id="${xu_ui_id}"]`).remove();
14971
+ return $div;
14972
+ } else {
14973
+ // $div.css('display', 'unset');
14974
+ temp_$div.replaceWith($div);
14975
+ return $div;
14976
+ }
14977
+ } else {
14978
+ if (ret.has_xu_render_attribute) {
14979
+ temp_$div.remove();
14980
+ return $div;
14981
+ }
14982
+ // $div.css('display', 'unset');
14983
+ temp_$div.replaceWith($div);
14984
+ return $div;
14985
+ }
14986
+ };
14987
+ if (!element || element === 'script') return await done();
14988
+ let str = '';
14989
+
14990
+ var temp_$div = await func.UI.screen.create_container(SESSION_ID, $root_container, nodeP, $container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, prop, null, null, element, null, str, true);
14991
+
14992
+ let temp_$container = $('<tmp>').data('xuData', $container.data().xuData);
14993
+ let $div = temp_$div.clone(true);
14994
+
14995
+ // $div.css('display', 'none');
14996
+
14997
+ $div.hover(
14998
+ function (e) {
14999
+ hover_in($div, e);
15000
+ },
15001
+ function (e) {
15002
+ hover_out();
15003
+ },
15004
+ );
15005
+ if (paramsP.paramsP === 'grid' || parent_infoP?.iterate_info) {
15006
+ $div.on('click contextmenu', function (e) {
15007
+ hover_in($div, e);
15008
+ });
15009
+ }
15010
+
15011
+ let ret = await func.UI.screen.set_attributes_new(SESSION_ID, is_skeleton, $root_container, nodeP, temp_$container, paramsP, parent_infoP, jobNoP, keyP, parent_nodeP, $div, true);
15012
+ if (ret.abort || nodeP.tagName === 'svg' || !_.isEmpty(nodeP?.attributes?.['xu-text']) || !_.isEmpty(nodeP?.attributes?.['xu-html']) || !_.isEmpty(nodeP?.attributes?.['xu-exp:xu-text']) || !_.isEmpty(nodeP?.attributes?.['xu-exp:xu-html'])) {
15013
+ return await done(ret);
15014
+ }
15015
+ // check if iterator made to prevent children render
15016
+
15017
+ if (nodeP?.attributes?.['xu-viewport'] == 'true') {
15018
+ // const xu_viewport = async function () {
15019
+ // const data = { $div: $div.clone(true), nodeP, parent_infoP, $root_container, paramsP, jobNoP, is_skeleton, keyP, refreshed_ds, parent_nodeP, check_existP, $container };
15020
+ // const container_id = $container.attr('xu-ui-id');
15021
+ // if (!UI_WORKER_OBJ.pending_for_viewport_render[container_id]) {
15022
+ // UI_WORKER_OBJ.pending_for_viewport_render[container_id] = { base_$div: $div, data: [], $container };
15023
+ // await iterate_child($div, nodeP, parent_infoP, $root_container);
15024
+ // } else {
15025
+ // $div.remove();
15026
+ // }
15027
+ // UI_WORKER_OBJ.pending_for_viewport_render[container_id].data.push(data);
15028
+
15029
+ // // if (!$div.children().length) {
15030
+ // // // render the first element to determine height
15031
+ // // await iterate_child($div, nodeP, parent_infoP, $root_container);
15032
+ // // // hover_in($div);
15033
+ // // // func.UI.worker.add_to_queue(SESSION_ID, 'gui event', 'render_viewport', data, null, null, paramsP.dsSessionP);
15034
+ // // } else {
15035
+ // // }
15036
+ // };
15037
+ const xu_viewport = function () {
15038
+ const observer_inViewport = new IntersectionObserver(
15039
+ function (entries) {
15040
+ entries.forEach((entry) => {
15041
+ if (entry.isIntersecting) {
15042
+ $(entry.target).trigger('inViewport');
15043
+
15044
+ // Optional: stop observing once triggered
15045
+ observer_inViewport.unobserve(entry.target);
15046
+ }
15047
+ });
15048
+ },
15049
+ {
15050
+ threshold: 0.1, // Trigger when 10% of element is visible
15051
+ },
15052
+ );
15053
+
15054
+ const observer_outViewport = new IntersectionObserver(
15055
+ function (entries) {
15056
+ entries.forEach((entry) => {
15057
+ if (!entry.isIntersecting) {
15058
+ // Element is OUT of viewport - trigger custom event
15059
+ $(entry.target).trigger('outViewport');
15060
+
15061
+ // Optional: stop observing once triggered
15062
+ // observer_outViewport.unobserve(entry.target);
15063
+ }
15064
+ });
15065
+ },
15066
+ {
15067
+ threshold: 0, // Trigger when element is completely out of view
15068
+ },
15069
+ );
15070
+
15071
+ let ui_job_id;
15072
+ $div.on('inViewport', function () {
15073
+ if ($div.children().length) {
15074
+ $div.removeClass('skeleton');
15075
+ return;
15076
+ }
15077
+
15078
+ // if (UI_WORKER_OBJ.cache[$div.attr('xu-ui-id')]) {
15079
+ // $div[0].style.removeProperty('height');
15080
+ // $div.removeClass('skeleton');
15081
+ // $div.html(UI_WORKER_OBJ.cache[$div.attr('xu-ui-id')]);
15082
+ // } else {
15083
+ hover_in($div);
15084
+ ui_job_id = func.UI.worker.add_to_queue(SESSION_ID, 'gui event', 'render_viewport', { $div, nodeP, parent_infoP, $root_container, paramsP, jobNoP, is_skeleton, keyP, refreshed_ds, parent_nodeP, check_existP, temp_$container }, null, null, paramsP.dsSessionP);
15085
+ // }
15086
+ observer_outViewport.observe($div[0]);
15087
+ });
15088
+
15089
+ $div.on('outViewport', function () {
15090
+ func.UI.worker.delete_job(SESSION_ID, ui_job_id);
15091
+
15092
+ if ($div.children().length) {
15093
+ // UI_WORKER_OBJ.cache[$div.attr('xu-ui-id')] = $div.children().clone(true);
15094
+ $div.empty();
15095
+ const height = $div?.data()?.xuData?.viewport_height || 10;
15096
+ if (typeof height !== 'undefined') {
15097
+ $div.css('height', height);
15098
+ }
15099
+ }
15100
+ // $div.addClass('skeleton');
15101
+ observer_inViewport.observe($div[0]);
15102
+ });
15103
+ $div.addClass('skeleton');
15104
+ observer_inViewport.observe($div[0]);
15105
+ };
15106
+ xu_viewport();
15107
+ } else {
15108
+ // if (ret.xu_render_background_processing) {
15109
+ // // let temp_$div = $div.clone(true);
15110
+ // iterate_child($div, nodeP, parent_infoP, $root_container);
15111
+ // } else {
15112
+ // await iterate_child($div, nodeP, parent_infoP, $root_container);
15113
+ // }
15114
+ if (!ret.xu_render_background_processing) {
15115
+ iterate_child($div, nodeP, parent_infoP, $root_container);
15116
+ }
15117
+ }
15118
+
15119
+ // const ret_iterate_child = await iterate_child($div, nodeP, parent_infoP, null, $root_container);
15120
+ return await done(ret);
15121
+ };
15122
+
15123
+ if (nodeP.content && nodeP.attributes) {
15124
+ nodeP.attributes['xu-content'] = nodeP.content;
15125
+ }
15126
+
15127
+ if (nodeP.tagName === 'xu-widget') {
15128
+ if (is_skeleton) return;
15129
+ return await fx['widget']();
15130
+ }
15131
+ if (fx[nodeP.tagName]) {
15132
+ return await fx[nodeP.tagName]();
15133
+ }
15134
+ // const xu_viewport = async function () {
15135
+ // const data = { $div, nodeP, parent_infoP, $root_container, paramsP, jobNoP, is_skeleton, keyP, refreshed_ds, parent_nodeP, check_existP, $container };
15136
+ // const container_id = $container.attr('xu-ui-id');
15137
+ // if (!UI_WORKER_OBJ.pending_for_viewport_render[container_id]) {
15138
+ // UI_WORKER_OBJ.pending_for_viewport_render[container_id] = { base_$div: $div, data: [], $container };
15139
+ // await iterate_child($div, nodeP, parent_infoP, $root_container);
15140
+ // }
15141
+ // UI_WORKER_OBJ.pending_for_viewport_render[container_id].data.push(data);
15142
+ // };
15143
+
15144
+ // if (nodeP?.attributes?.['xu-viewport'] == 'true') {
15145
+ // return await xu_viewport();
15146
+ // } else {
15147
+ if (!glb.new_xu_render) {
15148
+ return await draw_html_element_org(nodeP.tagName);
15149
+ }
15150
+ return await draw_html_element(nodeP.tagName);
15151
+
15152
+ // }
15153
+ };
15154
+
15155
+ func.UI.screen.refresh_document_changes_for_realtime_update = async function (SESSION_ID, doc_change) {
15156
+ let _session = SESSION_OBJ[SESSION_ID];
15157
+ for (const [key, _ds] of Object.entries(_session.DS_GLB)) {
15158
+ let prog_obj = await func.utils.VIEWS_OBJ.get(SESSION_ID, _ds.prog_id);
15159
+ if (prog_obj?.progDataSource?.dataSourceRealtime && prog_obj?.progDataSource?.dataSourceTableId === doc_change.table_id) {
15160
+ try {
15161
+ // disabled in purpose to support create row
15162
+
15163
+ if (!_ds.screen_params) continue;
15164
+ if (_ds.screen_params.is_panelP) {
15165
+ func.UI.screen.refresh_screen(SESSION_ID, null, key);
15166
+ } else {
15167
+ func.action.execute(SESSION_ID, 'act_refresh', _ds, null, null);
15168
+ }
15169
+ } catch (err) {
15170
+ // console.error(err);
15171
+ }
15172
+ }
15173
+ }
15174
+
15175
+ if (glb.new_xu_render) {
15176
+ for (const [ui_cache_key, ui_cache_val] of Object.entries(UI_WORKER_OBJ.xu_render_cache)) {
15177
+ let prog_obj = await func.utils.VIEWS_OBJ.get(SESSION_ID, ui_cache_val.paramsP.prog_id);
15178
+ if (prog_obj?.progDataSource?.dataSourceTableId === doc_change.table_id) {
15179
+ ui_cache_val.$div = null;
15180
+ }
15181
+ }
15182
+ }
15183
+ };
15184
+
15185
+ const get_parent_ds_fields = function (SESSION_ID, dsSessionP) {
15186
+ var _session = SESSION_OBJ[SESSION_ID];
15187
+ var _ds = _session.DS_GLB[dsSessionP];
15188
+
15189
+ const idx = func.common.find_ROWID_idx(_ds, _ds.currentRecordId);
15190
+ const data = _ds.data_feed.rows[idx];
15191
+
15192
+ let obj = {};
15193
+
15194
+ if (typeof _ds.parentDataSourceNo !== 'undefined') {
15195
+ obj = get_parent_ds_fields(SESSION_ID, _ds.parentDataSourceNo);
15196
+ }
15197
+
15198
+ return { ...data, ...obj };
15199
+ };
15200
+
15201
+ const get_xu_render_cache_str = async function (SESSION_ID, dsSessionP, exclude_vars = []) {
15202
+ const fields_obj = get_parent_ds_fields(SESSION_ID, dsSessionP);
15203
+
15204
+ let str = '';
15205
+
15206
+ for (const [key, val] of Object.entries(fields_obj)) {
15207
+ if (exclude_vars.includes(key)) continue;
15208
+ str += typeof val !== 'undefined' ? JSON.stringify(val) : '';
15209
+ }
15210
+
15211
+ return 'C-' + (await func.common.sha256(str));
15212
+ };
15213
+ func.UI.component = {};
11272
15214
 
11273
15215
  func.UI.component.create_app_modal_component = function (
11274
15216
  SESSION_ID,