walkthrough.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. define(["util", "ui", "jquery", "windowing", "templates", "templating", "session", "peers"], function (util, ui, $, windowing, templates, templating, session, peers) {
  5. var assert = util.assert;
  6. var walkthrough = util.Module("walkthrough");
  7. var onHideAll = null;
  8. var container = null;
  9. var slides = null;
  10. walkthrough.start = function (firstTime, doneCallback) {
  11. if (! container) {
  12. container = $(templates("walkthrough"));
  13. container.hide();
  14. ui.container.append(container);
  15. slides = container.find(".togetherjs-walkthrough-slide");
  16. slides.hide();
  17. var progress = $("#togetherjs-walkthrough-progress");
  18. slides.each(function (index) {
  19. var bullet = templating.sub("walkthrough-slide-progress");
  20. progress.append(bullet);
  21. bullet.click(function () {
  22. show(index);
  23. });
  24. });
  25. container.find("#togetherjs-walkthrough-previous").click(previous);
  26. container.find("#togetherjs-walkthrough-next").click(next);
  27. ui.prepareShareLink(container);
  28. container.find(".togetherjs-self-name").bind("keyup", function (event) {
  29. var val = $(event.target).val();
  30. peers.Self.update({name: val});
  31. });
  32. container.find(".togetherjs-swatch").click(function () {
  33. var picker = $("#togetherjs-pick-color");
  34. if (picker.is(":visible")) {
  35. picker.hide();
  36. return;
  37. }
  38. picker.show();
  39. picker.find(".togetherjs-swatch-active").removeClass("togetherjs-swatch-active");
  40. picker.find(".togetherjs-swatch[data-color=\"" + peers.Self.color + "\"]").addClass("togetherjs-swatch-active");
  41. var location = container.find(".togetherjs-swatch").offset();
  42. picker.css({
  43. top: location.top,
  44. // The -7 comes out of thin air, but puts it in the right place:
  45. left: location.left-7
  46. });
  47. });
  48. if (session.isClient) {
  49. container.find(".togetherjs-if-creator").remove();
  50. container.find(".togetherjs-ifnot-creator").show();
  51. } else {
  52. container.find(".togetherjs-if-creator").show();
  53. container.find(".togetherjs-ifnot-creator").remove();
  54. }
  55. TogetherJS.config.track("siteName", function (value) {
  56. value = value || document.title;
  57. container.find(".togetherjs-site-name").text(value);
  58. });
  59. ui.activateAvatarEdit(container, {
  60. onSave: function () {
  61. container.find("#togetherjs-avatar-when-saved").show();
  62. container.find("#togetherjs-avatar-when-unsaved").hide();
  63. },
  64. onPending: function () {
  65. container.find("#togetherjs-avatar-when-saved").hide();
  66. container.find("#togetherjs-avatar-when-unsaved").show();
  67. }
  68. });
  69. // This triggers substititions in the walkthrough:
  70. peers.Self.update({});
  71. session.emit("new-element", container);
  72. }
  73. assert(typeof firstTime == "boolean", "You must provide a firstTime boolean parameter");
  74. if (firstTime) {
  75. container.find(".togetherjs-walkthrough-firsttime").show();
  76. container.find(".togetherjs-walkthrough-not-firsttime").hide();
  77. } else {
  78. container.find(".togetherjs-walkthrough-firsttime").hide();
  79. container.find(".togetherjs-walkthrough-not-firsttime").show();
  80. }
  81. onHideAll = doneCallback;
  82. show(0);
  83. windowing.show(container);
  84. };
  85. function show(index) {
  86. slides.hide();
  87. $(slides[index]).show();
  88. var bullets = container.find("#togetherjs-walkthrough-progress .togetherjs-walkthrough-slide-progress");
  89. bullets.removeClass("togetherjs-active");
  90. $(bullets[index]).addClass("togetherjs-active");
  91. var $next = $("#togetherjs-walkthrough-next").removeClass("togetherjs-disabled");
  92. var $previous = $("#togetherjs-walkthrough-previous").removeClass("togetherjs-disabled");
  93. if (index == slides.length - 1) {
  94. $next.addClass("togetherjs-disabled");
  95. } else if (index === 0) {
  96. $previous.addClass("togetherjs-disabled");
  97. }
  98. }
  99. function previous() {
  100. var index = getIndex();
  101. index--;
  102. if (index < 0) {
  103. index = 0;
  104. }
  105. show(index);
  106. }
  107. function next() {
  108. var index = getIndex();
  109. index++;
  110. if (index >= slides.length) {
  111. index = slides.length-1;
  112. }
  113. show(index);
  114. }
  115. function getIndex() {
  116. var active = slides.filter(":visible");
  117. if (! active.length) {
  118. return 0;
  119. }
  120. for (var i=0; i<slides.length; i++) {
  121. if (slides[i] == active[0]) {
  122. return i;
  123. }
  124. }
  125. return 0;
  126. }
  127. walkthrough.stop = function () {
  128. windowing.hide(container);
  129. if (onHideAll) {
  130. onHideAll();
  131. onHideAll = null;
  132. }
  133. };
  134. session.on("hide-window", function () {
  135. if (onHideAll) {
  136. onHideAll();
  137. onHideAll = null;
  138. }
  139. });
  140. return walkthrough;
  141. });