Permalink
Browse files

Screen cycling and removing.

  • Loading branch information...
1 parent 19d837f commit d6ba790d7a03500c760efae577541c717820ace9 @mythmon committed Jan 11, 2013
Showing with 123 additions and 121 deletions.
  1. +73 −20 manager.js
  2. +9 −0 static/css/crimsontwins.less
  3. +41 −33 static/js/managed.js
  4. +0 −68 web.js
View
@@ -32,27 +32,59 @@ _.each(config.resetUrls, function(url) {
});
exports.addScreen = function(name) {
+ var id = utils.getId();
var screen = {
- id: utils.getId(),
+ id: id,
name: name,
- content: getDefaultContent()
+ content: null,
+ resetId: null
};
screens.push(screen);
- everyone.now.screenAdded(screen);
+ cycleScreen(screen.id);
+ sendScreenAdded(screen);
+};
+
+removeScreen = function(id) {
+ var screen = findScreen('id', id);
+ if (screen !== undefined) {
+ clearTimeout(screen.timeout);
+ screens = _.without(screens, screen);
+ sendScreenRemoved(screen);
+ }
+};
+
+findScreen = function(key, value, moveNextScreen) {
+ var found, index;
+ _.each(screens, function(s, i) {
+ if (s[key] === value) {
+ found = s;
+ index = i;
+ }
+ });
+ if (moveNextScreen && index === nextScreen) {
+ nextScreen = (nextScreen + 1) % screen.length;
+ }
+ return found;
+};
+
+cycleScreen = function(screen_id) {
+ var screen = findScreen('id', screen_id);
+ if (screen === undefined) {
+ return;
+ }
+ screen.content = getDefaultContent();
+
+ sendScreenChanged(screen);
+
+ screen.timeout = setTimeout(cycleScreen.bind(this, screen_id),
+ config.resetTime);
};
/* Put new content on the next screen in the line up. */
exports.setUrl = function(url, screen_name, callback) {
var screen;
if (screen_name) {
- _.each(screens, function(s, index) {
- if (s.name == screen_name) {
- screen = s;
- if (index == nextScreen) {
- nextScreen = (nextScreen + 1) % screen.length;
- }
- }
- });
+ screen = findScreen('name', screen_name, true);
}
// The above loop might fail, so check for that.
if (screen === undefined) {
@@ -62,15 +94,18 @@ exports.setUrl = function(url, screen_name, callback) {
contentForUrl(url, function(content) {
screen.content = content;
- everyone.now.screenChanged(screen);
+ sendScreenChanged(screen);
+ clearTimeout(screen.timeout);
+ screen.timeout = setTimeout(cycleScreen.bind(this, screen.id),
+ config.resetTime);
utils.async(callback, content);
});
};
exports.reset = function() {
_.each(screens, function(screen) {
screen.content = getDefaultContent();
- everyone.now.screenChanged(screen);
+ sendScreenChanged(screen);
});
};
@@ -84,7 +119,7 @@ function getDefaultContent() {
}
function contentForUrl(url, callback) {
- if (url.indexOf('://') == -1) {
+ if (url.indexOf('://') === -1) {
url = 'http://' + url;
}
@@ -189,10 +224,28 @@ everyone.now.getScreens = function(callback) {
utils.async(callback, screens);
};
-everyone.now.addScreen = function(name) {
- exports.addScreen(name);
-};
+everyone.now.addScreen = exports.addScreen;
-everyone.now.changeScreen = function(name) {
- console.log('change screen');
-};
+everyone.now.removeScreen = removeScreen;
+
+// Screen objects store some things that can't go over the wire. So don't
+// include those.
+function _getWireSafeScreen(screen) {
+ return {
+ 'id': screen.id,
+ 'name': screen.name,
+ 'content': screen.content
+ };
+}
+
+function sendScreenAdded(screen) {
+ everyone.now.screenAdded(_getWireSafeScreen(screen));
+}
+
+function sendScreenChanged(screen) {
+ everyone.now.screenChanged(_getWireSafeScreen(screen));
+}
+
+function sendScreenRemoved(screen) {
+ everyone.now.screenRemoved(_getWireSafeScreen(screen));
+}
@@ -50,6 +50,10 @@
background: #444;
color: #fff;
text-align: center;
+
+ button.close {
+ float: right;
+ }
}
.content {
@@ -137,3 +141,8 @@ i {
}
}
}
+
+button.close {
+ color: #f00;
+ cursor: pointer;
+}
View
@@ -37,28 +37,6 @@ function elementFor(content) {
}
}
-now.ready(function() {
- now.setUrl = function(url) {
- console.log('Loading url in iframe');
- updateContents(elementFor({type: 'html', url: url}));
- };
-
- now.setImage = function(url) {
- console.log('Loading image');
- updateContents(elementFor({type: 'image', url: url}));
- };
-
- now.screenChange = function(message) {
- };
-
- now.reset = function() {
- console.log('reset');
- window.location.reload();
- };
-
- ready('now');
-});
-
function resize() {
$('.wrap').css({
width: window.innerWidth,
@@ -149,6 +127,13 @@ function makeScreenPreview(screen, events) {
console.log('clicked on a screen');
selectScreen($(this).parent());
});
+ $elem.find('h1').append(
+ $('<button class="close">X</button>')
+ //.on('click', now.removeScreen.bind(this, screen.id)));
+ .on('click', function() {
+ console.log('removing screen');
+ now.removeScreen(screen.id);
+ }));
}
$elem.children('.content').html(elementFor(screen.content));
return $elem;
@@ -172,16 +157,39 @@ function selectScreen($elem) {
}
/******** Now.js connections ********/
-now.screenAdded = function(screen) {
- console.log("Adding screen: " + JSON.stringify(screen));
- $('.selector .meta').first().before(makeScreenPreview(screen));
-};
-
-now.screenChanged = function(screen) {
- console.log("Changing screen: " + JSON.stringify(screen));
- $preview = $('[name=screen-{id}]'.format(screen));
- $preview.find('.content').html(elementFor(screen.content));
- $preview.data('screen', screen);
-};
+now.ready(function() {
+ now.setUrl = function(url) {
+ console.log('Loading url in iframe');
+ updateContents(elementFor({type: 'html', url: url}));
+ };
+
+ now.setImage = function(url) {
+ console.log('Loading image');
+ updateContents(elementFor({type: 'image', url: url}));
+ };
+
+ now.reset = function() {
+ console.log('reset');
+ window.location.reload();
+ };
+
+ now.screenAdded = function(screen) {
+ console.log("Adding screen: " + JSON.stringify(screen));
+ $('.selector .meta').first().before(makeScreenPreview(screen));
+ };
+
+ now.screenChanged = function(screen) {
+ console.log("Changing screen: " + JSON.stringify(screen));
+ $preview = $('[name=screen-{id}]'.format(screen));
+ $preview.find('.content').html(elementFor(screen.content));
+ $preview.data('screen', screen);
+ };
+
+ now.screenRemoved = function(screen) {
+ $('[name=screen-{id}]'.format(screen)).remove();
+ };
+
+ ready('now');
+});
})();
View
@@ -18,71 +18,3 @@ exports.httpServer = http.createServer(function(request, response) {
files.serve(request, response);
});
}).listen(config.web.port);
-
-
-/*
-// Now.js connection
-var everyone = now.initialize(httpServer);
-screenIds = [];
-var currentScreen = 0;
-var resetTimers = {};
-
-everyone.now.clientReady = function() {
- screenIds.push(this.user.clientId);
- // Make the next url go to this screen.
- currentScreen = screenIds.length - 1;
- exports.showDefault(this.user.clientId);
-};
-
-everyone.disconnected(function() {
- console.log('screen disconnected ' + JSON.stringify(this));
- var index = screenIds.indexOf(this.user.clientId);
- if (index >= 0) {
- screenIds.splice(index, 1);
- }
- // Make sure that `currentScreen` is still valid.
- currentScreen = (currentScreen + 1) % screenIds.length;
- clearInterval(resetTimers[this.user.clientId]);
-});
-
-everyone.connected(function() {
- console.log('screen connected');
-});
-
-// Pick a screen to show a URL on, and kick off the process.
-exports.setUrl = function(url, screenId, callback) {
- if (screenId === -1) {
- screenId = screenIds[currentScreen];
- if (screenId === undefined) {
- utils.async(callback, "No screens connected.");
- return;
- }
- currentScreen = (currentScreen + 1) % screenIds.length;
- }
-
- now.getClient(screenId, function() {
- var screen = this;
- if (!screen || !screen.now) {
- console.log("Can't talk to screen: " + screen);
- clearTimeout(resetTimers[screenId]);
- return;
- }
- _processUrl(url, function(opts) {
- if (opts.url) {
- if (opts.type === 'image') {
- screen.now.setImage(opts.url);
- } else {
- screen.now.setUrl(opts.url);
- }
- }
-
- clearTimeout(resetTimers[screenId]);
- resetTimers[screenId] = setTimeout(function() {
- exports.showDefault(screenId);
- }, config.resetTime);
-
- utils.async(callback, _.extend({}, {screenId: screenId}, opts));
- });
- });
-};
-*/

0 comments on commit d6ba790

Please sign in to comment.