PageRenderTime 66ms CodeModel.GetById 20ms app.highlight 25ms RepoModel.GetById 15ms app.codeStats 0ms

/src/main/webapp/public/js/ideas/list.js

http://thoughtsite.googlecode.com/
JavaScript | 283 lines | 267 code | 9 blank | 7 comment | 72 complexity | 04bcacaa2c5a967bd1fe353e07aaf3a3 MD5 | raw file
  1google.setOnLoadCallback(function() {
  2    ie.progressStart();
  3    loadTagCloud();
  4    if($.url.param("type") && '' !=$.trim($.url.param("type"))) {
  5        switch($.url.param("type")){
  6        case idea.DEFAULT_TYPE :
  7            idea.setBrowseType(idea.DEFAULT_TYPE);
  8            break;
  9        case idea.SEARCH_TYPE :
 10            idea.setBrowseType(idea.SEARCH_TYPE);
 11            idea.setKeyword($.url.param("keyword"));
 12            break;
 13        case idea.TAG_TYPE :
 14            idea.setBrowseType(idea.TAG_TYPE);
 15            idea.browseByTag($.url.param("tag"));
 16            break;
 17        case idea.USER_TYPE :
 18            idea.setBrowseType(idea.USER_TYPE);
 19            break;
 20        default :
 21            idea.setBrowseType(idea.DEFAULT_TYPE);
 22        break;
 23        }
 24    }
 25    $("#sortIdeas").click( function() {
 26        $("ul#sort-dropdown").toggle(); return false;
 27    });
 28    idea.loadIdeas(0);
 29});
 30
 31function handleVotingForm(isLoggedIn) {
 32    if(isLoggedIn) {
 33    }
 34    else {
 35    }
 36}
 37
 38idea = {
 39    DEFAULT_TYPE : 'list',
 40    SEARCH_TYPE : 'search',
 41    TAG_TYPE : 'tag',
 42    USER_TYPE : 'user',
 43    DEFAULT_SORT_ORDER : 'publishDate',
 44    DATE_SORT_ORDER : 'publishDate',
 45    VOTE_SORT_ORDER : 'totalVotes',
 46    SORT_ORDER : 'publishDate',
 47    keyword : ''// for search
 48    
 49}
 50idea.setSortOrder = function (type) {
 51    if(undefined == type)
 52        this.SORT_ORDER = this.DEFAULT_SORT_ORDER;
 53    else
 54        this.SORT_ORDER = type;
 55}
 56idea.setKeyword = function (term) {
 57    this.keyword = term;
 58}
 59idea.setBrowseType = function (browseType) {
 60    this.browseBy = browseType;
 61}
 62idea.getBrowseType = function () {
 63    if(undefined == this.browseBy) {
 64        return this.DEFAULT_TYPE;
 65    }
 66    else {
 67        return this.browseBy;
 68    }
 69}
 70idea.browseByTag = function(tag) {
 71    this.setBrowseType(this.TAG_TYPE);
 72    this.tagName = tag;
 73    idea.loadIdeas(0);
 74}
 75
 76idea.loadIdeas = function(offset, heading) {
 77    var url = '';
 78    if(this.getBrowseType() == this.DEFAULT_TYPE) {
 79        url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/get.json?startIndex=' + offset);
 80    }
 81    else if(this.getBrowseType() == this.TAG_TYPE){
 82        //url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/byTag/' + this.tagName + '/' + offset + '.json');
 83    	url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/byTag/' + this.tagName + '.json?startIndex=' + offset);
 84        $('#listHeading').html('Ideas by tag "'+ this.tagName +'"');
 85    }
 86    else if(this.getBrowseType() == this.SEARCH_TYPE){
 87        url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/search?keyword=' + this.keyword + '&offset');
 88        $('#listHeading').html('Search "'+ this.keyword +'"');
 89    }
 90    else if(this.getBrowseType() == this.USER_TYPE){
 91    }
 92    url = url + '&orderBy=' + this.SORT_ORDER;
 93    $.get(
 94            url, 
 95            {}, 
 96            function (handle) {
 97                // render most recent idea
 98                if(handle != ''){
 99                    if(handle.viewStatus.status == ie.config.SUCCESS) {
100                        // TODO: Abhishek, Need to handle with Template :)
101                        $('#listData').html('');
102                        var counter = 0;
103                        for (i in handle.viewStatus.data.ideas) {
104                            counter++;
105                            if(counter > ie.config.RECORD_PER_PAGE)
106                                break;
107                            $('#listData').append(createHtml(handle.viewStatus.data.ideas[i]));
108                        }
109                        if(undefined != handle.viewStatus.data.paging)
110                            $('#pagination').html(ie.Paging.getHTML(handle.viewStatus.data.paging, 'idea.loadIdeas'));
111                        if(counter > 1) {
112                            $('#sortDate').click(function () {
113                                idea.sort('date');
114                            });
115                            $('#sortVote').click(function () {
116                                idea.sort('vote');
117                            });
118                        }
119                        ie.progressEnd();
120                    }
121                    else {
122                        $('#listData').html(handle.viewStatus.messages.ideas);
123                    }
124                }
125                else {
126                    ie.globalError();
127                }
128                ie.progressEnd();
129            }, 
130            ie.config.RESPONSE_TYPE
131    );
132}
133
134idea.sort = function (type) {
135    if('vote' == type) {
136        this.setSortOrder(this.VOTE_SORT_ORDER);
137        this.loadIdeas(0);
138    }
139    else {
140        this.setSortOrder(this.DATE_SORT_ORDER);
141        this.loadIdeas(0);
142        // by default sort on date
143    }
144}
145
146function createHtml(jsonData) {
147    var output = '';
148    output += '<div style="width:100%; height:180px;"  >';
149    if(ie.config.STATUS_PUBLISHED == jsonData['idea']['status']) {
150        output += '        <div class="ie-left ie-votes">';
151        output += '<div class="ie-top-mar-10 ie-center"><strong> Votes</strong>';
152        output += '    </div>';
153        output += '    <div class="ie-top-mar-10 ie-left">';
154        output += '      <a href="#vote" onclick="voteIdea(\'' + jsonData['idea']['key'] + '\', true)"><img src="' + ie.config.PUBLIC_URL + 'images/hand-up.gif" alt="" width="25" height="30" hspace="10" border="0" /></a><strong>+ ';
155        output += '<span id="pIdea' + jsonData['idea']['key'] + '">' + jsonData['idea']['totalPositiveVotes']+'</span></strong>';
156        output += '    </div>';
157        output += '    <div class="ie-clear ie-top-mar-5 ie-left" style="width: 85%">';
158        output += '      <a href="#vote" onclick="voteIdea(\'' + jsonData['idea']['key'] + '\', false)"><img src="' + ie.config.PUBLIC_URL + 'images/hand-dwn.gif" alt="" width="25" height="30" hspace="10" border="0" /></a><strong>- ';
159        output += '<span id="nIdea' + jsonData['idea']['key'] + '">' + jsonData['idea']['totalNegativeVotes']+'</span></strong>'; 
160        output += '    </div>';
161        output += '  </div>';
162    }
163    else if(ie.config.STATUS_OBJECTIONABLE == jsonData['idea']['status']) {
164        output += '        <div class="ie-left ie-votes">';
165        output += '<div class="ie-top-mar-10 ie-center"><strong> Objectionable Idea</strong>';
166        output += '    </div>';
167        output += '  </div>';
168    }
169    else if(ie.config.STATUS_DUPLICATE == jsonData['idea']['status']) {
170        output += '        <div class="ie-left ie-votes">';
171        output += '<div class="ie-top-mar-10 ie-center"><strong> Duplicate Idea</strong>';
172        output += '    </div>';
173        output += '  </div>';
174    }
175    else {
176        output += '        <div class="ie-left ie-votes">';
177        output += '<div class="ie-top-mar-10 ie-center"><strong> Unpublished Idea</strong>';
178        output += '    </div>';
179        output += '  </div>';
180    }
181    output += '  <div class="ie-right ie-text ie-detail">';
182    output += '    <h1 class="blu-heading">';
183    if(ie.config.STATUS_SAVED == jsonData['idea']['status']) {
184        output += '    <a href="' + ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/edit/'+ jsonData['idea']['key']) + '">';
185    }
186    else {
187        output += '    <a href="' + ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/show/'+ jsonData['idea']['key']) + '">';
188    }
189    output += ie.escapeHTML(jsonData['idea']['title']) + '</a></h1><br />';
190    output += ie.escapeHTML(jsonData['idea']['description']) + '<br/>';
191    output += '    <div class="ie-left ie-detail-info1">';
192    output += '      <strong>Tags:</strong>';
193    if(undefined != jsonData.tags) {
194        for(data in jsonData.tags) {
195            output += '      <a href="javascript:void(0)" onclick="idea.browseByTag(\'' + jsonData.tags[data]['title'] + '\');" class="ie-nm-blu">' + jsonData['tags'][data]['title'] + '</a>';
196        }
197    }
198    output += '    </div>';
199    if(undefined != jsonData.user) {
200        output += '    <div class="ie-right ie-detail-info2">';
201        output += '      <strong>Last Updated: </strong>'+ jsonData['idea']['lastUpdated']+'<br />';
202        output += '      <a href="' + ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'users/profile/'+ jsonData.user.userKey) + '" class="ie-nm-blu">' + jsonData.user.displayName + '</a><br />';
203        output += '      <strong>' + jsonData.user.reputationPoints + ' Points</strong><br />';
204        if(undefined != jsonData.user.userAwards)
205            output += '      <strong>' + jsonData.user.userAwards + ' Awards</strong></div>';
206        output += '  </div>';
207    }
208    output += '</div>';
209    return output;
210}
211
212function voteIdea(ideaKey, isPositiveVote) {
213    if(!ie.config.IS_LOGGEDIN_USER) {
214        ie.showError('Please login to vote on an idea.');
215		return;
216    }
217    if('' == ideaKey) {
218        return false;
219    }
220    var url = '';
221    if(isPositiveVote) {
222        url = '/ideas/voteIdea/' + ideaKey + '.json?isPositive=true';
223    }
224    else {
225        url = '/ideas/voteIdea/' + ideaKey + '.json?isPositive=false';
226    }
227    ie.progressStart();
228    $.get(
229            url, 
230            {}, 
231            function (handle) {
232                // render most recent idea
233                if(ie.config.SUCCESS == handle.viewStatus.status) {
234                    if(isPositiveVote) {
235                        $('#pIdea' + ideaKey).html(parseInt($('#pIdea' + ideaKey).html()) + 1);
236                    }
237                    else {
238                        $('#nIdea' + ideaKey).html(parseInt($('#nIdea' + ideaKey).html()) + 1);
239                    }
240                    ie.progressEnd();
241                }
242                else if(ie.config.ERROR == handle.viewStatus.status) {
243                    ie.showError(handle.viewStatus.messages.vote);
244                    ie.progressEnd();
245                }
246                else {
247                    ie.progressEnd();
248                    ie.globalError();
249                }
250            }, 
251            ie.config.RESPONSE_TYPE
252    );
253    
254}
255
256function loadTagCloud() {
257    $.get(
258            ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'tags/tagcloud' + '.json'), 
259            {}, 
260            function (handle) {
261                // render most recent idea
262                if(ie.config.SUCCESS == handle.viewStatus.status) {
263                    // TODO: Abhishek, Need to handle with Template :)
264                    var html = ie.TagCloud.render(handle.viewStatus.data.tags, 
265                            handle.viewStatus.data.weights, 
266                            {title : 'title', weightage : 'weightage', css: 'tag'},
267                            {url:ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/list?type=tag&tag=')}
268                            );
269                    $('#cloud').html(html);
270                    ie.progressEnd();
271                }
272                else if(ie.config.ERROR == handle.viewStatus.status) {
273                    $('#cloud').html(handle.viewStatus.messages.tags);
274                    ie.progressEnd();
275                }
276                else {
277                    ie.progressEnd();
278                    ie.globalError();
279                }
280            }, 
281            ie.config.RESPONSE_TYPE
282    );
283}