Permalink
Browse files

Merge pull request #18 from erikvold/ao-update

achievement-overlay module updates
  • Loading branch information...
2 parents 98afc9e + 7e92e1e commit c549d59976507f82f85122686fa84c02debf1fbc @msujaws committed Sep 23, 2012
Showing with 91 additions and 93 deletions.
  1. +91 −93 lib/achievement-overlay.js
@@ -1,78 +1,104 @@
+"use strict";
+
+const winUtils = require("window-utils");
+const {isBrowser} = require("api-utils/window/utils");
+const {unload} = require("unload+");
+
const NS_XUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
const NS_HTML = "http://www.w3.org/1999/xhtml";
+const debug = false;
+
exports.AchievementOverlay = function AchievementOverlay(options) {
var unloaders = [],
insertbefore = "browser-bottombox",
destroyed = false,
- destroyFuncs = [];
- const debug = false;
+ id = options.id;
+
+ winUtils.WindowTracker(OverlayTracker(options, insertbefore, unloaders, destroyed));
+
+ return {
+ destroy: function() {
+ if (destroyed) return;
+ destroyed = true;
+ unloaders.forEach(function(unload) unload());
+ },
+ show: function(data) {
+ for each (var window in winUtils.windowIterator()) {
+ if (!isBrowser(window)) return;
+
+ updateOverlay(
+ id, data, function(id) window.document.getElementById(id));
+ }
+ },
+ };
+};
+
+function updateOverlay(id, data, $) {
+ let overlay = $(id);
+ if (!overlay) return;
+
+ $("cheevos-achievementOverlay-achievement").className = data.awardsClass;
+ $("cheevos-achievementOverlay-h1").textContent = data.title;
+ $("cheevos-achievementOverlay-h2").textContent = data.text;
+ $("cheevos-achievementOverlay-points").textContent = data.points;
+ $("cheevos-achievementOverlay-meta").className = data.awardsClass;
+
+ overlay.hidden = false;
+ // Trigger a reflow so the setting of the attributes are separate.
+ overlay.boxObject.height;
+ overlay.setAttribute("fadeOut", true);
+}
+
+function node(doc, ns, type, attrs, parent) {
+ let ele = doc.createElementNS(ns, type);
+ attrs && Object.keys(attrs).forEach(function(attr) {
+ ele.setAttribute(attr, attrs[attr]);
+ });
+ parent && parent.appendChild(ele);
+ return ele;
+}
+
+function OverlayTracker(options, insertbefore, unloaders, destroyed) {
+ let id = options.id;
- var delegate = {
+ return {
onTrack: function (window) {
- if ("chrome://browser/content/browser.xul" != window.location || destroyed)
- return;
+ if (!isBrowser(window) || destroyed) return;
let doc = window.document;
function $(id) doc.getElementById(id);
- function xul(type) doc.createElementNS(NS_XUL, type);
- function html(type) doc.createElementNS(NS_HTML, type);
-
- let box1 = xul("hbox");
- let box2 = xul("hbox");
- let box3 = xul("hbox");
- let achievement = html("div");
- let award = html("img");
- let h1 = html("h1");
- let h2 = html("h2");
- let meta = html("div");
- let points = html("div");
- let medals = html("ol");
- let bronze = html("li");
- let silver = html("li");
- let gold = html("li");
-
- box1.setAttribute("hidden", true);
- box1.setAttribute("id", options.id);
+ let xul = node.bind(null, doc, NS_XUL);
+ let html = node.bind(null, doc, NS_HTML);
+
+ let box1 = xul("hbox", {
+ id: id,
+ hidden: true
+ });
+ let box2 = xul("hbox", {pack: "center"}, box1);
+ let box3 = xul("hbox", {id: id + "-message"}, box2);
+ let achievement = html("div", {id: id + "-achievement"}, box3);
+ let award = html("img", {id: id + "-award"}, achievement);
+ let h1 = html("h1", {id: id + "-h1"}, achievement);
+ let h2 = html("h2", {id: id + "-h2"}, achievement);
+ let meta = html("div", {id: id + "-meta"}, achievement);
+ let points = html("div", {id: id + "-points"}, meta);
+ let medals = html("ol", {id: id + "-medals"}, meta);
+ let bronze = html("li", {}, medals);
+ let silver = html("li", {}, medals);
+ let gold = html("li", {}, medals);
+
box1.addEventListener("command", function() {
- if (options.onCommand)
- options.onCommand({});
+ options.onCommand && options.onCommand({});
}, true);
- box2.setAttribute("pack", "center");
- box3.setAttribute("id", options.id + "-message");
- achievement.setAttribute("id", options.id + "-achievement");
- award.setAttribute("id", options.id + "-award");
- h1.setAttribute("id", options.id + "-h1");
- h2.setAttribute("id", options.id + "-h2");
- meta.setAttribute("id", options.id + "-meta");
- points.setAttribute("id", options.id + "-points");
- medals.setAttribute("id", options.id + "-medals");
h1.textContent = "Cheevo acquired!";
- box1.appendChild(box2);
- box2.appendChild(box3);
- box3.appendChild(achievement);
- achievement.appendChild(award);
- achievement.appendChild(h1);
- achievement.appendChild(h2);
- achievement.appendChild(meta);
- meta.appendChild(points);
- meta.appendChild(medals);
- medals.appendChild(bronze);
- medals.appendChild(silver);
- medals.appendChild(gold);
-
- let overlay = $(options.id);
- if (!overlay) {
- let b4,
- parentNode = $('browser-panel');
-
- if (insertbefore) {
- b4 = $(insertbefore);
- }
- overlay = parentNode.insertBefore(box1, b4);
- }
- this.onAchievementHidden = function(event) {
+ let overlay = $(id);
+ // if the overlay element dne, then add it
+ if (!overlay)
+ overlay = $("browser-panel").insertBefore(box1, $(insertbefore));
+
+ overlay.addEventListener("transitionend", function onAchievementHidden(event) {
if (debug)
console.log('onAchievementHidden reached');
if (event.propertyName != "opacity")
@@ -81,39 +107,11 @@ exports.AchievementOverlay = function AchievementOverlay(options) {
overlay.hidden = true;
overlay.removeAttribute("fadeOut");
}
- }.bind(this);
- overlay.addEventListener("transitionend", this.onAchievementHidden, false);
- },
- onUntrack: function (window) {}
- };
- var winUtils = require("window-utils");
- var tracker = new winUtils.WindowTracker(delegate);
+ }, false);
- return {
- destroy: function() {
- if (destroyed) return;
- destroyed = true;
- },
- _timeoutId: 0,
- show: function(data) {
- for each (var window in winUtils.windowIterator()) {
- if ("chrome://browser/content/browser.xul" != window.location) return;
-
- let doc = window.document;
- let $ = function (id) doc.getElementById(id);
- let overlay = $(options.id);
- if (overlay) {
- $("cheevos-achievementOverlay-achievement").className = data.awardsClass;
- $("cheevos-achievementOverlay-h1").textContent = data.title;
- $("cheevos-achievementOverlay-h2").textContent = data.text;
- $("cheevos-achievementOverlay-points").textContent = data.points;
- $("cheevos-achievementOverlay-meta").className = data.awardsClass;
- overlay.hidden = false;
- // Trigger a reflow so the setting of the attributes are separate.
- overlay.boxObject.height;
- overlay.setAttribute("fadeOut", true);
- }
- }
- },
+ unloaders.push(unload(function() {
+ overlay.parentNode.removeChild(overlay);
+ }, window));
+ }
};
-};
+}

0 comments on commit c549d59

Please sign in to comment.