/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

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