Skip to content

Commit

Permalink
Merge pull request #117 from martin-g/tour
Browse files Browse the repository at this point in the history
Issue #116: bootstrap-tour
  • Loading branch information
l0rdn1kk0n committed Dec 11, 2012
2 parents 6f0d4b1 + d9b58da commit b53a7a7
Show file tree
Hide file tree
Showing 12 changed files with 725 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.agilecoders.wicket.markup.html.bootstrap.extensions.references;

import org.apache.wicket.resource.JQueryPluginResourceReference;

/**
* * A JavaScript resource reference that contributes the jquery.cookie.js.
*/
public class JQueryCookieJsReference extends JQueryPluginResourceReference {

public static final JQueryCookieJsReference INSTANCE = new JQueryCookieJsReference();

public JQueryCookieJsReference() {
super(JQueryCookieJsReference.class, "js/jquery.cookie.js");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*!
* jQuery Cookie Plugin
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2011, Klaus Hartl
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
(function($) {
$.cookie = function(key, value, options) {

// key and at least value given, set cookie...
if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
options = $.extend({}, options);

if (value === null || value === undefined) {
options.expires = -1;
}

if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}

value = String(value);

return (document.cookie = [
encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}

// key and possibly options given, get cookie...
options = value || {};
var decode = options.raw ? function(s) { return s; } : decodeURIComponent;

var pairs = document.cookie.split('; ');
for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
}
return null;
};
})(jQuery);
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.agilecoders.wicket.markup.html.bootstrap.extensions.tour;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import de.agilecoders.wicket.markup.html.bootstrap.extensions.references.BootstrapDatepickerJsReference;
import de.agilecoders.wicket.markup.html.bootstrap.extensions.references.JQueryCookieJsReference;
import org.apache.wicket.markup.head.HeaderItem;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.request.resource.JavaScriptResourceReference;

/**
* A JavaScript resource reference that contributes the bootstrap-tour.js and
* its dependencies.
*/
public class BootstrapTourJsReference extends JavaScriptResourceReference
{
public static final BootstrapTourJsReference INSTANCE = new BootstrapTourJsReference();

public BootstrapTourJsReference() {
this(null, null, null);
}

public BootstrapTourJsReference(Locale locale, String style, String variation)
{
super(BootstrapTourJsReference.class, "js/bootstrap-tour.js", locale, style, variation);
}

@Override
public Iterable<? extends HeaderItem> getDependencies()
{
List<HeaderItem> dependencies = new ArrayList<HeaderItem>();
for (HeaderItem dep : super.getDependencies()) {
dependencies.add(dep);
}
dependencies.add(JavaScriptHeaderItem.forReference(JQueryCookieJsReference.INSTANCE));
dependencies.add(JavaScriptHeaderItem.forReference(BootstrapDatepickerJsReference.INSTANCE));
return dependencies;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package de.agilecoders.wicket.markup.html.bootstrap.extensions.tour;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.util.lang.Args;

/**
* A behavior that contributes the resources needed for
* <a href="http://sorich87.github.com/bootstrap-tour/">Bootstrap Tour</a>.
*/
public class TourBehavior extends Behavior {

private final List<TourStep> steps = new ArrayList<TourStep>();

/**
* Adds a step to the tour
*
* @param step The tour step
* @return {@code this} object, for chaining.
*/
public TourBehavior addStep(TourStep step) {
Args.notNull(step, "step");
steps.add(step);
return this;
}

@Override
public void renderHead(Component component, IHeaderResponse response) {
super.renderHead(component, response);

if (steps.size() > 0) {
response.render(JavaScriptHeaderItem.forReference(BootstrapTourJsReference.INSTANCE));

StringBuilder js = new StringBuilder();

js.append("(function() { var tour = new Tour();");
for (TourStep step : steps) {
js.append("tour.addStep(").append(step.toJsonString()).append(");");
}
js.append("tour.start();");
js.append(createExtraConfig());
js.append("})()");
response.render(OnDomReadyHeaderItem.forScript(js));
}
}

/**
* Allows contributing more JavaScript related to the tour.
* @return extra tour related JavaScript
*/
protected CharSequence createExtraConfig()
{
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package de.agilecoders.wicket.markup.html.bootstrap.extensions.tour;

import com.google.common.base.Preconditions;
import de.agilecoders.wicket.markup.html.bootstrap.common.AbstractConfig;
import de.agilecoders.wicket.markup.html.bootstrap.components.TooltipConfig;
import org.apache.wicket.Component;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.lang.Objects;

/**
* An configuration object representing a step in a tour
*/
public class TourStep extends AbstractConfig {

private static class Key<T> implements IKey {

/**
* Path to the page on which the step should be shown. this allows you to build tours that span several pages!.
*/
private static final Key<String> Path = new Key<String>("path", String.class, "");

/**
* jQuery selector to the HTML element on which the step popover should be shown.
*/
private static final Key<String> Element = new Key<String>("element", String.class, "");

/**
* How to position the popover - top | bottom | left | right..
*/
private static final Key<TooltipConfig.Placement> Placement =
new Key<TooltipConfig.Placement>("placement", TooltipConfig.Placement.class, TooltipConfig.Placement.right);

/**
* The step's title
*/
private static final Key<IModel> Title = new Key<IModel>("title", IModel.class, Model.of("Step"));

/**
* A flag indicating whether to apply a css fade transition to the tooltip.
*/
private static final Key<Boolean> Animation = new Key<Boolean>("animation", Boolean.class, Boolean.TRUE);

/**
* The step's content
*/
private static final Key<IModel> Content = new Key<IModel>("content", IModel.class, Model.of(""));

private final String key;
private final Class<T> type;
private final T defaultValue;

/**
* Construct.
*
* @param key string representation of this key
* @param type The object type
* @param defaultValue The default value
*/
private Key(final String key, final Class<T> type, final T defaultValue) {
this.key = key;
this.type = type;
this.defaultValue = defaultValue;
}

@Override
public String key() {
return key;
}

@Override
public void assertCorrectType(final Object value) {
Preconditions.checkArgument(type.isInstance(value));
}

@Override
public boolean isDefaultValue(final Object value) {
return Objects.equal(value, defaultValue);
}

@Override
public Object getDefaultValue() {
return defaultValue;
}
}

public TourStep path(String path) {
put(Key.Path, path);
return this;
}

public TourStep element(Component element) {
element.setOutputMarkupId(true);
put(Key.Element, "#" + element.getMarkupId());
return this;
}

public TourStep placement(TooltipConfig.Placement placement) {
put(Key.Placement, placement);
return this;
}

public TourStep title(IModel<String> title) {
put(Key.Title, wrap(title));
return this;
}

public TourStep content(IModel<String> content) {
put(Key.Content, wrap(content));
return this;
}

public TourStep animate(boolean animate) {
put(Key.Animation, animate);
return this;
}
}
Loading

0 comments on commit b53a7a7

Please sign in to comment.