123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- window.Spring = window.Spring || {};
- /* ERB style templates conflict with Jekyll HTML escaping */
- _.templateSettings = {
- evaluate : /\{@([\s\S]+?)@\}/g,
- interpolate : /\{@=([\s\S]+?)@\}/g,
- escape : /\{@-([\s\S]+?)@\}/g
- };
- Spring.ProjectDocumentationWidget = function () {
- var quickStartEl = $('[data-download-widget-controls]');
- var mavenWidgetEl = $('.js-download-maven-widget');
- var documentationEl = $('.js-documentation-widget');
- var projectUrl = apiBaseUrl + "/project_metadata/" + projectId;
- var promise = Spring.loadProject(projectUrl);
- promise.then(function (project) {
- Spring.buildDocumentationWidget(documentationEl, project);
- Spring.buildQuickStartWidget(quickStartEl, mavenWidgetEl, project);
- });
- };
- Spring.buildDocumentationWidget = function (documentationEl, project) {
- new Spring.DocumentationWidgetView({
- el: documentationEl,
- model: project,
- template: $("#project-documentation-widget-template").text()
- }).render();
- }
- Spring.buildQuickStartWidget = function (quickStartEl, mavenWidgetEl, project) {
- new Spring.QuickStartSelectorView({
- el: quickStartEl,
- model: project,
- template: $("#project-download-widget-controls-template").text(),
- snippetWidgetEl: mavenWidgetEl
- }).render();
- }
- Spring.loadProject = function (url) {
- return $.ajax(url, {
- dataType: 'jsonp',
- processData: false
- }).then(function (value) {
- return new Spring.Project(value);
- });
- }
- Spring.Release = function (data) {
- _.extend(this, data);
- }
- Spring.Release.prototype = {
- statusIconClass: function () {
- if (this.preRelease) {
- return "spring-icon-pre-release";
- } else if (this.generalAvailability) {
- return "spring-icon-ga-release";
- } else {
- return "spring-icon-snapshot-release";
- }
- }
- }
- Spring.Project = function (data) {
- _.extend(this, data);
- var self = this;
- this.releases = _.map(this.projectReleases, function (r) {
- return new Spring.Release(r);
- });
- return this;
- };
- Spring.DocumentationWidgetView = Backbone.View.extend({
- initialize: function () {
- this.template = _.template(this.options.template);
- _.bindAll(this, "render");
- },
- render: function () {
- this.$el.html(
- this.template(this.model)
- );
- return this;
- }
- });
- Spring.SnippetView = Backbone.View.extend({
- initialize: function () {
- var snippetType = this.options.snippetType;
- var downloadTemplate = $(document.createElement('div')).html($("#project-download-" + snippetType + "-widget-template").text());
- var repositoryTemplate = $(document.createElement('div')).html($("#project-repository-" + snippetType + "-widget-template").text());
- this.combinedTemplate = _.template(
- "<div class=\"highlight\"><pre><code>" +
- downloadTemplate.find("code:first").html() +
- "{@ if (repository) { @}" +
- repositoryTemplate.find("code:first").html() +
- "{@ } @}" +
- "</code></pre></div>"
- );
- _.bindAll(this, "render");
- },
- render: function () {
- var html = $(this.combinedTemplate(this.model));
- this.$el.html(html);
- if (ZeroClipboard.detectFlashSupport()) {
- Spring.buildCopyButton(html.find(":first"), "snippet");
- }
- return this;
- },
- remove: function() {
- this.undelegateEvents();
- this.$el.empty();
- this.unbind();
- }
- });
- Spring.QuickStartSelectorView = Backbone.View.extend({
- events: {
- "change .selector": "renderActiveWidget",
- "click .js-item": "changeDownloadSource"
- },
- initialize: function () {
- this.template = _.template(this.options.template);
- this.snippetWidgetEl = this.options.snippetWidgetEl;
- _.bindAll(this, "render", "renderActiveWidget", "changeDownloadSource", "_moveItemSlider", "selectCurrent");
- },
- render: function () {
- this.$el.html(
- this.template(this.model)
- );
- this.renderActiveWidget();
- this.selectCurrent();
- this.$('.selectpicker').selectpicker();
- return this;
- },
- selectCurrent: function() {
- var selectedIndex = $('.selectpicker [data-current="true"]').val();
- if(selectedIndex == undefined) {
- selectedIndex = 0;
- }
- this.$('.selectpicker').val(selectedIndex).change();
- },
- renderActiveWidget: function() {
- if(this.activeWidget != null) this.activeWidget.remove();
- this.activeWidget = new Spring.SnippetView({
- el: this.snippetWidgetEl,
- model: this.model.releases[this.$('.selector :selected').val()],
- snippetType: this.$('.js-active').data('snippet-type')
- });
- this.activeWidget.render();
- },
- changeDownloadSource: function (event) {
- var target = $(event.target);
- target.addClass("js-active");
- target.siblings().removeClass("js-active");
- this._moveItemSlider();
- this.renderActiveWidget();
- },
- _moveItemSlider: function () {
- var activeItem = $(".js-item-slider--wrapper .js-item.js-active");
- if (activeItem.length == 0) {
- return;
- } else {
- var activeItemPosition = activeItem.position();
- var activeItemOffset = activeItemPosition.left;
- var activeItemWidth = activeItem.outerWidth();
- var slider = $(".js-item--slider");
- var sliderPosition = slider.position();
- var sliderOffset = sliderPosition.left;
- var sliderTarget = activeItemOffset - sliderOffset;
- slider.width(activeItemWidth);
- slider.css("margin-left", sliderTarget);
- }
- }
- });
|