PageRenderTime 27ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/client/galaxy/scripts/mvc/tours.js

https://bitbucket.org/galaxy/galaxy-central/
JavaScript | 130 lines | 98 code | 11 blank | 21 comment | 7 complexity | 27e3bf8035352efa145e34c4f77f01db MD5 | raw file
Possible License(s): CC-BY-3.0
  1. /**
  2. * This is the primary galaxy tours definition, currently only used for
  3. * rendering a tour menu.
  4. *
  5. * For now it's intended to be plunked into the center display a-la
  6. * Galaxy.app.display, but we could use a modal as well for more flexibility.
  7. *
  8. * DBTODO - This is downright backbone abuse, rewrite it.
  9. */
  10. define(['libs/bootstrap-tour'],function(BootstrapTour) {
  11. var tour_opts = { storage: window.sessionStorage,
  12. onEnd: function(){
  13. sessionStorage.removeItem('activeGalaxyTour');
  14. },
  15. delay: 150, // Attempts to make it look natural
  16. orphan:true
  17. }
  18. var hooked_tour_from_data = function(data){
  19. _.each(data.steps, function(step) {
  20. if (step.preclick){
  21. step.onShow= function(){
  22. _.each(step.preclick, function(preclick){
  23. // TODO: click delay between clicks
  24. $(preclick).click();
  25. });
  26. };
  27. }
  28. if (step.postclick){
  29. step.onHide = function(){
  30. _.each(step.postclick, function(postclick){
  31. // TODO: click delay between clicks
  32. $(postclick).click();
  33. });
  34. };
  35. }
  36. if (step.textinsert){
  37. // Have to manually trigger a change here, for some
  38. // elements which have additional logic, like the
  39. // upload input box
  40. step.onShown= function(){
  41. $(step.element).val(step.textinsert).trigger("change")
  42. };
  43. }
  44. });
  45. return data;
  46. }
  47. // DBTODO: there's probably a better way to do this for pages which don't
  48. // have Galaxy objects.
  49. if (typeof Galaxy != "undefined") {
  50. var gxy_root = Galaxy.root;
  51. }else{
  52. var gxy_root = '/';
  53. }
  54. var TourItem = Backbone.Model.extend({
  55. urlRoot: gxy_root + 'api/tours',
  56. });
  57. var Tours = Backbone.Collection.extend({
  58. url: gxy_root + 'api/tours',
  59. model: TourItem,
  60. });
  61. var giveTour = function(tour_id){
  62. var url = gxy_root + 'api/tours/' + tour_id;
  63. $.getJSON( url, function( data ) {
  64. // Set hooks for additional click and data entry actions.
  65. tourdata = hooked_tour_from_data(data);
  66. console.log(tourdata);
  67. sessionStorage.setItem('activeGalaxyTour', JSON.stringify(data));
  68. // Store tour steps in sessionStorage to easily persist w/o hackery.
  69. var tour = new Tour(_.extend({
  70. steps: tourdata.steps,
  71. }, tour_opts));
  72. // Always clean restart, since this is a new, explicit giveTour execution.
  73. tour.init();
  74. tour.goTo(0);
  75. tour.restart();
  76. });
  77. }
  78. var ToursView = Backbone.View.extend({
  79. // initialize
  80. initialize: function(options) {
  81. var self = this;
  82. this.setElement('<div/>');
  83. this.model = new Tours()
  84. this.model.fetch({
  85. success: function( model ){
  86. self.render();
  87. },
  88. error: function( model, response ){
  89. // Do something.
  90. console.error("Failed to fetch tours.");
  91. }
  92. });
  93. },
  94. render: function(){
  95. var self = this;
  96. var tpl = _.template([
  97. "<h2>Galaxy Tours</h2>",
  98. "<p>This page presents a list of interactive tours available on this Galaxy server. ",
  99. "Select any tour to get started (and remember, you can click 'End Tour' at any time).</p>",
  100. "<ul>",
  101. '<% _.each(tours, function(tour) { %>',
  102. '<li>',
  103. '<a href="#" class="tourItem" data-tour.id=<%- tour.id %>>',
  104. '<%- tour.attributes.name || tour.id %>',
  105. '</a>',
  106. ' - <%- tour.attributes.description || "No description given." %>',
  107. '</li>',
  108. '<% }); %>',
  109. "</ul>"].join(''));
  110. this.$el.html(tpl({tours: this.model.models})).on("click", ".tourItem", function(e){
  111. giveTour($(this).data("tour.id"));
  112. });
  113. }
  114. });
  115. return {ToursView: ToursView,
  116. hooked_tour_from_data: hooked_tour_from_data,
  117. tour_opts: tour_opts,
  118. giveTour: giveTour}
  119. });