projectDocumentationWidget.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 resourcesEl = $('.project-sidebar-resource--wrapper');
  13. var projectUrl = apiBaseUrl + "/project_metadata/" + projectId;
  14. var promise = Spring.loadProject(projectUrl);
  15. var coursesPromise = Spring.loadCourses("https://pivotallms.biglms.com/api/courses");
  16. promise.then(function (project) {
  17. Spring.buildDocumentationWidget(documentationEl, project);
  18. Spring.buildQuickStartWidget(quickStartEl, mavenWidgetEl, project);
  19. });
  20. coursesPromise.then(function(courseware) {
  21. Spring.buildCoursesWidget(resourcesEl, courseware);
  22. });
  23. };
  24. Spring.buildDocumentationWidget = function (documentationEl, project) {
  25. new Spring.DocumentationWidgetView({
  26. el: documentationEl,
  27. model: project,
  28. template: $("#project-documentation-widget-template").text()
  29. }).render();
  30. }
  31. Spring.buildCoursesWidget = function (resourcesEl, courseware) {
  32. if(courseware.hasContent) {
  33. var tpl = $("#project-courses-widget-template").text();
  34. var view = new Spring.CoursesWidgetView({
  35. el: resourcesEl,
  36. model: courseware,
  37. template: $("#project-courses-widget-template").text()
  38. });
  39. view.render();
  40. }
  41. }
  42. Spring.buildQuickStartWidget = function (quickStartEl, mavenWidgetEl, project) {
  43. new Spring.QuickStartSelectorView({
  44. el: quickStartEl,
  45. model: project,
  46. template: $("#project-download-widget-controls-template").text(),
  47. snippetWidgetEl: mavenWidgetEl
  48. }).render();
  49. }
  50. Spring.loadProject = function (url) {
  51. return $.ajax(url, {
  52. dataType: 'jsonp',
  53. processData: false
  54. }).then(function (value) {
  55. return new Spring.Project(value);
  56. });
  57. }
  58. Spring.loadCourses = function (url) {
  59. return $.getJSON(url)
  60. .then(function(data) {
  61. return new Spring.Courseware(data);
  62. });
  63. }
  64. Spring.Release = function (data) {
  65. _.extend(this, data);
  66. }
  67. Spring.Release.prototype = {
  68. statusIconClass: function () {
  69. if (this.preRelease) {
  70. return "spring-icon-pre-release";
  71. } else if (this.generalAvailability) {
  72. return "spring-icon-ga-release";
  73. } else {
  74. return "spring-icon-snapshot-release";
  75. }
  76. }
  77. }
  78. Spring.Courseware = function (data) {
  79. this.courses = data["edu1"];
  80. this.talks = data["eng1"];
  81. this.hasCourses = this.courses != null;
  82. this.hasTalks = this.talks != null;
  83. this.hasContent = this.hasTalks || this.hasCourses;
  84. return this;
  85. };
  86. Spring.Project = function (data) {
  87. _.extend(this, data);
  88. var self = this;
  89. this.releases = _.map(this.projectReleases, function (r) {
  90. return new Spring.Release(r);
  91. });
  92. return this;
  93. };
  94. Spring.DocumentationWidgetView = Backbone.View.extend({
  95. initialize: function () {
  96. this.template = _.template(this.options.template);
  97. _.bindAll(this, "render");
  98. },
  99. render: function () {
  100. this.$el.html(
  101. this.template(this.model)
  102. );
  103. return this;
  104. }
  105. });
  106. Spring.CoursesWidgetView = Backbone.View.extend({
  107. initialize: function () {
  108. this.template = _.template(this.options.template);
  109. _.bindAll(this, "render");
  110. },
  111. render: function () {
  112. this.$el.append(
  113. this.template(this.model)
  114. );
  115. return this;
  116. }
  117. });
  118. Spring.SnippetView = Backbone.View.extend({
  119. initialize: function () {
  120. var snippetType = this.options.snippetType;
  121. var downloadTemplate = $(document.createElement('div')).html($("#project-download-" + snippetType + "-widget-template").text());
  122. var repositoryTemplate = $(document.createElement('div')).html($("#project-repository-" + snippetType + "-widget-template").text());
  123. this.combinedTemplate = _.template(
  124. "<div class=\"highlight\"><pre><code>" +
  125. downloadTemplate.find("code:first").html() +
  126. "{@ if (repository) { @}" +
  127. repositoryTemplate.find("code:first").html() +
  128. "{@ } @}" +
  129. "</code></pre></div>"
  130. );
  131. _.bindAll(this, "render");
  132. },
  133. render: function () {
  134. var html = $(this.combinedTemplate(this.model));
  135. this.$el.html(html);
  136. if (ZeroClipboard.detectFlashSupport()) {
  137. Spring.buildCopyButton(html.find(":first"), "snippet");
  138. }
  139. return this;
  140. },
  141. remove: function() {
  142. this.undelegateEvents();
  143. this.$el.empty();
  144. this.unbind();
  145. }
  146. });
  147. Spring.QuickStartSelectorView = Backbone.View.extend({
  148. events: {
  149. "change .selector": "renderActiveWidget",
  150. "click .js-item": "changeDownloadSource"
  151. },
  152. initialize: function () {
  153. this.template = _.template(this.options.template);
  154. this.snippetWidgetEl = this.options.snippetWidgetEl;
  155. _.bindAll(this, "render", "renderActiveWidget", "changeDownloadSource", "_moveItemSlider", "selectCurrent");
  156. },
  157. render: function () {
  158. this.$el.html(
  159. this.template(this.model)
  160. );
  161. this.renderActiveWidget();
  162. this.selectCurrent();
  163. this.$('.selectpicker').selectpicker();
  164. return this;
  165. },
  166. selectCurrent: function() {
  167. var selectedIndex = $('.selectpicker [data-current="true"]').val();
  168. if(selectedIndex == undefined) {
  169. selectedIndex = 0;
  170. }
  171. this.$('.selectpicker').val(selectedIndex).change();
  172. },
  173. renderActiveWidget: function() {
  174. if(this.activeWidget != null) this.activeWidget.remove();
  175. this.activeWidget = new Spring.SnippetView({
  176. el: this.snippetWidgetEl,
  177. model: this.model.releases[this.$('.selector :selected').val()],
  178. snippetType: this.$('.js-active').data('snippet-type')
  179. });
  180. this.activeWidget.render();
  181. },
  182. changeDownloadSource: function (event) {
  183. var target = $(event.target);
  184. target.addClass("js-active");
  185. target.siblings().removeClass("js-active");
  186. this._moveItemSlider();
  187. this.renderActiveWidget();
  188. },
  189. _moveItemSlider: function () {
  190. var activeItem = $(".js-item-slider--wrapper .js-item.js-active");
  191. if (activeItem.length == 0) {
  192. return;
  193. } else {
  194. var activeItemPosition = activeItem.position();
  195. var activeItemOffset = activeItemPosition.left;
  196. var activeItemWidth = activeItem.outerWidth();
  197. var slider = $(".js-item--slider");
  198. var sliderPosition = slider.position();
  199. var sliderOffset = sliderPosition.left;
  200. var sliderTarget = activeItemOffset - sliderOffset;
  201. slider.width(activeItemWidth);
  202. slider.css("margin-left", sliderTarget);
  203. }
  204. }
  205. });