2
0

projectDocumentationWidget.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. if (ZeroClipboard.detectFlashSupport()) {
  95. Spring.buildCopyButton(html.find(":first"), "snippet");
  96. }
  97. return this;
  98. },
  99. remove: function() {
  100. this.undelegateEvents();
  101. this.$el.empty();
  102. this.unbind();
  103. }
  104. });
  105. Spring.QuickStartSelectorView = Backbone.View.extend({
  106. events: {
  107. "change .selector": "renderActiveWidget",
  108. "click .js-item": "changeDownloadSource"
  109. },
  110. initialize: function () {
  111. this.template = _.template(this.options.template);
  112. this.snippetWidgetEl = this.options.snippetWidgetEl;
  113. _.bindAll(this, "render", "renderActiveWidget", "changeDownloadSource", "_moveItemSlider", "selectCurrent");
  114. },
  115. render: function () {
  116. this.$el.html(
  117. this.template(this.model)
  118. );
  119. this.renderActiveWidget();
  120. this.selectCurrent();
  121. this.$('.selectpicker').selectpicker();
  122. return this;
  123. },
  124. selectCurrent: function() {
  125. var selectedIndex = $('.selectpicker [data-current="true"]').val();
  126. if(selectedIndex == undefined) {
  127. selectedIndex = 0;
  128. }
  129. this.$('.selectpicker').val(selectedIndex).change();
  130. },
  131. renderActiveWidget: function() {
  132. if(this.activeWidget != null) this.activeWidget.remove();
  133. this.activeWidget = new Spring.SnippetView({
  134. el: this.snippetWidgetEl,
  135. model: this.model.releases[this.$('.selector :selected').val()],
  136. snippetType: this.$('.js-active').data('snippet-type')
  137. });
  138. this.activeWidget.render();
  139. },
  140. changeDownloadSource: function (event) {
  141. var target = $(event.target);
  142. target.addClass("js-active");
  143. target.siblings().removeClass("js-active");
  144. this._moveItemSlider();
  145. this.renderActiveWidget();
  146. },
  147. _moveItemSlider: function () {
  148. var activeItem = $(".js-item-slider--wrapper .js-item.js-active");
  149. if (activeItem.length == 0) {
  150. return;
  151. } else {
  152. var activeItemPosition = activeItem.position();
  153. var activeItemOffset = activeItemPosition.left;
  154. var activeItemWidth = activeItem.outerWidth();
  155. var slider = $(".js-item--slider");
  156. var sliderPosition = slider.position();
  157. var sliderOffset = sliderPosition.left;
  158. var sliderTarget = activeItemOffset - sliderOffset;
  159. slider.width(activeItemWidth);
  160. slider.css("margin-left", sliderTarget);
  161. }
  162. }
  163. });