cas.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* global trackGeoLocation, jqueryReady */
  2. /* exported resourceLoadedSuccessfully */
  3. function requestGeoPosition() {
  4. // console.log('Requesting GeoLocation data from the browser...');
  5. if (navigator.geolocation) {
  6. navigator.geolocation.getCurrentPosition(showGeoPosition, logGeoLocationError,
  7. {maximumAge: 600000, timeout: 5000, enableHighAccuracy: true});
  8. } else {
  9. // console.log('Browser does not support Geo Location');
  10. }
  11. }
  12. function logGeoLocationError(error) {
  13. switch (error.code) {
  14. case error.PERMISSION_DENIED:
  15. // console.log('User denied the request for GeoLocation.');
  16. break;
  17. case error.POSITION_UNAVAILABLE:
  18. // console.log('Location information is unavailable.');
  19. break;
  20. case error.TIMEOUT:
  21. // console.log('The request to get user location timed out.');
  22. break;
  23. default:
  24. // console.log('An unknown error occurred.');
  25. break;
  26. }
  27. }
  28. function showGeoPosition(position) {
  29. $('[name="geolocation"]').val(position.coords.latitude + ','
  30. + position.coords.longitude + ',' + position.coords.accuracy + ',' + position.timestamp);
  31. }
  32. function preserveAnchorTagOnForm() {
  33. $('#fm1').submit(function () {
  34. var location = self.document.location;
  35. var hash = decodeURIComponent(location.hash);
  36. if (hash != undefined && hash != '' && hash.indexOf('#') === -1) {
  37. hash = '#' + hash;
  38. }
  39. var action = $('#fm1').attr('action');
  40. if (action == undefined) {
  41. action = location.href;
  42. } else {
  43. var qidx = location.href.indexOf('?');
  44. if (qidx != -1) {
  45. var queryParams = location.href.substring(qidx);
  46. action += queryParams;
  47. }
  48. }
  49. action += hash;
  50. $('#fm1').attr('action', action);
  51. });
  52. }
  53. function areCookiesEnabled() {
  54. if ($.cookie == undefined) {
  55. return;
  56. }
  57. $.cookie('cookiesEnabled', 'true');
  58. var value = $.cookie('cookiesEnabled');
  59. $.removeCookie('cookiesEnabled');
  60. return value != undefined;
  61. }
  62. function disableEmptyInputFormSubmission() {
  63. var fields = $('#fm1 input[name="username"],[name="password"]');
  64. if (fields.length == 2) {
  65. fields.on('input', function (event) {
  66. var enableSubmission = $('#fm1 input[name="username"]').val().trim() &&
  67. $('#fm1 input[name="password"]').val().trim();
  68. if (enableSubmission) {
  69. $('#fm1 input[name=submit]').removeAttr('disabled');
  70. event.stopPropagation();
  71. } else {
  72. $('#fm1 input[name=submit]').attr('disabled', 'true');
  73. }
  74. });
  75. }
  76. /**
  77. * Handle auto-complete events to the extent possible.
  78. */
  79. if ($('#fm1 input[name="username"]').length > 0) {
  80. setTimeout(function () {
  81. var uid = $('#username').val();
  82. if (uid != null && uid != '') {
  83. $('#username').change();
  84. $('#username').focus();
  85. $('#fm1 input[name=submit]').removeAttr('disabled');
  86. }
  87. }, 100);
  88. }
  89. }
  90. function resourceLoadedSuccessfully() {
  91. $(document).ready(function () {
  92. if (trackGeoLocation) {
  93. requestGeoPosition();
  94. }
  95. if ($(':focus').length === 0) {
  96. $('input:visible:enabled:first').focus();
  97. }
  98. if (areCookiesEnabled()) {
  99. $('#cookiesDisabled').hide();
  100. } else {
  101. $('#cookiesDisabled').show();
  102. }
  103. disableEmptyInputFormSubmission();
  104. preserveAnchorTagOnForm();
  105. $('#capslock-on').hide();
  106. $('#fm1 input[name="username"],[name="password"]').trigger('input');
  107. $('#fm1 input[name="username"]').focus();
  108. $('#password').keypress(function (e) {
  109. var s = String.fromCharCode(e.which);
  110. if (s.toUpperCase() === s && s.toLowerCase() !== s && !e.shiftKey) {
  111. $('#capslock-on').show();
  112. } else {
  113. $('#capslock-on').hide();
  114. }
  115. });
  116. if (typeof(jqueryReady) == 'function') {
  117. jqueryReady();
  118. }
  119. });
  120. }