/src/main/webapp/public/js/projects/show.js

http://thoughtsite.googlecode.com/ · JavaScript · 172 lines · 134 code · 6 blank · 32 comment · 30 complexity · f8cd2c4f2751b41875c88ee6b6fcca77 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 ShowProjectDetailForm require
  17. * jquery 1.3.2
  18. */
  19. // onload set form focus validations
  20. google.setOnLoadCallback(function() {
  21. //Set project key
  22. projectDisplay.setProjectKey($('#projectKey').val());
  23. // set comment char limit to 3000 chars
  24. $('#comment').limit('3000','#charsLeft');
  25. //User require login before create project
  26. ie.Login.setLoggedinUserCallback('handleProjectPostCommentForm');
  27. //Expanded link when login
  28. $('#expandLink').click(function(){
  29. $('#commentLayer').toggle();
  30. if($.trim($(this).html()) == '+') {
  31. $(this).html('-');
  32. projectDisplay.expand($('#projectKey').val());
  33. }
  34. else {
  35. $(this).html('+');
  36. }
  37. });
  38. //Post comment when Post button is clicked
  39. $('#btnPostComment').click(function(){
  40. projectDisplay.postComment();
  41. });
  42. $('#commentLayer').toggle();
  43. projectDisplay.expand();
  44. })
  45. // User require login before create project
  46. function handleProjectPostCommentForm(isLoggedIn) {
  47. if(isLoggedIn) {
  48. $('#postComment').show();
  49. }
  50. else {
  51. $('#postComment').hide();
  52. }
  53. }
  54. var projectDisplay = {
  55. projectKey : ''
  56. }
  57. projectDisplay.setProjectKey = function(projectKey) {
  58. this.projectKey = projectKey;
  59. }
  60. projectDisplay.getProjectKey = function() {
  61. return this.projectKey;
  62. }
  63. projectDisplay.expand = function () {
  64. this.loadComments(0);
  65. ie.progressStart();
  66. }
  67. //Post comment when Post button is clicked
  68. projectDisplay.postComment = function() {
  69. if('' == $.trim($('#comment').val())) {
  70. $('#errComment').html('Please provide comments.');
  71. $('#postComment').focus();
  72. }
  73. else {
  74. ie.showRecaptcha('projectDisplay.postCommentFinal');
  75. }
  76. }
  77. //Submit the comment entered
  78. projectDisplay.postCommentFinal = function(recaptchaChallengeField, recaptchaResponseField) {
  79. $.post(
  80. ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'projectComments/postProjectComments.json'),
  81. {text: $.trim($('#comment').val()),
  82. projectKey: this.getProjectKey(),
  83. recaptchaChallengeField: recaptchaChallengeField,
  84. recaptchaResponseField: recaptchaResponseField},
  85. function (handle) {
  86. $('#errComment').html('');
  87. if(ie.config.SUCCESS == handle.viewStatus.status) {
  88. $('#errComment').html(handle.viewStatus.messages.comments);
  89. $('#comment').val('');
  90. }
  91. else {
  92. if(undefined != handle.viewStatus.messages.comments)
  93. $('#errComment').html(handle.viewStatus.messages.comments);
  94. if(undefined != handle.viewStatus.messages.captcha)
  95. $('#errComment').append(handle.viewStatus.messages.captcha);
  96. ie.progressEnd();
  97. }
  98. if('' != $('#errComment').html())
  99. $('#errComment').show();
  100. projectDisplay.expand();
  101. },
  102. ie.config.RESPONSE_TYPE
  103. );
  104. }
  105. //Render all the comments posted for a project
  106. projectDisplay.loadComments = function (offset) {
  107. if(undefined == offset || '' == offset) {
  108. offset = 0;
  109. }
  110. $.get(
  111. ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'projectComments/listProjectComments/'+ this.projectKey + '.json?startIndex=' + offset),
  112. {},
  113. function (handle) {
  114. // render all the comments
  115. if(ie.config.SUCCESS == handle.viewStatus.status) {
  116. $('#commentData').html('');
  117. var counter = 0;
  118. for (i in handle.viewStatus.data.comments) {
  119. counter++;
  120. if(counter > ie.config.RECORD_PER_PAGE)
  121. break;
  122. $('#commentData').append(projectDisplay.createHtml(handle.viewStatus.data.comments[i]));
  123. }
  124. $('#commentLayer').show();
  125. // Handles Pagination
  126. if(undefined != handle.viewStatus.data.paging)
  127. $('#pagination').html(ie.Paging.getHTML(handle.viewStatus.data.paging, 'projectDisplay.loadComments'));
  128. ie.progressEnd();
  129. }
  130. else {//Show error message when not success
  131. $('#commentData').html(handle.viewStatus.messages.comments);
  132. ie.progressEnd();
  133. }
  134. },
  135. ie.config.RESPONSE_TYPE
  136. );
  137. }
  138. //HTML to render the page loaded with comments
  139. projectDisplay.createHtml = function (jsonData) {
  140. var html = '';
  141. html += '<div class="ie-overview-desc ie-text">';
  142. if(jsonData.user) {
  143. html += '<img src="' + jsonData.user.thumbnailUrl + '" alt="" width="60" height="60" align="left" class="ie-right-mar-10 ie-bottom-mar-20 " />';
  144. }
  145. html += ie.escapeHTML(jsonData.comment.commentTextAsString);
  146. if(jsonData.user) {
  147. html += '<br/><div class="ie-sm-lg ie-top-mar-10 ie-left" style=" width:auto;">Posted by: ';
  148. html += jsonData.user.displayName + '&nbsp; |&nbsp; ' + jsonData.comment.createdOn;
  149. html += '</div>';
  150. }
  151. html += '<div class=" ie-top-mar-10 ie-right" style="width:auto;">';
  152. html += '<img border="0" src="' + ie.config.PUBLIC_URL + 'images/ic5.gif" alt="" width="20" height="20" />';
  153. html += '&nbsp; <a href="javascript:void(0);" onclick="ie.reportAbouse(ie.config.PROJECTS_COMMENT, \'' + jsonData.comment.key + '\')">Report abuse</a>';
  154. html += '</div>';
  155. html += '<div class="divider"> &nbsp;&nbsp;';
  156. html += '</div><div style="clear:both"></div>';
  157. html += '</div>';
  158. return html;
  159. }
  160. //Alert to show that a comment is reported as Abuse.
  161. function showAlert() {
  162. $('#dialog').html('<div style="padding-left: 40px; margin-top: 45px;">This Comment is reported as Abuse.</div>');
  163. $('#dialog').dialog('option', 'title', 'Alert!');
  164. $('#dialog').dialog('option', 'modal', true);
  165. $('#dialog').dialog('open');
  166. };