PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/pancake-web/pancake/web/static/js/views/stackheadinglistview.js

https://bitbucket.org/mozillapancake/pancake
JavaScript | 115 lines | 83 code | 23 blank | 9 comment | 2 complexity | b9628a2364d11cf4ae103c25b31d3505 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, MIT, Apache-2.0
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. // Displays a list of stacks as headlines
  5. define([
  6. '$',
  7. 'underscore',
  8. 'backbone',
  9. 'lib/template',
  10. 'views/listview',
  11. 'views/singleview',
  12. 'models/stackcollection',
  13. 'logger'
  14. ], function (
  15. $,
  16. util,
  17. Backbone,
  18. template,
  19. ListView,
  20. SingleView,
  21. StackCollection,
  22. logger
  23. ) {
  24. var StackModel = StackCollection.prototype.model;
  25. var PlaceSubheadingView = SingleView.extend({
  26. tagName: 'li',
  27. className: 'subheadingitem',
  28. template: template('<div class="media media--icon"><img src="{{ favicon_url }}"></div><span class="title"><a href="{{ place_url }}">{{ place_title }}</a></span>'),
  29. events: {
  30. 'click .title a': 'onClick'
  31. },
  32. initialize: function (options) {
  33. var modelJSON = this.model.toJSON();
  34. // Add a new property to the JSON -- favicon_url.
  35. // Allow the browser to tell us what the protocol and hostname are by
  36. // creating an `<a>` element and assigning the `place_url` to it.
  37. var a = document.createElement("a");
  38. a.href = modelJSON.place_url;
  39. modelJSON.favicon_url = a.protocol + "//" + a.hostname + "/favicon.ico";
  40. $(this.el).html(this.template(modelJSON));
  41. },
  42. onClick: function (e) {
  43. this.trigger('visit', this, this.model, e);
  44. }
  45. });
  46. // Define a single view template for stack headings.
  47. var __ListView = ListView.prototype;
  48. var StackHeadingView = ListView.extend({
  49. view: PlaceSubheadingView,
  50. className: 'headingitem',
  51. tagName: 'li',
  52. template: template('<span class="title heading-title">{{ stack_title }}</span><ul class="subheadinglist"></ul>'),
  53. attachPoints: {
  54. 'stack_title': '.title',
  55. 'list': '.subheadinglist'
  56. },
  57. events: {
  58. 'click .heading-title': 'onHeadingClick'
  59. },
  60. initialize: function (options) {
  61. var model = this.model;
  62. if (!(model instanceof StackModel)) throw new Error(
  63. 'StackHeadingView requires a StackModel.'
  64. );
  65. this.collection = model.matches();
  66. __ListView.initialize.call(this, options);
  67. },
  68. onHeadingClick: function(e){
  69. // delegate the click to the first model in the collection
  70. var firstModel = this.collection.at(0);
  71. if(firstModel){
  72. this.trigger('visit', this, firstModel, e);
  73. }
  74. }
  75. });
  76. var loadingMarkup = '<div class="placeholder hidden">'+
  77. '<span class="loading-spinner white-spinner"></span>'+
  78. '<span class="loading-label">Loading...</span>'+
  79. '</div>';
  80. var StackHeadingListView = ListView.extend({
  81. declaredClass: 'StackHeadingListView',
  82. className: 'headinglist',
  83. tagName: 'div',
  84. view: StackHeadingView,
  85. attachPoints: {
  86. 'list': '.views',
  87. 'placeholder': '.placeholder'
  88. },
  89. template: template('<ul class="views"></ul>' + loadingMarkup)
  90. });
  91. return StackHeadingListView;
  92. });