/src/main/webapp/public/js/home.js

http://thoughtsite.googlecode.com/ · JavaScript · 129 lines · 93 code · 5 blank · 31 comment · 29 complexity · 142f1424f8a4ac59791d511d96e98bdc MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. /**
  16. * file is responsible to handle all javascript tasks on home page
  17. * require jquery 1.3.2
  18. */
  19. // onload set form focus validations
  20. google.setOnLoadCallback(function() {
  21. ie.Login.setLoggedinUserCallback('home.handleSaveIdeaForm');
  22. // categories required for the form
  23. ie.loadCategories('category', ''/*selected cat id*/);
  24. // on focus idea title
  25. $('#title').focus(function () {
  26. if($('#title').val() == 'Idea Title') {
  27. $('#title').val('');
  28. }
  29. });
  30. $('#title').blur(function () {
  31. if($('#title').val() == '') {
  32. $('#title').val('Idea Title');
  33. }
  34. });
  35. $('#description').focus(function () {
  36. if($('#description').val() == 'Description') {
  37. $('#description').val('');
  38. }
  39. });
  40. $('#description').blur(function () {
  41. if($('#description').val() == '') {
  42. $('#description').val('Description');
  43. }
  44. });
  45. $('#tags').focus(function () {
  46. if($('#tags').val() == 'Tags') {
  47. $('#tags').val('');
  48. }
  49. });
  50. $('#tags').blur(function () {
  51. if($('#tags').val() == '') {
  52. $('#tags').val('Tags');
  53. }
  54. });
  55. // set description char limit to 3000 chars
  56. $('#description').limit('3000','#charsLeft');
  57. // Put autocomplete functionality on Tags
  58. $("#tags").autocomplete(ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'tags/suggest/'), {
  59. width: 200,
  60. dataType: "json",
  61. parse: function(handle) {
  62. if(handle.viewStatus.status == ie.config.SUCCESS) {
  63. return $.map(handle.viewStatus.data.tags, function(row) {
  64. return {
  65. data: row,
  66. value: row.key,
  67. result: row.title
  68. }
  69. });
  70. }
  71. else {
  72. return;
  73. }
  74. },
  75. formatItem: function(row) {
  76. return row.title;
  77. },
  78. multiple: true,
  79. matchContains: false,
  80. highlight: false,
  81. formatted: false
  82. });
  83. });
  84. /**
  85. * handle save idea on the basis of user login as login is must for save idea
  86. */
  87. home.handleSaveIdeaForm = function (isUserLoggedIn) {
  88. if(isUserLoggedIn) {
  89. $('#btnSaveIdea').unbind('click');
  90. $('#btnSaveIdea').bind('click', function () {
  91. if($.trim($('#title').val()) == '' || $.trim($('#title').val()) == 'Idea Title') {
  92. $('#errDisplay').html('Please add Idea title');
  93. $('#errDisplay').show();
  94. return;
  95. }
  96. if($('#description').val() == 'Description') {
  97. $('#description').val('');
  98. }
  99. if($('#tags').val() == 'Tags') {
  100. $('#tags').val('');
  101. }
  102. $('#submitIdeaForm').submit();
  103. });
  104. }
  105. else {
  106. $('#btnSaveIdea').unbind('click');
  107. $('#btnSaveIdea').click(function () {
  108. ie.Login.showLoginPopup();
  109. });
  110. }
  111. }
  112. /**
  113. * validate save idea form
  114. * @return boolean
  115. */
  116. home.validateForm = function () {
  117. // only title field is required to save an idea
  118. if($.trim($('#title').val()) == '' || $.trim($('#title').val()) == 'Idea Title') {
  119. $('#errDisplay').html('Please add Idea title');
  120. $('#errDisplay').show();
  121. return false;
  122. }
  123. return true;
  124. }