"use strict"; var luna_utils = {}; // luna_utils.add_spacer_div_npx(container_div_id); luna_utils.add_spacer_div_npx = function(container_div_id, npx) { if (!container_div_id) { return; } var $container = $("#" + container_div_id); if ($container.length === 0) { return; } var $table = $container.find("table").first(); if ($table.length === 0) { return; } var spacer_div_id = container_div_id + "_right_spacer_div_npx"; var $spacer = $("#" + spacer_div_id); /* ----------------------------------------- Stable overflow detection ----------------------------------------- */ var tableScrollWidth = $table[0].scrollWidth; var containerClientWidth = $container[0].clientWidth; var needsSpacer = tableScrollWidth > containerClientWidth; /* ----------------------------------------- If no overflow → remove spacer ----------------------------------------- */ if (!needsSpacer) { if ($spacer.length) { $spacer.remove(); } return; } /* ----------------------------------------- Ensure horizontal layout ----------------------------------------- */ $container.css({ "display": "inline-flex", "max-width": "100%" }); $table.css({ "flex": "0 0 auto" }); /* ----------------------------------------- Create spacer if missing ----------------------------------------- */ if ($spacer.length === 0) { $spacer = $("
").attr("id", spacer_div_id).insertAfter($table); } /* ----------------------------------------- Apply spacer CSS ----------------------------------------- */ $spacer.css({ "width": npx + "px", "flex": "0 0 auto" }); }; luna_utils.get_current_date_mm_yyyy = function() { var now = new Date(); var year = now.getFullYear(); var month = now.getMonth() + 1; // Months are 0-indexed // Add leading zero if needed if (month < 10) { month = '0' + month; } return month + '/' + year; }; // luna_utils.do_blur(do_blur_condition, jquery_selector); luna_utils.do_blur = function(do_blur_condition, jquery_selector) { if (do_blur_condition) { let element_with_focus = $(jquery_selector); if (element_with_focus.length > 0) { $(element_with_focus[0]).blur(); } } }; // luna_utils.focus_if_not_on_mob(jquery_selector); luna_utils.focus_if_not_on_mob = function(jquery_selector) { if (typeof cordova != 'undefined' || luna_utils.is_mobile_browser()) { return; } $(jquery_selector).focus(); }; // luna_utils.select_if_not_on_mob(jquery_selector); luna_utils.select_if_not_on_mob = function(jquery_selector) { if (typeof cordova != 'undefined' || luna_utils.is_mobile_browser()) { return; } $(jquery_selector).select(); }; /* input = "1.00", output : "1" input = "1", output : "1" input = "2.3", output : "2.3" input = "2.29", output : "2.29" input = "10", output : "10" input = "10,000.00", output : "10,000" input = "10,000.02", output : "10,000.02" input = "10,000.20", output : "10,000.2" input = "xxx", output : "xxx" */ luna_utils.remove_trailing_zeros_from_formatted_number = function(input) { // Check if the input contains a decimal point if (!input.includes('.')) { return input; } // Remove trailing zeros after the decimal point // This regex matches trailing zeros after a decimal point at the end of the string let result = input.replace(/(\.\d*?)0+$/, '$1'); // If we're left with just a decimal point (e.g., "1."), remove it result = result.replace(/\.$/, ''); return result; }; luna_utils.are_all_options_selected = function(multiple_select_fieldID) { let selected_options = $("#" + multiple_select_fieldID).multipleSelect('getSelects'); let all_options = $("#" + multiple_select_fieldID).find('option'); return selected_options.length === all_options.length && all_options.length > 0; }; luna_utils.populate_field_from_hash_param = function(fieldID, paramID) { const unexpected_value = 'BnO9QQSLcfizIYK2GCc4'; let param_value = luna_app.get_hash_param_value(paramID, unexpected_value); if (param_value != unexpected_value) { let element = $("#" + fieldID); if (element.hasClass('multiselect')) { element.multipleSelect('setSelects', [param_value]); } else { element.val(param_value); } return true; } return false; }; luna_utils.sleep = async function(ms) { return new Promise(resolve => setTimeout(resolve, ms)); }; luna_utils.wait_for_selector = async function(selector, timeout = 60000, pollInterval = 100) { const endTime = Date.now() + timeout; let found; do { found = $(selector); if (found.length > 0) { return found; } console.log("Waiting for selector ... " + selector); await luna_utils.sleep(pollInterval); } while (Date.now() < endTime); // throw new Error("Timeout waiting for selector: " + selector); console.log("Timeout waiting for selector: " + selector); return false; }; luna_utils.escapeHtmlAttr = function(str) { if (typeof str == 'undefined' || str === null) { return ''; } if (str !== '' && typeof str == 'string') { str = luna_utils.replaceAll(str, '&', '&'); str = luna_utils.replaceAll(str, '"', '"'); str = luna_utils.replaceAll(str, '<', '<'); str = luna_utils.replaceAll(str, '>', '>'); str = luna_utils.replaceAll(str, "'", '''); } return str; }; luna_utils.shuffle = function(str) { return str.split('').sort(function () { return Math.random() - 0.5; }).join(''); }; luna_utils.generate_random_password = function() { const letters = 'abdfghjkmnpqrstwxyz'; const specials = '@$'; const digits = '2345679'; // const shuffle = str => str.split('').sort(() => Math.random() - 0.5).join(''); const part1 = luna_utils.shuffle(letters).substring(0, 2); const part2 = luna_utils.shuffle(specials).substring(0, 1); const part3 = luna_utils.shuffle(digits).substring(0, 3); const part4 = luna_utils.shuffle(letters).substring(0, 2); return part1 + part2 + part3 + part4; }; // luna_utils.show_modal(title, body_html, add_share_btn); luna_utils.show_modal = function(title, body_html, add_share_btn) { luna_modal.show_modal(title, body_html, add_share_btn); }; // let ddmmyyy = luna_utils.convert_from_yyyymmdd_to_ddmmyyy(yyyymmdd) // Has test cases luna_utils.convert_from_yyyymmdd_to_ddmmyyy = function(yyyymmdd) { let [year, month, day] = yyyymmdd.split('-'); day = day.padStart(2, '0'); month = month.padStart(2, '0'); return `${day}/${month}/${year}`; }; // let ddmmyyy__hhmmss = luna_utils.convert_from_yyyymmdd__hhmmss_to_ddmmyyy__hhmmss(yyyymmdd__hhmmss) luna_utils.convert_from_yyyymmdd__hhmmss_to_ddmmyyy__hhmmss = function(yyyymmdd__hhmmss) { let [yyyymmdd, hhmmss] = yyyymmdd__hhmmss.split(' '); const ddmmyyy = luna_utils.convert_from_yyyymmdd_to_ddmmyyy(yyyymmdd); return ddmmyyy + " " + hhmmss; }; luna_utils.show_solution = function(error_message, solution, close_current_page) { const response = { response_code: -1, response_message: error_message, solution: solution, }; close_current_page = typeof close_current_page == 'undefined' ? true : close_current_page; if (luna_app.is_mobile_app) { if (close_current_page) { dsplma.render_menu(false); } dsplma.show_response_message(response); } else { if (close_current_page) { luna_app.close_page(); } luna_app.show_response_message(response); } }; luna_utils.get_solution = function(priv_id) { let sub_steps = []; if (priv_id == 10001) { sub_steps = [ 'Login to ERP as Administrator', 'Go to Control Panel -> Administration -> Assign Privilege Scope', 'Select Privilege Feed Attendance', 'Select Year ' + luna_utils.get_session_name(luna_datac.current_academic_session) + '', 'Select User ' + luna_datac.login_id + '', 'Click Show button', 'Tick required Class(es)', 'Click Save button', ]; } if (priv_id == 5001) { sub_steps = [ 'Login to ERP as Administrator', 'Go to Control Panel -> Administration -> Assign Privilege Scope', 'Select Privilege Feed Marks', 'Select Year ' + luna_utils.get_session_name(luna_datac.current_academic_session) + '', 'Select User ' + luna_datac.login_id + '', 'Select Class as applicable', 'Click Show button', 'Tick required Subject(s)', 'Click Save button', ]; } if (priv_id == 4059) { sub_steps = [ 'Login to ERP as Administrator', 'Go to Control Panel -> Administration -> Assign Privilege Scope', 'Select Privilege Student Module', 'Select Year ' + luna_utils.get_session_name(luna_datac.current_academic_session ) + '', 'Select User ' + luna_datac.login_id + '', 'Click Show button', 'Tick required Class(es)', 'Click Save button', ]; } let solution = [ {'heading' : 'Request your Administrator to do this:', 'sub_steps' : sub_steps}, {'heading' : 'After your Administrator completes the above steps, you need to log off and login again'}, ]; if (sub_steps.length == 0) { solution = []; } return solution; }; /* luna_utils.fetch = async function(url, options, success_function) { try { console.log('fetch_using_get. a1 ' + url); let response = await fetch(url); console.log('fetch_using_get. a2 ' + JSON.stringify(response)); let text = await response.text(); console.log('fetch_using_get. a3'); success_function(text); console.log('fetch_using_get. a4'); } catch(err) { console.log('fetch_using_get err'); console.log(err.message); console.log(err.name); // console.log(err.stack); console.log(JSON.stringify(err)); } }; luna_utils.fetch_using_get = function(url, options, success_function) { try { console.log('fetch_using_get a1 a' + url); fetch(url).then(function(response) { console.log('fetch_using_get a2'); response.text().then( function(text) { console.log('fetch_using_get a3'); success_function(text); console.log('fetch_using_get a4'); } ); }); console.log('fetch_using_get a1 b' + url); } catch(err) { console.log('fetch_using_get err'); console.log(JSON.stringify(err)); } }; */ luna_utils.log = function(input) { if (luna_app.is_mobile_app) { dsplma.log(input); } else { console.log(input); } }; // Same as dsplma.get_current_time_hh_mm_ss luna_utils.get_current_time_hh_mm_ss = function() { const date = new Date(); let hours = date.getHours(); let minutes = date.getMinutes(); let seconds = date.getSeconds(); hours = hours > 9 ? hours : '0' + hours; minutes = minutes > 9 ? minutes : '0' + minutes; seconds = seconds > 9 ? seconds : '0' + seconds; return hours + ":" + minutes + ":" + seconds; }; luna_utils.process_luna_datac = function() { luna_datac.bank_fee_collection_modes = luna_utils.filter_JSON_Array(luna_datac.fee_collection_modes, 'fcm_type', 'BANK'); luna_datac.all_subjects = luna_datac.subjects.concat(luna_datac.aggregate_subjects); luna_datac.all_subjects = luna_utils.sortJsonArrayByKey(luna_datac.all_subjects, 'sequence') }; // Has test cases luna_utils.auto_correct_mobile_num_field = function($field) { try { let value = $field.val(); value = "" + value; let new_val = luna_utils.replaceAll(value, ' ', ''); new_val = luna_utils.replaceAll(new_val, '/', ','); if (new_val != value) { $field.val(new_val); value = new_val; } return value; } catch(err) { return 'ERR9484727'; // luna_utils.auto_correct_mobile_num_field2($field); } }; // Has test cases luna_utils.set_val = function(field_id, value, default_value) { value = typeof value == 'undefined' ? default_value : value; $("#" + field_id).val(value); }; luna_utils.get_current_month_num = function() { const d = new Date(); return 1 + d.getMonth(); }; luna_utils.get_response = function(___ca, data2send, success_function, options) { if (luna_app.is_mobile_app) { dsplma.sendMessageToServer(___ca, data2send, success_function); } else { let request_object = { ___ca : ___ca, data2send : data2send, success_function : success_function }; if (typeof options != 'undefined') { for (let attr in options) { request_object[attr] = options[attr]; } } luna_ws.get_response(request_object); } }; luna_utils.show_response_message = function(response) { if (luna_app.is_mobile_app) { return dsplma.show_response_message(response); } return luna_app.show_response_message(response); }; // Has test cases // Returns output in this format : 2-Mar-2025 luna_utils.today_dd_mmm_yyyy = function() { const date = new Date(); const formattedDate = date.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric', }).replace(/ /g, '-'); return formattedDate; }; luna_utils.fill_luna_dynamic_css = function() { var luna_dynamic_css = ''; var att_codes = luna_datac.att_codes; for (var x=0; x