Commit: 4ca102ac2b7ff6c2ede8e9580acf970e4d240496

Author: gwoo | Date: 2009-11-05 21:53:24 -0800
some updates to reduce some code and clean up the api
diff --git a/config/routes.php b/config/routes.php index f34abcc..dfe8d0b 100644 --- a/config/routes.php +++ b/config/routes.php @@ -5,7 +5,7 @@ use \lithium\http\Router; Router::connect('/', array('controller' => 'anologue', 'action' => 'index')); Router::connect('/new', array('controller' => 'anologue', 'action' => 'add')); Router::connect('/say/{:id}', array('controller' => 'anologue', 'action' => 'say')); -Router::connect('/json/{:id}', array('controller' => 'anologue', 'action' => 'json')); +Router::connect('/{:id}.{:type}', array('controller' => 'anologue', 'action' => 'view')); Router::connect('/{:id}', array('controller' => 'anologue', 'action' => 'view')); ?> diff --git a/controllers/AnologueController.php b/controllers/AnologueController.php index 07fb5d7..b206021 100644 --- a/controllers/AnologueController.php +++ b/controllers/AnologueController.php @@ -6,38 +6,44 @@ use \app\models\Anologue; use \lithium\http\Media; class AnologueController extends \lithium\action\Controller { - + public function index() { $this->set(array('index' => true)); $this->render(); } - + public function say() { $status = 'error'; if (!empty($this->request->params['id'])) { $data = $this->request->data; - $data['ip'] = $this->request->get('env:REMOTE_ADDR'); + $data['ip'] = $this->request->env('REMOTE_ADDR'); if (!empty($data)) { + $status = 'fail'; $result = Anologue::addMessage($this->request->params['id'], $data); - if (!empty($result->ok)) { - $status = 'success'; - } else { - $status = 'fail'; - } + $status = (!empty($result->ok)) ? 'success' : 'fail'; } } - $this->render(array('json' => $this->_jsend($status))); + $this->render(array('json' => (object) compact('status', 'data'))); } - + public function view() { - $anonymousAvatar = Media::asset('anonymous.png', 'image'); + $status = 'error'; + $data = null; if (!empty($this->request->params['id'])) { - $anologue = Anologue::findById($this->request->params['id']); - $this->set(compact('anologue')); - $this->render(); + $data = Anologue::findById($this->request->params['id']); + $status = (!empty($data)) ? 'success' : 'fail'; } + $result = array(); + $this->set(compact('data')); + + if (!empty($this->request->params['type'])) { + $result = array( + $this->request->params['type'] => (object) compact('status', 'anologue') + ); + } + $this->render($result); } - + public function add() { $anologue = Anologue::create(); if (!empty($anologue->error)) { @@ -45,28 +51,6 @@ class AnologueController extends \lithium\action\Controller { } $this->redirect(array('controller' => 'anologue', 'action' => 'view', 'id' => $anologue->_id)); } - - public function json() { - $status = 'error'; - $anologue = false; - if (!empty($this->request->params['id'])) { - $status = 'fail'; - $anologue = Anologue::findById($this->request->params['id']); - if ($anologue) { - $status = "success"; - } - } - $data = new \stdClass(); - $data->anologue = $anologue; - $this->render(array('json' => $this->_jsend($status, $data))); - } - - protected function _jsend($status = "fail", $data = null) { - $jsend = new \stdClass(); - $jsend->status = $status; - $jsend->data = $data; - return $jsend; - } } -?> +?> \ No newline at end of file diff --git a/views/anologue/index.html.php b/views/anologue/index.html.php index 8880a3b..14b77ee 100644 --- a/views/anologue/index.html.php +++ b/views/anologue/index.html.php @@ -3,7 +3,7 @@ <div class="header"> <header> <div class="width-constraint"> - <?php echo $this->html->image('/img/anologue.png', array('id' => 'anologue-logo')); ?> + <?php echo $this->html->image('anologue.png', array('id' => 'anologue-logo')); ?> <h1 class="title">anologue</h1> </div> </header> @@ -24,7 +24,7 @@ <div class="aside excellence"> <aside> - <?php echo $this->html->image('/img/excellent.jpg', array('alt' => 'sixty-nine, dudes!', 'id' => 'anologue-excellent')); ?> + <?php echo $this->html->image('excellent.jpg', array('alt' => 'sixty-nine, dudes!', 'id' => 'anologue-excellent')); ?> <span>your future us's use anologue!</span> </aside> </div> diff --git a/views/anologue/view.html.php b/views/anologue/view.html.php index 7bc59cd..14e829a 100644 --- a/views/anologue/view.html.php +++ b/views/anologue/view.html.php @@ -1,3 +1,6 @@ +<?php + $avatar = 'http://'.$_SERVER['HTTP_HOST'].'/'.$this->_request->env('base').'/img/anonymous.png'; +?> <form id="anologue-form"> <div class="anologue-settings"> @@ -18,7 +21,7 @@ </div> <h1 class="smaller-title"><?php echo $this->html->link('anologue', array('controller' => 'anologue', 'action' => 'index')); ?></h1> -<h3 class="hash"><?php echo $this->html->link($anologue->_id, array('action' => 'view', 'id' => $anologue->_id), array('title' => 'Copy this url and give it to others')); ?></h3> +<h3 class="hash"><?php echo $this->html->link($data->_id, array('action' => 'view', 'id' => $data->_id), array('title' => 'Copy this url and give it to others')); ?></h3> <div class="anologue-help"> @@ -28,14 +31,15 @@ </div> <ul id="anologue" class="anologue"> -<?php if (!empty($anologue->messages)) { ?> - <?php foreach ($anologue->messages as $key => $message) { ?> +<?php if (!empty($data->messages)) { ?> + <?php foreach ($data->messages as $key => $message) { ?> <li class="message" id="message-<?php echo md5($message->timestamp . $message->author);?>"> <ul class="data"> <li class="time"><?php echo date('G:i:s', $message->timestamp);?></li> <li class="ip"><?php echo $message->ip;?></li> <li class="author"> - <?php echo $this->html->image('http://gravatar.com/avatar/'.$message->email.'?s=16&d=http://'.$_SERVER['HTTP_HOST'].'/img/anonymous.png'); ?> + <?php echo $this->html->image( + "http://gravatar.com/avatar/{$message->email}?s=16&d={$avatar}"); ?> <span title="<?php echo $this->html->escape($message->author);?>">&laquo; <?php echo $this->html->escape($message->author);?> &raquo;</span> </li> <li class="text"><div class="markdown"><?php echo $this->html->escape($message->text); ?></div></li> @@ -63,13 +67,15 @@ <?php echo $this->html->script(array( 'http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js', - '/js/md5.jquery.js', - '/js/showdown.js', - '/js/anologue.js', + 'md5.jquery.js', 'showdown.js', 'anologue.js', )); ?> <script type="text/javascript" charset="utf-8"> $(document).ready(function() { - anologue.setup({line: <?php echo count($anologue->messages); ?>, anologue: <?php echo json_encode($anologue); ?>, icon: 'http://<?php echo $_SERVER['HTTP_HOST']; ?>/img/anonymous.png' }); + anologue.setup({ + id: '<?=$data->_id?>', + base: '<?php echo $this->_request->env('base') ?>', + line: <?php echo count($data->messages); ?>, + icon: '<?php echo $avatar; ?>' + }); }); -</script> - +</script> \ No newline at end of file diff --git a/webroot/css/anologue.css b/webroot/css/anologue.css index 9860a77..8f967ac 100644 --- a/webroot/css/anologue.css +++ b/webroot/css/anologue.css @@ -21,7 +21,7 @@ p.last { padding:.5em 0 4em 0; } .width-constraint { display:block; width:1002px; margin:0 auto; } -.header { background:#4c2c59 url(/img/bg.png); padding:2em 1em; margin:0 0 1em 0; } +.header { background:#4c2c59 url(../img/bg.png); padding:2em 1em; margin:0 0 1em 0; } .header .aside { text-align:right; vertical-align:bottom; } .footer { text-align:center; padding:0 0 2em 0; } .footer a { border:none; margin:0 auto; } @@ -52,7 +52,7 @@ p.last { padding:.5em 0 4em 0; } .anologue .text .markdown { } .anologue .text .markdown ul { padding:.25em 1em; } - .anologue-speak { position:fixed; z-index:50; bottom:0; left:0; display:block; padding:.5em 2%; width:96%; clear:both; background:#4c2c59 url(/img/bg.png); color:white; } + .anologue-speak { position:fixed; z-index:50; bottom:0; left:0; display:block; padding:.5em 2%; width:96%; clear:both; background:#4c2c59 url(../img/bg.png); color:white; } .anologue-speak span.label { display:block; position:absolute; z-index:100; top:.5em; left:.5em; font-size:.85em; padding:.5em 1em; width:19.5em; color:white; text-align:right; } .anologue-speak .label.about { top:3em; text-align:left; } .anologue-speak .label.about a { color:white; color:#77458c; border:none; } diff --git a/webroot/js/anologue.js b/webroot/js/anologue.js index 496ea80..7f8654f 100644 --- a/webroot/js/anologue.js +++ b/webroot/js/anologue.js @@ -1,16 +1,13 @@ var anologue = { _config: { - timeout: null, + id: 0, + base : null, line: 0, - anologue: {}, - converter: {}, - lastInput: null, icon: null }, - + setup: function(config) { this._config = config; - this._config.converter = new Showdown.converter(); $("#anologue-form").submit(function() { anologue.say(); return false; @@ -23,28 +20,41 @@ var anologue = { }); this.markdown(); this.listener(); - this.setupSpeaker(); $("#anologue-author").focus(); }, - + + //the set time out method + timeout: null, + + //setup the listener on a timeout + run: function () { + clearTimeout(this.timeout); + this.timeout = setTimeout(function() { + anologue.listener(); + }, 2000); + }, + + //get messages listener: function() { - $.getJSON('/json/' + this._config.anologue._id + '?_='+(new Date().getTime()), null, function(response) { + var url = this._config.base + '/' + this._config.id + '.json?_=' + (new Date().getTime()); + $.getJSON(url, function(response) { if (response.status != "success") { return anologue.alert(response.status); } - anologue._config.anologue = response.data.anologue; - if (response.data.anologue.messages != null) { - for (var i = anologue._config.line; i < response.data.anologue.messages.length; i++) { - anologue.push(response.data.anologue.messages[i]); + if (response.data.messages != null) { + for (var i = anologue._config.line; i < response.data.messages.length; i++) { + anologue.render(response.data.messages[i]); anologue._config.line++; } anologue.markdown(); + anologue.run(); } - anologue._config.timeout = setTimeout(function() { anologue.listener(); }, 2000); }); }, - + + //add message say: function() { + clearTimeout(this.timeout); var data = { author: $('#anologue-author').val(), email: $('#anologue-email').val(), @@ -53,21 +63,21 @@ var anologue = { if (data.author == '') { data.author = 'anonymous'; } - this._config.lastInput = $("#anologue-text").val(); + var input = $("#anologue-text").val(); $("#anologue-text").val(""); - clearTimeout(this._config.timeout); - $.post("/say/" + this._config.anologue._id, data, function(response) { - + $.post(this._config.base + "/say/" + this._config.id, data, function(response) { if (response.status != 'success') { - $("#anologue-text").val(anologue._config.lastInput); + $("#anologue-text").val(input); // this might occur if someone sends an update at the same time... anologue.alert('Hold your horses, Spammy McSpamsky. Wait until your last message goes through and try sending your message again.'); } anologue.listener(); }, "json"); + }, - - push: function(message) { + + //output messages + render: function(message) { var timeroo = new Date(); var timestamp = timeroo.getHours() + ':' + timeroo.getMinutes() + ':' + timeroo.getSeconds(); var id = 'message-' + $.md5(message.timestamp + message.author); @@ -95,15 +105,15 @@ var anologue = { }, 'normal'); } }, - + setupSpeaker: function() { if (!$.browser.msie) { var speaker = $("#anologue-speaker"); speaker.attr('controls', false); speaker.attr('autobuffer', true); - var tape = '/media/hey.mp3'; + var tape = this._config.base + '/media/hey.mp3'; if ($.browser.mozilla) { - tape = '/media/hey.ogg'; + tape = this._config.base + '/media/hey.ogg'; } speaker.attr('src', tape); this.hey(); @@ -111,24 +121,25 @@ var anologue = { $('.sound').hide(); } }, - + hey: function() { if (!$.browser.msie) { $('#anologue-speaker').get(0).play(); } }, - + alert: function(message) { alert(message); return null; }, - + markdown: function() { $('.markdown').each(function() { if (!$(this).hasClass('marked')) { - var text = anologue._config.converter.makeHtml($(this).html()); + var showdown = new Showdown.converter(); + var text = showdown.makeHtml($(this).html()); $(this).html(text).addClass('marked'); } }); } -} +} \ No newline at end of file