Project

General

Profile

« Previous | Next » 

Revision 08ec2d99

Added by Stephen Beaver over 9 years ago

Completes #5159
All duplicated JS removed to included file

View differences:

src/usr/local/www/services_ntpd.php
501 501

  
502 502
?>
503 503

  
504
<script>
505
	// If this variable is declared, any help text will not be deleted when rows are added
506
	// IOW the help text will appear on every row
507
	retainhelp = true;
508
</script>
509

  
504 510
<script>
505 511
//<![CDATA[
506 512
events.push(function(){
507 513

  
508
	function setMasks() {
509
		// Find all ipaddress masks and make dynamic based on address family of input
510
		$('span.pfIpMask + select').each(function (idx, select){
511
			var input = $(select).prevAll('input[type=text]');
512

  
513
			input.on('change', function(e){
514
				var isV6 = (input.val().indexOf(':') != -1), min = 0, max = 128;
515
				if (!isV6)
516
					max = 32;
517

  
518
				if (input.val() == "")
519
					return;
520

  
521
				while (select.options.length > max)
522
					select.remove(0);
523

  
524
				if (select.options.length < max)
525
				{
526
					for (var i=select.options.length; i<=max; i++)
527
						select.options.add(new Option(i, i), 0);
528
				}
529
			});
530

  
531
			// Fire immediately
532
			input.change();
533
		});
534
	}
535

  
536
	// Complicated function to move all help text associated with this input id to the same id
537
	// on the row above. That way if you delete the last row, you don't lose the help
538
	function moveHelpText(id) {
539
		$('#' + id).parent('div').parent('div').find('input').each(function() {	 // For each <span></span>
540
			var fromId = this.id;
541
			var toId = decrStringInt(fromId);
542
			var helpSpan;
543

  
544
			if(!$(this).hasClass('pfIpMask') && !$(this).hasClass('btn')) {
545

  
546
				helpSpan = $('#' + fromId).parent('div').parent('div').find('span:last').clone();
547
				if($(helpSpan).hasClass('help-block')) {
548
					if($('#' + decrStringInt(fromId)).parent('div').hasClass('input-group'))
549
						$('#' + decrStringInt(fromId)).parent('div').after(helpSpan);
550
					else
551
						$('#' + decrStringInt(fromId)).after(helpSpan);
552
				}
553
			}
554
		});
555
	}
556

  
557
	// Increment the number at the end of the string
558
	function bumpStringInt( str )	{
559
	  var data = str.match(/(\D*)(\d+)(\D*)/), newStr = "";
560

  
561
	  if( data )
562
		newStr = data[ 1 ] + ( Number( data[ 2 ] ) + 1 ) + data[ 3 ];
563

  
564
	  return newStr || str;
565
	}
566

  
567
	// Decrement the number at the end of the string
568
	function decrStringInt( str )	{
569
	  var data = str.match(/(\D*)(\d+)(\D*)/), newStr = "";
570

  
571
	  if( data )
572
		newStr = data[ 1 ] + ( Number( data[ 2 ] ) - 1 ) + data[ 3 ];
573

  
574
	  return newStr || str;
575
	}
576

  
577
	// Called after a delete so that there are no gaps in the numbering. Most of the time the config system doesn't care about
578
	// gaps, but I do :)
579
	function renumber() {
580
		var idx = 0;
581

  
582
		$('.repeatable').each(function() {
583

  
584
			$(this).find('input').each(function() {
585
				$(this).prop("id", this.id.replace(/\d+$/, "") + idx);
586
				$(this).prop("name", this.name.replace(/\d+$/, "") + idx);
587
			});
588

  
589
			$(this).find('select').each(function() {
590
				$(this).prop("id", this.id.replace(/\d+$/, "") + idx);
591
				$(this).prop("name", this.name.replace(/\d+$/, "") + idx);
592
			});
593

  
594
			$(this).find('label').attr('for', $(this).find('label').attr('for').replace(/\d+$/, "") + idx);
595

  
596
			idx++;
597
		});
598
	}
599

  
600
	function delete_row(row) {
601
		$('#' + row).parent('div').parent('div').remove();
602
		renumber();
603
	}
604

  
605
	function add_row() {
606
		// Find the lst repeatable group
607
		var lastRepeatableGroup = $('.repeatable:last');
608

  
609
		// Clone it
610
		var newGroup = lastRepeatableGroup.clone(true);
611

  
612
		// Increment the suffix number for each input elemnt in the new group
613
		$(newGroup).find('input').each(function() {
614
			$(this).prop("id", bumpStringInt(this.id));
615
			$(this).prop("name", bumpStringInt(this.name));
616
			if(!$(this).is('[id^=delete]'))
617
				$(this).val('');
618
		});
619

  
620
		// Do the same for selectors
621
		$(newGroup).find('select').each(function() {
622
			$(this).prop("id", bumpStringInt(this.id));
623
			$(this).prop("name", bumpStringInt(this.name));
624
			// If this selector lists mask bits, we need it to be reset to all 128 options
625
			// and no items selected, so that automatic v4/v6 selection still works
626
			if($(this).is('[id^=address_subnet]')) {
627
				$(this).empty();
628
				for(idx=128; idx>0; idx--) {
629
					$(this).append($('<option>', {
630
						value: idx,
631
						text: idx
632
					}));
633
				}
634
			}
635
		});
636

  
637
		// And for "for" tags
638
		$(newGroup).find('label').attr('for', bumpStringInt($(newGroup).find('label').attr('for')));
639
		$(newGroup).find('label').text(""); // Clear the label. We only want it on the very first row
640

  
641
		// Insert the updated/cloned row
642
		$(lastRepeatableGroup).after(newGroup);
643
/*
644
		// Delete any help text from the group we have cloned
645
		$(lastRepeatableGroup).find('.help-block').each(function() {
646
			$(this).remove();
647
		});
648
*/
649
		setMasks();
650

  
651
		$('[id^=address]').autocomplete({
652
			source: addressarray
653
		});
654
	}
655

  
656
	// These are action buttons, not submit buttons
657
	$('[id^=addrow]').prop('type','button');
658
	$('[id^=delete]').prop('type','button');
659

  
660
	// on click . .
661
	$('[id^=addrow]').click(function() {
662
		add_row();
663
	});
664

  
665
	$('[id^=delete]').click(function(event) {
666
		if($('.repeatable').length > 1) {
667
	//		moveHelpText(event.target.id);
668
			delete_row(event.target.id);
669
		}
670
		else
671
			alert('<?php echo gettext("You may not delete the last one!")?>');
672
	});
673

  
674 514
	// Make the ‘clear’ button a plain button, not a submit button
675 515
	$('#btnadvstats').prop('type','button');
676 516

  

Also available in: Unified diff