Skip to content

Commit

Permalink
Permalink module now updates the document title.
Browse files Browse the repository at this point in the history
  • Loading branch information
reid committed Sep 27, 2010
1 parent 2777217 commit 3dfded3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
45 changes: 37 additions & 8 deletions s7/s7-permalink.js
Expand Up @@ -3,28 +3,57 @@ YUI.add("s7-permalink", function (Y) {
// enable indexing by search engines
Y.HistoryHash.hashPrefix = "!";

var history = new Y.HistoryHash();
var history = new Y.HistoryHash,
title,
titleContent;

Y.S7.on("start", function () {
title = Y.one("title");
titleContent = title.get("innerHTML");

Y.S7.fire("position", history.get("slide") || 1);
});

Y.S7.on("navigate", function (idx) {
Y.log("history, add: " + idx);
Y.log("history, current: " + Y.S7.currentSlide);
history.addValue("slide", idx);
});

Y.on("history:change", function (ev) {
if (ev.src !== Y.HistoryHash.SRC_HASH) return;
var idx = 1;
if (ev.changed.slide) idx = ev.changed.slide.newVal;
Y.S7.fire("position", idx);
Y.S7.on("transition", function (ev) {
var next = ev.details[1],
idx = next.getData("slide"),
slideTitle;

if (idx == 1) {
// ignore the title slide,
// because I like it that way.
slideTitle = titleContent;
} else {
var h1 = next.one("h1");
if (h1) slideTitle = h1.get("innerHTML");
if (!slideTitle) slideTitle = "Slide " + next.getData("slide");
slideTitle = titleContent + ": " + slideTitle;
}

title.setContent(slideTitle);
});

// no matter what ev.src this came from, we will get
// two history:change events for every change
// that's ok, since position only acts if a change occurs

function positioner (idx) {
if (idx && idx.newVal) idx = idx.newVal;
else idx = 1;
Y.S7.fire("position", idx);
}

history.on("slideChange", positioner);
history.on("slideRemove", positioner);

}, "0.0.1", {
requires : [
"s7-slideshow",
"node",
"history"
]
});
22 changes: 15 additions & 7 deletions s7/s7-slideshow.js
Expand Up @@ -4,40 +4,48 @@ YUI.add("s7-slideshow", function (Y) {
Y.augment(Y.S7, Y.EventTarget);

Y.S7.on("start", function () {
// give every slide a #slide{n} id
Y.all(".slide").each(function (node, idx) {
node.set("id", "slide" + (idx+1));
idx++;
node.set("id", "slide" + idx);
node.setData("slide", idx);
});
// navigate to slide 1
Y.S7.fire("position", 1);
});

// many not be absolute
// warp: give a relative number of steps to navigate to
Y.S7.on("warp", function (rel, mouseEvent) {
if (mouseEvent && mouseEvent.halt) mouseEvent.halt(); // prevent navigation to "#"

var idx = Y.S7.currentSlide + parseInt(rel, 10);
Y.log("WARP! current: " + Y.S7.currentSlide);
Y.log("WARP! rel: " + rel);
Y.log("WARP! to: " + idx);

Y.log("warp: to slide " + idx + " from slide " + Y.S7.currentSlide);
Y.S7.fire("position", idx);
});

// position: give the slide number you'd like to navigate to
Y.S7.on("position", function (next) {
// can't go earlier than the first slide
// can't go further than the last slide
next = Math.max(1, next),
next = Math.min(next, Y.all(".slide").size());
Y.log("POSITION:REQUEST! will position to: " + next);
Y.log("position: should the next slide be " + next);

var previous = Y.S7.currentSlide || 1;
Y.S7.currentSlide = parseInt(next, 10);

if (previous != next) {
Y.log("position: yes, firing transition and navigate");
Y.S7.fire("navigate", next); // fired only when navigation is happening
Y.S7.fire("transition",
Y.one("#slide" + previous),
Y.one("#slide" + next)
);
Y.S7.fire("navigate", next);
}
});

// transition: moves from slide A to B. may be overriden with preventDefault.
Y.S7.publish("transition", {
emitFacade : true,
defaultFn : function (ev) {
Expand Down

0 comments on commit 3dfded3

Please sign in to comment.