1- function onChange ( control , oldValue , newValue , isLoading ) {
2- if ( isLoading || newValue === oldValue ) {
3- return ;
4- }
5-
6- showProgressBar ( control , newValue ) ;
1+ function onLoad ( ) {
2+ var fieldName = 'short_description' ; // Change to your field name
3+ var maxLength = 160 ;
4+ var warningThreshold = 0.7 ;
5+ var criticalThreshold = 0.9 ;
6+
7+ var fieldElement = g_form . getControl ( fieldName ) ;
8+ if ( ! fieldElement ) return ;
9+
10+ // Add progress bar initially
11+ addProgressBar ( fieldElement , fieldElement . value ) ;
12+
13+ // Attach keyup event for real-time updates
14+ fieldElement . addEventListener ( 'keyup' , function ( ) {
15+ addProgressBar ( fieldElement , fieldElement . value ) ;
16+ } ) ;
717}
818
9- function showProgressBar ( control , value ) {
10- var maxLength = 160 ; // Configurable maximum length
11- var warningThreshold = 0.7 ; // Show yellow at 70% capacity
12- var criticalThreshold = 0.9 ; // Show red at 90% capacity
13-
19+ function addProgressBar ( fieldElement , value ) {
20+ var maxLength = 160 ;
21+ var warningThreshold = 0.7 ;
22+ var criticalThreshold = 0.9 ;
23+
1424 // Remove any existing progress bar
15- var existingBar = gel ( control . name + '_progress' ) ;
25+ var existingBar = document . getElementById ( fieldElement . name + '_progress' ) ;
1626 if ( existingBar ) {
1727 existingBar . parentNode . removeChild ( existingBar ) ;
1828 }
19-
29+
2030 // Create progress bar container
2131 var container = document . createElement ( 'div' ) ;
22- container . id = control . name + '_progress' ;
32+ container . id = fieldElement . name + '_progress' ;
2333 container . style . cssText = 'width: 100%; height: 4px; background: #e0e0e0; margin-top: 4px; border-radius: 2px; transition: all 0.3s ease;' ;
24-
34+
2535 // Create progress bar fill
2636 var fill = document . createElement ( 'div' ) ;
2737 fill . style . cssText = 'height: 100%; width: 0%; border-radius: 2px; transition: all 0.3s ease;' ;
28-
38+
2939 // Calculate percentage
3040 var percent = ( value . length / maxLength ) * 100 ;
31- percent = Math . min ( percent , 100 ) ; // Cap at 100%
32-
41+ percent = Math . min ( percent , 100 ) ;
42+
3343 // Set fill width and color
3444 fill . style . width = percent + '%' ;
3545 if ( percent >= criticalThreshold * 100 ) {
36- fill . style . backgroundColor = '#ff4444' ; // Red
46+ fill . style . backgroundColor = '#ff4444' ;
3747 } else if ( percent >= warningThreshold * 100 ) {
38- fill . style . backgroundColor = '#ffbb33' ; // Yellow
48+ fill . style . backgroundColor = '#ffbb33' ;
3949 } else {
40- fill . style . backgroundColor = '#00C851' ; // Green
50+ fill . style . backgroundColor = '#00C851' ;
4151 }
42-
52+
4353 // Create percentage label
4454 var label = document . createElement ( 'div' ) ;
4555 label . style . cssText = 'font-size: 11px; color: #666; margin-top: 2px; text-align: right;' ;
4656 label . textContent = Math . round ( percent ) + '% used' ;
47-
57+
4858 // Assemble and insert the progress bar
4959 container . appendChild ( fill ) ;
5060 container . appendChild ( label ) ;
51-
52- // Insert after the control
53- var parent = control . getElement ( ) . parentNode ;
54- parent . insertBefore ( container , control . getElement ( ) . nextSibling ) ;
55-
61+
62+ // Insert after the field element
63+ var parent = fieldElement . parentNode ;
64+ parent . insertBefore ( container , fieldElement . nextSibling ) ;
65+
5666 // Add warning message if over limit
5767 if ( value . length > maxLength ) {
5868 g_form . addErrorMessage ( 'This field exceeds the maximum length of ' + maxLength + ' characters' ) ;
5969 } else {
6070 g_form . clearMessages ( ) ;
6171 }
62- }
72+ }
0 commit comments