PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/menu.app.js

https://bitbucket.org/yashdoshi89/rjs-compile-test
JavaScript | 150 lines | 100 code | 27 blank | 23 comment | 6 complexity | c77aec4646a6597d14c8473dbe7f5b5f MD5 | raw file
  1. define([
  2. '$',
  3. 'underscore',
  4. 'backbone',
  5. 'xmessage',
  6. 'lib/template'
  7. ], function($, util, Backbone, xmessage, template){
  8. // events I emit:
  9. // session.change
  10. // the stack id
  11. //
  12. // events I observe
  13. // session.new
  14. // FIXME: we need to require config! but its hardwired to main's requirements
  15. // so for now, we reference the global and some hardcoded values
  16. var SessionModel = Backbone.Model.extend({
  17. // need a service method to return details on an individual session(?)
  18. });
  19. var SessionCollection = Backbone.Collection.extend({
  20. url: function(){
  21. // http://localhost/{username}/{service}/{method}
  22. var urlTemplate = template(config.latticeUrl);
  23. return urlTemplate({
  24. latticeRoot: config.latticeRoot,
  25. username: config.username || 'sam@sam-i-am.com',
  26. service: 'session',
  27. method: 'active'
  28. });
  29. },
  30. // extend fetch to look for the results array in the response.d property
  31. parse : function(resp) {
  32. return resp.d || resp.active_sessions;
  33. }
  34. });
  35. // A widget that contains the views for a collection of sites.
  36. var SessionsView = Backbone.View.extend({
  37. initialize: function (options) {
  38. options = options || {};
  39. this.collection = options.collection || new SessionCollection();
  40. this.bridge = options.bridge;
  41. this.collection
  42. .bind('add', this.render, this)
  43. .bind('reset', this.render, this)
  44. .bind('fetch', this.renderLoader, this)
  45. .bind('success', this.removeLoader, this);
  46. // View events
  47. },
  48. events: {
  49. 'click .session-item': 'onitemclick'
  50. },
  51. onitemclick: function(evt) {
  52. var itemNode = evt.currentTarget;
  53. console.log("session item clicked: ", itemNode.getAttribute("data-itemid"));
  54. },
  55. renderLoader: function () {
  56. // Create a new loader and assign it to a property so we can easily
  57. // remove it later. Creating a new loader every time means we don't
  58. // have to undo the animation leftovers from removeLoader.
  59. this.$loader = $('<div class="loading pin-center">Loading&hellip;</div>')
  60. .css({ opacity: 0 });
  61. $(this.el).append(this.$loader);
  62. this.$loader.animate({ opacity: 0.5, scale: 1 }, 500, 'ease-out');
  63. return this;
  64. },
  65. removeLoader: function () {
  66. var $loader = this.$loader;
  67. $loader.animate(
  68. { opacity: 0, scale: 1.5 },
  69. 400,
  70. 'ease-in',
  71. // After animating the loader, remove it from the DOM.
  72. function () { $loader.remove(); }
  73. );
  74. return this;
  75. },
  76. render: function () {
  77. var pages = this.collection.pluck('active_page'),
  78. imgTemplate = template(config.thumbnailUrl),
  79. list = this.el;
  80. list.innerHTML = "";
  81. if(pages.length) {
  82. // fix me: update DOM in one swoop: probably we'll template this
  83. pages.forEach(function(page){
  84. var itemNode = document.createElement("li");
  85. itemNode.setAttribute("data-itemid", page.id);
  86. itemNode.setAttribute("title", page.title);
  87. itemNode.className = "session-item";
  88. list.appendChild(itemNode);
  89. var img = new Image(50,50);
  90. img.src = imgTemplate({ thumbnail_key: page.thumb_id });
  91. itemNode.appendChild(img);
  92. });
  93. } else {
  94. list.innerHTML = "<li>No active session</li>";
  95. }
  96. }
  97. });
  98. var activeSessions = new SessionCollection([], {
  99. });
  100. var sessionsView = new SessionsView({
  101. el: document.getElementById("menu"),
  102. collection: activeSessions
  103. });
  104. // preload up existing sessions
  105. activeSessions.fetch();
  106. // The visit handler is called by the main app when it detects
  107. // that the current page has changed. This is just a demo.
  108. xmessage.registerHandler("session.new", function(msg) {
  109. var sessionData = msg.data,
  110. currentSessionId = sessionData.session_id;
  111. console.log(window.name + " xmessage handler: ", msg.type, arguments);
  112. // TODO: maintain a local session list and insert the new session
  113. console.log("re-fetching the active sessions");
  114. activeSessions.fetch({
  115. success: function(){
  116. }
  117. });
  118. });
  119. // notify we're ready to start receiving events
  120. xmessage.sendMessage("top", "ready", [{
  121. source: window.name, type: "ready"
  122. }], function (result) {});
  123. });