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

http://thoughtsite.googlecode.com/ · JavaScript · 137 lines · 107 code · 4 blank · 26 comment · 27 complexity · 4226830f7760e1fd7347bb594fcb051d 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. google.setOnLoadCallback(function() {
  16. ie.progressStart();
  17. $('#tagKeyword').focus(function () {
  18. if($('#tagKeyword').val() == 'Search Tag') {
  19. $('#tagKeyword').val('');
  20. }
  21. });
  22. $('#tagKeyword').blur(function () {
  23. if($('#tagKeyword').val() == '') {
  24. $('#tagKeyword').val('Search Tag');
  25. }
  26. });
  27. $('#searchTag').click(function(){
  28. if('' == $.trim($('#tagKeyword').val()) || 'Search Tag' == $.trim($('#tagKeyword').val())) {
  29. alert('Please provide search keyword.');
  30. return false;
  31. }
  32. var elem = $('.page_sel');
  33. elem.removeClass('page_sel');
  34. elem.addClass('page_no');
  35. tagList.setListType('search');
  36. loadTags($('#tagKeyword').val());
  37. });
  38. // initialize history
  39. $.historyInit(loadTags, "tag-list");
  40. // start with char A
  41. var isFirstElem = true;
  42. $('.page_no').each(function() {
  43. // url with [A-Z]1,
  44. $(this).attr('href', '#'+$(this).html());
  45. // so that we can easily handle browser back
  46. $(this).attr('id', 'tag-'+$(this).html());
  47. // override click
  48. $(this).click(function(){
  49. // for history management
  50. var hash = this.href;
  51. hash = hash.replace(/^.*#/, '');
  52. // moves to a new page.
  53. // pageload is called at once.
  54. // hash don't contain "#", "?"
  55. tagList.setListType('list');
  56. $.historyLoad(hash);
  57. return false;
  58. });
  59. // need to load first elements tag
  60. if(isFirstElem) {
  61. $(this).removeClass('page_no');
  62. $(this).addClass('page_sel');
  63. isFirstElem = false;
  64. loadTags($(this).html());
  65. }
  66. });
  67. });
  68. var tagList = {};
  69. tagList.setListType = function(listType) {
  70. this.type = listType;
  71. }
  72. tagList.getListType = function() {
  73. if(undefined == this.type)
  74. return 'list';
  75. return this.type;
  76. }
  77. function loadTags(keyword) {
  78. var url = '';
  79. if(tagList.getListType() == 'list') {
  80. url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'tags/list/'+ keyword + '.json');
  81. if(undefined == keyword || '' == keyword) {
  82. initial = 'A';
  83. }
  84. selectLink(keyword);
  85. }
  86. else {
  87. url = ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'tags/search?keyword='+ keyword);
  88. }
  89. $.get(
  90. url,
  91. {},
  92. function (handle) {
  93. // render most recent idea
  94. if(ie.config.SUCCESS == handle.viewStatus.status) {
  95. // TODO: Abhishek, Need to handle with Template :)
  96. if(tagList.getListType() == 'search') {
  97. var html = ie.TagCloud.render(
  98. handle.viewStatus.data.tags,
  99. {"maxWeight":10,"minWeight":10},
  100. {title : 'title', weightage : 'weightage', css: 'tag'},
  101. {url:ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/list?type=tag&tag=')}
  102. );
  103. }
  104. else {
  105. var html = ie.TagCloud.render(
  106. handle.viewStatus.data.tags,
  107. handle.viewStatus.data.weights,
  108. {title : 'title', weightage : 'weightage', css: 'tag'},
  109. {url:ie.buildUrl(ie.config.REQUEST_IDEA_EXCHANGE, 'ideas/list?type=tag&tag=')}
  110. );
  111. }
  112. $('#cloud-main').html(html);
  113. ie.progressEnd();
  114. }
  115. else if(ie.config.ERROR == handle.viewStatus.status) {
  116. $('#cloud-main').html(handle.viewStatus.messages.tags);
  117. ie.progressEnd();
  118. }
  119. else {
  120. ie.progressEnd();
  121. ie.globalError();
  122. }
  123. },
  124. ie.config.RESPONSE_TYPE
  125. );
  126. }
  127. function selectLink(initial) {
  128. var selectedElem = $('.page_sel');
  129. $(selectedElem).removeClass('page_sel');
  130. $(selectedElem).addClass('page_no');
  131. $('#tag-' + initial).removeClass('page_no');
  132. $('#tag-' + initial).addClass('page_sel');
  133. }