CategoriesView.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Categories View is used for displaying the lists of categories
  3. * and subcategories when user has pressed 'Categories' button in
  4. * the main view, Places View
  5. */
  6. qype.views.CategoriesView = Class.create(View, {
  7. initialize: function($super, id) {
  8. $super(id);
  9. this.activeCategory = undefined;
  10. this.activeCategoryName = undefined;
  11. this.data.categories = undefined;
  12. // Create new snippet and bind to a DOM node via id
  13. var header = new Snippet("categoriesHeader");
  14. // Add default template
  15. // header.onDefault.template.push(qype.str.categories_header_main);
  16. header.onDefault.template.push({
  17. fn: function() {return (this.activeCategoryName) ?
  18. qype.str.categories_header_sub + " (" + this.activeCategoryName + ")" :
  19. qype.str.categories_header_main},
  20. base: this
  21. });
  22. // Create new snippet and bind to a DOM node via id
  23. var content = new Snippet("categoriesContent");
  24. // Add default template
  25. content.onDefault.template.push("<div class='progress'></div>");
  26. content.onData.template.push({
  27. fn: this.constructCategoriesHtml,
  28. base: this
  29. });
  30. content.onData.doUse = function() {return content.data.categories !== undefined;};
  31. // And finally register the Snippets to this view
  32. this.registerSnippets({
  33. header: header,
  34. content: content
  35. });
  36. this.registerSubscribes([
  37. {
  38. eventName: "categoryClicked",
  39. callback: this.onItemClick,
  40. context: this
  41. }
  42. ]);
  43. },
  44. templates: {
  45. showAll: new Template(
  46. '<div class="item all" onclick="EventManager.publish({name:\'#{eventName}\', id:\'#{id}\', categoryName:\'#{categoryName}\', isLeaf:true})">' +
  47. ' <div class="name">#{title}</div>' +
  48. ' <div class="button next"></div>' +
  49. '</div>'
  50. ),
  51. category: new Template(
  52. '<div class="item" onclick="EventManager.publish({name:\'#{eventName}\', id:\'#{id}\', categoryName:\'#{title}\', isLeaf:#{isLeaf}})">' +
  53. ' <div class="name">#{title}</div>' +
  54. ' <div class="button next"></div>' +
  55. '</div>'
  56. )
  57. },
  58. build: function($super, params) {
  59. $super(params);
  60. if(params && params.categoryId) {
  61. if(this.activeCategory == params.categoryId && this.data.subcategories) {
  62. this.setCategoriesContent(this.data.subcategories);
  63. if(params.categoryName) {
  64. this.activeCategoryName = params.categoryName;
  65. }
  66. }
  67. else {
  68. // Set content to 'loading' state
  69. this.snippets.content.data.categories = undefined;
  70. util.depressurizeAll("item");
  71. this.snippets.header.rewrite();
  72. this.snippets.content.rewrite();
  73. this.getSubCategories(params.categoryId, params.categoryName);
  74. }
  75. }
  76. else {
  77. this.activeCategory = undefined;
  78. this.activeCategoryName = undefined;
  79. if(this.data.mainCategories) {
  80. this.setCategoriesContent(this.data.mainCategories);
  81. }
  82. else {
  83. this.getCategories();
  84. }
  85. }
  86. },
  87. setCategoriesContent: function(categories) {
  88. // The content snippet has only one set of categories
  89. this.snippets.content.data.categories = categories;
  90. },
  91. onItemClick: function(evt) {
  92. util.log("{CategoriesView.onItemClick} start");
  93. if(evt.isLeaf) {
  94. this.engine.activateView("placesView", {categoryId: evt.id, categoryName: evt.categoryName});
  95. }
  96. else {
  97. this.engine.activateView(this.id, {categoryId: evt.id, categoryName: evt.categoryName});
  98. }
  99. },
  100. constructCategoriesHtml: function() {
  101. var cats = undefined;
  102. if(cats = this.snippets.content.data.categories) {
  103. cats = cats.results;
  104. var arr = [];
  105. // Add "Show all" item on the top if in a subcategory
  106. if(this.activeCategory) {
  107. arr.push(this.templates.showAll.evaluate({
  108. id: this.activeCategory,
  109. title: qype.str.categories_show_all,
  110. categoryName: this.activeCategoryName,
  111. eventName: "categoryClicked"
  112. }));
  113. }
  114. var t = this.templates.category;
  115. for(var i = 0; i < cats.length; i++) {
  116. var cat = cats[i].category;
  117. arr.push(t.evaluate({
  118. id: cat.id.substring(cat.id.lastIndexOf("/") + 1),
  119. title: cat.title.value,
  120. eventName: "categoryClicked",
  121. isLeaf: cat.links.length < 3
  122. }));
  123. }
  124. return arr.join("");
  125. }
  126. return "";
  127. },
  128. getCategories: function() {
  129. qype.API.get("place_categories", {
  130. "onSuccess": this.onGetCategories.bind(this),
  131. "onFailure": this.onFailure
  132. });
  133. },
  134. onGetCategories: function(response) {
  135. util.log("{CategoriesView.onGetCategories} start");
  136. this.data.mainCategories = response.responseJSON;
  137. this.setCategoriesContent(this.data.mainCategories);
  138. util.depressurizeAll("item");
  139. this.snippets.header.rewrite();
  140. this.snippets.content.rewrite();
  141. util.pressurizeAll("item");
  142. },
  143. onFailure: function(response) {
  144. util.log("{CategoriesView.onFailure}: start, response = ");
  145. util.log(response);
  146. },
  147. /**
  148. * Deliver also the categoryName along with the function call since it can't
  149. * be acquired easily when getting the actual subcategories result.
  150. *
  151. * Both categoryId and categoryName are set only when the API call is successful.
  152. */
  153. getSubCategories: function(categoryId, categoryName) {
  154. qype.API.get("place_categories", {
  155. "onSuccess": this.onGetSubCategories.bind(this, categoryId, categoryName),
  156. "onFailure": this.onFailure
  157. }, categoryId + "/children");
  158. },
  159. onGetSubCategories: function(categoryId, categoryName, response) {
  160. util.log("{CategoriesView.onGetSubCategories} start");
  161. this.activeCategory = categoryId;
  162. this.activeCategoryName = categoryName;
  163. this.data.subcategories = response.responseJSON;
  164. this.setCategoriesContent(this.data.subcategories);
  165. util.depressurizeAll("item");
  166. this.snippets.header.rewrite();
  167. this.snippets.content.rewrite();
  168. util.pressurizeAll("item");
  169. }
  170. });