Source for file InstantClientScript.phtml

Documentation is available at InstantClientScript.phtml

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework - InstantClientScript template.
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Forms
  11. 11:  *
  12. 12:  * @param      string    $formName 
  13. 13:  * @param      this      InstantClientScript
  14. 14:  * @return     void 
  15. 15:  */
  16. 16:  
  17. 17:  
  18. 18:  
  19. 19: ?>
  20. 20: <script type="text/javascript">
  21. 21: /* <![CDATA[ */
  22. 22:  
  23. 23: var nette = nette || { };
  24. 24:  
  25. 25: nette.getValue = function(elem) {
  26. 26:     if (!elem) {
  27. 27:         var undefined;
  28. 28:         return undefined;
  29. 29:     }
  30. 30:  
  31. 31:     if (!elem.nodeName) { // radio
  32. 32:         for (var i = 0, len = elem.length; i < len; i++) {
  33. 33:             if (elem[i].checked) {
  34. 34:                 return elem[i].value;
  35. 35:             }
  36. 36:         }
  37. 37:         return null;
  38. 38:     }
  39. 39:  
  40. 40:     if (elem.nodeName.toLowerCase() === 'select') {
  41. 41:         var index = elem.selectedIndex, options = elem.options;
  42. 42:  
  43. 43:         if (index < 0) {
  44. 44:             return null;
  45. 45:  
  46. 46:         } else if (elem.type === 'select-one') {
  47. 47:             return options[index].value;
  48. 48:         }
  49. 49:  
  50. 50:         for (var i = 0, values = [], len = options.length; i < len; i++) {
  51. 51:             if (options[i].selected) {
  52. 52:                 values.push(options[i].value);
  53. 53:             }
  54. 54:         }
  55. 55:         return values;
  56. 56:     }
  57. 57:  
  58. 58:     if (elem.type === 'checkbox') {
  59. 59:         return elem.checked;
  60. 60:     }
  61. 61:  
  62. 62:     return elem.value.replace(/^\s+|\s+$/g, '');
  63. 63: }
  64. 64:  
  65. 65:  
  66. 66: nette.getFormValidators = function(form) {
  67. 67:     var name = form.getAttributeNode('name').nodeValue;
  68. 68:     return this.forms[name] ? this.forms[name].validators : [];
  69. 69: }
  70. 70:  
  71. 71:  
  72. 72: nette.validateControl = function(control) {
  73. 73:     var validator = this.getFormValidators(control.form)[control.name];
  74. 74:     return validator ? validator(control) : null;
  75. 75: }
  76. 76:  
  77. 77:  
  78. 78: nette.validateForm = function(sender) {
  79. 79:     var form = sender.form || sender;
  80. 80:     var validators = this.getFormValidators(form);
  81. 81:     for (var name in validators) {
  82. 82:         var error = validators[name](sender);
  83. 83:         if (error) {
  84. 84:             if (form[name].focus) {
  85. 85:                 form[name].focus();
  86. 86:             }
  87. 87:             alert(error);
  88. 88:             return false;
  89. 89:         }
  90. 90:     }
  91. 91:     return true;
  92. 92: }
  93. 93:  
  94. 94:  
  95. 95: nette.toggle = function(id, visible) {
  96. 96:     var elem = document.getElementById(id);
  97. 97:     if (elem) {
  98. 98:         elem.style.display = visible ? "" : "none";
  99. 99:     }
  100. 101:  
  101. 102:  
  102. 103: nette.forms = nette.forms || { };
  103. 104:  
  104. 105: nette.forms[<?php echo $formName ?>] = {
  105. 106:     validators: {
  106. 107: <?php $count count($this->validateScripts)?>
  107. 108: <?php foreach ($this->validateScripts as $name => $validateScript)?>
  108. 109:         <?php echo json_encode((string) $name?>: function(sender) {
  109. 110:             var res, form = sender.form || sender;
  110. 111: <?php echo String::indent($validateScript3?>
  111. 112:         }<?php echo --$count ',' '' ?>
  112. 113:  
  113. 114: <?php endforeach ?>
  114. 115:     },
  115. 116:  
  116. 117:     toggle: function(sender) {
  117. 118:         var visible, res, form = document.forms[<?php echo $formName ?>];
  118. 119: <?php echo String::indent($this->toggleScript2?>
  119. 120:     }
  120. 122:  
  121. 123:  
  122. 124: <?php if ($this->toggleScript)?>
  123. 125: nette.forms[<?php echo $formName ?>].toggle();
  124. 126: <?php endif ?>
  125. 127:  
  126. 128: /* ]]> */
  127. 129: </script>