/modules/mod_base/lib/js/modules/z.inputoverlay.js

https://code.google.com/p/zotonic/ · JavaScript · 123 lines · 50 code · 8 blank · 65 comment · 11 complexity · 48183ba8522ce577dbb8ffd66105f8e5 MD5 · raw file

  1. /* inputoverlay js
  2. ----------------------------------------------------------
  3. @package: Zotonic 2010
  4. @Author: Marc Worrell <marc@worrell.nl>
  5. Copyright 2010 Marc Worrell
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. ---------------------------------------------------------- */
  16. /*
  17. This widget overlays a label field with an input field. When the input
  18. is empty the label is visible. When there is content then the label is hidden.
  19. HTML:
  20. <p class="do_inputoverlay">
  21. <span>Username</span>
  22. <input type="text" id="username" name="username" value="" />
  23. </p>
  24. CSS:
  25. p.do_inputoverlay {
  26. margin: 0px;
  27. padding: 0px;
  28. position: relative;
  29. height: 40px;
  30. font-size: 18px;
  31. }
  32. p.do_inputoverlay input {
  33. position: absolute;
  34. left: 0px;
  35. background: none;
  36. font-size: 18px;
  37. }
  38. p.do_inputoverlay span {
  39. position: absolute;
  40. left: 8px;
  41. top: 5px;
  42. color: #aaa;
  43. }
  44. p.do_inputoverlay span.focus {
  45. color: #d8d8d8;
  46. }
  47. p.do_inputoverlay span.hidden {
  48. display: none;
  49. }
  50. */
  51. $.widget("ui.inputoverlay",
  52. {
  53. _init: function()
  54. {
  55. var self = this;
  56. var obj = this.element;
  57. var input = $('input', obj);
  58. var span = $('span', obj);
  59. if (!input.length) {
  60. input = $('textarea', obj);
  61. }
  62. var func = function(focus) {
  63. if ($(input).val() == "") {
  64. if (focus) {
  65. $(span).removeClass('hidden').addClass('focus');
  66. } else {
  67. $(span).removeClass('hidden').removeClass('focus');
  68. }
  69. } else {
  70. $(span).removeClass('focus').addClass('hidden');
  71. }
  72. };
  73. input.change(function() {
  74. func(true);
  75. }).focus(function() {
  76. func(true);
  77. }).blur(function() {
  78. func(false);
  79. }).keydown(function() {
  80. setTimeout(function(){func(true);},10);
  81. }).keyup(function() {
  82. func(true);
  83. });
  84. input.closest("form").bind("reset", function() {
  85. setTimeout(function(){func(true);},10);
  86. });
  87. span.click(function() {
  88. input.focus();
  89. });
  90. if (input.attr('autocomplete') == 'on') {
  91. setInterval(function() {
  92. if ($(input).val() == "") {
  93. $(span).removeClass('hidden');
  94. } else {
  95. $(span).addClass('hidden');
  96. }
  97. }, 100);
  98. }
  99. }
  100. });