Files
spring-batch/spring-batch-docs/asciidoc/jsfiles/projectDocumentationWidget.js
Glenn Renfro bff2c14671 Docs support toggling Java Config and XML
This is a demo of the functionality and it located in the domain.adoc file.

added code to handl PDF ifdef

JavaScript added to support slider control

If view in local file system use safari.   (Cookies not supported on local filesysem on chrome)
Remaining bug...  if user selects xml and refreshes the examples still show XML but slider goes back to JAVA.

Could use a new tool suite.  But this is based on the Spring.io toggle.

Implemented java vs xml toggle in reference documentation

This commit ports the javascript widget used in Sagan for toggling
between Maven and Gradle on each project page and uses it to toggle
between java based config and XML based config.  The preference the user
selects is cookied so that it should stick between pages in the multiple
page layout as well as for future visits to the documentation.

The only page that has implemented the toggle in this commit is the
domain.adoc page.  The spring-batch-intro.adoc has the toggle reference
in it, but it's only there to verify multiple includes are working.

Polishing
2017-08-24 14:36:29 -04:00

177 lines
4.8 KiB
JavaScript

window.Spring = window.Spring || {};
var entry = 0;
/* 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 codeEl = $('[code-widget-controls]');
var codeWidgetEl = $('.js-code-maven-widget');
Spring.buildCodeWidget(codeEl, codeWidgetEl);
var displayValue = Cookies.get("widget.display");
if(displayValue === 'xml') {
$('.xml_snip_item').each(function() {
var widget = $(this).closest('.toggleWidget');
if(!widget.is(":hidden")) {
$(this).trigger("click");
}
});
}
else if(displayValue === 'java') {
$('.java_snip_item').each(function() {
var widget = $(this).closest('.toggleWidget');
if(!widget.is(":hidden")) {
$(this).trigger("click");
}
});
}
};
Spring.buildCodeWidget = function (codeEl, codeWidgetEl) {
new Spring.CodeSelectorView({
el: codeEl,
template: $("#code-widget-controls-template").text(),
snippetWidgetEl: codeWidgetEl
}).render();
}
function isJavaVisible(displayVal, currentVal) {
var result= false;
if(entry == 1 && (displayVal === '' || displayVal === 'widget.display=java')) {
result = true;
}
else if (entry > 1) {
result = (currentVal === 'java');
}
return result;
}
Spring.SnippetView = Backbone.View.extend({
initialize: function () {
var displayVal = document.cookie;
entry++;
var javaDisplayStatus = this.options.snippetType;
if(isJavaVisible(displayVal, javaDisplayStatus)) {
$('.javaContent').each(function() {
$(this).show();
});
$('.xmlContent').each(function() {
$(this).hide();
});
javaDisplayStatus = 'widget.display=java';
} else {
$('.xmlContent').each(function() {
$(this).show();
});
$('.javaContent').each(function() {
$(this).hide();
});
javaDisplayStatus = 'widget.display=xml';
}
document.cookie = javaDisplayStatus;
_.bindAll(this, "render");
},
remove: function() {
this.undelegateEvents();
this.$el.empty();
this.unbind();
}
});
Spring.CodeSelectorView = Backbone.View.extend({
events: {
"change .selector": "renderActiveWidget",
"click .js-item": "changeCodeSource"
},
initialize: function () {
this.template = _.template(this.options.template);
this.snippetWidgetEl = this.options.snippetWidgetEl;
_.bindAll(this, "render", "renderActiveWidget", "changeCodeSource", "_moveItemSlider", "selectCurrent");
},
render: function () {
this.$el.html(
this.template(this.model)
);
this.renderActiveWidget();
this.selectCurrent();
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,
snippetType: this.$('.js-active').data('snippet-type')
});
this.activeWidget.render();
},
changeCodeSource: 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 = 34;
if(activeItem.data("snippet-type") === "xml") {
if(activeItemPosition.left < 100) {
activeItemOffset = activeItemPosition.left - 2;
}
else {
activeItemOffset = 78;
}
}
var slider = $(".js-item--slider");
var sliderPosition = slider.position();
var sliderOffset = sliderPosition.left;
var sliderTarget = activeItemOffset - sliderOffset;
slider.width(40);
slider.css("margin-left", sliderTarget);
}
}
});