projectDocumentationWidget.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. window.Spring = window.Spring || {};
  2. /* ERB style templates conflict with Jekyll HTML escaping */
  3. _.templateSettings = {
  4. evaluate : /\{@([\s\S]+?)@\}/g,
  5. interpolate : /\{@=([\s\S]+?)@\}/g,
  6. escape : /\{@-([\s\S]+?)@\}/g
  7. };
  8. Spring.ProjectDocumentationWidget = function () {
  9. var quickStartEl = $('[data-download-widget-controls]');
  10. var mavenWidgetEl = $('.js-download-maven-widget');
  11. var documentationEl = $('.js-documentation-widget');
  12. var projectUrl = apiBaseUrl + "/project_metadata/" + projectId;
  13. var promise = Spring.loadProject(projectUrl);
  14. promise.then(function (project) {
  15. Spring.buildDocumentationWidget(documentationEl, project);
  16. Spring.buildQuickStartWidget(quickStartEl, mavenWidgetEl, project);
  17. });
  18. };
  19. Spring.buildDocumentationWidget = function (documentationEl, project) {
  20. new Spring.DocumentationWidgetView({
  21. el: documentationEl,
  22. model: project,
  23. template: $("#project-documentation-widget-template").text()
  24. }).render();
  25. }
  26. Spring.buildQuickStartWidget = function (quickStartEl, mavenWidgetEl, project) {
  27. new Spring.QuickStartSelectorView({
  28. el: quickStartEl,
  29. model: project,
  30. template: $("#project-download-widget-controls-template").text(),
  31. snippetWidgetEl: mavenWidgetEl
  32. }).render();
  33. }
  34. Spring.loadProject = function (url) {
  35. return $.ajax(url, {
  36. dataType: 'jsonp',
  37. processData: false
  38. }).then(function (value) {
  39. return new Spring.Project(value);
  40. });
  41. }
  42. Spring.Release = function (data) {
  43. _.extend(this, data);
  44. }
  45. Spring.Release.prototype = {
  46. statusIconClass: function () {
  47. if (this.preRelease) {
  48. return "spring-icon-pre-release";
  49. } else if (this.generalAvailability) {
  50. return "spring-icon-ga-release";
  51. } else {
  52. return "spring-icon-snapshot-release";
  53. }
  54. }
  55. }
  56. Spring.Project = function (data) {
  57. _.extend(this, data);
  58. var self = this;
  59. this.releases = _.map(this.projectReleases, function (r) {
  60. return new Spring.Release(r);
  61. });
  62. return this;
  63. };
  64. Spring.DocumentationWidgetView = Backbone.View.extend({
  65. initialize: function () {
  66. this.template = _.template(this.options.template);
  67. _.bindAll(this, "render");
  68. },
  69. render: function () {
  70. this.$el.html(
  71. this.template(this.model)
  72. );
  73. return this;
  74. }
  75. });
  76. Spring.SnippetView = Backbone.View.extend({
  77. initialize: function () {
  78. var snippetType = this.options.snippetType;
  79. var downloadTemplate = $(document.createElement('div')).html($("#project-download-" + snippetType + "-widget-template").text());
  80. var repositoryTemplate = $(document.createElement('div')).html($("#project-repository-" + snippetType + "-widget-template").text());
  81. this.combinedTemplate = _.template(
  82. "<div class=\"highlight\"><pre><code>" +
  83. downloadTemplate.find("code:first").html() +
  84. "{@ if (repository) { @}" +
  85. repositoryTemplate.find("code:first").html() +
  86. "{@ } @}" +
  87. "</code></pre></div>"
  88. );
  89. _.bindAll(this, "render");
  90. },
  91. render: function () {
  92. var html = $(this.combinedTemplate(this.model));
  93. this.$el.html(html);
  94. Spring.buildCopyButton(html.find(":first"), "snippet");
  95. return this;
  96. },
  97. remove: function() {
  98. this.undelegateEvents();
  99. this.$el.empty();
  100. this.unbind();
  101. }
  102. });
  103. Spring.QuickStartSelectorView = Backbone.View.extend({
  104. events: {
  105. "change .selector": "renderActiveWidget",
  106. "click .js-item": "changeDownloadSource"
  107. },
  108. initialize: function () {
  109. this.template = _.template(this.options.template);
  110. this.snippetWidgetEl = this.options.snippetWidgetEl;
  111. _.bindAll(this, "render", "renderActiveWidget", "changeDownloadSource", "_moveItemSlider", "selectCurrent");
  112. },
  113. render: function () {
  114. this.$el.html(
  115. this.template(this.model)
  116. );
  117. this.renderActiveWidget();
  118. this.selectCurrent();
  119. this.$('.selectpicker').selectpicker();
  120. return this;
  121. },
  122. selectCurrent: function() {
  123. var selectedIndex = $('.selectpicker [data-current="true"]').val();
  124. if(selectedIndex == undefined) {
  125. selectedIndex = 0;
  126. }
  127. this.$('.selectpicker').val(selectedIndex).change();
  128. },
  129. renderActiveWidget: function() {
  130. if(this.activeWidget != null) this.activeWidget.remove();
  131. this.activeWidget = new Spring.SnippetView({
  132. el: this.snippetWidgetEl,
  133. model: this.model.releases[this.$('.selector :selected').val()],
  134. snippetType: this.$('.js-active').data('snippet-type')
  135. });
  136. this.activeWidget.render();
  137. },
  138. changeDownloadSource: function (event) {
  139. var target = $(event.target);
  140. target.addClass("js-active");
  141. target.siblings().removeClass("js-active");
  142. this._moveItemSlider();
  143. this.renderActiveWidget();
  144. },
  145. _moveItemSlider: function () {
  146. var activeItem = $(".js-item-slider--wrapper .js-item.js-active");
  147. if (activeItem.length == 0) {
  148. return;
  149. } else {
  150. var activeItemPosition = activeItem.position();
  151. var activeItemOffset = activeItemPosition.left;
  152. var activeItemWidth = activeItem.outerWidth();
  153. var slider = $(".js-item--slider");
  154. var sliderPosition = slider.position();
  155. var sliderOffset = sliderPosition.left;
  156. var sliderTarget = activeItemOffset - sliderOffset;
  157. slider.width(activeItemWidth);
  158. slider.css("margin-left", sliderTarget);
  159. }
  160. }
  161. });