PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

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